25

I have gentoo and windows 7 installed alongside on my only hard drive. I want to start my windows install from within gentoo through qemu. Boot menu works fine but windows bluescreens on me soon after I select it.

How would I make it work?

(I assume windows is missing drivers required for the qemu-emulated hardware to work.)

7
  • make sure that you run qemu with correct architecture. Also, windows will fail to boot if it awaits HDD controller to be AHCI and it's not (and vice versa).
    – gelraen
    Oct 3, 2011 at 6:44
  • Architecture is good, but I don't think there's anything one can do about the ahci since qemu doesn't seem to support it ...
    – McEnroe
    Oct 4, 2011 at 17:39
  • Try this: avforums.com/forums/windows-7/…
    – gelraen
    Oct 5, 2011 at 7:08
  • @gelraen the problems is that your link describes the reverse process of what I want to do. I can't risk just trying it out without knowing that there is a chance it working out or at least that it's 100% reversible.
    – McEnroe
    Oct 5, 2011 at 14:57
  • That post is exactly about reverting back from AHCI to ATA. And you always can turn off AHCI on physical machine and change it back, or even simply backup registry files.
    – gelraen
    Oct 6, 2011 at 7:34

4 Answers 4

16

Booting an native, physical Windows 7 partition can be done by this:

  1. Prepare requisites (Windows 7 installation media, Virtio drivers).
  2. Make your hard drive accessible read-only for your current user.
  3. 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

  1. Prepare an ISO or, if you have a drive at hand, the real installation media for Win7.
  2. 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

  1. 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
    
  2. 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 :(]

8
  • 1
    The 10 reputation thing is a pain. It stopped me, and a lot of others, from giving the detailed kind of help we wanted. =/ I just uploaded mine to imgur and pasted the links, FYI. Jul 10, 2019 at 22:14
  • To be honest, 10 points are not too hard to get. The issue is that I am not able to find the screenshots anymore. I am pretty sure that they are somewhere on my disks, but ... :/
    – doak
    Jul 14, 2019 at 11:22
  • Can it be read/write? Oct 14, 2020 at 15:59
  • 1
    Sure. Don't forget to remove -snapshot if you actually want to persist the stuff.
    – doak
    Oct 15, 2020 at 16:06
  • 1
    A quick update: I also managed to get the drivers installed with drvload.exe E:\amd64\w7\viostor.inf Jan 21, 2021 at 12:10
7

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!

2
  • This worked for me on arch linux
    – jameh
    Feb 25, 2020 at 2:14
  • Very helpful, I've got a basic case working. My next challenge is to try and get my old physical Windows XP C: drive to run using as a real root disk inside qemu. Dec 16, 2022 at 6:08
4

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.

1
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .