Skip to content

Instantly share code, notes, and snippets.

@k-amin07
Last active June 13, 2024 13:03
Show Gist options
  • Save k-amin07/d6ea1a3626a36e2ade1a59be934a3d9e to your computer and use it in GitHub Desktop.
Save k-amin07/d6ea1a3626a36e2ade1a59be934a3d9e to your computer and use it in GitHub Desktop.
Using a Windows installation in a virtual machine in Linux

Introduction:

This guide shows how to virtualize and dual boot the same OS. My host OS is linux (Manjaro, which is based on Arch) and my guest OS is Windows 10.

Configuration:

  1. Manjaro installed on my SSD, in linux it appears as the primary drive (/dev/sda).
  2. Windows is installed on a partition on secondary hard disk (/dev/sdb as linux calls it).
  3. Virtualization enabled in BIOS.

Installation:

We need a few packages to get started. Here is the command for Arch based distros with pacman.

sudo pacman -S virt-manager qemu vde2 ebtables dnsmasq bridge-utils openbsd-netcat

Enabling virtualization:

We need to enable and start libvirtd.service. This is basically the linux's virtualization service. To do that issue the following commands.

sudo systemctl enable libvirtd.service sudo systemctl start libvirtd.service

Install and Setup OVMF:

This is required for UEFI systems. It probably can be skipped on BIOS systems.

sudo pacman -S ovmf

Check /usr/share/ovmf/x64/ folder. It should contain the following files:

  1. OVMF_CODE.fd
  2. OVMF.fd
  3. OVMF_VARS.fd

Copy OVMF_VARS.fd files to .config folder in your $HOME directory:

sudo cp /usr/share/ovmf/x64/OVMF_VARS.fd $HOME/.config/qemu-windows.nvram

Add write permissions to the file just copied:

sudo chmod u+w $HOME/.config/qemu-windows.nvram

UefiShell.iso:

Now we need a file called UefiShell.iso. Unfortunately, I was unable to find it. However, if we install edk2-ovmf package from fedora, this file would be generated in /usr/share/edk2/ovmf. So open the pamac manager (GUI for pacman), search for the said package and install it. After edk2-ovmf is installed, issue the following commands:

sudo cp /usr/share/edk2/ovmf/UefiShell.iso /opt/UefiShell.iso

sudo mkdir /temp

Run VM:

Now to launch the VM, issue the following command:

sudo qemu-system-x86_64 --enable-kvm -cpu host -m 3072 -smp 5 -mem-path /dev/hugepages -display sdl -vga qxl -device qemu-xhci,id=xhci -device virtio-tablet,wheel-axis=true -soundhw hda -netdev user,id=vmnic,smb=/temp -device virtio-net,netdev=vmnic -drive file=/usr/share/ovmf/x64/OVMF_CODE.fd,if=pflash,format=raw,unit=0,readonly=on -drive file=$HOME/.config/qemu-windows.nvram,if=pflash,format=raw,unit=1 -drive file=/dev/sdb,index=0,media=disk,driver=raw -cdrom /opt/UefiShell.iso

Here /dev/sdb is my windows disk. Yours could be different. If everything was successful, QEMU GUI will appear. Press escape, a BIOS-like boot manager menu will appear. Here navigate to "UEFI QEMU hard disk" option and press enter.

Finishing Notes:

This guide is for a basic setup, qemu options can be tweaked to improve performance and the VM output can be directed to a dedicated monitor through GPU passthrough. Guide for GPU passthrough without dualboot can be found in my gists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment