Running Fedora 37 on QEMU x86_64 machine
Tags: qemu, libvirt, fedora, kvm
Published: 2023-01-06 17:33:43

In my previous post, I played with QEMU by installing Debian on an emulated arm machine. It was challenging and I ended up spending a lot of time for it. But I think it was a good exercise nevertheless.

However, as my mentors suggested, it's much better to use the same QEMU target architecture as your host. Therefore, here I'll describe the steps I followed to run Fedora 37 on QEMU x86_64 machine.

But first, here's a little bit about KVM.

###What is KVM and how does it relate to QEMU?

[KVM](https://www.linux-kvm.org/page/Main_Page) (Kernel-based Virtual Machine) is an open source full virtualization solution for Linux on x86 machine with virtualization hardware extension (Intel VT or AMD-V). To see if the virtualization extension exists, run the following to look for `svm` (for AMD-V) or `vmx` (for Intel VT) flags.

```
grep -E 'svm|vmx' /proc/cpuinfo
```

On my computer, the result contains `svm` which means I have a AMD-V.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sx267qd9pwm0lezx9yr7.png)

To see if KVM is enabled, run the following:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uj4ch5hbr2ziyolmaaw2.png)

Hypervisor is a software that provides the virtualization to the underlying hardware; it creates and runs Virtual Machines. [This IBM tutorial](https://developer.ibm.com/tutorials/l-hypervisor/) was a great read to understand about virtualization and hypervisors. KVM is a type 1 (bare metal) hypervisor which means that it has a direct access to the hardware resources.

Whereas, QEMU is a type 2 hypervisor which means it runs on top of the host OS. QEMU is also tightly integrated with KVM. If KVM is enabled on the host, and guest and host OS have the same architecture, QEMU can have KVM to translate some of the instructions (CPU and memory calls) making it much faster. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6vc50msoy3fbyjfem8xg.png)
Source: [Researchgate](https://www.researchgate.net/profile/Tolga-Soyata/publication/281177318/figure/fig1/AS:284451906048014@1444830027668/Comparison-of-Xen-KVM-and-QEMU.png)

###Libvirt

[Libvirt](https://wiki.libvirt.org/page/FAQ) is collection of software that provides a convenient way to manage virtual machines and other virtualization functionality. 
Tools such as `virt-builder`, `virt-install` and `virt-manager` that are based on `libvirt` make building and installing virtual machines much easier.

####Virt-builder

[`Virt-builder`](https://libguestfs.org/virt-builder.1.html#description) is a tool a tool that quickly builds virtual machine images. It's part of [`libguestfs`](https://www.libguestfs.org/) which is a set of tools to build, access, modify virtual machine disk images and more.

First, install `libguestfs`.

```
sudo apt install libguestfs-tools
```

The following will list all the virtual machine images you can create:

```
virt-builder --list
``` 

Build Fedora image. The size specified with `-size 30G` will not be the actual size of the image. It just means that the newly created image can be expanded up to 30G.

```
virt-builder  \
    --ssh-inject=root:file:/home/amar/.ssh/id_rsa.pub  \ 
    --root-password=password:vm_root_password   \
    --output=fedora37.qcow2 --format=qcow2   \
    --selinux-relabel --size 30G fedora-37
```


####Virt-install and Virt-manager

Create a VM using the disk image created above with `virt-builder`:
```
virt-install 
    --connect qemu:///system --name f37-vm–import \
    --ram 2048 --vcpus 2 --cpu host   \
    --disk bus=virtio,path=fedora37.qcow2   \ 
    --network network=default,model=virtio  \
    --os-variant fedora37
```

Or use [`virt-manager`](https://virt-manager.org/) which is a GUI tool that allows you to create a VM with one-click.

However, when I try to use `virt-install` or `virt-manager`, I got the following error even though virtualization hardware is present and enabled.

`ERROR    Host does not support any virtualization options`

###Direct install

I had a network problem when I tried direct install with `qemu-system-x86_64`. Therefore, first install slirp, configure and build QEMU again with `--enable-slirp`.

```
sudo apt install libslirp-dev
../configure --enable-slirp
```

Then directly run VM with QEMU:

```
qemu-system-x86_64 -smp 2 -m 1G -M q35,accel=kvm   \
    -drive file=fedora37.qcow2,format=qcow2,if=virtio  \   
    -nic user,model=virtio
```

When the VM runs, login using the root password set when building the disk image.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lww9xsrk7ir7bnrlneb7.png)