From 933cd014fa1d14a65e6868136b1cc266e5f4e3bd Mon Sep 17 00:00:00 2001 From: Antoine Date: Wed, 11 Mar 2020 00:43:33 +0100 Subject: [PATCH] finish os stage 3 download extract, configure mount and chroot --- packer/Dockerfile | 7 ++ packer/assets/ansible/ansible.cfg | 5 +- packer/assets/ansible/inventory_test.ini | 6 +- .../ansible/roles/setup/defaults/main.yml | 6 +- .../ansible/roles/setup/tasks/configure.yml | 69 ++++++++++++++++++ .../assets/ansible/roles/setup/tasks/main.yml | 3 + .../ansible/roles/setup/tasks/mount.yml | 43 ++++++++++++ .../assets/ansible/roles/setup/tasks/os.yml | 70 +++++++++++++++++++ .../ansible/roles/setup/tasks/partition.yml | 5 +- .../ansible/roles/setup/tasks/umount.yml | 16 +++++ .../assets/ansible/roles/setup/vars/main.yml | 22 ++++++ packer/assets/ansible/run-ansible.sh | 5 +- packer/assets/gentoo.json | 2 +- 13 files changed, 250 insertions(+), 9 deletions(-) create mode 100644 packer/assets/ansible/roles/setup/tasks/configure.yml create mode 100644 packer/assets/ansible/roles/setup/tasks/mount.yml create mode 100644 packer/assets/ansible/roles/setup/tasks/os.yml create mode 100644 packer/assets/ansible/roles/setup/tasks/umount.yml create mode 100644 packer/assets/ansible/roles/setup/vars/main.yml diff --git a/packer/Dockerfile b/packer/Dockerfile index 304b60a..295d321 100644 --- a/packer/Dockerfile +++ b/packer/Dockerfile @@ -18,6 +18,13 @@ RUN addgroup -g 1000 "${GROUP}" && \ echo "$USER ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/$USER && \ chmod 0440 /etc/sudoers.d/$USER +# setup mitogen to enhance ansible speed +ENV MITOGEN_VERSION="0.2.9" +RUN wget https://networkgenomics.com/try/mitogen-${MITOGEN_VERSION}.tar.gz && \ + tar xvf mitogen-${MITOGEN_VERSION}.tar.gz && \ + mv mitogen-${MITOGEN_VERSION} /mitogen && \ + rm mitogen-${MITOGEN_VERSION}.tar.gz + USER "${USER}" ENV PACKER_LOG=1 diff --git a/packer/assets/ansible/ansible.cfg b/packer/assets/ansible/ansible.cfg index d313d1b..566c522 100644 --- a/packer/assets/ansible/ansible.cfg +++ b/packer/assets/ansible/ansible.cfg @@ -1,2 +1,5 @@ [defaults] -host_key_checking = false \ No newline at end of file +host_key_checking = false + +strategy_plugins = /mitogen/ansible_mitogen/plugins/strategy +strategy = mitogen_linear \ No newline at end of file diff --git a/packer/assets/ansible/inventory_test.ini b/packer/assets/ansible/inventory_test.ini index d5a8320..e51dd49 100644 --- a/packer/assets/ansible/inventory_test.ini +++ b/packer/assets/ansible/inventory_test.ini @@ -1,4 +1,4 @@ [default] -100.64.0.24 ansible_ssh_pass=toor ansible_ssh_port=5556 ansible_user=root -#192.168.1.24 ansible_ssh_pass=toor ansible_ssh_port=5556 ansible_user=root - +100.64.0.24 ansible_ssh_pass=toor ansible_ssh_port=5556 ansible_user=root debug=true +;192.168.1.24 ansible_ssh_pass=toor ansible_ssh_port=5556 ansible_user=root debug=true +;192.168.1.22 ansible_ssh_pass=toor ansible_ssh_port=5556 ansible_user=root debug=true diff --git a/packer/assets/ansible/roles/setup/defaults/main.yml b/packer/assets/ansible/roles/setup/defaults/main.yml index bc21c02..8035a7d 100644 --- a/packer/assets/ansible/roles/setup/defaults/main.yml +++ b/packer/assets/ansible/roles/setup/defaults/main.yml @@ -24,7 +24,6 @@ devices_system: part_end: 100% fstype: "lvm" -vg_name: vg_system lvs: - name: "swap" size: "1G" @@ -34,8 +33,13 @@ lvs: size: "22G" type: "ext4" descr: "Root partition" + mount_dir: "{{ mount_base_dir }}" - name: "home" size: "7G" type: "ext4" descr: "home partition" + mount_dir: "{{ mount_base_dir }}/home" +# default swapiness value +swapiness: "60" +gentoo_version: "lastest" \ No newline at end of file diff --git a/packer/assets/ansible/roles/setup/tasks/configure.yml b/packer/assets/ansible/roles/setup/tasks/configure.yml new file mode 100644 index 0000000..6714fa8 --- /dev/null +++ b/packer/assets/ansible/roles/setup/tasks/configure.yml @@ -0,0 +1,69 @@ +--- +# configure and setup os + +- name: + set_fact: + make_conf: + - path: "GENTOO_MIRRORS=\"{{ mirror_source }}\"" + regexp: "^GENTOO_MIRRORS=\".*\"" + - path: "COMMON_FLAGS=\"{{ gcc_common_args }}\"" + regexp: "^COMMON_FLAGS=" + - path: "MAKEOPTS=\"{{ make_opts }}\"" + regexp: "^MAKEOPTS=" + tags: + - configure + +- name: "Configure Gentoo_mirrors" + lineinfile: + path: "{{ mount_base_dir }}/etc/portage/make.conf" + regexp: "{{ item.regexp }}" + line: "{{ item.path }}" + state: present + loop: "{{ make_conf }}" + tags: + - configure + +- name: "Create portage repo conf dir" + file: + path: "{{ portage_conf_dir_path }}" + state: directory + tags: + - configure + +- name: "Copy portage conf file" + copy: + src: "{{ item.src }}" + dest: "{{ item.dest }}" + remote_src: yes + loop: + - src: "{{ mount_base_dir }}/usr/share/portage/config/repos.conf" + dest: "{{ portage_conf_dir_path }}/gentoo.conf" + - src: "/etc/resolv.conf" + dest: "{{ mount_base_dir }}/etc/resolv.conf" + tags: + - configure + +#- name: create portage directory +# command: chroot /mnt/gentoo bash -c 'source /etc/profile; mkdir /usr/portage' creates=/mnt/gentoo/usr/portage +# tags: +# - chroot +# +#- name: emerge-webrsync +# command: chroot /mnt/gentoo bash -c 'source /etc/profile; /usr/bin/emerge-webrsync' creates=/mnt/gentoo/usr/portage/xfce-extra +# tags: +# - chroot +# +#- name: set zoneinfo +# command: chroot /mnt/gentoo bash -c 'cp /usr/share/zoneinfo/{{ timezone }} /etc/localtime; echo "{{ timezone }}" > /etc/timezone' +# tags: +# - chroot +# +#- name: Configure Hosts File +# template: src=hosts dest=/mnt/gentoo/etc/hosts +# +#- name: Set Hostname +# template: src=hostname dest=/mnt/gentoo/etc/conf.d/hostname +# +# +#- name: Configure Fstab +# copy: src=fstab dest=/mnt/gentoo/etc/fstab diff --git a/packer/assets/ansible/roles/setup/tasks/main.yml b/packer/assets/ansible/roles/setup/tasks/main.yml index 5fe6b8c..efe3be1 100644 --- a/packer/assets/ansible/roles/setup/tasks/main.yml +++ b/packer/assets/ansible/roles/setup/tasks/main.yml @@ -2,6 +2,9 @@ # setup gentoo - import_tasks: partition.yml +- import_tasks: mount.yml +- import_tasks: os.yml +- import_tasks: configure.yml #- import_tasks: users.yml #- import_tasks: security.yml diff --git a/packer/assets/ansible/roles/setup/tasks/mount.yml b/packer/assets/ansible/roles/setup/tasks/mount.yml new file mode 100644 index 0000000..cd4fb7a --- /dev/null +++ b/packer/assets/ansible/roles/setup/tasks/mount.yml @@ -0,0 +1,43 @@ +--- + +- name: "Enable swap partition" + command: "swapon /dev/{{ vg_name }}/{{ item.name }}" + loop: "{{ lvs }}" + when: "'swap' in item.type" + tags: + - mount + +- name: "Set swappiness value" + sysctl: + name: vm.swappiness + value: "{{ swapiness }}" + state: present + tags: + - mount + +- name: "Create mountpoints base dir" + file: + path: "{{ mount_base_dir }}" + state: directory + tags: + - mount + +- name: "Create mountpoints" + file: + path: "{{ item.mount_dir }}" + state: directory + loop: "{{ lvs }}" + when: "item.mount_dir is defined" + tags: + - mount + +- name: "Mount partition" + mount: + path: "{{ item.mount_dir }}" + src: "/dev/{{ vg_name }}/{{ item.name }}" + fstype: "{{ item.type }}" + state: mounted + loop: "{{ lvs }}" + when: "item.mount_dir is defined" + tags: + - mount diff --git a/packer/assets/ansible/roles/setup/tasks/os.yml b/packer/assets/ansible/roles/setup/tasks/os.yml new file mode 100644 index 0000000..bd0afe6 --- /dev/null +++ b/packer/assets/ansible/roles/setup/tasks/os.yml @@ -0,0 +1,70 @@ +--- +# retrieve gentoo os and copy it + +- name: "Set time" + command: "ntpd -q -g" + tags: + - os + +- name: "Retrieve last version" + shell: > + curl {{ latest_url }} 2>&1 | + awk -F/ '/\/stage3-amd64-[0-9TZ]+.tar.xz/{print $1}' + args: + warn: no + when: "gentoo_version is not defined or gentoo_version == 'lastest'" + register: "stage_path" + tags: + - os + +- set_fact: + url_dl: "{{ mirror_stage }}/releases/amd64/autobuilds/{{ stage_path.stdout | default(gentoo_version) }}/stage3-amd64-{{ stage_path.stdout | default(gentoo_version) }}.tar.xz" + tags: + - os + +- name: "Download stage3 archive version {{ stage_path.stdout }}" + get_url: + url: "{{ url_dl }}" + dest: "/root/stage3-amd64.tar.xz" + register: "archive_downloaded_result" + tags: + - os + +- name: "Extract stage3 archive" + unarchive: + src: "{{ archive_downloaded_result.dest }}" + dest: "{{ mount_base_dir }}" + remote_src: yes + creates: "{{ mount_base_dir }}/bin" + tags: + - os + +- name: "Mount /proc" + mount: + src: /proc + path: "{{ mount_base_dir }}/proc" + state: mounted + fstype: proc + tags: + - os + +- name: "Mount /dev bind volume" + mount: + src: "/{{ item.path }}" + path: "{{ mount_base_dir }}/{{ item.path }}" + opts: rbind,make-rslave + state: mounted + fstype: none + loop: + - path: dev + - path: sys + tags: + - os + +- name: "Cleanup Stage3 Archive" + file: + path: "{{ archive_downloaded_result.dest }}" + state: absent + when: "debug is not defined or debug == false" + tags: + - os diff --git a/packer/assets/ansible/roles/setup/tasks/partition.yml b/packer/assets/ansible/roles/setup/tasks/partition.yml index c58b0e8..fd656aa 100644 --- a/packer/assets/ansible/roles/setup/tasks/partition.yml +++ b/packer/assets/ansible/roles/setup/tasks/partition.yml @@ -38,6 +38,10 @@ tags: - storage +- import_tasks: "umount.yml" + tags: + - storage + - name: "Create list of lvs" lvol: vg: "{{ vg_name }}" @@ -52,7 +56,6 @@ fstype: "{{ item.type }}" dev: "/dev/{{ vg_name }}/{{ item.name }}" force: yes - resizefs: yes loop: "{{ lvs }}" tags: - storage \ No newline at end of file diff --git a/packer/assets/ansible/roles/setup/tasks/umount.yml b/packer/assets/ansible/roles/setup/tasks/umount.yml new file mode 100644 index 0000000..c97feda --- /dev/null +++ b/packer/assets/ansible/roles/setup/tasks/umount.yml @@ -0,0 +1,16 @@ +--- +# umount and disabe swap + +- name: "disable swap partition" + command: "swapoff -a" + tags: + - umount + +- name: "Umount partition" + mount: + path: "{{ item.mount_dir }}" + state: "unmounted" + loop: "{{ lvs | reverse | list }}" + when: "item.mount_dir is defined" + tags: + - umount diff --git a/packer/assets/ansible/roles/setup/vars/main.yml b/packer/assets/ansible/roles/setup/vars/main.yml new file mode 100644 index 0000000..1bfb4fd --- /dev/null +++ b/packer/assets/ansible/roles/setup/vars/main.yml @@ -0,0 +1,22 @@ +--- +# non overridable variable + +vg_name: vg_system +mount_base_dir: /mnt/gentoo +mirror_stage: "https://gentoo.osuosl.org" +latest_url: "{{ mirror_stage }}/releases/amd64/autobuilds/latest-stage3.txt" + +# compilation arguments +mirror_source: > + http://gentoo.mirrors.ovh.net/gentoo-distfiles/ + ftp://ftp.free.fr/mirrors/ftp.gentoo.org/ + http://ftp.free.fr/mirrors/ftp.gentoo.org/ + https://mirrors.soeasyto.com/distfiles.gentoo.org/ + http://mirrors.soeasyto.com/distfiles.gentoo.org/ + ftp://mirrors.soeasyto.com/distfiles.gentoo.org/ + http://gentoo.modulix.net/gentoo/ +gcc_common_args: "-march=native -O2 -pipe" +make_opts: "-j2" + +# portage variable +portage_conf_dir_path: "{{ mount_base_dir }}/etc/portage/repos.conf/" \ No newline at end of file diff --git a/packer/assets/ansible/run-ansible.sh b/packer/assets/ansible/run-ansible.sh index 92cb961..846a204 100755 --- a/packer/assets/ansible/run-ansible.sh +++ b/packer/assets/ansible/run-ansible.sh @@ -1,7 +1,8 @@ #!/bin/bash PLAYBOOK="${1:-playbook.yml}" -DEBUG="-vvv" +#DEBUG="-vvv" +TAGS="--tags configure" # run playbook on aready running qemu vm -ansible-playbook -i inventory_test.ini "$PLAYBOOK" --extra-vars "ssh_pub=$(cat ../id_rsa_qemu) generated_user=antoine generated_group=antoine" +ansible-playbook $DEBUG -i inventory_test.ini $TAGS --extra-vars "ssh_pub=$(cat ../id_rsa_qemu) generated_user=antoine generated_group=antoine" $PLAYBOOK diff --git a/packer/assets/gentoo.json b/packer/assets/gentoo.json index 5f8618e..09d8ca8 100644 --- a/packer/assets/gentoo.json +++ b/packer/assets/gentoo.json @@ -83,7 +83,7 @@ { "type": "ansible", "playbook_file": "/packer/ansible/playbook.yml", - "extra_arguments": [ "--extra-vars", "ssh_pub={{user `ssh_pub_key`}} generated_user={{user `generated_user`}} generated_group={{user `generated_group`}}" ], + "extra_arguments": [ "--extra-vars", "ssh_pub={{user `ssh_pub_key`}} generated_user={{user `generated_user`}} generated_group={{user `generated_group`}} gentoo_version={{user `version`}}" ], "groups": "default", "user": "root", "ansible_env_vars": [ "ANSIBLE_CONFIG=/packer/ansible/ansible.cfg" ]