+ fi
+}
+# }}}
+
+# prepare VM image for usage with debootstrap {{{
+prepare_vm() {
+ if [ -z "$VIRTUAL" ] ; then
+ return 0 # be quiet by intention
+ fi
+
+ if [ -b "$TARGET" ] && [ -n "$VMFILE" ] ; then
+ eerror "Error: specified virtual disk target ($TARGET) is an existing block device."
+ bailout 1
+ fi
+ if [ ! -b "$TARGET" ] && [ -z "$VMFILE" ] ; then
+ eerror "Error: specified virtual disk target ($TARGET) does not exist yet."
+ bailout 1
+ fi
+
+ # make sure loop module is present and a usable loop device exists
+ modprobe loop || true
+ if ! losetup -f >/dev/null 2>&1; then
+ eerror "Error finding usable loop device"
+ bailout 1
+ fi
+
+ # if dm-mod isn't available then kpartx will fail with
+ # "Is device-mapper driver missing from kernel? [...]"
+ modprobe dm-mod || true
+ if ! grep -q 'device-mapper' /proc/misc >/dev/null 2>&1 ; then
+ eerror "Device-mapper support missing in kernel."
+ bailout 1
+ fi
+
+ ORIG_TARGET="$TARGET" # store for later reuse
+
+ if [ -n "$VMFILE" ]; then
+ qemu-img create -f raw "${TARGET}" "${VMSIZE}"
+ fi
+ if [ -n "$VMEFI" ]; then
+ parted -s "${TARGET}" 'mklabel gpt'
+ parted -s "${TARGET}" 'mkpart ESP fat32 1MiB 101MiB'
+ parted -s "${TARGET}" 'set 1 boot on'
+ parted -s "${TARGET}" 'mkpart bios_grub 101MiB 102MiB'
+ parted -s "${TARGET}" 'set 2 bios_grub on'
+ parted -s "${TARGET}" 'mkpart primary ext4 102MiB 100%'
+
+ else
+ # arm64 support largely only exists for GPT
+ if [ "$ARCH" = 'arm64' ]; then
+ einfo "Setting up GPT partitions for arm64"
+ parted -s "${TARGET}" 'mklabel gpt'
+ parted -s "${TARGET}" 'mkpart ESP fat32 1MiB 10MiB'
+ parted -s "${TARGET}" 'set 1 boot on'
+ parted -s "${TARGET}" 'mkpart LINUX ext4 10MiB 100%'
+ else
+ parted -s "${TARGET}" 'mklabel msdos'
+ if [ "$FIXED_DISK_IDENTIFIERS" = "yes" ] ; then
+ einfo "Adjusting disk signature to a fixed (non-random) value"
+ MBRTMPFILE=$(mktemp)
+ dd if="${TARGET}" of="${MBRTMPFILE}" bs=512 count=1
+ echo -en "\\x41\\x41\\x41\\x41" | dd of="${MBRTMPFILE}" conv=notrunc seek=440 bs=1
+ dd if="${MBRTMPFILE}" of="${TARGET}" conv=notrunc
+ fi
+ parted -s "${TARGET}" 'mkpart primary ext4 4MiB 100%'
+ parted -s "${TARGET}" 'set 1 boot on'
+ fi
+ fi
+
+ DEVINFO=$(kpartx -asv "$TARGET") # e.g. 'add map loop0p1 (254:5): 0 20477 linear 7:0 3' - will be multi-line for arm64
+ if [ -z "${DEVINFO}" ] ; then
+ eerror "Error setting up loopback device."
+ bailout 1
+ fi
+
+ # if we're building for arm64, we operate on the first line of $DEVINFO which is the EFI partition
+ if [ "$ARCH" = 'arm64' ]; then
+ LOOP_PART="${DEVINFO##add map }" # 'loop0p1 (254:5): 0 20477 linear 7:0 3'
+ LOOP_PART="${LOOP_PART// */}" # 'loop0p1'
+ LOOP_DISK="${LOOP_PART%p*}" # 'loop0'
+ export ARM_EFI_TARGET="/dev/mapper/$LOOP_PART"
+ DEVINFO=${DEVINFO##*$'\n'} # now set $DEVINFO to the last line which is the OS partition
+ fi
+
+ # hopefully this always works as expected
+ LOOP_PART="${DEVINFO##add map }" # 'loop0p1 (254:5): 0 20477 linear 7:0 3'
+ LOOP_PART="${LOOP_PART// */}" # 'loop0p1'
+ if [ -n "$VMEFI" ]; then
+ export EFI_TARGET="/dev/mapper/$LOOP_PART" # '/dev/mapper/loop0p1'
+ LOOP_PART="${LOOP_PART%p1}p3"
+ fi
+ LOOP_DISK="${LOOP_PART%p*}" # 'loop0'
+ export TARGET="/dev/mapper/$LOOP_PART" # '/dev/mapper/loop0p1'
+
+ if [ -z "$TARGET" ] ; then
+ eerror "Error: target could not be set to according /dev/mapper/* device."
+ bailout 1
+ fi
+}
+# }}}
+
+# make VM image bootable {{{
+grub_install() {
+ if [ -z "${VIRTUAL}" ] ; then
+ return 0
+ fi
+ if [ "${GRUB_INSTALL}" != "yes" ] ; then
+ einfo "Not installing GRUB as requested via \$GRUB_INSTALL=$GRUB_INSTALL"
+ return 0
+ fi
+
+ if ! mount "${TARGET}" "${MNTPOINT}" ; then
+ eerror "Error: Mounting ${TARGET} failed, can not continue."
+ bailout 1
+ fi
+
+ if [ -n "${ARM_EFI_TARGET}" ]; then
+ mkdir -p "${MNTPOINT}"/boot/efi
+ if ! mount "${ARM_EFI_TARGET}" "${MNTPOINT}"/boot/efi ; then
+ eerror "Error: Mounting ${ARM_EFI_TARGET} failed, can not continue."
+ bailout 1
+ fi
+ fi
+
+ mount -t proc none "${MNTPOINT}"/proc
+ mount -t sysfs none "${MNTPOINT}"/sys
+ mount -t devtmpfs udev "${MNTPOINT}"/dev
+ mount -t devpts devpts "${MNTPOINT}"/dev/pts
+
+ if [ -n "$ARM_EFI_TARGET" ]; then
+ einfo "Installing Grub as bootloader into EFI."
+
+ chroot "${MNTPOINT}" grub-install --target=arm64-efi --efi-directory=/boot/efi --bootloader-id=debian --recheck --no-nvram --removable
+ # Has chroot-script installed GRUB to MBR using grub-install (successfully), already?
+ # chroot-script skips installation for unset ${GRUB}
+ elif [[ -z "${GRUB}" ]] || ! dd if="${GRUB}" bs=512 count=1 2>/dev/null | cat -v | grep -Fq GRUB; then
+ einfo "Installing Grub as bootloader."
+
+ if ! chroot "${MNTPOINT}" dpkg --list grub-pc 2>/dev/null | grep -q '^ii' ; then
+ echo "Notice: grub-pc package not present yet, installing it therefore."
+ # shellcheck disable=SC2086
+ DEBIAN_FRONTEND=$DEBIAN_FRONTEND chroot "$MNTPOINT" apt-get -y --no-install-recommends install $DPKG_OPTIONS grub-pc
+ fi
+
+ mkdir -p "${MNTPOINT}/boot/grub"
+ if ! [ -d "${MNTPOINT}"/usr/lib/grub/i386-pc/ ] ; then
+ eerror "Error: grub not installed inside Virtual Machine. Can not install bootloader."
+ bailout 1
+ fi
+ cp -a "${MNTPOINT}"/usr/lib/grub/i386-pc "${MNTPOINT}/boot/grub/"
+
+ if [ -n "$VMEFI" ]; then
+
+ mkdir -p "${MNTPOINT}"/boot/efi
+ mount -t vfat "${EFI_TARGET}" "${MNTPOINT}"/boot/efi
+
+ if ! chroot "${MNTPOINT}" dpkg --list shim-signed 2>/dev/null | grep -q '^ii' ; then
+ echo "Notice: shim-signed package not present yet, installing it therefore."
+ # shellcheck disable=SC2086
+ DEBIAN_FRONTEND=$DEBIAN_FRONTEND chroot "$MNTPOINT" apt-get -y --no-install-recommends install $DPKG_OPTIONS shim-signed
+ fi
+
+ if [ "$(dpkg --print-architecture)" = "arm64" ]; then
+ if ! chroot "${MNTPOINT}" dpkg --list grub-efi-arm64-signed 2>/dev/null | grep -q '^ii' ; then
+ echo "Notice: grub-efi-arm64-signed package not present yet, installing it therefore."
+ # shellcheck disable=SC2086
+ DEBIAN_FRONTEND=$DEBIAN_FRONTEND chroot "$MNTPOINT" apt-get -y --no-install-recommends install $DPKG_OPTIONS grub-efi-arm64-bin grub-efi-arm64-signed
+ fi
+ chroot "$MNTPOINT" grub-install --target=arm64-efi --efi-directory=/boot/efi --uefi-secure-boot --removable "/dev/$LOOP_DISK"
+ elif [ "$(dpkg --print-architecture)" = "i386" ]; then
+ if ! chroot "${MNTPOINT}" dpkg --list grub-efi-ia32-signed 2>/dev/null | grep -q '^ii' ; then
+ echo "Notice: grub-efi-ia32-signed package not present yet, installing it therefore."
+ # shellcheck disable=SC2086
+ DEBIAN_FRONTEND=$DEBIAN_FRONTEND chroot "$MNTPOINT" apt-get -y --no-install-recommends install $DPKG_OPTIONS grub-efi-ia32-bin grub-efi-ia32-signed
+ fi
+ chroot "$MNTPOINT" grub-install --target=i386-efi --efi-directory=/boot/efi --uefi-secure-boot --removable "/dev/$LOOP_DISK"
+ chroot "$MNTPOINT" grub-install --target=i386-pc "/dev/$LOOP_DISK"
+ else
+ if ! chroot "${MNTPOINT}" dpkg --list grub-efi-amd64-signed 2>/dev/null | grep -q '^ii' ; then
+ echo "Notice: grub-efi-amd64-signed package not present yet, installing it therefore."
+ # shellcheck disable=SC2086
+ DEBIAN_FRONTEND=$DEBIAN_FRONTEND chroot "$MNTPOINT" apt-get -y --no-install-recommends install $DPKG_OPTIONS grub-efi-amd64-bin grub-efi-amd64-signed
+ fi
+ chroot "$MNTPOINT" grub-install --target=x86_64-efi --efi-directory=/boot/efi --uefi-secure-boot --removable "/dev/$LOOP_DISK"
+ chroot "$MNTPOINT" grub-install --target=i386-pc "/dev/$LOOP_DISK"
+ fi
+ else
+ dd if="${MNTPOINT}/usr/lib/grub/i386-pc/boot.img" of="${ORIG_TARGET}" conv=notrunc bs=440 count=1
+ case "${_opt_filesystem}" in
+ f2fs)
+ chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos f2fs
+ ;;
+ xfs)
+ chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos xfs
+ ;;
+ # NOTE - we might need to distinguish between further filesystems
+ *)
+ chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos ext2
+ ;;
+ esac
+
+ dd if="${MNTPOINT}/tmp/core.img" of="${ORIG_TARGET}" conv=notrunc seek=1
+ rm -f "${MNTPOINT}/tmp/core.img"
+ fi
+ fi
+
+ # workaround for Debian bug #918590 with lvm + udev:
+ # WARNING: Device /dev/... not initialized in udev database even after waiting 10000000 microseconds
+ if [ -d /run/udev ] ; then
+ einfo "Setting up bind-mount /run/udev"
+ mkdir -p "${MNTPOINT}"/run/udev
+ mount --bind /run/udev "${MNTPOINT}"/run/udev
+ fi
+
+ if [ -n "${BOOT_APPEND}" ] ; then
+ echo "Adding BOOT_APPEND configuration ['${BOOT_APPEND}'] to /etc/default/grub."
+ sed -i "/GRUB_CMDLINE_LINUX_DEFAULT/ s#\"\$# ${BOOT_APPEND}\"#" "${MNTPOINT}/etc/default/grub"
+ fi
+
+ einfo "Updating grub configuration file."
+ chroot "${MNTPOINT}" update-grub
+ chroot "${MNTPOINT}" sync
+
+ case "$RELEASE" in
+ jessie)
+ einfo "Applying workaround for GRUB font path bug in jessie (Debian #787685)."
+ mkdir -p "${MNTPOINT}/boot/grub/fonts/"
+ cp "${MNTPOINT}/usr/share/grub/unicode.pf2" "${MNTPOINT}/boot/grub/fonts/"
+ ;;
+ esac
+
+ if grep -q '^GRUB_DISABLE_LINUX_UUID=.*true' "${MNTPOINT}"/etc/default/grub 2>/dev/null ; then
+ ewarn "GRUB_DISABLE_LINUX_UUID is set to true in /etc/default/grub, not adjusting root= in grub.cfg."
+ ewarn "Please note that your system might NOT be able to properly boot."
+ elif [ -z "$ARM_EFI_TARGET" ]; then
+ einfo "Adjusting grub.cfg for successful boot sequence."
+ sed -i "s;root=[^ ]\\+;root=UUID=$TARGET_UUID;" "${MNTPOINT}"/boot/grub/grub.cfg
+ fi
+
+ # workaround for Debian bug #918590 with lvm + udev:
+ # WARNING: Device /dev/... not initialized in udev database even after waiting 10000000 microseconds
+ try_umount 3 "${MNTPOINT}"/run/udev
+
+ try_umount 3 "${MNTPOINT}"/proc
+ try_umount 3 "${MNTPOINT}"/sys
+ try_umount 3 "${MNTPOINT}"/dev/pts
+ try_umount 3 "${MNTPOINT}"/dev
+
+ try_umount 3 "${MNTPOINT}"/boot/efi
+
+}
+# }}}
+
+# unmount VM image {{{
+umount_target() {
+ if [ -z "${VIRTUAL}" ] ; then
+ return 0
+ fi
+
+ try_umount 3 "${MNTPOINT}"/boot/efi
+
+ try_umount 3 "${MNTPOINT}"
+ kpartx -d "${ORIG_TARGET}" >/dev/null
+ # Workaround for a bug in kpartx which doesn't clean up properly,
+ # see Debian Bug #891077 and Github-PR grml/grml-debootstrap#112
+ if dmsetup ls | grep -q "^${LOOP_PART} "; then
+ kpartx -d "/dev/${LOOP_DISK}" >/dev/null