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
network-config
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.
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
-m: memory assigned-machine: acceleration supported andmachine 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 namednet00-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 networkvnmet-host- shared with the host only, for a very isolated setupvmnet-shared- networking via NAT with the host gives internet access.
display: can have multiple options.-display default,show-cursor=onwill launch a new window and none will launch qemu without any display.serial: super useful for debuggingmon:stdiooption will output the console on the terminal screen where qemu was lauched.file: qemu-serial-logs.txtwill 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
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.
snapshot of the qcow2 instance
qcow2 offers snapshot features we can leverage those features to create and restore snapshots.
listing available snapshots
All available snapshots can be listed with the following command.