Running Ubuntu in a virtual machine on QEMU(Quick Emulator)

Nikos Mouzakitis
3 min readMar 12, 2021

--

In this story, we will run a Linux distribution using QEMU(https://wiki.qemu.org)

First we will need to obtain an iso image of the Linux distro that we will run in the virtual machine. In this example we will use Ubuntu 20.10 which you can find in this link(https://ubuntu.com/download/desktop)

Click on Download for Ubuntu 20.10

After completion of downloading of the iso file, in order to run the virtual machine we create the following script:

#!/bin/bash
qemu-system-x86_64 -M pc — enable-kvm -cpu host -m 4096 -hda ubuntu-20.10-desktop-amd64.iso

After running the above command we can inspect that are in the Grub menu:

GRUBmenu

and after waiting some time,we are prompted either to try or to install Ubuntu.

Of course in the previous script we have not specified any memory or disk on which we could possibly install the Ubuntu.

For this purpose, a qcow2 format disk will be created in which the ubuntu will be installed.(Note that at least 8.9GB are required for this installation.)

To create the aformentioned disk, the following script is used:

#!/bin/bash
qemu-img create -f qcow2 hda.qcow2 16G

By that we are creating a disk of type qcow2,which will be able to hold at most 16GB.

Now to install Ubuntu we run :

#!/bin/bash
qemu-system-x86_64 -M pc — enable-kvm -cpu host -m 4096 \
-device virtio-net-pci,netdev=net0,romfile=”” \
-netdev type=user,id=net0 \
-device virtio-blk-pci,drive=drv0 \
-drive format=qcow2,file=hda.qcow2,if=none,id=drv0 \
-object rng-random,filename=/dev/urandom,id=rng0 \
-device virtio-rng-pci,rng=rng0 \
-device virtio-scsi \
-device scsi-cd,drive=cd \
-drive if=none,id=cd,file=ubuntu-20.10-desktop-amd64.iso

As we can observe, we pass the iso image in the cd drive and we have also
included in our emulated system a pci block device with the hda.qcow2 disk we have previously created. This time when booting we can install Ubuntu.

Installing Ubuntu on the virtual machine.

After installation completes,we can shutdown the virtual machine(or even kill it).

Restart it using the next script,where we have completely removed the downloaded image:

#!/bin/bash
qemu-system-x86_64 -M pc — enable-kvm -cpu host -m 4096 \
-device virtio-net-pci,netdev=net0,romfile=”” \
-netdev type=user,id=net0 \
-device virtio-blk-pci,drive=drv0 \
-drive format=qcow2,file=hda.qcow2,if=none,id=drv0 \
-object rng-random,filename=/dev/urandom,id=rng0 \
-device virtio-rng-pci,rng=rng0 \

Booting on the installed Ubuntu

As we can see we have successfully managed to boot into our installed system in the virtual machine using qemu.

--

--

Nikos Mouzakitis
Nikos Mouzakitis

Written by Nikos Mouzakitis

Graduate of Mathematics Department in University of Aegean, Currently Computer Engineer undergrad.

No responses yet