From caf565673be97c0c82cd1c25dbbb0a0caec96d9e Mon Sep 17 00:00:00 2001 From: Olivier Bourdon Date: Mon, 18 Jun 2018 17:19:02 +0200 Subject: [PATCH] Fix for proper LVM support Without this fix, a LVM based ubuntu-minimal image will fail booting due to the fact that the boot process will not be able to retrieve the root filesystem using LABEL=(cloud)img-rootfs Change-Id: If4ecf65868563f7b799160a58af6312bedf956bf --- diskimage_builder/elements/lvm/README.rst | 15 +++++ diskimage_builder/elements/lvm/element-deps | 3 + .../elements/lvm/finalise.d/60-bootloader | 65 +++++++++++++++++++ .../elements/lvm/package-installs.yaml | 2 + .../elements/lvm/root.d/10-lvm-check-distro | 17 +++++ 5 files changed, 102 insertions(+) create mode 100644 diskimage_builder/elements/lvm/README.rst create mode 100644 diskimage_builder/elements/lvm/element-deps create mode 100755 diskimage_builder/elements/lvm/finalise.d/60-bootloader create mode 100644 diskimage_builder/elements/lvm/package-installs.yaml create mode 100755 diskimage_builder/elements/lvm/root.d/10-lvm-check-distro diff --git a/diskimage_builder/elements/lvm/README.rst b/diskimage_builder/elements/lvm/README.rst new file mode 100644 index 00000000..41ac7133 --- /dev/null +++ b/diskimage_builder/elements/lvm/README.rst @@ -0,0 +1,15 @@ +===== +lvm +===== +This is the LVM support element for Ubuntu Xenial. + +Note that this element requires initramfs-tools and lvm2 +packages to be added to the DIB image using -p option. +If this is not the case, an error will be triggered. + +This element enables that an image build with a customized +DIB_BLOCK_DEVICE_CONFIG containing LVM volumes will boot +properly. + +On CentOS like distributions, you should use dracut-regenerate +element instead of current lvm element. diff --git a/diskimage_builder/elements/lvm/element-deps b/diskimage_builder/elements/lvm/element-deps new file mode 100644 index 00000000..03e2cdb6 --- /dev/null +++ b/diskimage_builder/elements/lvm/element-deps @@ -0,0 +1,3 @@ +bootloader +pkg-map +ubuntu-minimal diff --git a/diskimage_builder/elements/lvm/finalise.d/60-bootloader b/diskimage_builder/elements/lvm/finalise.d/60-bootloader new file mode 100755 index 00000000..0c68f337 --- /dev/null +++ b/diskimage_builder/elements/lvm/finalise.d/60-bootloader @@ -0,0 +1,65 @@ +#!/bin/bash + +# Configure grub. Note that the various conditionals here are to handle +# different distributions gracefully. + +if [ ${DIB_DEBUG_TRACE:-1} -gt 0 ]; then + set -x +fi +set -eu +set -o pipefail + +BOOT_DEV=$IMAGE_BLOCK_DEVICE + +# All available devices, handy for some bootloaders... +declare -A DEVICES +eval DEVICES=( $IMAGE_BLOCK_DEVICES ) + +# This might be better factored out into a per-distro 'install-bootblock' +# helper. +if [ -d /boot/grub2 ]; then + GRUB_CFG=/boot/grub2/grub.cfg +elif [ -d /boot/grub ]; then + GRUB_CFG=/boot/grub/grub.cfg +fi + +if type grub2-mkconfig >/dev/null; then + GRUB_MKCONFIG="grub2-mkconfig -o $GRUB_CFG" +else + GRUB_MKCONFIG="grub-mkconfig -o $GRUB_CFG" +fi + +# Retrieve root filesystem mount point to check if it is LVM based +ROOTFS=$(awk '$2=="/"{print}' /proc/mounts) +# Standard filesystems are mounted as /dev/mapper/loop* within DIB +# so we need to exclude this case +if echo "$ROOTFS" | grep -qv '/dev/mapper/loop'; then + # LVM based filesystems are mounted as /dev/mapper/- within DIB + if echo "$ROOTFS" | grep -q '/dev/mapper/'; then + # Check if initramfs-tools are installed + if [ ! -d /etc/initramfs-tools ]; then + echo "You must have initramfs-tools installed for LVM based root filesystem to work properly" + exit 1 + fi + # Check if system supports LVM + if ! type pvs >/dev/null 2>&1; then + echo "You must have lvm2 package installed for LVM based root filesystem to work properly" + exit 1 + fi + # Check for appropriate filesystem support (for fsck) + FSTYPE=$(echo $ROOTFS | awk '{print $3}') + if ! type fsck.$FSTYPE >/dev/null 2>&1; then + echo "You must have utility package for $FSTYPE filesystem installed for LVM based root filesystem to work properly (fsck.$FSTYPE is missing)" + exit 1 + fi + # Change the current GRUB_DEVICE from LABEL=(cloud)img-rootfs to /dev/mapper/... + sed -i -e "s?^GRUB_DEVICE=.*?GRUB_DEVICE=$(echo $ROOTFS | awk '{print $1}')?" /etc/default/grub + # This is required for LVM2 driver to be included in the + # ramdisk of the image. + echo "lvm2" >> /etc/initramfs-tools/modules + # We need to regenerated the init ramdisk at this point + update-initramfs -u -v + fi +fi + +$GRUB_MKCONFIG diff --git a/diskimage_builder/elements/lvm/package-installs.yaml b/diskimage_builder/elements/lvm/package-installs.yaml new file mode 100644 index 00000000..9ac6aa5d --- /dev/null +++ b/diskimage_builder/elements/lvm/package-installs.yaml @@ -0,0 +1,2 @@ +lvm2: +initramfs-tools: diff --git a/diskimage_builder/elements/lvm/root.d/10-lvm-check-distro b/diskimage_builder/elements/lvm/root.d/10-lvm-check-distro new file mode 100755 index 00000000..1a54caf5 --- /dev/null +++ b/diskimage_builder/elements/lvm/root.d/10-lvm-check-distro @@ -0,0 +1,17 @@ +#!/bin/bash + +if [ ${DIB_DEBUG_TRACE:-0} -gt 0 ]; then + set -x +fi +set -eu +set -o pipefail + +if [ 'ubuntu' != $DISTRO_NAME ]; then + echo "Only ubuntu is supported for LVM support. The DISTRO is set to $DISTRO_NAME" + exit 1 +fi + +if [ 'xenial' != $DIB_RELEASE ]; then + echo "Only xenial is supported for LVM support. The DIB_RELEASE is set to $DIB_RELEASE" + exit 1 +fi