ubuntu-cloud-image/build.sh

142 lines
4.1 KiB
Bash
Raw Normal View History

2022-03-24 05:32:02 +00:00
#!/bin/bash
OS_CODENAME=focal
2022-06-02 23:15:43 +00:00
OS_VERSION=20.04
2022-03-24 05:32:02 +00:00
DEV=/dev/nbd0
2022-06-02 23:15:43 +00:00
TYPE=cloud
DOWNLOAD=yes
MAAS_ID=20220524
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"
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
}
2022-03-24 05:32:02 +00:00
2022-06-02 23:15:43 +00:00
sudo apt -yq install syslinux syslinux-common syslinux-efi extlinux f2fs-tools
2022-03-24 05:32:02 +00:00
2022-06-02 23:15:43 +00:00
mkdir -p build/${TYPE} mnt out squashmount
2022-03-24 05:32:02 +00:00
2022-06-02 23:15:43 +00:00
download
2022-03-24 05:32:02 +00:00
2022-06-02 23:15:43 +00:00
KERNEL_VERSION="$(file build/${TYPE}/vmlinuz |cut -d, -f2|cut -d\ -f3)"
2022-03-24 05:32:02 +00:00
echo "Detected kernel ${KERNEL_VERSION}"
echo "Creating empty image"
2022-06-02 23:15:43 +00:00
qemu-img create -f qcow2 build/${TYPE}/ubuntu-${OS_CODENAME}.qcow2 2G || exit 1
2022-03-24 05:32:02 +00:00
echo "attaching image"
sudo modprobe nbd
2022-06-02 23:15:43 +00:00
sudo qemu-nbd -c "${DEV}" build/${TYPE}/ubuntu-${OS_CODENAME}.qcow2 || exit 1
2022-03-24 05:32:02 +00:00
echo "partitioning"
sudo parted -s "${DEV}" -- \
mklabel gpt \
unit MiB \
mkpart '"Ubuntu"' btrfs 1 100% \
set 1 legacy_boot on
until [ -b "${DEV}1" ] || [ -b "${DEV}p1" ]; do
sleep 1
done
2022-06-02 23:15:43 +00:00
2022-03-24 05:32:02 +00:00
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
2022-06-02 23:15:43 +00:00
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
2022-03-24 05:32:02 +00:00
sync
echo "mounting root"
2022-06-02 23:15:43 +00:00
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
2022-03-24 05:32:02 +00:00
echo "unpacking root fs"
2022-06-02 23:15:43 +00:00
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
2022-03-24 05:32:02 +00:00
echo "installing kernel and inintd"
sudo mkdir -p mnt/boot
2022-06-02 23:15:43 +00:00
sudo cp -ar build/${TYPE}/initrd.img "mnt/boot/initrd.img-${KERNEL_VERSION}"
sudo cp -ar build/${TYPE}/vmlinuz "mnt/boot/vmlinuz-${KERNEL_VERSION}"
2022-03-24 05:32:02 +00:00
sudo ln -s "vmlinuz-${KERNEL_VERSION}" mnt/boot/vmlinuz
sudo ln -s "initrd.img-${KERNEL_VERSION}" mnt/boot/initrd.img
echo "installing legacy syslinux files"
sudo mkdir -p mnt/boot/syslinux
sudo cp /usr/lib/syslinux/modules/bios/*.c32 mnt/boot/syslinux/
sudo cp syslinux/{mainmenu,syslinux}.cfg mnt/boot/syslinux
if [ -n "$1" ]; then
echo "Patching UUID $1"
sudo sed -i -e "s/?/?uuid=$1\&/g" mnt/boot/syslinux/mainmenu.cfg
cat mnt/boot/syslinux/mainmenu.cfg
fi
echo "here is the fstab"
2022-06-02 23:15:43 +00:00
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
2022-03-24 05:32:02 +00:00
echo "installing syslinux bootloader"
sudo extlinux -i mnt/boot/syslinux
echo "umounting"
sync
2022-06-02 23:15:43 +00:00
if [ ${TYPE} != "cloud" ]; then
umount /mnt/boot
fi
2022-03-24 05:32:02 +00:00
sudo umount mnt
echo "detaching"
sudo qemu-nbd -d "${DEV}"
echo "compressing"
2022-06-02 23:15:43 +00:00
qemu-img convert -cpO qcow2 build/${TYPE}/ubuntu-${OS_CODENAME}.qcow2 out/ubuntu-${OS_CODENAME}-${TYPE}.qcow2
2022-03-24 05:32:02 +00:00
# rm -f out/ubuntu-${OS_CODENAME}.qcow2
echo "done"