Run Ubuntu in Windows using QEMU without MSYS2

Qemu is an alternative for VirtualBox, VMWare Workstation Player, and other virtualization applications. WSL2 installation is easier and flexible enough to support most Linux-based appalications and packages. If WSL2 or virtualizaion software like VirtualBox is getting your job done, then I do not see a point in putting extra effort for installing Linux-based distribution using Qemu.

Why are we here? There are cases when WSL2 needs kernel-level modifications. Recently, I was planning to install Mininet on my Windows via Ubuntu running on WSL2. While I was installing the Mininet, I encountered a few prerequisite packages that require kernel-level modifications. This means that I need to restart the Linux installation and take care of some changes to its kernel. This process becomes tedious and most times, it is not worth the effort.

What we will do: We will install Qemu, create an Ubuntu image, and run the Ubuntu from Qemu.

Before you begin: Hyperlinks are in Blue, commands are in Red.

My system configuration: Windows 11, 32GB RAM, and AMD Ryzen 7. Yours may be different and I would assume that you will replace the versions/names of the files and commands where needed.

This installation does not use MSYS2 that is recommended by the Qemu website: https://www.qemu.org/download/#windows

Steps:

Step 1: Download an ISO image of Ubuntu. I chose the ubuntu-20.04.6-desktop-amd64.iso image from here: http://releases.ubuntu.com/focal/

Step 2: Download the Qemu source by choosing one of the latest releases from here: https://download.qemu.org/

  • I chose the qemu-8.0.0-rc3.tar.xz

  • Once the qemu-8.0.0-rc3.tar.xz file is downloaded, extract the file. By default, this will create “qemu-8.0.0-rc3” folder.

  • Move the extracted directory “qemu-8.0.0-rc3” into a location of your choice in your machine.

Step 3: We will create a disk image (extension “.img”) using the qemu source that was downloaded and extracted.

  • Open a terminal inside the “qemu-8.0.0-rc3” folder.

  • Move the ubuntu-20.04.6-desktop-amd64.iso into the “qemu-8.0.0-rc3” folder.

  • Run the command below to create a disk image (“img”) file. The command uses the Qemu disk image utility called “qemu-img”.

qemu-img create -f qcow2 Ubuntu20.img 20G

  • This creates a disk image called “ubuntu20.img” with 20GB of disk space. You can increase or decrease the disk space as you wish.

Step 4: Now we will install Qemu on Windows. I know this is confusing because we previously downloaded Qemu source (“qemu-8.0.0-rc3”). So you might be wondering why not use the Qemu source to install it on your Windows?

Windows installations from source code can be tricky. One of the reasons Qemu recommends MSYS2 for installation is precisely to automate the installation process. While this process is better than installing from the source, I found it to be tedious as it relies on MSYS2, which itself can have dependencies and can break the existing ones. I like to avoid that.

What we will be doing to avoid MSYS2 method is installing Qemu using an executable binary.

  • Download the “qemu-w64-setup-20230414.exe“ from here: https://qemu.weilnetz.de/w64/

  • Run the qemu-w64-setup-20230414.exe and finish the installation procedure. Keep note of the Qemu installation location on your Windows. You will be prompted to chose the Qemu installation folder. The default is “C:/Program Files/qemu”. You will need this path later.

Step 5: Finally, we will run through the boot loading process and then boot up the Ubuntu VM.

  • Before you boot up your Ubuntu VM on qemu, move the disk image “ubuntu20.img” and the ISO image “ubuntu-20.04.6-desktop-amd64.iso“ into the Qemu installation folder i.e. “C:/Program Files/qemu” or wherever you chose to for the installation folder during the installation.

  • Install the kernel installer by running the boot command as below.

    ./qemu-system-x86_64.exe -m 8G -hda .\ubuntu20.img -nographic

    Press “ctrl+B” to load the iPXE command line before the boot process is finished.

    You will see a iPXE command line (“>iPXE>”)

  • Inside the iPXE command line, configure the dhcp server. This is required because Qemu will download and install the kernel from the internet. In fact, iPXE was created as boot loader over the network. If you interested in learning more on iPXE, go to: https://ipxe.org/

    dhcp

  • Now lets install the linux kernel and “initrd.gz” file by pasting the full path of the two files in the iPXE command line.

    You can find the two files here: debian_installation_files.

  • http://ftp.debian.org/debian/dists/buster/main/installer-amd64/current/images/netboot/debian-installer/amd64/

  • We are only interested in the linux kernel and the initrd.gz files. Run the following commands (good resource on why we need the two commands: here)

    kernel http://ftp.debian.org/debian/dists/buster/main/installer-amd64/current/images/netboot/debian-installer/amd64/linux

    initrd http://ftp.debian.org/debian/dists/buster/main/installer-amd64/current/images/netboot/debian-installer/amd64/initrd.gz

  • Now we are ready to the boot up our Ubuntu VM

    boot

    This will take you through the Ubuntu installation process. Finish the installation by following the prompt from the boot loader. Notice that your disk image “ubuntu20.img” has been modified!

Now your Ubuntu VM is installed on Qemu!

If you want to start the Ubuntu VM, first open a fresh terminal from the qemu installation folder (“C:/Program Files/qemu/”). This folder contains the boot loader (“qemu-system-x86_64.exe”) and the Ubuntu disk image (“ubuntu20.img”). We do not need the ISO file anymore.

You can type in the following command from the terminal that you just opened from the qemu installation folder.

./qemu-system-x86_64.exe -m 8G -hda .\ubuntu20.img -nographic

This boots up an Ubuntu VM with 8GB memory (“-m” option specifies the memory) and no GUI (“-nographic“ option). The “-hda” options tells the bootloader to load the image from the hard disk.

You can find other options to pass here: Qemu wikibooks

Previous
Previous

Hardware-assisted Security for Ag-IoT

Next
Next

Move your WSL to cloud and save storage space