Chapter 8

BYO kernel and initrd for development

Sometimes I needed to test my own initrd and kernel with MAAS, so this is how I do it.

Prerequisites

Build the kernel boot-kernel and initrd boot-initrd! Then, create a qemu disk:

qemu-img create -f qcow2 rootfs.qcow2 2G

and say you have MAAS running at 172.0.2.2. Then you would simply

qemu-system-x86_64 \
  -kernel boot-kernel \
  -initrd boot-initrd \
  -append "nomodeset root=squash:http://172.0.2.2:5248/images/6cd63c9/ubuntu/amd64/ga-20.04/focal/stable/squashfs ip=::::C220M4:BOOTIF ip6=off cc:{'datasource_list': ['MAAS']}end_cc cloud-config-url=http://172.0.2.2:5248/MAAS/metadata/latest/enlist-preseed/?op=get_enlist_preseed ro overlayroot=tmpfs overlayroot_cfgdisk=disabled apparmor=0 log_host=172.0.2.2 log_port=5247 --- iscsi_auto BOOTIF=01-52-54-00-ab-cd-ef" \
  -drive file=rootfs.qcow2,format=qcow2,if=virtio \
  -m 2048M \
  -netdev user,id=net-test \
  -device virtio-net-pci,netdev=net-test,mac=52:54:00:ab:cd:ef

Just to test a valid scenario, just download the kernel and the initrd from images.maas.io and try them. Then you can try your ones!

Create a custom initrd

cd /tmp
mkdir initrd
cd initrd/
mkdir -p bin sbin etc proc sys usr/bin usr/sbin dev
cp /bin/busybox bin/
cd bin
for cmd in sh mount echo; do   ln -s busybox $cmd; done
cd ..
cat > init << 'EOF'
#!/bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
echo "Hello, world!"
exec sh
EOF

chmod +x init
sudo mknod dev/console c 5 1
sudo mknod dev/null c 1 3
find . | cpio -H newc -o | gzip > ../initrd.img