[ansible] re-write grub install as ansible tasks

remove install-grub.sh script in favor of proper ansible tasks

Change-Id: I74ef330bcefdf0a88df7572cb0cbcf2ba07d3dc8
This commit is contained in:
Pavlo Shchelokovskyy 2017-01-23 18:57:53 +00:00
parent 0800a24d5f
commit b4fc5fae01
5 changed files with 89 additions and 59 deletions

View File

@ -0,0 +1 @@
tmp_rootfs_mount: /tmp/rootfs

View File

@ -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

View File

@ -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

View File

@ -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' }}"

View File

@ -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 }}