How to boot a physical Windows partition with qemu?
Solution 1:
Booting an native, physical Windows 7 partition can be done by this:
- Prepare requisites (Windows 7 installation media, Virtio drivers).
- Make your hard drive accessible read-only for your current user.
- Boot it using QEMU in snapshot mode.
This does not change anything on the physical drive (in fact it is read-only). If you want to persist the stuff, remove -snapshot
from the QEMU invocation or use commit all
within the QEMU console.
By the way, this works fine for Windows 10 as well.
Details
Prepare requisites
- Prepare an ISO or, if you have a drive at hand, the real installation media for Win7.
- Download Virtio drivers, e.g. for example this drivers from Fedora:
https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/latest-virtio/virtio-win.iso
Take care to use the correct installation media matching the installed Windows architecture (32bit vs 64bit). The following commands assume a 64bit architecture.
Make disk accessible
This assumes your hard drive is /dev/sda
and has be executed as root:
chmod g-w /dev/sda
chgrp $USER /dev/sda
This changes will (probably) be lost at next reboot as udev will usually recreate all device files.
Booting with QEMU
-
This again assumes your hard drive is
/dev/sda
. Networking is disabled:qemu-system-x86_64 -snapshot -m 3G -enable-kvm -usbdevice tablet \ -drive file=/dev/sda,if=virtio \ -drive file=<win7-installation-media> \ -drive file=<virtio-driver-media>,media=cdrom -boot d \ -net none
-
Now boot from CD/DVD to start Windows 7 recovery, load the Virtio driver and run the following command in the recovery command prompt. Ensure that you replace the drive letters with the proper ones:
dism /image:g:\ /add-driver /driver:e:\viostor\w7\amd64
Drive G:
is the drive containing your Windows 7 installation. Drive E:
represent the CDROM drive with the Virtio drivers.
In order to show some help for this command variant use dism /image:g:\ /?
.
The dism
command was copied from this Super User answer.
[I am sorry, screenshots were prepared, but I was unable to post them due to the 10 reputation threshold. Unfortunately I am unable to find them in the meantime :(]
Solution 2:
I have managed to do this on archlinux, mostly by adapting examples that other (smarter) people provided in blogs.
Minimal Version:
install deps
sudo pacman -S qemu # qemu itself
sudo pacman -S ovmf # intel EFI driver
cp /usr/share/ovmf/x64/OVMF_CODE.fd bios.bin # copy OVMF_CODE.fd to bios.bin somewhere. It MUST be renamed.
start qemu
qemu-system-x86_64 \
-enable-kvm `# enable KVM optimiations` \
-L . `# dir with bios.bin` \
--bios bios.bin `# bios.bin itself` \
-m 8G `# provide reasonable amount of ram` \
-cpu host `# match the CPU type exactly` \
-drive file=/dev/sda,format=raw,media=disk `# load raw HDD`
Performance Optimizations
The following flags were also recommended to me, and I have been using them successfully.
# emulate exact host cpu,
# enable hyper-v enlightenments
-enable-kvm
-cpu host,hv_relaxed,hv_spinlocks=0x1fff,hv_vapic,hv_time
-machine type=pc,accel=kvm
# use all available CPU cores
-smp $(nproc)
Viewer
I used spice as a viewer:
-daemonize `# don't start monitor, we connect using RDP` \
-vga qxl \
-spice port=5930,disable-ticketing \
-device virtio-serial \
-device virtserialport,chardev=spicechannel0,name=com.redhat.spice.0 \
-chardev spicevmc,id=spicechannel0,name=vdagent \
Followed by the following to display viewer
spicy -h 127.0.0.1 -p 5930 & disown spicy; # spice-gtk
Windows Bootloader Warning
Finally, beware of the windows bootloader if you are using multiple disks. I have a windows-install now that will not boot unless a second non-OS drive is also present.
References
I wish I kept my sources here - I remember at least the following were super helpful:
https://wiki.qemu.org/Main_Page https://qemu.weilnetz.de/doc/qemu-doc.html https://wiki.archlinux.org/index.php/QEMU https://wiki.gentoo.org/wiki/QEMU https://www.suse.com/documentation/sles11/book_kvm/data/part_2_book_book_kvm.html
Good Luck!
Solution 3:
From what I saw in this post, you can't use qemu to boot an existing installation of Windows.
The primary argument being given is that, when windows is installed the Product key is associated with the Motherboard of your computer. Now, when you try to run Windows using qemu, windows perceives qemu emulator as a different motherboard and thus gives problems.
Refer to the post for more details.
Solution 4:
Here is what I use for a physical Windows 10 drive. Note that in many cases Windows will fail to boot if you don't use the "-cpu host
" flag.
Just replace /dev/nvme0n1
with your drive eg. /dev/sda
or /dev/sdb
qemu-system-x86_64 --enable-kvm -cpu host -smp 8 -m 8192 -drive format=raw,file=/dev/nvme0n1 # Replace this last parameter
Source.