Add MAAS support
Some checks failed
buildbot/multibuild_parent Build done.
buildbot/travis_bionic Build done.

- unfinished
This commit is contained in:
Peter Šurda 2022-06-03 07:15:43 +08:00
parent 436fa2b312
commit 8a3fe39a22
Signed by: PeterSurda
GPG Key ID: 3E47497CF67ABB95

View File

@ -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
mkdir -p build mnt out
if [ ${TYPE} == cloud ]; then
ROOTPART="${DEV}p1"
else
BOOTPART="${DEV}p1"
ROOTPART="${DEV}p2"
fi
function download(){
if [ -z "$DOWNLOAD" ]; then
return
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
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
}
KERNEL_VERSION="$(file build/vmlinuz |cut -d, -f2|cut -d\ -f3)"
sudo apt -yq install syslinux syslinux-common syslinux-efi extlinux f2fs-tools
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
if [ ${TYPE} == "cloud" ]; then
echo "mkfs.btrfs"
sudo mkfs.btrfs -L cloudimg-rootfs "${DEV}p1" || exit 1
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"