How to create virtual machines using QEMU
Tags: virtualmachine, tutorial, opensource
Published: 2023-09-20 21:40:16

## Introduction
QEMU is one of the most powerful virtual machine client ever created but there's one issue with it, it's a command line program, that means it does not have a GUI (Graphical User Interface) for that reasons, not everyone chooses QEMU as it's virtual machine client but in this post, I will show you how to create a virtual machine with QEMU.
Before getting started, you need to know that we are going to be doing the examples in macOS so all the commands are exclusively valid on macOS but with a simple search, you can get their equivalent commands for Linux and Windows.

## Why I'm doing this post?
This post is made mainly for all those Apple Silicon users trying to virtualise systems like Windows Server or Ubuntu Server, those systems are not ARM compatible and that means, they cannot be virtualised from a ARM machine like the M1 or M2 Macs. For that reasons im making this post, to try to help all those Apple Silicon users to virtualise their OSs.

## Installation
As we cannot start the house by the top, first we need to install QEMU, to do so we can use homebrew, so we just need to run the following command:

```
brew install qemu
```
After doing this, we can check that the installation has been successful by executing the following command:

```
qemu-system-x86_64 --version
```
After executing those commands, you should have qemu installed on your device, now we have to create our disk image.

## Creating a disk image
Before creating a virtual machine we have to create a virtual disk, to do so we have to execute the following command:

```
qemu-img create -f qcow2 nombre_de_tu_archivo.qcow2 tamaño_del_disco
```
Well, that may sound a little bit weird for you so let's 'decode' it :D
First of all we are using 'qemu-img' which is the command that makes us able to generate virtual disks compatible with qemu, then we are writing 'create' which does what it says, it creates the virtual disk and then the '-f' flag which stands for 'format' followed by 'qcow2', let's stop here to explain what qcow2 is.

### What's qcow2?
qcow2 (QEMU Copy On Write 2) is the second version of the qcow format which is used by QEMU, it's function is to store the hard drive contents of QEMU. qcow2 is the format that replaces the first version of qcow. **NOTE**: qcow2 format has replaced the original qcow format, however QEMU still can mount both qcow and qcow2 disks.

Now you know what qcow2 is, we can proceed and create the virtual disk, this is how you should see the command:

![Image showing the execution of the qemu-img command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jen6rbrkacsxf0v1prjv.png)

## Virtual machine creation

### Choose the architecture
Once the virtual disk has been created, we can finally proceed and create our virtual machine, so the first step is to choose the architecture that we are going to use, commonly you will use x86_64 so you have to choose the command that has that suffix but if you want to use arm just choose the command that has that suffix and you are ready to go. 

```
qemu-system-x86_64
```

### Boot settings
Next we are to use the flag '-boot d' which indicates that we want to boot the machine from the CD-ROM, next we will use the '-cdrom' flag to indicate the machine the image that we want to load in the CD-ROM, following the flag we have to introduce the path to the ISO file of the OS we want to install.

```
-boot d -cdrom path_to_the_iso_file.iso
```

### Virtual disk selection
Finally we have to use the '-hda' flag to indicate the virtual disk that we want to use, following the flag we have to introduce the path to the virtual disk that we created in the previous step.

```
-hda /path/to/virtual/disk.qcow2
```

Now all the steps are done and we should have the following command:

```
qemu-system-x86_64 -m memory -boot d -cdrom path_to_the_iso_file.iso -hda /path/to/virtual/disk.qcow2
```

Once we execute this command a new window should pop up showing the boot of the operating system that we have installed. 

## Final thoughts
This is how you can create any virtual machine of any CPU architecture on an Apple Silicon Mac, this is a thing that actually you can't do on VMware or VirtualBox which is extremely disappointing and with QEMU you are able to do it, there's a software called UTM available for macOS for free which is a 'graphical version' of QEMU but it's performance is not very great and personally I prefer working with QEMU, it's more difficult as you need to learn the commands and to work with the command line interface which is something that most of users don't like but also it's much more powerful and it's performance is faster than almost any other virtual machine client.

I hope you've enjoyed this quick post and you've found it helpful and let me know in the comments if you would like to see a more advanced post of QEMU seeing other commands to configure more advanced settings of the virtual machines.
See you next time :D