Skip to content

Instantly share code, notes, and snippets.

@mikaelhg
Last active March 24, 2024 19:18
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mikaelhg/7a67901affe56bdf22eb398606945a23 to your computer and use it in GitHub Desktop.
Save mikaelhg/7a67901affe56bdf22eb398606945a23 to your computer and use it in GitHub Desktop.
Running Ubuntu 21.04 on QEMU MicroVM

Running Ubuntu 21.04 on QEMU MicroVM

I mean, in this era of containers, it's not like you'd need this very often, but if you do, you do.

  1. 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

  1. 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
  1. 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
  1. Check out the docs

https://github.com/qemu/qemu/blob/master/docs/system/i386/microvm.rst

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment