I mean, in this era of containers, it's not like you'd need this very often, but if you do, you do.
- Download the required image files
https://cloud-images.ubuntu.com/releases/hirsute/release/unpacked/ubuntu-21.04-server-cloudimg-amd64-vmlinuz-generic https://cloud-images.ubuntu.com/releases/hirsute/release/unpacked/ubuntu-21.04-server-cloudimg-amd64-initrd-generic https://cloud-images.ubuntu.com/releases/hirsute/release/ubuntu-21.04-server-cloudimg-amd64-disk-kvm.img
- Prepare your VM's disk image
qemu-img create -f qcow2 -F qcow2 -b `pwd`/ubuntu-21.04-server-cloudimg-amd64-disk-kvm.img test.img
cat >user-data <<EOF
#cloud-config
hostname: ubuntu1
fqdn: ubuntu1.localdomain
manage_etc_hosts: true
ssh_pwauth: true
disable_root: true
users:
- name: ubuntu
home: /home/ubuntu
shell: /bin/bash
groups: sudo
sudo: ALL=(ALL) NOPASSWD:ALL
ssh-authorized-keys:
- `cat ~/.ssh/id_ed25519.pub`
EOF
cloud-localds user-data.img user-data
- Run it
sudo qemu-system-x86_64 \
-M microvm,x-option-roms=off,pit=off,pic=off,isa-serial=off,rtc=off \
-enable-kvm -cpu host -m 512m -smp 2 \
-kernel ubuntu-21.04-server-cloudimg-amd64-vmlinuz-generic \
-append "earlyprintk=hvc0 console=hvc0 root=/dev/vda1" \
-initrd ubuntu-21.04-server-cloudimg-amd64-initrd-generic \
-nodefaults -no-user-config -nographic \
-chardev stdio,id=virtiocon0 \
-device virtio-serial-device \
-device virtconsole,chardev=virtiocon0 \
-drive id=test,file=test.img,format=qcow2,if=none \
-device virtio-blk-device,drive=test \
-netdev tap,id=tap0,script=no,downscript=no \
-device virtio-net-device,netdev=tap0 \
-drive file=user-data.img,format=raw
- Check out the docs
https://github.com/qemu/qemu/blob/master/docs/system/i386/microvm.rst