Update for physical disks and EFI
Some checks failed
buildbot/multibuild_parent Build done.
buildbot/travis_bionic Build done.

This commit is contained in:
Peter Šurda 2022-06-21 11:17:42 +08:00
parent 8a3fe39a22
commit 8ad514203e
Signed by: PeterSurda
GPG Key ID: 3E47497CF67ABB95
3 changed files with 97 additions and 64 deletions

145
build.sh
View File

@ -3,62 +3,83 @@
OS_CODENAME=focal OS_CODENAME=focal
OS_VERSION=20.04 OS_VERSION=20.04
DEV=/dev/nbd0 DEV=/dev/nbd0
TYPE=cloud NBD=yes
TYPE=maas
DOWNLOAD=yes DOWNLOAD=yes
MAAS_ID=20220524 # MAAS_ID=20220601
if [ ${TYPE} == cloud ]; then # partition block device
ROOTPART="${DEV}p1" lastchar=${DEV: -1}
else EFIPART="${DEV}1"
BOOTPART="${DEV}p1" BOOTPART="${DEV}2"
ROOTPART="${DEV}p2" ROOTPART="${DEV}3"
if [ "$lastchar" -eq "$lastchar" ] 2>/dev/null; then
EFIPART="${DEV}p1"
BOOTPART="${DEV}p2"
ROOTPART="${DEV}p3"
fi 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(){ function download(){
if [ -z "$DOWNLOAD" ]; then if [ -z "$DOWNLOAD" ]; then
return return
fi fi
echo "Downloading" echo "Downloading"
if [ ${TYPE} == "cloud" ]; then wget -N -P download \
wget -O build/${TYPE}/initrd.img \ 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-initrd-generic https://cloud-images.ubuntu.com/${OS_CODENAME}/current/unpacked/${OS_CODENAME}-server-cloudimg-amd64-vmlinuz-generic \
wget -O build/${TYPE}/vmlinuz \ https://cloud-images.ubuntu.com/${OS_CODENAME}/current/${OS_CODENAME}-server-cloudimg-amd64-root.tar.xz \
https://cloud-images.ubuntu.com/${OS_CODENAME}/current/unpacked/${OS_CODENAME}-server-cloudimg-amd64-vmlinuz-generic https://images.maas.io/ephemeral-v3/stable/${OS_CODENAME}/amd64/${MAAS_ID}/ga-${OS_VERSION}/generic/boot-initrd \
wget -O build/${TYPE}/rootfs.tar.xz \ https://images.maas.io/ephemeral-v3/stable/${OS_CODENAME}/amd64/${MAAS_ID}/ga-${OS_VERSION}/generic/boot-kernel \
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}/squashfs
else
wget -O build/${TYPE}/initrd.img \
https://images.maas.io/ephemeral-v3/stable/${OS_CODENAME}/amd64/${MAAS_ID}/ga-${OS_VERSION}/boot-initrd # CLOUD
wget -O build/${TYPE}/vmlinuz \ ln -f download/${OS_CODENAME}-server-cloudimg-amd64-vmlinuz-generic cloud-vmlinuz
https://images.maas.io/ephemeral-v3/stable/${OS_CODENAME}/amd64/${MAAS_ID}/ga-${OS_VERSION}/boot-kernel ln -f download/${OS_CODENAME}-server-cloudimg-amd64-initrd-generic cloud-initrd.img
wget -O build/${TYPE}/rootfs.squashfs \ ln -f download/${OS_CODENAME}-server-cloudimg-amd64-root.tar.xz rootfs.tar.xz
https://cloud-images.ubuntu.com/${OS_CODENAME}/amd64/${MAAS_ID}/squashfs
fi # 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 sudo apt -yq install syslinux syslinux-common syslinux-efi extlinux f2fs-tools dosfstools
mkdir -p build/${TYPE} mnt out squashmount mkdir -p build/${TYPE} download mnt out squashmount
download #get_maas_id
#download
KERNEL_VERSION="$(file build/${TYPE}/vmlinuz |cut -d, -f2|cut -d\ -f3)" KERNEL_VERSION="$(file ${TYPE}-vmlinuz |cut -d, -f2|cut -d\ -f3)"
echo "Detected kernel ${KERNEL_VERSION}" echo "Detected kernel ${KERNEL_VERSION}"
echo "Creating empty image" if [ "$NBD" ]; then
qemu-img create -f qcow2 build/${TYPE}/ubuntu-${OS_CODENAME}.qcow2 2G || exit 1 echo "Creating empty image"
qemu-img create -f qcow2 build/${TYPE}/ubuntu-${OS_CODENAME}.qcow2 5G || exit 1
echo "attaching image" echo "attaching image"
sudo modprobe nbd sudo modprobe nbd
sudo qemu-nbd -c "${DEV}" build/${TYPE}/ubuntu-${OS_CODENAME}.qcow2 || exit 1 sudo qemu-nbd -c "${DEV}" build/${TYPE}/ubuntu-${OS_CODENAME}.qcow2 || exit 1
fi
echo "partitioning" echo "partitioning"
sudo parted -s "${DEV}" -- \ sudo parted -s "${DEV}" -- \
mklabel gpt \ mklabel gpt \
unit MiB \ unit MiB \
mkpart '"Ubuntu"' btrfs 1 100% \ 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 set 1 legacy_boot on
until [ -b "${DEV}1" ] || [ -b "${DEV}p1" ]; do until [ -b "$EFIPART" ] && [ -b "$BOOTPART" ] && [ -b "$ROOTPART" ]; do
sleep 1 sleep 1
done done
@ -68,14 +89,17 @@ sudo dd bs=440 count=1 conv=notrunc \
if=/usr/lib/syslinux/mbr/gptmbr.bin of="$DEV" 2> /dev/null if=/usr/lib/syslinux/mbr/gptmbr.bin of="$DEV" 2> /dev/null
sync 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 if [ ${TYPE} == "cloud" ]; then
echo "mkfs.btrfs" echo "mkfs.btrfs (root)"
sudo mkfs.btrfs -L rootfs ${ROOTPART}" || exit 1 sudo mkfs.btrfs -L rootfs ${ROOTPART} || exit 1
else else
echo "mkfs.btrfs" echo "mkfs.f2fs (root)"
sudo mkfs.btrfs -L boot ${BOOTPART}" || exit 1 sudo mkfs.f2fs -l rootfs -O extra_attr ${ROOTPART} || exit 1
echo "mkfs.f2fs"
sudo mkfs.f2fs -l rootfs -O extra_attr,inode_checksum,sb_checksum,compression ${ROOTPART} || exit 1
fi fi
sync sync
@ -84,22 +108,24 @@ if [ ${TYPE} == "cloud" ]; then
sudo mount -t btrfs ${ROOTPART} mnt || exit 1 sudo mount -t btrfs ${ROOTPART} mnt || exit 1
else else
sudo mount -t f2fs ${ROOTPART} mnt || exit 1 sudo mount -t f2fs ${ROOTPART} mnt || exit 1
sudo mkdir -p mnt/boot
sudo mount -t btrfs ${BOOTPART} mnt/boot || exit 1
fi 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" echo "unpacking root fs"
if [ ${TYPE} == "cloud" ]; then if [ ${TYPE} == "cloud" ]; then
sudo tar xJf build/${TYPE}/rootfs.tar.xz -C mnt || exit 1 sudo tar xJf rootfs.tar.xz -C mnt || exit 1
else else
sudo mount -t squashfs build/${TYPE}/rootfs.squashfs squashmount -o ro,loop || exit 1 sudo mount -t squashfs download/squashfs squashmount -o ro,loop || exit 1
cp -ar squashmount/. mnt cp -ar squashmount/. mnt
umount squashmount
fi fi
echo "installing kernel and inintd" echo "installing kernel and inintd"
sudo mkdir -p mnt/boot sudo cp -ar ${TYPE}-initrd.img "mnt/boot/initrd.img-${KERNEL_VERSION}"
sudo cp -ar build/${TYPE}/initrd.img "mnt/boot/initrd.img-${KERNEL_VERSION}" sudo cp -ar ${TYPE}-vmlinuz "mnt/boot/vmlinuz-${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 "vmlinuz-${KERNEL_VERSION}" mnt/boot/vmlinuz
sudo ln -s "initrd.img-${KERNEL_VERSION}" mnt/boot/initrd.img sudo ln -s "initrd.img-${KERNEL_VERSION}" mnt/boot/initrd.img
@ -107,6 +133,11 @@ echo "installing legacy syslinux files"
sudo mkdir -p mnt/boot/syslinux sudo mkdir -p mnt/boot/syslinux
sudo cp /usr/lib/syslinux/modules/bios/*.c32 mnt/boot/syslinux/ sudo cp /usr/lib/syslinux/modules/bios/*.c32 mnt/boot/syslinux/
sudo cp syslinux/{mainmenu,syslinux}.cfg 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 if [ -n "$1" ]; then
echo "Patching UUID $1" echo "Patching UUID $1"
sudo sed -i -e "s/?/?uuid=$1\&/g" mnt/boot/syslinux/mainmenu.cfg sudo sed -i -e "s/?/?uuid=$1\&/g" mnt/boot/syslinux/mainmenu.cfg
@ -118,24 +149,26 @@ if [ ${TYPE} == "cloud" ]; then
echo "LABEL=rootfs / btrfs defaults 0 1" | sudo tee mnt/etc/fstab echo "LABEL=rootfs / btrfs defaults 0 1" | sudo tee mnt/etc/fstab
else else
echo "LABEL=rootfs / f2fs defaults 0 1" | sudo tee mnt/etc/fstab 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 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" echo "installing syslinux bootloader"
sudo extlinux -i mnt/boot/syslinux sudo extlinux -i mnt/boot/syslinux
echo "umounting" echo "umounting"
sync sync
if [ ${TYPE} != "cloud" ]; then sudo umount mnt/boot/EFI
umount /mnt/boot sudo umount mnt/boot
fi
sudo umount mnt sudo umount mnt
echo "detaching" if [ "$NBD" ]; then
sudo qemu-nbd -d "${DEV}" 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 "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
echo "done" echo "done"

View File

@ -4,9 +4,9 @@ TOTALTIMEOUT 3000
LABEL Current LABEL Current
KERNEL /boot/vmlinuz KERNEL /boot/vmlinuz
INITRD /boot/initrd.img INITRD /boot/initrd.img
APPEND root=LABEL=cloudimg-rootfs ds=nocloud-net;s=https://cloud-init.sysdeploy.org/apiv2?filetype= APPEND root=LABEL=rootfs ds=nocloud-net;s=https://cloud-init.sysdeploy.org/apiv2?filetype=
LABEL Previous LABEL Previous
KERNEL /boot/vmlinuz.old KERNEL /boot/vmlinuz.old
INITRD /boot/initrd.img.old INITRD /boot/initrd.img.old
APPEND root=LABEL=cloudimg-rootfs ds=nocloud-net;s=https://cloud-init.sysdeploy.org/apiv2?filetype= APPEND root=LABEL=rootfs ds=nocloud-net;s=https://cloud-init.sysdeploy.org/apiv2?filetype=

View File

@ -1,4 +1,4 @@
MENU TITLE Legacy boot MENU TITLE Legacy boot
UI vesamenu.c32 UI vesamenu.c32
INCLUDE /boot/syslinux/mainmenu.cfg INCLUDE /syslinux/mainmenu.cfg