Using QEMU on Macbook

In attempts to keep up with DevOps you really should know how to deploy, manage, destroy KVM images. Recently I was supplied a qcow2 image to deploy but at work we are supplied VMWare boxes which won’t work. I can’t deploy a virtual in a virtual instance and I need hardware and what I have to work with is a Macbook Pro. So here is how I did this and hoping it helps you as well.

First make sure you have xcode installed and if not its as easy as opening terminal and pasting the below command. You should be prepared to wait a bit, its not a fast process but its a huge download. You will eventually get a message that says The software was installed.

ShellScript
xcode-select --install

Now you have the Pre-Req of xcode installed the next step is to install MacPorts (very similar to Homebrew if you are more familiar with that).

Go to MacPorts.org/install.php and select which version of Mac OS you are running (if you don’t know.. click the Apple in top left corner and click on About This Mac and you should see what version you are running.)

The .pkg will download and just double-click the pkg and go through the prompts (its very easy install, no decisions needed).

Now let’s install QEMU which is as simple as running the following command from your terminal. It will take a bit to install so give it some time.

ShellScript
sudo port install qemu

You should see a prompt that says: Continue? [Y/n] <– type y and press enter

**NOTE: you should occassionally check to see if macports has an update by running: sudo port selfupdate && sudo port upgrade qemu

Now you’ll need a way to manage storage, network and other goodies and for that you need something like virt-manager installed which is as easy as another command like this to run in your terminal

ShellScript
sudo port install virt-manager

You will eventually be prompted with: Continue? [Y/n]: <– type y and press enter to begin the download and install of virt-manager.

Now let’s install AlmaLinux as a sample.

ShellScript
qemu-img create -f qcow2 AlmaLinux-9.3-x86_64-dvd.qcow2 20G

In this step we boot up the machine with the AlmaLinux ISO. Copy and paste the following in your terminal

ShellScript
qemu-system-x86_64 \
    -machine type=q35,accel=hvf \
    -cpu Nehalem \
    -smp 2 \
    -hda AlmaLinux-9.3-x86_64-dvd.qcow2 \
    -cdrom ./Downloads/AlmaLinux-9.3-x86_64-dvd.iso \
    -m 8G \
    -vga virtio \
    -usb \
    -device usb-tablet \
    -display default,show-cursor=on

The options are:

  • -machine: The emulated machine and the accelerator. q35 is the newest machine type and HVF is the macOS native hypervisor.
  • -cpu: The default is Ivory but for my i7 mac I had to change this to Nehalem to make it work. (qemu-system-x86_64 -cpu help)
  • -smp: Number of CPUs to use
  • -m: Amount of memory to use
  • -hda: Disk drive (the one we created earlier)
  • -cdrom: The ISO image to put into the CD drive
  • -vga: The graphic card to use. I found virtio (based on Virgil to have the best performance but a warning.. setting up Linux is difficult since I couldn’t see the Continue button at the bottom but I figured it out with Tab and guess)
  • -usb: Enable USB host controller
  • -device Adding a “usb-tablet” as an input device. I’m running this on a laptop and without this setting the mouse did not work.
  • -display: To show the mouse cursor (disabled by default)

Now after the machine is booted up the Ubuntu installer will run. Follow the installation steps and don’t restart the VM at the end of the installation, instead shut it down by stopping the qemu process with CTRL-C on the host.

Since you hit the CTRL-C above ending the creation of the qcow2 image so now you don’t need to point to the cdrom and now when you want to launch that qcow2 image to boot Alma Linux back up based on how you configured it you just run

ShellScript
qemu-system-x86_64 \
    -machine type=q35,accel=hvf \
    -cpu Nehalem \
    -smp 2 \
    -hda AlmaLinux-9.3-x86_64-dvd.qcow2 \
    -m 8G \
    -vga virtio \
    -usb \
    -device usb-tablet \
    -display default,show-cursor=on

Hoping this helps you get started with the free virtualization on your Mac which is typically faster then others but draw backs is qemu doesn’t have guest additions like VMWare Fusion, Parallels, VirtualBox does that you can drag and drop, share clipboard, etc..

Related Articles

Responses