From 92977510591ea197e65d35fff7005aa345244b53 Mon Sep 17 00:00:00 2001 From: Pat Viafore Date: Tue, 4 Jun 2019 10:50:44 -0500 Subject: [PATCH 1/6] Add kvm image --- .../ubuntu-cpc/hooks.d/base/kvm-image.binary | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 live-build/ubuntu-cpc/hooks.d/base/kvm-image.binary diff --git a/live-build/ubuntu-cpc/hooks.d/base/kvm-image.binary b/live-build/ubuntu-cpc/hooks.d/base/kvm-image.binary new file mode 100644 index 00000000..ded54c61 --- /dev/null +++ b/live-build/ubuntu-cpc/hooks.d/base/kvm-image.binary @@ -0,0 +1,59 @@ +#!/bin/bash -ex +# vi: ts=4 expandtab +# +# Generate KVM images +# + +case ${SUBPROJECT:-} in + minimized) + echo "Skipping minimized $0 builds" + exit 0 + ;; + *) + ;; +esac + +# Only allow amd64 builds for now +case $ARCH in + amd64) + ;; + *) + echo "Linux KVM images are not supported for $ARCH yet."; + exit 0;; +esac + +. config/functions + +mount_d=$(mktemp -d) + +create_derivative "disk" "kvm" #sets ${derivative_img} +mount_disk_image ${derivative_img} ${mount_d} + +# unmount disk image and remove created folders on exit +cleanup_kvm() { + if [ -d "$mount_d" ]; then + umount_disk_image "$mount_d" + fi + rm -rf ${mount_d} ${derivative_img} +} +trap cleanup_kvm EXIT + +chroot ${mount_d} apt-get update + +divert_grub "${mount_d}" +replace_kernel ${mount_d} "linux-kvm" +undivert_grub "${mount_d}" + +#setup grub correctly +env DEBIAN_FRONTEND=noninteractive chroot "${mount_d}" update-grub +replace_grub_root_with_label "${mount_d}" + +# Remove initramfs for kvm image +env DEBIAN_FRONTEND=noninteractive chroot "${mount_d}" apt-get \ + purge -y initramfs-tools busybox-initramfs + +env DEBIAN_FRONTEND=noninteractive chroot "${mount_d}" rm \ + -rf /boot/initrd.img-* /boot/initrd.img + +create_manifest ${mount_d} livecd.ubuntu-cpc.kvm.manifest +convert_to_qcow2 binary/boot/disk-kvm.ext4 livecd.ubuntu-cpc.kvm.img \ No newline at end of file From f1718bf3e48bf3dbc48ea287118231cf195ce142 Mon Sep 17 00:00:00 2001 From: Pat Viafore Date: Tue, 4 Jun 2019 10:52:49 -0500 Subject: [PATCH 2/6] Adding hooks and making replace_kernel function --- live-build/functions | 14 ++++++++++++++ live-build/ubuntu-cpc/hooks.d/base/series/base | 1 + live-build/ubuntu-cpc/hooks.d/base/series/kvm | 2 ++ 3 files changed, 17 insertions(+) create mode 100644 live-build/ubuntu-cpc/hooks.d/base/series/kvm diff --git a/live-build/functions b/live-build/functions index 72fdb056..2bdbb623 100644 --- a/live-build/functions +++ b/live-build/functions @@ -877,3 +877,17 @@ is_live_layer () { done return 1 } + +replace_kernel () { + mountpoint=$1 + new_kernel=$2 + + # Install custom kernel (N.B. the trailing + retains linux-base during this + # operation) + env DEBIAN_FRONTEND=noninteractive chroot "${mountpoint}" apt-get \ + remove --purge --assume-yes '^linux-.*' 'linux-base+' + env DEBIAN_FRONTEND=noninteractive chroot "${mountpoint}" apt-get \ + update --assume-yes + env DEBIAN_FRONTEND=noninteractive chroot "${mountpoint}" apt-get \ + install --assume-yes "${new_kernel}" +} \ No newline at end of file diff --git a/live-build/ubuntu-cpc/hooks.d/base/series/base b/live-build/ubuntu-cpc/hooks.d/base/series/base index f04bdec7..55333887 100644 --- a/live-build/ubuntu-cpc/hooks.d/base/series/base +++ b/live-build/ubuntu-cpc/hooks.d/base/series/base @@ -2,6 +2,7 @@ depends root-dir depends tarball depends squashfs depends disk-image +depends kvm depends qcow2 depends vmdk depends vagrant diff --git a/live-build/ubuntu-cpc/hooks.d/base/series/kvm b/live-build/ubuntu-cpc/hooks.d/base/series/kvm new file mode 100644 index 00000000..6c5d8b8c --- /dev/null +++ b/live-build/ubuntu-cpc/hooks.d/base/series/kvm @@ -0,0 +1,2 @@ +depends disk-image +base/kvm-image.binary \ No newline at end of file From d1c8c42d9c71bd57ba1838336fdbe9bedcd1ee26 Mon Sep 17 00:00:00 2001 From: Pat Viafore Date: Tue, 4 Jun 2019 14:23:25 -0500 Subject: [PATCH 3/6] Add changelog --- debian/changelog | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/debian/changelog b/debian/changelog index 924dc2b0..d3991fa3 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +livecd-rootfs (2.590) eoan; urgency=medium + + * Add support for linux-kvm images + + -- Patrick Viafore Tue, 04 Jun 2019 02:13:00 -0500 + livecd-rootfs (2.589) eoan; urgency=medium * Add support for the 'kassel' subflavor of UC18 images. From 5d425edb863841c6341b55d9124fca1f9ee9ea43 Mon Sep 17 00:00:00 2001 From: Pat Viafore Date: Tue, 4 Jun 2019 15:31:46 -0500 Subject: [PATCH 4/6] Various fixes from code review. Cleaning indices; removing superfluous apt-get update; adding debug messages; adding better output name --- .../ubuntu-cpc/hooks.d/base/kvm-image.binary | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/live-build/ubuntu-cpc/hooks.d/base/kvm-image.binary b/live-build/ubuntu-cpc/hooks.d/base/kvm-image.binary index ded54c61..716d148a 100644 --- a/live-build/ubuntu-cpc/hooks.d/base/kvm-image.binary +++ b/live-build/ubuntu-cpc/hooks.d/base/kvm-image.binary @@ -1,9 +1,11 @@ -#!/bin/bash -ex +#!/bin/bash -eux # vi: ts=4 expandtab # -# Generate KVM images +# Generate KVM image # +echo "Building KVM image" + case ${SUBPROJECT:-} in minimized) echo "Skipping minimized $0 builds" @@ -38,7 +40,6 @@ cleanup_kvm() { } trap cleanup_kvm EXIT -chroot ${mount_d} apt-get update divert_grub "${mount_d}" replace_kernel ${mount_d} "linux-kvm" @@ -55,5 +56,9 @@ env DEBIAN_FRONTEND=noninteractive chroot "${mount_d}" apt-get \ env DEBIAN_FRONTEND=noninteractive chroot "${mount_d}" rm \ -rf /boot/initrd.img-* /boot/initrd.img -create_manifest ${mount_d} livecd.ubuntu-cpc.kvm.manifest -convert_to_qcow2 binary/boot/disk-kvm.ext4 livecd.ubuntu-cpc.kvm.img \ No newline at end of file +# Remove indices +env DEBIAN_FRONTEND=noninteractive chroot "${mount_d}" apt-get \ + clean + +create_manifest ${mount_d} livecd.ubuntu-cpc.disk-kvm.manifest +convert_to_qcow2 binary/boot/disk-kvm.ext4 livecd.ubuntu-cpc.disk-kvm.img \ No newline at end of file From 7b2d783047f316650ceec84a6ec26cc2f0e61767 Mon Sep 17 00:00:00 2001 From: Pat Viafore Date: Tue, 4 Jun 2019 16:03:21 -0500 Subject: [PATCH 5/6] Adding apt-get autoremove and setting IMAGE_STR --- live-build/functions | 2 ++ live-build/ubuntu-cpc/hooks.d/base/kvm-image.binary | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/live-build/functions b/live-build/functions index 2bdbb623..ade899f0 100644 --- a/live-build/functions +++ b/live-build/functions @@ -890,4 +890,6 @@ replace_kernel () { update --assume-yes env DEBIAN_FRONTEND=noninteractive chroot "${mountpoint}" apt-get \ install --assume-yes "${new_kernel}" + env DEBIAN_FRONTEND=noninteractive chroot "${mountpoint}" apt-get \ + autoremove --purge --assume-yes } \ No newline at end of file diff --git a/live-build/ubuntu-cpc/hooks.d/base/kvm-image.binary b/live-build/ubuntu-cpc/hooks.d/base/kvm-image.binary index 716d148a..cb98a2b1 100644 --- a/live-build/ubuntu-cpc/hooks.d/base/kvm-image.binary +++ b/live-build/ubuntu-cpc/hooks.d/base/kvm-image.binary @@ -5,7 +5,7 @@ # echo "Building KVM image" - +IMAGE_STR="# CLOUD_IMG: This file was created/modified by the Cloud Image build process" case ${SUBPROJECT:-} in minimized) echo "Skipping minimized $0 builds" @@ -61,4 +61,4 @@ env DEBIAN_FRONTEND=noninteractive chroot "${mount_d}" apt-get \ clean create_manifest ${mount_d} livecd.ubuntu-cpc.disk-kvm.manifest -convert_to_qcow2 binary/boot/disk-kvm.ext4 livecd.ubuntu-cpc.disk-kvm.img \ No newline at end of file +convert_to_qcow2 ${derivative_img} livecd.ubuntu-cpc.disk-kvm.img \ No newline at end of file From be817e0bd137009ee158eafe16bc292421e105bb Mon Sep 17 00:00:00 2001 From: Pat Viafore Date: Thu, 6 Jun 2019 08:15:40 -0500 Subject: [PATCH 6/6] Unmount the image before we convert to qcow2 to avoid corruption --- live-build/ubuntu-cpc/hooks.d/base/kvm-image.binary | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/live-build/ubuntu-cpc/hooks.d/base/kvm-image.binary b/live-build/ubuntu-cpc/hooks.d/base/kvm-image.binary index cb98a2b1..1598390d 100644 --- a/live-build/ubuntu-cpc/hooks.d/base/kvm-image.binary +++ b/live-build/ubuntu-cpc/hooks.d/base/kvm-image.binary @@ -32,6 +32,9 @@ create_derivative "disk" "kvm" #sets ${derivative_img} mount_disk_image ${derivative_img} ${mount_d} # unmount disk image and remove created folders on exit +# even though we unmount manually before we convert to +# qcow2, we have this here just in case we error out before +# that step cleanup_kvm() { if [ -d "$mount_d" ]; then umount_disk_image "$mount_d" @@ -61,4 +64,10 @@ env DEBIAN_FRONTEND=noninteractive chroot "${mount_d}" apt-get \ clean create_manifest ${mount_d} livecd.ubuntu-cpc.disk-kvm.manifest + +# unmount disk image to prevent corruption +# and remove it so the trap doesn't try to unmount it again +umount_disk_image ${mount_d} +rm -rf ${mount_d} + convert_to_qcow2 ${derivative_img} livecd.ubuntu-cpc.disk-kvm.img \ No newline at end of file