commit: 2810d76d9481ded6f68d0faad642bd9b022436e3 Author: Andreas K. Hüttel <dilfridge <AT> gentoo <DOT> org> AuthorDate: Fri Oct 11 22:07:25 2024 +0000 Commit: Andreas K. Hüttel <dilfridge <AT> gentoo <DOT> org> CommitDate: Fri Oct 11 22:07:25 2024 +0000 URL: https://gitweb.gentoo.org/proj/catalyst.git/commit/?id=2810d76d
Improve cleanup in case of errors Signed-off-by: Andreas K. Hüttel <dilfridge <AT> gentoo.org> targets/support/create-qcow2.sh | 74 ++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 31 deletions(-) diff --git a/targets/support/create-qcow2.sh b/targets/support/create-qcow2.sh index 2d7a046a..7e3f2573 100755 --- a/targets/support/create-qcow2.sh +++ b/targets/support/create-qcow2.sh @@ -30,81 +30,93 @@ source ${clst_shdir}/support/functions.sh # Let's assume these are deps of catalyst and thus present. # -# create a new qcow2 disk image file -qemu-img create -f qcow2 "${1}.tmp.qcow2" ${clst_qcow2_size/%iB/} || die "Cannot create qcow2 file" +mymountpoint="${1}.tmp.mnt" -# connect the qcow2 file to a network block device # TODO: find next free device modprobe -q nbd mydevice=/dev/nbd0 -qemu-nbd -c ${mydevice} -f qcow2 "${1}.tmp.qcow2" || die "Cannot connect qcow2 file to nbd0" + +# This script requires slightly more stringent cleanup in case of errors... +qcow2die() { + echo "Something went wrong. Cleaning up..." + # here we just ignore errors + umount "${mymountpoint}/boot" + umount "${mymountpoint}" + qemu-nbd -d "${mydevice}" + + die $@ +} + +# create a new qcow2 disk image file +qemu-img create -f qcow2 "${1}.tmp.qcow2" ${clst_qcow2_size/%iB/} || qcow2die "Cannot create qcow2 file" + +# connect the qcow2 file to a network block device +qemu-nbd -c ${mydevice} -f qcow2 "${1}.tmp.qcow2" || qcow2die "Cannot connect qcow2 file to nbd0" # create a GPT disklabel -parted -s ${mydevice} mklabel gpt || die "Cannot create disklabel" +parted -s ${mydevice} mklabel gpt || qcow2die "Cannot create disklabel" # create an EFI boot partition -parted -s ${mydevice} -- mkpart gentoo_efi fat32 1M ${clst_qcow2_efisize} || die "Cannot create EFI partition" +parted -s ${mydevice} -- mkpart gentoo_efi fat32 1M ${clst_qcow2_efisize} || qcow2die "Cannot create EFI partition" # mark it as EFI boot partition -parted -s ${mydevice} -- type 1 C12A7328-F81F-11D2-BA4B-00A0C93EC93B || die "Cannot set EFI partition UUID" +parted -s ${mydevice} -- type 1 C12A7328-F81F-11D2-BA4B-00A0C93EC93B || qcow2die "Cannot set EFI partition UUID" # note down name mypartefi=${mydevice}p1 # create the root partition -parted -s ${mydevice} -- mkpart gentoo_root ${clst_qcow2_roottype} ${clst_qcow2_efisize}GiB -1M || die "Cannot create root partition" +parted -s ${mydevice} -- mkpart gentoo_root ${clst_qcow2_roottype} ${clst_qcow2_efisize}GiB -1M || qcow2die "Cannot create root partition" # mark it as generic linux filesystem partition -parted -s ${mydevice} -- type 2 0FC63DAF-8483-4772-8E79-3D69D8477DE4 || die "Cannot set root partition UUID" +parted -s ${mydevice} -- type 2 0FC63DAF-8483-4772-8E79-3D69D8477DE4 || qcow2die "Cannot set root partition UUID" # note down name mypartroot=${mydevice}p2 # re-read the partition table -partprobe ${mydevice} || die "Probing partition table failed" +partprobe ${mydevice} || qcow2die "Probing partition table failed" # make a vfat filesystem in p1 -mkfs.fat -F 32 ${mypartefi} || die "Formatting EFI partition failed" +mkfs.fat -F 32 ${mypartefi} || qcow2die "Formatting EFI partition failed" # make an xfs filesystem in p2 -mkfs.xfs ${mypartroot} || die "Formatting root partition failed" +mkfs.xfs ${mypartroot} || qcow2die "Formatting root partition failed" # mount things -# we need a mount point- how do we get one? -mymountpoint="${1}.tmp.mnt" -mkdir -p "${mymountpoint}" || die "Could not create root mount point" -mount ${mypartroot} "${mymountpoint}" || die "Could not mount root partition" -mkdir -p "${mymountpoint}"/boot || die "Could not create boot mount point" -mount ${mypartefi} "${mymountpoint}/boot" || die "Could not mount boot partition" +mkdir -p "${mymountpoint}" || qcow2die "Could not create root mount point" +mount ${mypartroot} "${mymountpoint}" || qcow2die "Could not mount root partition" +mkdir -p "${mymountpoint}"/boot || qcow2die "Could not create boot mount point" +mount ${mypartefi} "${mymountpoint}/boot" || qcow2die "Could not mount boot partition" # copy contents in; the source is the stage dir and not any "iso content" -cp -a "${clst_stage_path}"/* "${mymountpoint}/" || die "Could not copy content into mounted image" +cp -a "${clst_stage_path}"/* "${mymountpoint}/" || qcow2die "Could not copy content into mounted image" # at this point we have a working system # create a CONTENTS.gz file -pushd "${mymountpoint}" || die "Could not cd into mountpoint" -find . > "${1}.CONTENTS" || die "Could not list files in mountpoint" -popd || die "Could not cd out of mountpoint" -gzip "${1}.CONTENTS" || die "Could not compress file list" +pushd "${mymountpoint}" || qcow2die "Could not cd into mountpoint" +find . > "${1}.CONTENTS" || qcow2die "Could not list files in mountpoint" +popd || qcow2die "Could not cd out of mountpoint" +gzip "${1}.CONTENTS" || qcow2die "Could not compress file list" # note: the following must already have been done by the stage2: # - rudimentary configuration -# - installation of cloud-init +# - installation of cloud-init if requested # - installation of kernel # - installation of fallback efi loader # - enabling of services # luckily efi requires no image magic, just regular files... # unmount things -umount "${mymountpoint}/boot" || die "Could not unmount boot partition" -umount "${mymountpoint}" || die "Could not unmount root partition" +umount "${mymountpoint}/boot" || qcow2die "Could not unmount boot partition" +umount "${mymountpoint}" || qcow2die "Could not unmount root partition" # disconnect the nbd -qemu-nbd -d ${mydevice} || die "Could not disconnect nbd0" +qemu-nbd -d ${mydevice} || qcow2die "Could not disconnect nbd0" # rewrite with stream compression -qemu-img convert -c -O qcow2 "${1}.tmp.qcow2" "${1}" || die "Could not compress QCOW2 file" +qemu-img convert -c -O qcow2 "${1}.tmp.qcow2" "${1}" || qcow2die "Could not compress QCOW2 file" # clean up -rm "${1}.tmp.qcow2" || die "Could not delete uncompressed QCOW2 file" -rmdir "${mymountpoint}/boot" || die "Could not remove boot mountpoint" -rmdir "${mymountpoint}" || die "Could not remove root mountpoint" +rm "${1}.tmp.qcow2" || qcow2die "Could not delete uncompressed QCOW2 file" +rmdir "${mymountpoint}/boot" || qcow2die "Could not remove boot mountpoint" +rmdir "${mymountpoint}" || qcow2die "Could not remove root mountpoint" # Finished...