Proper configuration using a nocloud iso image
The provided "vmbuilders/ubuntu.sh" is a good start but it didn't work for me to configure the ubuntu cloud image: The openssh-server failed to initialize, my user was not created, no network connection. Instead of using the initramfs we can just provide an iso image with the necessary 'user-data' and 'meta-data' files to initialize the image at the first boot.
# create cloudinit config iso
rm -rf iso_folder seed.iso
mkdir iso_folder
cat << EOF > iso_folder/user-data
#cloud-config
users:
- default
- name: $USER
lock_passwd: False
gecos: $USER
groups: [adm, audio, cdrom, dialout, dip, floppy, lxd, netdev, plugdev, sudo, video]
sudo: ["ALL=(ALL) NOPASSWD:ALL"]
shell: /bin/bash
ssh-authorized-keys:
- $(cat ~/.ssh/id_rsa.pub | head -n 1)
bootcmd:
- apt remove -y irqbalance
network:
version: 2
renderer: networkd
ethernets:
enp0s1:
dhcp4: true
EOF
Plenty of examples here. Then to create the iso file:
touch iso_folder/meta-data
hdiutil makehybrid -iso -joliet -iso-volume-name cidata -joliet-volume-name cidata -o seed.iso iso_folder
Then add the seed.iso file to the vm.conf
cat << EOF > vm.conf
kernel=vmlinux
initrd=initrd
cmdline=console=hvc0 irqfixup root=/dev/vda
cpu-count=1
memory-size=1024
disk=disk.img
cdrom=seed.iso
network=nat
EOF
There's no need to launch the initramfs anymore to edit the image. The openssh initialization, user creation and networking do work for me now. Full example: https://gist.github.com/alexsarmiento/c52874738c3bdb96424c57b297b2678d
FYI, this gave me an idea, so using cloud-init nocloud, I cobbled together a project to make using Virtualization.Framework easier.
This is it if you want to give it a go: https://github.com/dmarkey/macos-virt
Thanks so much to @gyf304 for the starting point and the POC.