Skip to content

Qemu setup in mac

Introduction

Qemu or quick emulator is used to run virtual machines on mac, linux 😕 not sure if it works on windows. The very distinction from a virtualization platform is that it is able to emulate a different hardware so that we can do a quick testing of tool in any platform using qemu.

qcow2 format

While installation and setup is easy we can use the qcow format being used in the cloud provider. It is an os installed hard disk with a small size. We can use this rather than installing the os on the hard disk and using it. It is essentially a template that can be used with qemu and also can be resized, customized with cloud init when using with qemu.

download qcow2 for ubuntu

wget -c https://cloud-images.ubuntu.com/releases/noble/release/ubuntu-24.04-server-cloudimg-amd64.img

cloud-init Customization

Create a folder named cloud-init-config. Create the files with the following names. * user-data - for customization * meta-data - for instance id and hostname customization * network-config - for netplan related setup

user-data

#cloud-config
#cloud-config
users:
  - name: roshankhatri
    sudo: ['ALL=(ALL) NOPASSWD:ALL']
    shell: /bin/bash
    passwd: "$6$yAb9MAxkskhMSmD7$/ywdqFOgjoHLLoH6pr41OuXg/vqjddHirPZk4qfaswXBcE65u5QjS.Q6z7YBoF5LhE3nsCeVD95sTZ2loagIR0"
    ssh_authorized_keys:
      - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAKOBCgkaUJJSpF+pMXBJAfStIvP7DKTPdcBcQd8e2rT topofeverest8848@gmail.com
    lock_passwd: false
package_update: true
package_upgrade: true
ssh:
  emit_keys_to_console: false
timezone: Asia/Kathmandu

meta-data

instance-id: i-kernel08326
local-hostname: kernel-buildserver

network-config

version: 2
ethernets:
  enp0s2:
    dhcp4: true

Note

The network interfaces depend on the machine type selected on qemu. The default machine type supports an interface with name ens3 and the q35 machine type supports an interface with name enp0s2. 😠

cloud-init iso generation

The cloud-init iso is used to pass user-data, meta-data and network-config data to the qcow2 image on boot time. The iso image is prepared using the following command. It needs to have a label of cidata.

mkisofs -output seed.iso -volid cidata -joliet -rock cloud-init-config/user-data cloud-init-config/meta-data cloud-init-config/network-config

Check the cidata label on the iso file with the following command.

file seed.iso
It outputs the following
seed.iso: ISO 9660 CD-ROM filesystem data 'cidata'

qemu launch and instance

A ubuntu instance can be launched with the qemu with the following command.

sudo qemu-system-x86_64 \
  -m 4G \
  -machine accel=tcg,type=q35 \
  -smp 2 \
  -drive if=virtio,file=ubuntu-24.04-server-cloudimg-amd64.img,format=qcow2 \
  -drive file=seed.iso,media=cdrom \
  -device virtio-net,netdev=net00 \
  -netdev vmnet-bridged,id=net00,ifname=en0 \
  -display none \
  -serial mon:stdio
It is almost straight forward with the following options. 😉

  • -m: memory assigned
  • -machine: acceleration supported and 👉 machine type affects the naming of nic card
  • -drive: hard disk the qcow2 drive we used in the system
  • -drive: cd rom containing cloud init image prepared on the earlier stage.
  • -device: the network device named net00
  • -netdev: the device presented to the os

Info

For some wired reasons mac did not support SLiRP protocol used to forward ports. So, using vmnet supported devices remained. It has three formats like the ones used in virtualbox for accessing the hosts.

  • vmnet-bridged - bridged with the wirless nic card, works like another host on the network
  • vnmet-host - shared with the host only, for a very isolated setup
  • vmnet-shared - networking via NAT with the host gives internet access.
  • display: can have multiple options. -display default,show-cursor=on will launch a new window and none will launch qemu without any display.
  • serial: super useful for debugging mon:stdio option will output the console on the terminal screen where qemu was lauched. file: qemu-serial-logs.txt will write the bootup logs to the file specified.

clear cloud-init data on instance

once the instance has powered up and you want to clear the cloud-init data used by the instance. Run the following command on the instance and reboot. It will clear all the cloud-init data and force to load new data. It is required when machine type or network-config is changed on the cloud-init

sudo cloud-init clean --logs
sudo reboot

resize qcow2 image using qemu

Since the qcow2 image is limited to 2GB in size. We can resize the qcow2 image using the following command.

qemu-img resize ubuntu-24.04-server-cloudimg-amd64.img 20G

snapshot of the qcow2 instance

qcow2 offers snapshot features we can leverage those features to create and restore snapshots.

qemu-img snapshot -c "00-pristine" ubuntu-24.04-server-cloudimg-amd64.img

listing available snapshots

All available snapshots can be listed with the following command.

qemu-img snapshot -l ubuntu-24.04-server-cloudimg-amd64.img