QEMU on macOS Mojave (OS X)

Due to my current employer hardware policy I had to choose between a MacBook Pro (15-inch, 2017) or a Dell XPS 13 when I joined to the company, at that time, I chose the MacBook Pro because it has a Unix based OS, as I was get used to using KVM to create labs with VMs on my Debian box, an old fancy thing considering Docker I know, but I’m learning Terraform and I’d like to use it to provision my infra labs with the libvirt provider on macOS.

QEMU is a generic and open source machine emulator and virtualizer.


Installing QEMU

$ brew install qemu
$ qemu-system-x86_64 --version
QEMU emulator version 4.2.0
Copyright (c) 2003-2019 Fabrice Bellard and the QEMU Project developers

Installing libvirt

$ brew install libvirt
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink sbin/libvirtd
/usr/local/sbin is not writable.
==> libvirt
To have launchd start libvirt now and restart at login:
  brew services start libvirt
Or, if you don't want/need a background service you can just run:

Let’s check the linking problem:

$ brew link libvirt
Linking /usr/local/Cellar/libvirt/6.1.0...
Error: Could not symlink sbin/libvirtd
/usr/local/sbin is not writable.

# brew link libvirt
Error: Running Homebrew as root is extremely dangerous and no longer supported.
As Homebrew does not drop privileges on installation you would be giving all
build scripts full access to your system.

$ ls -lrt /usr/local/sbin
ls: /usr/local/sbin: No such file or directory

# mkdir /usr/local/sbin
# chmod 777 /usr/local/sbin/
$ brew link libvirt
Linking /usr/local/Cellar/libvirt/6.1.0... 151 symlinks created

Starting libvirt

$ brew services start libvirt
==> Tapping homebrew/services
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'...
remote: Enumerating objects: 83, done.
remote: Counting objects: 100% (83/83), done.
remote: Compressing objects: 100% (46/46), done.
remote: Total 686 (delta 30), reused 63 (delta 22), pack-reused 603
Receiving objects: 100% (686/686), 190.88 KiB | 1.13 MiB/s, done.
Resolving deltas: 100% (269/269), done.
Tapped 1 command (39 files, 264.4KB).
==> Successfully started `libvirt` (label: homebrew.mxcl.libvirt)

Create a disk image

In this step you could download a cloud GNU/Linux qcow2 file:

$ mkdir -p ~/qemu/images
$ wget --no-check-certificate https://cdimage.debian.org/cdimage/openstack/current-10/debian-10-openstack-amd64.qcow2 -O ~/qemu/debian-10-openstack-amd64.qcow2

Or download a GNU/Linux image, for example a Debian ISO and create the qcow2 disk image file:

$ wget --no-check-certificate https://cdimage.debian.org/debian-cd/10.3.0/amd64/iso-cd/debian-10.3.0-amd64-netinst.iso -O ~/qemu/images/debian-10.3.0-amd64-netinst.iso

$ qemu-img create -f qcow2 ~/qemu/debian-10.3.0-amd64.qcow2 10G
Formatting '/Users/XXX/qemu/debian-10.3.0-amd64.qcow2', fmt=qcow2 size=10737418240 cluster_size=65536 lazy_refcounts=off refcount_bits=16

Launch QEMU Debian VM

If you choose a cloud image you could launch the VM using the following command:

$ qemu-system-x86_64 \
  -m 2048 \
  -vga virtio \
  -show-cursor \
  -usb \
  -device usb-tablet \
  -enable-kvm \
  -drive file=~/qemu/debian-10-openstack-amd64.qcow2,if=virtio \
  -net user,hostfwd=tcp::2222-:22 -net nic \
  -machine accel=hvf \
  -cpu host

If you choose an ISO it’s needed to install the OS so it’s needed to launch the VM like that and follow the installation steps:

$ qemu-system-x86_64 \
  -m 2048 \
  -vga virtio \
  -show-cursor \
  -usb \
  -device usb-tablet \
  -enable-kvm \
  -cdrom ~/qemu/images/debian-10.3.0-amd64-netinst.iso \
  -drive file=~/qemu/debian-10.3.0-amd64.qcow2,if=virtio \
  -net user,hostfwd=tcp::2222-:22 -net nic \
  -machine accel=hvf \
  -cpu host

In my case I have to click in Allow button in the following dialog:

Reference links:

That’s all Folks!!!

“Knowing yourself is the beginning of all wisdom.”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s