diff --git a/ironic_staging_drivers/ansible/playbooks/roles/configure/defaults/main.yaml b/ironic_staging_drivers/ansible/playbooks/roles/configure/defaults/main.yaml new file mode 100644 index 0000000..9fdad71 --- /dev/null +++ b/ironic_staging_drivers/ansible/playbooks/roles/configure/defaults/main.yaml @@ -0,0 +1 @@ +tmp_rootfs_mount: /tmp/rootfs diff --git a/ironic_staging_drivers/ansible/playbooks/roles/configure/files/install_grub.sh b/ironic_staging_drivers/ansible/playbooks/roles/configure/files/install_grub.sh deleted file mode 100755 index 8a2af96..0000000 --- a/ironic_staging_drivers/ansible/playbooks/roles/configure/files/install_grub.sh +++ /dev/null @@ -1,57 +0,0 @@ -#!/bin/sh - -# code from DIB bash ramdisk -readonly target_disk=$1 -readonly root_part=$2 -readonly root_part_mount=/mnt/rootfs - -# We need to run partprobe to ensure all partitions are visible. -# On some test environments this is too fast -# and kernel does not have time to react to changes -partprobe $target_disk -sleep 5 - -mkdir -p $root_part_mount - -mount $root_part $root_part_mount -if [ $? != "0" ]; then - echo "Failed to mount root partition $root_part on $root_part_mount" - exit 1 -fi - -mkdir -p $root_part_mount/dev -mkdir -p $root_part_mount/sys -mkdir -p $root_part_mount/proc - -mount -o bind /dev $root_part_mount/dev -mount -o bind /sys $root_part_mount/sys -mount -o bind /proc $root_part_mount/proc - -# Find grub version -V= -if [ -x $root_part_mount/usr/sbin/grub2-install ]; then - V=2 -fi - -# Install grub -ret=1 -if chroot $root_part_mount /bin/sh -c "/usr/sbin/grub$V-install ${target_disk}"; then - echo "Generating the grub configuration file" - - # tell GRUB2 to preload its "lvm" module to gain LVM booting on direct-attached disks - if [ "$V" = "2" ]; then - echo "GRUB_PRELOAD_MODULES=lvm" >> $root_part_mount/etc/default/grub - fi - chroot $root_part_mount /bin/sh -c "/usr/sbin/grub$V-mkconfig -o /boot/grub$V/grub.cfg" - ret=$? -fi - -umount $root_part_mount/dev -umount $root_part_mount/sys -umount $root_part_mount/proc -umount $root_part_mount - -if [ $ret != "0" ]; then - echo "Installing grub bootloader failed" -fi -exit $ret diff --git a/ironic_staging_drivers/ansible/playbooks/roles/configure/tasks/grub.yaml b/ironic_staging_drivers/ansible/playbooks/roles/configure/tasks/grub.yaml index 91691f1..86e5929 100644 --- a/ironic_staging_drivers/ansible/playbooks/roles/configure/tasks/grub.yaml +++ b/ironic_staging_drivers/ansible/playbooks/roles/configure/tasks/grub.yaml @@ -1,3 +1,79 @@ -- name: install grub +- name: discover grub-install command + find: + paths: + - "{{ tmp_rootfs_mount }}/usr/sbin" + pattern: "grub*-install" + register: grub_install_found + +- name: discover grub-mkconfig command + find: + paths: + - "{{ tmp_rootfs_mount }}/usr/sbin" + pattern: "grub*-mkconfig" + register: grub_config_found + +- name: find grub config file + find: + paths: + - "{{ tmp_rootfs_mount }}/boot" + pattern: "grub*.cfg" + recurse: yes + register: grub_file_found + +- name: test if all needed grub files were found + assert: + that: + - "{{ grub_install_found.matched > 0 }}" + - "{{ grub_config_found.matched > 0 }}" + - "{{ grub_file_found.matched > 0 }}" + +- name: set paths to grub commands + set_fact: + grub_install_cmd: "{{ grub_install_found.files[0].path | replace(tmp_rootfs_mount,'') }}" + grub_config_cmd: "{{ grub_config_found.files[0].path | replace(tmp_rootfs_mount,'') }}" + grub_config_file: "{{ grub_file_found.files[0].path | replace(tmp_rootfs_mount,'') }}" + +- name: make dirs for chroot become: yes - script: install_grub.sh {{ ironic_root_device }} {{ ironic_image_target }} + file: + state: directory + path: "{{ tmp_rootfs_mount }}/{{ item }}" + with_items: + - dev + - sys + - proc + +- name: mount dirs for chroot + become: yes + command: mount -o bind /{{ item }} {{ tmp_rootfs_mount }}/{{ item }} + with_items: + - dev + - sys + - proc + +- block: + - name: get grub version string + become: yes + command: chroot {{ tmp_rootfs_mount }} /bin/sh -c '{{ grub_install_cmd }} --version' + register: grub_version_string + - name: install grub to disk + become: yes + command: chroot {{ tmp_rootfs_mount }} /bin/sh -c '{{ grub_install_cmd }} /dev/{{ ansible_devices | first }}' + - name: preload lvm modules for grub2 + become: yes + lineinfile: + dest: "{{ tmp_rootfs_mount }}/etc/default/grub" + state: present + line: GRUB_PRELOAD_MODULES=lvm + when: "{{ grub_version_string.stdout.split() | last | first == '2' }}" + - name: create grub config + become: yes + command: chroot {{ tmp_rootfs_mount }} /bin/sh -c '{{ grub_config_cmd }} -o {{ grub_config_file }}' + always: + - name: unmount dirs for chroot + become: yes + command: umount {{ tmp_rootfs_mount }}/{{ item }} + with_items: + - dev + - sys + - proc diff --git a/ironic_staging_drivers/ansible/playbooks/roles/configure/tasks/main.yaml b/ironic_staging_drivers/ansible/playbooks/roles/configure/tasks/main.yaml index b93f055..aa1090c 100644 --- a/ironic_staging_drivers/ansible/playbooks/roles/configure/tasks/main.yaml +++ b/ironic_staging_drivers/ansible/playbooks/roles/configure/tasks/main.yaml @@ -1,2 +1,4 @@ +- include: mounts.yaml + when: "{{ ironic.image.type | default('whole-disk-image') == 'partition' }}" - include: grub.yaml when: "{{ ironic.image.type | default('whole-disk-image') == 'partition' }}" diff --git a/ironic_staging_drivers/ansible/playbooks/roles/configure/tasks/mounts.yaml b/ironic_staging_drivers/ansible/playbooks/roles/configure/tasks/mounts.yaml new file mode 100644 index 0000000..870fa9a --- /dev/null +++ b/ironic_staging_drivers/ansible/playbooks/roles/configure/tasks/mounts.yaml @@ -0,0 +1,8 @@ +- name: create tmp mount point for root + file: + state: directory + path: "{{ tmp_rootfs_mount }}" + +- name: mount user image root + become: yes + command: mount {{ ironic_image_target }} {{ tmp_rootfs_mount }}