From 10db57cf85a99152e57b409dfae30505bc210d25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sp=C3=B6hel?= Date: Wed, 7 Apr 2021 08:27:25 +0200 Subject: Adding basic functionality to create VM images using debconf. --- share/build-scripts/qemu | 240 +++++++++++++++++++++++------------------------ 1 file changed, 118 insertions(+), 122 deletions(-) (limited to 'share/build-scripts') diff --git a/share/build-scripts/qemu b/share/build-scripts/qemu index 28c5262..3bfa046 100755 --- a/share/build-scripts/qemu +++ b/share/build-scripts/qemu @@ -1,6 +1,7 @@ #!/bin/sh # Copyright (C) 2014-2021 Daniel Baumann +# 2021 Simon Spöhel # # SPDX-License-Identifier: GPL-3.0+ # @@ -35,7 +36,7 @@ Parameters () GETOPT_LONGOPTIONS="bind:,bind-ro:,script:,name:,preseed-file:" GETOPT_OPTIONS="b:,s:,n:,p:" - PARAMETERS="$(getopt --longoptions ${GETOPT_LONGOPTIONS} --name=${SCRIPT} --options ${GETOPT_OPTIONS} --shell sh -- ${@})" + PARAMETERS="$(getopt --longoptions ${GETOPT_LONGOPTIONS} --name=${SCRIPT} --options ${GETOPT_OPTIONS} --shell sh -- "${@}")" if [ "${?}" != "0" ] then @@ -127,137 +128,73 @@ then exit 1 fi -Mount () +Create_Disk () { - # Mounting rw bind mounts - if [ -n "${BIND}" ] - then - BINDS="$(echo ${BIND} | sed -e 's|;| |g')" + # FIXME image size as arg or preseed + IMG_SIZE=12000 + #BOOT_METHOD="bios" + BOOT_METHOD="efi" + + dd if=/dev/zero of="${MACHINES}/${NAME}.img" bs=1024k count=0 seek="${IMG_SIZE}" + parted "${MACHINES}/${NAME}.img" mklabel gpt + + + case "${BOOT_METHOD}" in + bios) + # FIXME number partitions and size scrip and everything + parted -a optimal "${MACHINES}/${NAME}.img" mkpart BIOS-BOOT 1MB 2MB + parted "${MACHINES}/${NAME}.img" set 1 bios_grub on + parted -a optimal "${MACHINES}/${NAME}.img" mkpart SYSTEM 2MB 100% + SYSTEM_PARTITION="p2" + ;; - for ENTRY in ${BINDS} - do - SOURCE="$(echo ${ENTRY} | awk -F: '{ print $1 }')" - TARGET="$(echo ${ENTRY} | awk -F: '{ print $2 }')" + efi) + parted -a optimal "${MACHINES}/${NAME}.img" mkpart EFI fat32 1M 1000M + parted "${MACHINES}/${NAME}.img" set 1 esp on + parted -a optimal "${MACHINES}/${NAME}.img" mkpart SYSTEM 1000MB 100% + EFI_PARTITION="p1" + SYSTEM_PARTITION="p2" + ;; - mkdir -p "${SOURCE}" - mkdir -p "${MACHINES}/${NAME}/${TARGET}" + *) + echo "Error, unrecognized BOOT_METHOD" >&2 + exit 1 + esac - mount -o bind "${SOURCE}" "${MACHINES}/${NAME}/${TARGET}" - done - fi + kpartx -av "${MACHINES}/${NAME}.img" + # FIXME loop number hardcoded + mkfs.ext4 "/dev/mapper/loop0${SYSTEM_PARTITION}" - # Mounting ro bind mounts - if [ -n "${BIND_RO}" ] + if [ ! -z "${EFI_PARTITION}" ] then - BINDS_RO="$(echo ${BIND_RO} | sed -e 's|;| |g')" - - for ENTRY in ${BINDS_RO} - do - SOURCE="$(echo ${ENTRY} | awk -F: '{ print $1 }')" - TARGET="$(echo ${ENTRY} | awk -F: '{ print $2 }')" - - mkdir -p "${SOURCE}" - mkdir -p "${MACHINES}/${NAME}/${TARGET}" - - mount -o rbind "${SOURCE}" "${MACHINES}/${NAME}/${TARGET}" - done + mkfs.vfat -F 32 "/dev/mapper/loop0${EFI_PARTITION}" fi - # Mounting overlay mounts - if [ -n "${CNT_OVERLAY}" ] - then - CNT_OVERLAYS="$(echo ${CNT_OVERLAY} | sed -e 's|;| |g')" - - COUNT="0" - for CNT_OVERLAY in ${CNT_OVERLAYS} - do - DIRECTORY_LOWER="$(echo ${CNT_OVERLAY} | awk -F: '{ print $1 }')" - DIRECTORY_UPPER="$(echo ${CNT_OVERLAY} | awk -F: '{ print $2 }')" - DIRECTORY_WORK="$(echo ${CNT_OVERLAY} | awk -F: '{ print $3 }')" - DIRECTORY_MERGED="$(echo ${CNT_OVERLAY} | awk -F: '{ print $4 }')" - - COUNT="$((${COUNT} + 1))" - CNT_OVERLAY_OPTION="$(echo ${CNT_OVERLAY_OPTIONS} | awk -F ';' "{ print \$${COUNT} }")" - - for DIRECTORY in "${DIRECTORY_LOWER}" "${DIRECTORY_UPPER}" "${DIRECTORY_WORK}" "${DIRECTORY_MERGED}" - do - mkdir -p "${DIRECTORY}" - done + # FIXME swap, more discs +} - if ! findmnt -n -o SOURCE "${DIRECTORY_MERGED}" | grep -qs '^overlay-' - then - if [ -n "${CNT_OVERLAY_OPTION}" ] - then - CNT_OVERLAY_OPTION="-o ${CNT_OVERLAY_OPTION}" - fi +Mount_Disk () +{ + # FIXME probably not necessary + mkdir -p "${MACHINES}/${NAME}" - mount -t overlay overlay-${NAME} ${CNT_OVERLAY_OPTION} -olowerdir="${DIRECTORY_LOWER}",upperdir="${DIRECTORY_UPPER}",workdir="${DIRECTORY_WORK}" "${DIRECTORY_MERGED}" - fi - done - fi + # FIXME partition number hardcoded + mount "/dev/mapper/loop0${SYSTEM_PARTITION}" "${MACHINES}/${NAME}" } Umount () { - # Unmounting overlay mounts - if [ -n "${CNT_OVERLAY}" ] - then - CNT_OVERLAYS="$(echo ${CNT_OVERLAY} | sed -e 's|;| |g')" - - for CNT_OVERLAY in ${CNT_OVERLAYS} - do - DIRECTORY_LOWER="$(echo ${CNT_OVERLAY} | awk -F: '{ print $1 }')" - DIRECTORY_UPPER="$(echo ${CNT_OVERLAY} | awk -F: '{ print $2 }')" - DIRECTORY_WORK="$(echo ${CNT_OVERLAY} | awk -F: '{ print $3 }')" - DIRECTORY_MERGED="$(echo ${CNT_OVERLAY} | awk -F: '{ print $4 }')" - - umount -f "${DIRECTORY_MERGED}" > /dev/null 2>&1 || true - - for DIRECTORY in "${DIRECTORY_LOWER}" "${DIRECTORY_UPPER}" "${DIRECTORY_WORK}" "${DIRECTORY_MERGED}" - do - rmdir --ignore-fail-on-non-empty --parents ${DIRECTORY} > /dev/null 2>&1 || true - done - done - - # empty workdir otherwise there might happen stale file handles - if [ -d "${DIRECTORY_WORK}" ] - then - rm --preserve-root --one-file-system -rf "${DIRECTORY_WORK}"/* - fi - fi - - # Unmounting ro bind mounts - if [ -n "${BIND_RO}" ] - then - # unmount in reverse order to allow nested bind mounts - BINDS_RO="$(echo ${BIND_RO} | sed -e 's|;| |g' | awk '{ for (i=NF; i>=1; i--) printf "%s ", $i; print ""}')" - - for ENTRY in ${BINDS_RO} - do - TARGET="$(echo ${ENTRY} | awk -F: '{ print $2 }')" - - umount -f "${MACHINES}/${NAME}/${TARGET}" > /dev/null 2>&1 || true - done - fi - - # Unmounting rw bind mounts - if [ -n "${BIND}" ] - then - # unmount in reverse order to allow nested bind mounts - BINDS="$(echo ${BIND} | sed -e 's|;| |g' | awk '{ for (i=NF; i>=1; i--) printf "%s ", $i; print ""}')" - - for ENTRY in ${BINDS} - do - TARGET="$(echo ${ENTRY} | awk -F: '{ print $2 }')" - - umount -f "${MACHINES}/${NAME}/${TARGET}" > /dev/null 2>&1 || true - done - fi - # Unmounting pseudo-filesystems umount -f "${DIRECTORY}/dev/pts" > /dev/null 2>&1 || true umount -f "${DIRECTORY}/proc" > /dev/null 2>&1 || true umount -f "${DIRECTORY}/sys" > /dev/null 2>&1 || true + umount -f "${DIRECTORY}/dev" > /dev/null 2>&1 || true + umount -f "${DIRECTORY}/boot/efi" > /dev/null 2>&1 || true + + # Unmounting disk images + # FIXME unmount additinal images + umount -f "${DIRECTORY}" > /dev/null 2>&1 || true + kpartx -d "${MACHINES}/${NAME}.img" } Chroot () @@ -282,6 +219,14 @@ Upgrade_system () mount -o bind /proc "${DIRECTORY}/proc" mount -o bind /sys "${DIRECTORY}/sys" + # also mount dev + mount -o bind /dev "${DIRECTORY}/dev" + + if [ ! -z "${EFI_PARTITION}" ] + then + mkdir -p "${DIRECTORY}/boot/efi" + mount "/dev/mapper/loop0${EFI_PARTITION}" "${DIRECTORY}/boot/efi" + fi # Disable dpkg syncing cat > "${DIRECTORY}/etc/dpkg/dpkg.cfg.d/${SOFTWARE}" << EOF @@ -322,8 +267,11 @@ Cleanup_system () # Unmount pseudo-filesystems umount -f "${DIRECTORY}/dev/pts" > /dev/null 2>&1 || true + # WARNING: dev/pts is still mounted! umount -f "${DIRECTORY}/proc" > /dev/null 2>&1 || true umount -f "${DIRECTORY}/sys" > /dev/null 2>&1 || true + umount -f "${DIRECTORY}/dev" > /dev/null 2>&1 || true + umount -f "${DIRECTORY}/boot/efi" > /dev/null 2>&1 || true } Debconf () @@ -372,7 +320,7 @@ Debootstrap () DIRECTORY="${1}" EXCLUDE="ifupdown" - INCLUDE="dbus" + INCLUDE="dbus,linux-image-amd64,grub-pc" # apt repositories INCLUDE="${INCLUDE},gnupg" @@ -466,10 +414,6 @@ Configure_system () { DIRECTORY="${1}" - # Overwrite resolv.conf from cache with hosts resolv.conf - rm -f "${DIRECTORY}/etc/resolv.conf" - cp /etc/resolv.conf "${DIRECTORY}/etc" - echo "${NAME}" > "${DIRECTORY}/etc/hostname" # Configure apt @@ -1108,15 +1052,67 @@ fi Upgrade_system "${CACHE}/${DISTRIBUTION}_${ARCHITECTURE}" || echo "W: If upgrading the system failed, try removing the cache for your distribution in /var/cache/${PROGRAM}" Cleanup_system "${CACHE}/${DISTRIBUTION}_${ARCHITECTURE}" +# FIXME workaround: /dev/pts in CACHE is still mounted without the following line +umount -f "${DIRECTORY}/dev/pts" > /dev/null 2>&1 || true + ## Specific parts -mkdir -p "${MACHINES}" -cp -a "${CACHE}/${DISTRIBUTION}_${ARCHITECTURE}" "${MACHINES}/${NAME}" +Create_Disk +Mount_Disk -Mount +# FIXME this is evil +cp -a "${CACHE}/${DISTRIBUTION}_${ARCHITECTURE}/"* "${MACHINES}/${NAME}/" Configure_system "${MACHINES}/${NAME}" Configure_network "${MACHINES}/${NAME}" Configure_systemd_networkd "${MACHINES}/${NAME}" + +# configure grub +cat > "${MACHINES}/${NAME}/tmp/device.map" << EOF +(hd0) /dev/loop0 +(hd0,gpt1) /dev/mapper/loop0p1 +(hd0,gpt2) /dev/mapper/loop0p2 +EOF + +case "${BOOT_METHOD}" in + bios) + grub-install \ + --no-floppy \ + --grub-mkdevicemap="${MACHINES}/${NAME}/tmp/device.map" \ + --modules="biosdisk part_gpt ext2 configfile normal multiboot" \ + --root-directory="${MACHINES}/${NAME}" \ + /dev/loop0 + ;; + + efi) + # FIXME + chroot "${MACHINES}/${NAME}" /bin/sh -c 'DEBIAN_FRONTEND=noninteractive; apt-get install grub-efi -y' + chroot "${MACHINES}/${NAME}" grub-install --efi-directory=/boot/efi --no-bootsector --target x86_64-efi --no-nvram --no-uefi-secure-boot --force-extra-removable + echo "pass" + ;; + + *) + echo "Error, unrecognized BOOT_METHOD" >&2 + exit 1 +esac + +chroot "${MACHINES}/${NAME}" update-grub + +# Configure fstab + +# FIXME DIRECTORY +cat > "${DIRECTORY}/etc/fstab" << EOF +# /etc/fstab: static file system information. +# +# Use 'blkid' to print the universally unique identifier for a +# device; this may be used with UUID= as a more robust way to name devices +# that works even if disks are added and removed. See fstab(5). +# +# + +UUID=$(lsblk --fs --noheadings --output UUID /dev/mapper/loop0${SYSTEM_PARTITION}) / ext4 errors=remount-ro 0 1 +EOF + + Cleanup_system "${MACHINES}/${NAME}" Commands "${MACHINES}/${NAME}" -- cgit v1.2.3