From 8a3fe39a22f7a79861147558aab632b34a0f7f08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20=C5=A0urda?= Date: Fri, 3 Jun 2022 07:15:43 +0800 Subject: [PATCH] Add MAAS support - unfinished --- build.sh | 91 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 74 insertions(+), 17 deletions(-) diff --git a/build.sh b/build.sh index 7b43b16..b3d38fd 100755 --- a/build.sh +++ b/build.sh @@ -1,26 +1,56 @@ #!/bin/bash OS_CODENAME=focal +OS_VERSION=20.04 DEV=/dev/nbd0 +TYPE=cloud +DOWNLOAD=yes +MAAS_ID=20220524 -#sudo apt -yq install syslinux syslinux-common syslinux-efi extlinux +if [ ${TYPE} == cloud ]; then + ROOTPART="${DEV}p1" +else + BOOTPART="${DEV}p1" + ROOTPART="${DEV}p2" +fi -mkdir -p build mnt out +function download(){ + if [ -z "$DOWNLOAD" ]; then + return + fi + echo "Downloading" + if [ ${TYPE} == "cloud" ]; then + wget -O build/${TYPE}/initrd.img \ + https://cloud-images.ubuntu.com/$OS_CODENAME/current/unpacked/${OS_CODENAME}-server-cloudimg-amd64-initrd-generic + wget -O build/${TYPE}/vmlinuz \ + https://cloud-images.ubuntu.com/${OS_CODENAME}/current/unpacked/${OS_CODENAME}-server-cloudimg-amd64-vmlinuz-generic + wget -O build/${TYPE}/rootfs.tar.xz \ + https://cloud-images.ubuntu.com/${OS_CODENAME}/current/${OS_CODENAME}-server-cloudimg-amd64-root.tar.xz + else + wget -O build/${TYPE}/initrd.img \ + https://images.maas.io/ephemeral-v3/stable/${OS_CODENAME}/amd64/${MAAS_ID}/ga-${OS_VERSION}/boot-initrd + wget -O build/${TYPE}/vmlinuz \ + https://images.maas.io/ephemeral-v3/stable/${OS_CODENAME}/amd64/${MAAS_ID}/ga-${OS_VERSION}/boot-kernel + wget -O build/${TYPE}/rootfs.squashfs \ + https://cloud-images.ubuntu.com/${OS_CODENAME}/amd64/${MAAS_ID}/squashfs +fi +} -echo "Downloading" -#wget -O build/initrd.img https://cloud-images.ubuntu.com/$OS_CODENAME/current/unpacked/${OS_CODENAME}-server-cloudimg-amd64-initrd-generic -#wget -O build/vmlinuz https://cloud-images.ubuntu.com/${OS_CODENAME}/current/unpacked/${OS_CODENAME}-server-cloudimg-amd64-vmlinuz-generic -#wget -O build/rootfs.tar.xz https://cloud-images.ubuntu.com/${OS_CODENAME}/current/${OS_CODENAME}-server-cloudimg-amd64-root.tar.xz +sudo apt -yq install syslinux syslinux-common syslinux-efi extlinux f2fs-tools -KERNEL_VERSION="$(file build/vmlinuz |cut -d, -f2|cut -d\ -f3)" +mkdir -p build/${TYPE} mnt out squashmount + +download + +KERNEL_VERSION="$(file build/${TYPE}/vmlinuz |cut -d, -f2|cut -d\ -f3)" echo "Detected kernel ${KERNEL_VERSION}" echo "Creating empty image" -qemu-img create -f qcow2 build/ubuntu-${OS_CODENAME}.qcow2 2G || exit 1 +qemu-img create -f qcow2 build/${TYPE}/ubuntu-${OS_CODENAME}.qcow2 2G || exit 1 echo "attaching image" sudo modprobe nbd -sudo qemu-nbd -c "${DEV}" build/ubuntu-${OS_CODENAME}.qcow2 || exit 1 +sudo qemu-nbd -c "${DEV}" build/${TYPE}/ubuntu-${OS_CODENAME}.qcow2 || exit 1 echo "partitioning" sudo parted -s "${DEV}" -- \ @@ -32,25 +62,44 @@ until [ -b "${DEV}1" ] || [ -b "${DEV}p1" ]; do sleep 1 done + echo "installing hybrid bootloader" sudo dd bs=440 count=1 conv=notrunc \ if=/usr/lib/syslinux/mbr/gptmbr.bin of="$DEV" 2> /dev/null sync -echo "mkfs.btrfs" -sudo mkfs.btrfs -L cloudimg-rootfs "${DEV}p1" || exit 1 +if [ ${TYPE} == "cloud" ]; then + echo "mkfs.btrfs" + sudo mkfs.btrfs -L rootfs ${ROOTPART}" || exit 1 +else + echo "mkfs.btrfs" + sudo mkfs.btrfs -L boot ${BOOTPART}" || exit 1 + echo "mkfs.f2fs" + sudo mkfs.f2fs -l rootfs -O extra_attr,inode_checksum,sb_checksum,compression ${ROOTPART} || exit 1 +fi sync echo "mounting root" -sudo mount -t btrfs "${DEV}p1" mnt || exit 1 +if [ ${TYPE} == "cloud" ]; then + sudo mount -t btrfs ${ROOTPART} mnt || exit 1 +else + sudo mount -t f2fs ${ROOTPART} mnt || exit 1 + sudo mkdir -p mnt/boot + sudo mount -t btrfs ${BOOTPART} mnt/boot || exit 1 +fi echo "unpacking root fs" -sudo tar xJf build/rootfs.tar.xz -C mnt || exit 1 +if [ ${TYPE} == "cloud" ]; then + sudo tar xJf build/${TYPE}/rootfs.tar.xz -C mnt || exit 1 +else + sudo mount -t squashfs build/${TYPE}/rootfs.squashfs squashmount -o ro,loop || exit 1 + cp -ar squashmount/. mnt +fi echo "installing kernel and inintd" sudo mkdir -p mnt/boot -sudo cp -ar build/initrd.img "mnt/boot/initrd.img-${KERNEL_VERSION}" -sudo cp -ar build/vmlinuz "mnt/boot/vmlinuz-${KERNEL_VERSION}" +sudo cp -ar build/${TYPE}/initrd.img "mnt/boot/initrd.img-${KERNEL_VERSION}" +sudo cp -ar build/${TYPE}/vmlinuz "mnt/boot/vmlinuz-${KERNEL_VERSION}" sudo ln -s "vmlinuz-${KERNEL_VERSION}" mnt/boot/vmlinuz sudo ln -s "initrd.img-${KERNEL_VERSION}" mnt/boot/initrd.img @@ -65,20 +114,28 @@ if [ -n "$1" ]; then fi echo "here is the fstab" -echo "LABEL=cloudimg-rootfs / btrfs defaults 0 1" | sudo tee mnt/etc/fstab +if [ ${TYPE} == "cloud" ]; then + echo "LABEL=rootfs / btrfs defaults 0 1" | sudo tee mnt/etc/fstab +else + echo "LABEL=rootfs / f2fs defaults 0 1" | sudo tee mnt/etc/fstab + echo "LABEL=boot /boot btrfs defaults 0 1" | sudo tee -a mnt/etc/fstab +fi echo "installing syslinux bootloader" sudo extlinux -i mnt/boot/syslinux echo "umounting" sync +if [ ${TYPE} != "cloud" ]; then + umount /mnt/boot +fi sudo umount mnt echo "detaching" sudo qemu-nbd -d "${DEV}" echo "compressing" -qemu-img convert -cpO qcow2 build/ubuntu-${OS_CODENAME}.qcow2 out/ubuntu-${OS_CODENAME}.qcow2 +qemu-img convert -cpO qcow2 build/${TYPE}/ubuntu-${OS_CODENAME}.qcow2 out/ubuntu-${OS_CODENAME}-${TYPE}.qcow2 # rm -f out/ubuntu-${OS_CODENAME}.qcow2 echo "done"