From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id D05F7158046 for ; Fri, 11 Oct 2024 22:07:48 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 843C6E29BE; Fri, 11 Oct 2024 22:07:47 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 646C4E29BE for ; Fri, 11 Oct 2024 22:07:47 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 6B8793431E6 for ; Fri, 11 Oct 2024 22:07:46 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 9B6721CBE for ; Fri, 11 Oct 2024 22:07:44 +0000 (UTC) From: "Andreas K. Hüttel" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Andreas K. Hüttel" Message-ID: <1728684445.2810d76d9481ded6f68d0faad642bd9b022436e3.dilfridge@gentoo> Subject: [gentoo-commits] proj/catalyst:dilfridge/qcow2 commit in: targets/support/ X-VCS-Repository: proj/catalyst X-VCS-Files: targets/support/create-qcow2.sh X-VCS-Directories: targets/support/ X-VCS-Committer: dilfridge X-VCS-Committer-Name: Andreas K. Hüttel X-VCS-Revision: 2810d76d9481ded6f68d0faad642bd9b022436e3 X-VCS-Branch: dilfridge/qcow2 Date: Fri, 11 Oct 2024 22:07:44 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 899dab68-5fb3-49ac-aa2c-1de6d36859e9 X-Archives-Hash: be5c4c5001f7470261d5d9215a680ad8 commit: 2810d76d9481ded6f68d0faad642bd9b022436e3 Author: Andreas K. Hüttel gentoo org> AuthorDate: Fri Oct 11 22:07:25 2024 +0000 Commit: Andreas K. Hüttel gentoo 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 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...