diff options
author | Simon Spöhel <simon@spoehel.ch> | 2023-08-24 14:35:18 +0000 |
---|---|---|
committer | Simon Spöhel <simon@spoehel.ch> | 2023-08-24 14:41:06 +0000 |
commit | 7c674a11ca49df28f78f49a876660891ad7b4355 (patch) | |
tree | 6e0663b70509c15ec97f15ba24137117d30a9acb /share/build-scripts/debconf | |
parent | Adding todo file (FIXME). (diff) | |
download | compute-tools-7c674a11ca49df28f78f49a876660891ad7b4355.tar.xz compute-tools-7c674a11ca49df28f78f49a876660891ad7b4355.zip |
basic cnt build for vms (debconf only)
Diffstat (limited to 'share/build-scripts/debconf')
-rwxr-xr-x | share/build-scripts/debconf | 170 |
1 files changed, 164 insertions, 6 deletions
diff --git a/share/build-scripts/debconf b/share/build-scripts/debconf index 900242d..8307695 100755 --- a/share/build-scripts/debconf +++ b/share/build-scripts/debconf @@ -31,11 +31,12 @@ CACHE="/var/cache/${PROGRAM}/build-$(basename ${SCRIPT})" CONFIG="/etc/${SOFTWARE}/config" HOOKS="/etc/${SOFTWARE}/hooks" MACHINES="/var/lib/machines" +VMS="/srv/container/vms" Parameters () { - GETOPT_LONGOPTIONS="bind:,bind-ro:,script:,name:,preseed-file:" - GETOPT_OPTIONS="b:,s:,n:,p:" + GETOPT_LONGOPTIONS="bind:,bind-ro:,boot-method:,script:,name:,preseed-file:,vm" + GETOPT_OPTIONS="b:,s:,n:,p:,v:" PARAMETERS="$(getopt --longoptions ${GETOPT_LONGOPTIONS} --name=${SCRIPT} --options ${GETOPT_OPTIONS} --shell sh -- ${@})" @@ -60,6 +61,11 @@ Parameters () shift 2 ;; + --boot-method) + BOOT_METHOD="${2}" + shift 2 + ;; + --cnt.auto) # ignore shift 2 @@ -85,6 +91,11 @@ Parameters () shift 2 ;; + --vm) + VM="TRUE" + shift 1 + ;; + --) shift 1 break @@ -100,7 +111,7 @@ Parameters () Usage () { - echo "Usage: container build -n|--name NAME -s|--script ${SCRIPT} -- [-p|--preseed-file FILE]" >&2 + echo "Usage: container build -n|--name NAME -s|--script ${SCRIPT} [--boot-method systemd-nswawn|qemu] -- [-p|--preseed-file FILE]" >&2 exit 1 } @@ -251,9 +262,20 @@ Umount () fi # Unmounting pseudo-filesystems + # FIXME this should not be necessary + umount -f "${DIRECTORY}/dev" > /dev/null 2>&1 || true 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 + + # Unmounting disk images + if [ "${BOOT_METHOD}" = "qemu" ] + then + umount -f "${DIRECTORY}/boot/efi" > /dev/null 2>&1 || true + umount -f "${DIRECTORY}" > /dev/null 2>&1 || true + rmdir "${MACHINES}/${NAME}" + kpartx -d "${VMS}/${NAME}/root.img" + fi } Chroot () @@ -368,7 +390,8 @@ Bootstrap () DIRECTORY="${1}" EXCLUDE="ifupdown" - INCLUDE="dbus" + #FIXME amd64 hardcoded + INCLUDE="dbus,linux-image-amd64,grub-efi-amd64" # apt repositories INCLUDE="${INCLUDE},gnupg" @@ -1101,6 +1124,60 @@ EOF done } +Configure_vm () +{ + DIRECTORY="${1}" + + # FIXME duplicate definition + EFI_PARTITION="p1" + SYSTEM_PARTITION="p2" + EFI_UUID="$(lsblk --output UUID --noheadings /dev/mapper/loop0${EFI_PARTITION})" + ROOT_UUID="$(lsblk --output UUID --noheadings /dev/mapper/loop0${SYSTEM_PARTITION})" + echo "UUID=${ROOT_UUID} / ext4 discard,noatime,errors=remount-ro 0 1" >> ${DIRECTORY}/etc/fstab + echo "UUID=${EFI_UUID} /boot/efi vfat umask=0077 0 1" >> ${DIRECTORY}/etc/fstab + + mkdir -p ${DIRECTORY}/boot/efi/EFI/progress-linux + cp ${DIRECTORY}/usr/lib/grub/x86_64-efi/monolithic/grubx64.efi ${DIRECTORY}/boot/efi/EFI/progress-linux/ + +cat > ${DIRECTORY}/boot/efi/EFI/progress-linux/grub.cfg << EOF +search.fs_uuid ${ROOT_UUID} root +set prefix=(\$root)'/boot/grub' +configfile \$prefix/grub.cfg +EOF + +cat >> ${DIRECTORY}/etc/initramfs-tools/modules << EOF + +# enable virtio +virtio_pci +virtio_blk +virtio_net +EOF + + Chroot ${DIRECTORY} update-initramfs -u + + mount -o bind /dev ${DIRECTORY}/dev + Chroot ${DIRECTORY} update-grub + + # FIXME workaround for grub not being installed as EFI/boot/bootx86.efi + mkdir "${DIRECTORY}/boot/efi/EFI/boot" + echo 'fs0:\EFI\progress-linux\grubx64.efi' > "${DIRECTORY}/boot/efi/EFI/boot/startup.nsh" + + sed -i \ + -e "s|@BOOT_METHOD@|${BOOT_METHOD}|g" \ + -e "s|@CPU@|${CPU}|g" \ + -e "s|@MEMORY@|${MEMORY}|g" \ + -e "s|@DISPLAY@|${DISPLAY}|g" \ + -e "s|@SERIAL@|${SERIAL}|g" \ + -e "s|@MONITOR@|${MONITOR}|g" \ + -e "s|@FIRMWARE@|${FIRMWARE}|g" \ + -e "s|@DRIVE@|${DRIVE}|g" \ + -e "s|@NETDEV@|${NETDEV}|g" \ + -e "s|@QEMU_RAW@|${QEMU_RAW}|g" \ + "${CONFIG}/${NAME}.conf" + +} + + Commands () { DIRECTORY="${1}" @@ -1117,6 +1194,7 @@ Commands () -e "s|^bind=.*|bind=${BIND}|g" \ -e "s|^bind-ro=.*|bind-ro=${BIND_RO}|g" \ -e "s|^network-veth-extra=.*|network-veth-extra=${HOST_INTERFACE_NAME}:eno1|g" \ + -e "s|@PRIMARY_TAP@|${HOST_INTERFACE_NAME}|g" \ "${CONFIG}/${NAME}.conf" if [ "${NETWORK_NUMBER}" -ge 2 ] @@ -1179,6 +1257,67 @@ Commands () esac } +Create_vm_disk () +{ + mkdir -p "${VMS}/${NAME}" + + # FIXME drive-image file size as arg or preseed + DRIVE_SIZE=12000 + + # FIXME image name + dd if=/dev/zero of="${VMS}/${NAME}/root.img" bs=1024k count=0 seek="${DRIVE_SIZE}" + parted "${VMS}/${NAME}/root.img" mklabel gpt + + + # FIXME only for root image + case "${FIRMWARE}" in + legacy) + # FIXME number partitions and size scrip and everything + parted -a optimal "${VMS}/${NAME}/root.img" mkpart BIOS-BOOT 1MB 2MB + parted "${VMS}/${NAME}/root.img" set 1 bios_grub on + parted -a optimal "${VMS}/${NAME}/root.img" mkpart SYSTEM 2MB 100% + SYSTEM_PARTITION="p2" + ;; + + efi) + parted -a optimal "${VMS}/${NAME}/root.img" mkpart EFI fat32 1M 1000M + parted "${VMS}/${NAME}/root.img" set 1 esp on + parted -a optimal "${VMS}/${NAME}/root.img" mkpart SYSTEM 1000MB 100% + EFI_PARTITION="p1" + SYSTEM_PARTITION="p2" + + # efi bios + cp /usr/share/qemu/OVMF.fd "${VMS}/${NAME}/firmware.fd" + ;; + + *) + echo "Error, unrecognized firware." >&2 + exit 1 + esac + + kpartx -av "${VMS}/${NAME}/root.img" + # FIXME loop number hardcoded + mkfs.ext4 "/dev/mapper/loop0${SYSTEM_PARTITION}" + + if [ ! -z "${EFI_PARTITION}" ] + then + mkfs.vfat -F 32 "/dev/mapper/loop0${EFI_PARTITION}" + fi + + # FIXME swap, more discs +} + +Mount_vm_disk () +{ + # FIXME probably not necessary + mkdir -p "${MACHINES}/${NAME}" + + # FIXME partition number hardcoded + mount "/dev/mapper/loop0${SYSTEM_PARTITION}" "${MACHINES}/${NAME}" + mkdir -p "${MACHINES}/${NAME}/boot/efi" + mount "/dev/mapper/loop0${EFI_PARTITION}" "${MACHINES}/${NAME}/boot/efi" +} + # Trap function trap 'Umount' EXIT HUP INT QUIT TERM @@ -1218,6 +1357,13 @@ else INSTALLER="image" fi +case "${BOOT_METHOD}" in + qemu) + Create_vm_disk + Mount_vm_disk + ;; +esac + case "${INSTALLER}" in bootstrap) ## Dependencies @@ -1246,8 +1392,11 @@ case "${INSTALLER}" in Cleanup_system "${CACHE}/${DISTRIBUTION}_${ARCHITECTURE}" ## Specific parts - mkdir -p "${MACHINES}" - cp -a "${CACHE}/${DISTRIBUTION}_${ARCHITECTURE}" "${MACHINES}/${NAME}" + + # FIXME is there a better way to do this? + mkdir -p "${MACHINES}/${NAME}" + cp -a "${CACHE}/${DISTRIBUTION}_${ARCHITECTURE}/"* "${MACHINES}/${NAME}/" + ;; image) @@ -1300,6 +1449,15 @@ Mount Configure_system "${MACHINES}/${NAME}" Configure_network "${MACHINES}/${NAME}" Configure_systemd_networkd "${MACHINES}/${NAME}" + +case "${BOOT_METHOD}" in + qemu) + echo DEBUG: Configure_vm start + Configure_vm "${MACHINES}/${NAME}" + echo DEBUG: Configure_vm end + ;; +esac + Cleanup_system "${MACHINES}/${NAME}" Commands "${MACHINES}/${NAME}" |