Header Ads

How To Use QEMU To Test Operating Systems & Distributions

QEMU is a virtualization technology emulator that allows you to run operating systems and Linux distributions easily on your current system without the need to install them or burn their ISO files, it is like VMware or VirtualBox, you can use it at anytime to emulate running any operating system you want on a lot of devices and architecture.
QEMU is free and open source; it’s licensed under GPL 2, it has the ability to run under both KVM and XEN models (if you enabled virtualization technology from your BIOS first) and offers a lot of options and virtualization options. In this article, we’ll explain how to use QEMU and install it.

How To Install QEMU

Fortunately, QEMU is available to install from almost all the official Linux distributions repositories, which is a good thing for us since we won’t need to download or install anything from other than the official packages sources for our system.
To install QEMU on Ubuntu/Linux Mint:
sudo apt install qemu qemu-kvm libvirt-bin
To install QEMU on Red Hat/CentOS/Fedora (replace dnf with yum for Red Hat/CentOS):
sudo dnf install qemu qemu-kvm
To install QEMU on SUSE/openSUSE
sudo zypper in qemu
To install QEMU on Arch Linux:
sudo pacman -S qemu

How To Use QEMU

Now let’s start with using and testing, QEMU provides a lot of options, architectures and formats to use, and we will see many different examples in order to understand the possible ways to achieve our goals using QEMU.
First, we have to create a virtual hard drive image if we want to install our virtual operating system somewhere, this image file will contain all the data and files of the operating system after installation. To do this, we can use the “qemu-img” tool.
To create an image file with the size of 10GB and qcow2 format (default format for QEMU images), run:
qemu-img create -f qcow2 testing-image.img 10G
Note that a new file called “testing-image.img” is now created at your home folder (or the place where you run the terminal), note also that the size of this file is not 10 Gigabytes, it’s around 150KB only; QEMU won’t use any space unless needed by the virtual operating system, but it will set the maximum allowed space for that image to 10 Gigabytes only
Now that we’ve created our image file, if we have an ISO file for a Linux distribution or any other operating system and we want to test it using QEMU and use the new image file we created as a hard drive, we can run:
qemu-system-x86_64 -m 1024 -boot d -enable-kvm -smp 3 -net nic -net user -hda testing-image.img -cdrom ubuntu-16.04.iso
Let’s explain the previous command part by part:
  • -m 1024: Here we chose the RAM amount that we want to provide for QEMU when running the ISO file, we chose 1024MB here, you can change it if you like according to your needs.
  • -boot -d: The boot option allows us to specify the boot order, which device should be booted first? -d means that the CD-ROM will be the first, then QEMU will boot normally to the hard drive image, we have used the -cdrom option as you can see at the end of the command. You can use -c if you want to boot the hard drive image first.
  • -enable-kvm: This is a very important option, it allows us to use the KVM technology to emulate the architecture we want, without it, QEMU will use software rendering which is very slow, that’s why we must use this option, just make sure that the virtualization options are enabled from your computer BIOS.
  • -smp 3: If we want to use more than 1 core for the emulated operating system, we can use this option, here we chose to use 3 cores to run the virtual image which will make it faster, you should change this number according to your computer’s CPU.
  • -net nic -net user: By using these options, we will enable an Ethernet internet connaction to be available in the running virtual machine by default.
  • -hda testing-image.img: Here we specified the path for the hard drive which will be used, in our case, it was the testing-image.img file which we created before.
  • -cdrom ubuntu-16.04.iso: Finally we told QEMU that we want to boot our ISO file “ubuntu-16.04.iso”.
After you run the previous command, QEMU will start for you as a standalone window:

Ubuntu 16.04 LTS Running Under QEMU
Ubuntu 16.04 LTS Running Under QEMU
Now if you want to just boot from the image file without the ISO file (for example if you have finished installing and now you always want to boot the installed system), you can just remove the -cdrom option:
qemu-system-x86_64 -m 1024 -boot d -enable-kvm -smp 3 -net nic -net user -hda testing-image.img
Note that in this tutorial, we used the x86_64 architecture to run the emulation, if you want, you can choose from a lot of other available architectures to test your systems on:
ls /usr/bin | grep qemu-system*
qemu-system-aarch64
qemu-system-alpha
qemu-system-arm
qemu-system-cris
qemu-system-i386
qemu-system-lm32
qemu-system-m68k
qemu-system-microblaze
qemu-system-microblazeel
qemu-system-mips
qemu-system-mips64
qemu-system-mips64el
qemu-system-mipsel
qemu-system-moxie
qemu-system-or32
qemu-system-ppc
qemu-system-ppc64
qemu-system-ppc64le
qemu-system-ppcemb
qemu-system-sh4
qemu-system-sh4eb
qemu-system-sparc
qemu-system-sparc64
qemu-system-tricore
qemu-system-unicore32
qemu-system-x86_64
qemu-system-x86_64-spice
qemu-system-xtensa
qemu-system-xtensaeb
For example, if you want to emulate the i386 architecture, you should run the following command:
qemu-system-i386 -m 1024 -boot d -enable-kvm -smp 3 -net nic -net user -hda testing-image.img
Just make sure that the ISO files for Linux distributions and other operating systems you use are for the same architecture that you use on QEMU, because if they are different, you may not be able to boot it up or install it.
If you want to use QEMU to boot from a CD / DVD inserted at your disk drive, then you can easily do:
qemu-system-x86_64 -m 1024 -boot d -enable-kvm -smp 3 -net nic -net user -hda testing-image.img -cdrom /dev/cdrom

Conclusion

QEMU is a very effective technology to emulate virtual operating systems, if offers huge possibilities for usage and testing and gives a very nice performance, it’s free and open source and available in all major Linux distributions’ repositories, which makes it one of the best emulating and virtualization software on the Linux desktop.
For further reading, we recommend checking the official documentation and the wiki page for QEMU.

No comments