ubuntu-cloud-image/build.sh
Peter Šurda 8ad514203e
Some checks failed
buildbot/multibuild_parent Build done.
buildbot/travis_bionic Build done.
Update for physical disks and EFI
2022-06-21 11:17:42 +08:00

175 lines
5.3 KiB
Bash
Executable File

#!/bin/bash
OS_CODENAME=focal
OS_VERSION=20.04
DEV=/dev/nbd0
NBD=yes
TYPE=maas
DOWNLOAD=yes
# MAAS_ID=20220601
# partition block device
lastchar=${DEV: -1}
EFIPART="${DEV}1"
BOOTPART="${DEV}2"
ROOTPART="${DEV}3"
if [ "$lastchar" -eq "$lastchar" ] 2>/dev/null; then
EFIPART="${DEV}p1"
BOOTPART="${DEV}p2"
ROOTPART="${DEV}p3"
fi
function get_maas_id(){
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"
}
function download(){
if [ -z "$DOWNLOAD" ]; then
return
fi
echo "Downloading"
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
}
sudo apt -yq install syslinux syslinux-common syslinux-efi extlinux f2fs-tools dosfstools
mkdir -p build/${TYPE} download mnt out squashmount
#get_maas_id
#download
KERNEL_VERSION="$(file ${TYPE}-vmlinuz |cut -d, -f2|cut -d\ -f3)"
echo "Detected kernel ${KERNEL_VERSION}"
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
echo "partitioning"
sudo parted -s "${DEV}" -- \
mklabel gpt \
unit MiB \
mkpart '"EFI system partition"' fat32 1 32MiB \
mkpart '"Boot"' btrfs 32MiB 512MiB \
mkpart '"Ubuntu"' btrfs 512MiB 100% \
set 1 esp on \
set 1 legacy_boot on
until [ -b "$EFIPART" ] && [ -b "$BOOTPART" ] && [ -b "$ROOTPART" ]; 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.vfat (EFI)"
sudo mkfs.vfat -n EFI ${EFIPART}
echo "mkfs.btrfs (boot)"
sudo mkfs.btrfs -L boot ${BOOTPART} || exit 1
if [ ${TYPE} == "cloud" ]; then
echo "mkfs.btrfs (root)"
sudo mkfs.btrfs -L rootfs ${ROOTPART} || exit 1
else
echo "mkfs.f2fs (root)"
sudo mkfs.f2fs -l rootfs -O extra_attr ${ROOTPART} || exit 1
fi
sync
echo "mounting root"
if [ ${TYPE} == "cloud" ]; then
sudo mount -t btrfs ${ROOTPART} mnt || exit 1
else
sudo mount -t f2fs ${ROOTPART} mnt || exit 1
fi
sudo mkdir -p mnt/boot
sudo mount -t btrfs ${BOOTPART} mnt/boot || exit 1
sudo mkdir -p mnt/boot/EFI
sudo mount -t vfat ${EFIPART} mnt/boot/EFI || exit 1
echo "unpacking root fs"
if [ ${TYPE} == "cloud" ]; then
sudo tar xJf rootfs.tar.xz -C mnt || exit 1
else
sudo mount -t squashfs download/squashfs squashmount -o ro,loop || exit 1
cp -ar squashmount/. mnt
umount squashmount
fi
echo "installing kernel and inintd"
sudo cp -ar ${TYPE}-initrd.img "mnt/boot/initrd.img-${KERNEL_VERSION}"
sudo cp -ar ${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
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
echo "installing EFI syslinux files"
sudo mkdir -p mnt/EFI/BOOT
sudo cp /usr/lib/SYSLINUX.EFI/efi64/syslinux.efi mnt/EFI/BOOT/BOOTX64.EFI
sudo cp /usr/lib/syslinux/modules/efi64/ldlinux.e64 mnt/EFI/BOOT/ldlinux.e64
sudo cp /usr/lib/syslinux/modules/efi64/*.c32 mnt/EFI/BOOT/
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"
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
echo "LABEL=boot /boot btrfs defaults 0 1" | sudo tee -a mnt/etc/fstab
echo "LABEL=EFI /boot/EFI vfat defaults 0 1" | sudo tee -a mnt/etc/fstab
echo "installing syslinux bootloader"
sudo extlinux -i mnt/boot/syslinux
echo "umounting"
sync
sudo umount mnt/boot/EFI
sudo umount mnt/boot
sudo umount mnt
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
echo "done"