ubuntu-cloud-image/build.sh

168 lines
4.8 KiB
Bash
Raw Permalink 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-06-25 16:01:41 +00:00
DEV=/dev/sda
# NBD=yes, NBD=
NBD=
# TYPE=maas, TYPE=cloud
2022-06-21 03:17:42 +00:00
TYPE=maas
2022-06-25 16:01:41 +00:00
# DOWNLOAD=yes, DOWNLOAD=
2022-06-02 23:15:43 +00:00
DOWNLOAD=yes
2022-06-25 16:01:41 +00:00
MAAS_ID=
2022-06-21 03:17:42 +00:00
# partition block device
lastchar=${DEV: -1}
2022-06-25 16:01:41 +00:00
BOOTPART="${DEV}1"
ROOTPART="${DEV}2"
2022-06-21 03:17:42 +00:00
if [ "$lastchar" -eq "$lastchar" ] 2>/dev/null; then
2022-06-25 16:01:41 +00:00
BOOTPART="${DEV}p1"
ROOTPART="${DEV}p2"
2022-06-02 23:15:43 +00:00
fi
2022-06-21 03:17:42 +00:00
function get_maas_id(){
2022-06-25 16:01:41 +00:00
if [ -n "$MAAS_ID" ]; then
return
fi
2022-06-21 03:17:42 +00:00
streams=$(mktemp)
wget -O $streams https://images.maas.io/ephemeral-v3/stable/streams/v1/com.ubuntu.maas:stable:v3:download.json
MAAS_ID=$(jq -r '.products[]|select(.version=="'"$OS_VERSION"'" and .arch=="amd64" and .kflavor=="generic" and .subarch=="ga-'"$OS_VERSION"'")|.versions|keys|max' \
"$streams")
rm -f $streams
echo "MAAS_ID=$MAAS_ID"
}
2022-06-02 23:15:43 +00:00
function download(){
if [ -z "$DOWNLOAD" ]; then
return
fi
echo "Downloading"
2022-06-21 03:17:42 +00:00
wget -N -P download \
https://cloud-images.ubuntu.com/$OS_CODENAME/current/unpacked/${OS_CODENAME}-server-cloudimg-amd64-initrd-generic \
https://cloud-images.ubuntu.com/${OS_CODENAME}/current/unpacked/${OS_CODENAME}-server-cloudimg-amd64-vmlinuz-generic \
https://cloud-images.ubuntu.com/${OS_CODENAME}/current/${OS_CODENAME}-server-cloudimg-amd64-root.tar.xz \
https://images.maas.io/ephemeral-v3/stable/${OS_CODENAME}/amd64/${MAAS_ID}/ga-${OS_VERSION}/generic/boot-initrd \
https://images.maas.io/ephemeral-v3/stable/${OS_CODENAME}/amd64/${MAAS_ID}/ga-${OS_VERSION}/generic/boot-kernel \
https://images.maas.io/ephemeral-v3/stable/${OS_CODENAME}/amd64/${MAAS_ID}/squashfs
# CLOUD
ln -f download/${OS_CODENAME}-server-cloudimg-amd64-vmlinuz-generic cloud-vmlinuz
ln -f download/${OS_CODENAME}-server-cloudimg-amd64-initrd-generic cloud-initrd.img
ln -f download/${OS_CODENAME}-server-cloudimg-amd64-root.tar.xz rootfs.tar.xz
# MAAS
ln -f download/boot-kernel maas-vmlinuz
ln -f download/boot-initrd maas-initrd.img
2022-06-02 23:15:43 +00:00
}
2022-03-24 05:32:02 +00:00
2022-06-25 16:01:41 +00:00
sudo apt -yq install syslinux syslinux-common syslinux-efi extlinux f2fs-tools dosfstools jq
2022-03-24 05:32:02 +00:00
2022-06-21 03:17:42 +00:00
mkdir -p build/${TYPE} download mnt out squashmount
2022-03-24 05:32:02 +00:00
2022-06-25 16:01:41 +00:00
get_maas_id
download
2022-03-24 05:32:02 +00:00
2022-06-21 03:17:42 +00:00
KERNEL_VERSION="$(file ${TYPE}-vmlinuz |cut -d, -f2|cut -d\ -f3)"
2022-03-24 05:32:02 +00:00
echo "Detected kernel ${KERNEL_VERSION}"
2022-06-21 03:17:42 +00:00
if [ "$NBD" ]; then
echo "Creating empty image"
qemu-img create -f qcow2 build/${TYPE}/ubuntu-${OS_CODENAME}.qcow2 5G || exit 1
echo "attaching image"
sudo modprobe nbd
sudo qemu-nbd -c "${DEV}" build/${TYPE}/ubuntu-${OS_CODENAME}.qcow2 || exit 1
fi
2022-03-24 05:32:02 +00:00
echo "partitioning"
sudo parted -s "${DEV}" -- \
2022-06-21 03:17:42 +00:00
mklabel gpt \
unit MiB \
2022-06-25 16:01:41 +00:00
mkpart '"Boot"' btrfs 1 512MiB \
2022-06-21 03:17:42 +00:00
mkpart '"Ubuntu"' btrfs 512MiB 100% \
set 1 esp on \
set 1 legacy_boot on
2022-06-25 16:01:41 +00:00
until [ -b "$BOOTPART" ] && [ -b "$ROOTPART" ]; do
2022-06-21 03:17:42 +00:00
sleep 1
2022-03-24 05:32:02 +00:00
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 \
2022-06-21 03:17:42 +00:00
if=/usr/lib/syslinux/mbr/gptmbr.bin of="$DEV" 2> /dev/null
2022-03-24 05:32:02 +00:00
sync
2022-06-21 03:17:42 +00:00
echo "mkfs.btrfs (boot)"
2022-06-25 16:01:41 +00:00
sudo mkfs.btrfs -f -L boot ${BOOTPART} || exit 1
2022-06-21 03:17:42 +00:00
2022-06-02 23:15:43 +00:00
if [ ${TYPE} == "cloud" ]; then
2022-06-21 03:17:42 +00:00
echo "mkfs.btrfs (root)"
2022-06-25 16:01:41 +00:00
sudo mkfs.btrfs -f -L rootfs ${ROOTPART} || exit 1
2022-06-02 23:15:43 +00:00
else
2022-06-21 03:17:42 +00:00
echo "mkfs.f2fs (root)"
2022-06-25 16:01:41 +00:00
sudo mkfs.f2fs -f -l rootfs -O extra_attr ${ROOTPART} || exit 1
2022-06-02 23:15:43 +00:00
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
fi
2022-06-25 16:01:41 +00:00
echo "mounting boot"
2022-06-21 03:17:42 +00:00
sudo mkdir -p mnt/boot
sudo mount -t btrfs ${BOOTPART} mnt/boot || exit 1
2022-03-24 05:32:02 +00:00
echo "unpacking root fs"
2022-06-02 23:15:43 +00:00
if [ ${TYPE} == "cloud" ]; then
2022-06-21 03:17:42 +00:00
sudo tar xJf rootfs.tar.xz -C mnt || exit 1
2022-06-02 23:15:43 +00:00
else
2022-06-21 03:17:42 +00:00
sudo mount -t squashfs download/squashfs squashmount -o ro,loop || exit 1
2022-06-25 16:01:41 +00:00
sudo cp -ar squashmount/. mnt
sudo umount squashmount
2022-06-02 23:15:43 +00:00
fi
2022-03-24 05:32:02 +00:00
echo "installing kernel and inintd"
2022-06-21 03:17:42 +00:00
sudo cp -ar ${TYPE}-initrd.img "mnt/boot/initrd.img-${KERNEL_VERSION}"
sudo cp -ar ${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
fi
2022-06-21 03:17:42 +00:00
echo "LABEL=boot /boot btrfs defaults 0 1" | sudo tee -a mnt/etc/fstab
2022-03-24 05:32:02 +00:00
echo "installing syslinux bootloader"
sudo extlinux -i mnt/boot/syslinux
echo "umounting"
sync
2022-06-21 03:17:42 +00:00
sudo umount mnt/boot
2022-03-24 05:32:02 +00:00
sudo umount mnt
2022-06-21 03:17:42 +00:00
if [ "$NBD" ]; then
echo "detaching"
sudo qemu-nbd -d "${DEV}"
echo "compressing"
qemu-img convert -cpO qcow2 build/${TYPE}/ubuntu-${OS_CODENAME}.qcow2 out/ubuntu-${OS_CODENAME}-${TYPE}.qcow2
# rm -f out/ubuntu-${OS_CODENAME}.qcow2
fi
2022-03-24 05:32:02 +00:00
echo "done"