PoC
Some checks failed
buildbot/travis_bionic Build done.

This commit is contained in:
Peter Šurda 2022-03-24 13:32:02 +08:00
commit 436fa2b312
Signed by: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87
4 changed files with 104 additions and 0 deletions

84
build.sh Executable file
View File

@ -0,0 +1,84 @@
#!/bin/bash
OS_CODENAME=focal
DEV=/dev/nbd0
#sudo apt -yq install syslinux syslinux-common syslinux-efi extlinux
mkdir -p build mnt out
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
KERNEL_VERSION="$(file build/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
echo "attaching image"
sudo modprobe nbd
sudo qemu-nbd -c "${DEV}" build/ubuntu-${OS_CODENAME}.qcow2 || exit 1
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
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.btrfs"
sudo mkfs.btrfs -L cloudimg-rootfs "${DEV}p1" || exit 1
sync
echo "mounting root"
sudo mount -t btrfs "${DEV}p1" mnt || exit 1
echo "unpacking root fs"
sudo tar xJf build/rootfs.tar.xz -C mnt || exit 1
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 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"
echo "LABEL=cloudimg-rootfs / btrfs defaults 0 1" | sudo tee mnt/etc/fstab
echo "installing syslinux bootloader"
sudo extlinux -i mnt/boot/syslinux
echo "umounting"
sync
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
# rm -f out/ubuntu-${OS_CODENAME}.qcow2
echo "done"

12
syslinux/mainmenu.cfg Normal file
View File

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

4
syslinux/syslinux.cfg Executable file
View File

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

4
syslinux/syslx64.cfg Executable file
View File

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