Skip to content

Instantly share code, notes, and snippets.

@groldo
Created April 26, 2023 15:59
Show Gist options
  • Select an option

  • Save groldo/b45dfe41af7acafcf2985f90083b7e05 to your computer and use it in GitHub Desktop.

Select an option

Save groldo/b45dfe41af7acafcf2985f90083b7e05 to your computer and use it in GitHub Desktop.
start a microvm with qemu
#!/bin/bash
sudo apt install qemu
wget https://cloud-images.ubuntu.com/releases/jammy/release/unpacked/ubuntu-22.04-server-cloudimg-amd64-vmlinuz-generic
wget https://cloud-images.ubuntu.com/releases/jammy/release/unpacked/ubuntu-22.04-server-cloudimg-amd64-initrd-generic
wget https://cloud-images.ubuntu.com/releases/jammy/release/ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img
qemu-img create -f qcow2 -F qcow2 -b `pwd`/ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img test.img 8G
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
passwd: \$6\$rounds=4096\$DiGfrvgin.sMd183\$FP9xuny2Xv6zgaqCt5sfiWpTOjhzRwfr57H.0J0DZmHJMWjdDScT54XPQvbN3uJBAuTYIuxiLqFsg9zODTIzl1
# password: test
lock_passwd: false
ssh-authorized-keys:
- `cat ~/.ssh/id_rsa.pub`
EOF
cloud-localds user-data.img user-data
sudo qemu-system-x86_64 \
-M microvm,x-option-roms=off,pit=off,pic=off,isa-serial=off,rtc=off,accel=hvf\
-enable-kvm -cpu host -m 2G -smp 2 \
-kernel ubuntu-22.04-server-cloudimg-amd64-vmlinuz-generic \
-initrd ubuntu-22.04-server-cloudimg-amd64-initrd-generic \
-append "earlyprintk=hvc0 console=hvc0 root=/dev/vda1" \
-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 user,id=net0,net=192.168.76.0/24,dhcpstart=192.168.76.9,hostfwd=tcp:0.0.0.0:2222-:22 \
-device virtio-net-device,netdev=net0 \
-drive file=user-data.img,format=raw,id=mytest \
-device virtio-blk-device,drive=mytest \
-daemonize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment