Setting up a VM with QEMU on Windows 11
So QEMU has always intimidated me. I see posts on reddit recommending this product. I've always been intimidated by the documentation, maybe just the plethora of information and lack of the quick start have always made me turn away and use other solutions like VirtualBox or Hyper-V which are much more intuitive. If anyone from QEMU reads this, I think what you're doing is great but your docs need a quickstart/cliffnotes because there are plenty of blog posts like this one that try to explain the process, and (maybe) become dated when new versions are released.
Anyways, if you are new to Virtualization or Linux I'd recommend picking up VirtualBox and playing around with installing Ubuntu with a desktop environment. I used to use VirtualBox in a former role to build Windows images to deploy to end-users laptops, and while the performance wasn't always great, it got the job done and taught me a lot about networking, computers, and virtualization.
Onto the meat and potatoes. QEMU seems like a great solution, especially since it is all command line based. This makes it a great candidate to automate Virtual Machine (VM) deployment. Virtualbox also has command line interfaces, but reddit tells me QEMU is much more performant. This is my first rodeo, so time will tell.
What you'll need:
Some hard drive space
QEMU
QEMU Windows Installer
A Linux image, I'm using the debian DVD iso
https://cdimage.debian.org/debian-cd/current/amd64/iso-dvd/
Once you download each of these, you'll want to verify the SHA-512 hash to ensure the authenticity of the download. On Windows Powershell you can use the command certutil -hashfile “.\path\to\downloaded\exe” SHA512
This will spit out a hash you can verify against the one provided by QEMU and Debian, the hash in the Powershell prompt should match the hash provided in the downloads directory on the site. If it doesn't, delete the download and try another download mirror until one matches. If the hash doesn't match, someone has injected a file into the download link and its probably malware, or at least not authentic.
Once you have both QEMU and a Linux ISO downloaded, you then need to install QEMU. In Windows, its a graphical installer and you can click through the prompts and let the install complete. Once completed you need to locate where it installed. On my machine I located it at C:\Program Files\qemu
You then need to add this to your path variable, to do this you need to go to the Windows Settings, and About. Or you can right click on This PC and go to properties. You'll see an “Advanced System Settings” option, click on that. A window will pop up with System Properties, and under the Advanced tab there is an Environment Variables option. Click on that and a new window will pop up with User Variables and System variables. Under each is a Path option. I selected System Variable's Path option and added C:\Program Files\qemu
As an option. You could add it to the User variables if a bunch of people use your machine and you only want it configured for your account. Anyways, click OK on all the windows to confirm the update, then close any open Powershell/terminal windows and reopen them. You then should be able to run qemu-system-x86_64 -help
And you should see some help information pop up. If you see an error message, then the path variable didn't save and you should try those steps again, or possibly rebooting your machine. In the event you can't get this to work, you can just execute the software directly from it's installation path C:\Program Files\qemu\qemu-system-x86_64.exe
I am using this binary because I am on an x86_64 machine, if you are using a different architecture on your host machine, then you should review the QEMU documentation and select the appropriate binary.
Once you have QEMU installed, its time to get onto building a VM!
If you just want to test that QEMU is working you can run something like qemu-system-x86_64 -boot d -cdrom \path\to\downloaded\linux\iso -m 2048
This boots a vm with 2048 bytes (2gig) of ram, with no hard disk attached. You can use this to see if the ISO loads and for things like a live USB install of ubuntu.
For more info on commands see the qemu man page.
However, if you want a disk attached to the VM then we first need to create the disk, then include the disk in the command to launch the VM. To create a disk you can use a command like qemu-img create -f vdi debian_hd.vdi 50G
Where qemu-img
creates the disk, -f
is the format of the disk, here I am using the VDI format since I am familiar with virtual box, and I am alloting 50gigs of space for the virtual disk.
Once this is created, you then need to boot the VM with the ISO installer as a CD-ROM:
qemu-system-x8664.exe -machine type=q35,accel=tcg -m 4096 -cpu qemu64 -smp 4 -drive file=C:\Users\bee\qemuvms\debian\debianhd.vdi,format=vdi,if=virtio -cdrom C:\Users\bee\qemuvms\debian\debian-12.6.0-amd64-DVD-1.iso -boot d -device virtio-net-pci,netdev=unet -netdev user,id=unet,hostfwd=tcp::2222-:22 -serial mon:stdio
This command should boot you to the ISO installer. The -machine
flag sets it the type to q35
which is a PCI express-based machine, and accel
is set to tcg
which is Tiny Code Generator, a software based emulator. -m 4096
sets the ram to 4gigs. -cpu qemu64
is a generic processor to emulate provided by qemu. -smp 4
sets the machine to 4 use 4 cores. -drive
is the location of the virtual hard drive we created, with a vdi
format and using the virtio
interface. -cdrom
is the path to your linux iso file. -boot
sets the device to boot to the cd-rom drive d
. -device virtio-net-pci,netdev=unet
sets a virtual network card. -netdev user,id-unet,hostfwd=tcp::2222-:22
forwards tcp traffic from port 2222 to the host port 22 on the virtual machine for SSH access. -serial mon:stdio
redirects the virtual machines serial port to the input/output of the console so it can be accessed from the current terminal window.
Once you run this command, and all goes well you should see an installer. I am installing Debian, and I have never had luck with virtualization and setting disk encryption, so do not set that option. Go through the debian installer and disable the Desktop environment and GNOME and enable SSH server and standard system utilities. I left everything else as default. The Graphical installation will eventually complete and then you will need to run a new command to boot the machine without the CD-ROM so it boots to the hard disk qemu-system-x8664.exe -machine type=q35,accel=tcg -m 4096 -cpu qemu64 -smp 4 -drive file=C:\Users\bee\qemuvms\debian\debian_hd.vdi,format=vdi,if=virtio -boot order=c -device virtio-net-pci,netdev=unet -netdev user,id=unet,hostfwd=tcp::2222-:22 -serial mon:stdio
Upon running this, if all went well you will be greeted with a prompt to login and then you have your virtual machine! The next task is to set up a bridged network adapter so other machines on my network can access this machine, as this is certainly much more useful if you can ssh into it.
This post seems to provide some insight into how to set up a TAP adapter to enable this functionality:
https://gist.github.com/arvati/546617042fcf2669f330b739075c1c5d