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.)

McEnroe
  • 381
  • 1
  • 4
  • 5
  • 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 03 '11 at 06: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 04 '11 at 17:39
  • Try this: http://www.avforums.com/forums/windows-7/944905-windows-7-ahci.html#post8951321 – gelraen Oct 05 '11 at 07: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 05 '11 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 06 '11 at 07:34
  • @gelraen in that case please point out a specific post that describes this problem. The one you like initially asks my question but he never receives a response. Also, turning ahci off in my physical machine is not an options since my gentoo (and my performace) depends on it. Also, what do you mean by just backing up registry files exactly? I'm afraid your suggestion is a bit too general for me. I'm just not sure what to do ... – McEnroe Oct 06 '11 at 20:37
  • Just curious... does it have to be QEMU? Or, can VMware be used? I know VMware will boot existing partitions, but I'm not sure about VirtualBox or Bochs. – kodybrown Nov 28 '15 at 17:00

4 Answers4

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 :(]

doak
  • 364
  • 3
  • 11
  • 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. – Brent Rittenhouse Jul 10 '19 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 '19 at 11:22
  • Can it be read/write? – Heath Mitchell Oct 14 '20 at 15:59
  • 1
    Sure. Don't forget to remove `-snapshot` if you actually want to persist the stuff. – doak Oct 15 '20 at 16:06
  • I've used a Windows 7 ISO and could not get the DISM command to work. Got errors 2, 87 and 200 and something... the only way to get the virtio drivers installed was to proceed with the installation until the drive selection UI and use the "Install Drivers" button to graphically search and install them, then cancel the wizard and go back to the console. – Samir Aguiar Jan 21 '21 at 03:09
  • 1
    A quick update: I also managed to get the drivers installed with `drvload.exe E:\amd64\w7\viostor.inf` – Samir Aguiar Jan 21 '21 at 12:10
  • As far as I understand, `drvload` installs the driver to the *current* Windows installation, which maybe differs from your use case. In case you boot from the installation media/recovery to install the drivers for your regular installation, you will need the `dism` variant. Anyway, +1 for the useful hint. – doak Jul 05 '21 at 18:27
  • I didn't realize I could do `chmod /dev/...`. That was the missing piece of the puzzle for me. – Sridhar Sarnobat Dec 18 '22 at 01:48
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!

willjp
  • 79
  • 1
  • 2
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.

Hashken
  • 513
  • 1
  • 5
  • 16
  • 3
    VirtualBox can advertise itself as arbitrary motherboard (https://forums.virtualbox.org/viewtopic.php?t=33356) perhaps QEMU can as well. – RushPL Jun 16 '14 at 17:45
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.

Pablo A
  • 1,470
  • 13
  • 21
Areeb Soo Yasir
  • 255
  • 2
  • 3