QEMU/Windows guest

From Gentoo Wiki
< QEMU
Jump to:navigation Jump to:search

This article describes the setup of a Windows guest using QEMU.

Configuration

Host

To create a disk image for the virtual machine, run:

user $qemu-img create -f qcow2 WindowsVM.img 25G
Tip
Consider allocating more space if you are aiming for newer Windows versions as e.g. Win 10 Pro 22H2 after fresh install already occupies 20G. You may extend disk images easily by qemu-img resize later on, but as Windows rescue partition is located at the end of the disk this needs further steps.

Download a Windows driver image from this location.

Since QEMU requires a lot of options, it would be a good idea to put them into a shell script, e.g.:

FILE WindowsVM.sh
#!/bin/sh
exec qemu-system-x86_64 -enable-kvm \
        -cpu host \
        -drive file=WindowsVM.img,if=virtio \
        -net nic -net user,hostname=windowsvm \
        -m 1G \
        -monitor stdio \
        -name "Windows" \
        "$@"

Change the path to the disk image WindowsVM.img in the script. Additional options can be used when calling the script. To boot the disk image, run:

user $./WindowsVM.sh -boot d -drive file=WINDOWS.iso,media=cdrom -drive file=DRIVER.iso,media=cdrom

Install the Windows guest. See the guest section for optimum support. After the installation start the script without the additional options.

Note
When upgrading to QEMU 1.3 the -enable-kvm must be added to keep the guest responsive. Also change -net nic to -net nic,model=rtl8139 to have a network interface in the guest.
Note
When upgrading to QEMU 1.5.3, add -display sdl,frame=off,window_close=off" to keep a full screen session full screen. Without this option there may be a (new) menu bar at the top of the screen, pushing the Windows bottom menu off screen.
Note
When upgrading to QEMU 2.0.0 replace qemu-kvm with qemu-system-<CPUTYPE> (e.g. qemu-system-x86_64) (-enable-kvm switch may need to be added); the qemu-kvm wrapper has been removed - see bug #506566
Note
German (and perhaps other) localized Windows versions likely are not able to setup any virtual network card properly on QEMU >=6.0 due to a problem in Windows - see QEMU issue #774

Guest

  • During installation at the partition step Windows doesn't detect the VirtIO hard drive. Windows will require the viostor driver from the driver image listed above.
  • After installation the VirtIO Ethernet adapter will appear without a driver. Windows will require the netkvm driver from the driver image listed above.
  • For 64-bit Windows 7 Intel HDA is available as an option (QEMU option: -soundhw hda)
  • Windows 10 guest audio emulation is tricky for ALSA users. Pass these options to QEMU:-audiodev alsa,id=snd0,out.try-poll=off -device ich9-intel-hda -device hda-output,audiodev=snd0
    • This assumes use of the default ALSA device for playback.
    • we cannot use AC97 for Windows 10 because there is no driver for it.
    • out.try-poll=off is an odd option, but without it I got nothing but clicks and pops during playback.
    • there may be more options required for line-in and line-out (-device hda-duplex) or microphone and line-out (-device hda-micro).
    • For PulseAudio, see ArchWiki
  • USB 2.0 pass through can be configured from host to guest with variations of: -usb -device usb-ehci,id=ehci -device usb-host,bus=ehci.0,vendorid=1452
  • For Windows 8.1 USB tablet is available only with USB 2.0 pass through (QEMU option: -device usb-ehci,id=ehci -device usb-tablet,bus=ehci.0
    • The USB tablet device helps the Windows guest to accurately track mouse movements. Without it mouse movements will be jerky.
  • Another device that can be presented to the Windows guest is the random number generator. Add QEMU option: -device virtio-rng-pci . Now install the viorng driver from the driver image.
  • For Windows 10, to boot using UEFI the sys-firmware/edk2-ovmf is required on the host, then add QEMU option: -bios /usr/share/edk2-ovmf/OVMF_CODE.fd. to the qemu call. This option is essential for running Hyper-V guest images.

SPICE

QEMU with SPICE support enables (among other things) the more powerful QXL display device and makes clipboard sharing possible (copy/paste between clients and the virtual machine).

To use SPICE with QEMU enable the following USE flag in package.use:

FILE /etc/portage/package.useSetting USE variable
app-emulation/qemu   spice

Build QEMU:

root #emerge app-emulation/qemu

To connect spice server, a client like net-misc/spice-gtk is required.

Guest

On Windows guests, install Windows guest tools.

On Windows 8.1 guest, to set screen resolution more than 1024x768, install drivers from https://fedoraproject.org/wiki/Windows_Virtio_Drivers#Direct_download and QXL WDDM DOD driver. If trying to install Windows guest tool, QXL WDDM DOD driver won't work well because of Windows SPICE agent .

To try the new qlx-dod driver linked above, in order to use the driver, update' the basic windows display adapter driver and point it to the unzipped folder for the new driver. This can be found by: Right click the Start button -> Device Manager. Expand 'Display adapters' and right click on the sub entry and hit update driver.

Initialization script

To run QEMU from a script (the spicy viewer requires net-misc/spice-gtk):

FILE WindowsVM.sh
#!/bin/sh
SPICE_PORT=5924
qemu-system-x86_64 -enable-kvm -daemonize \
    -cpu host \
    -drive file=WindowsVM.img,if=virtio \
    -net nic -net user,hostname=windowsvm \
    -m 1G \
    -vga qxl \
    -spice port=${SPICE_PORT},disable-ticketing=on \
    -usbdevice tablet \
    -device virtio-serial \
    -chardev spicevmc,id=vdagent,name=vdagent \
    -device virtserialport,chardev=vdagent,name=com.redhat.spice.0 \
    "$@"
exec spicy --title Windows 127.0.0.1 -p ${SPICE_PORT}

Or remote-viewer (app-emulation/virt-viewer):

FILE WindowsVM.sh
#!/bin/sh
SPICE_PORT=5924
qemu-system-x86_64 -enable-kvm -daemonize \
    -cpu host \
    -drive file=WindowsVM.img,if=virtio \
    -net nic -net user,hostname=windowsvm \
    -m 1G \
    -vga qxl \
    -spice port=${SPICE_PORT},disable-ticketing=on \
    -usbdevice tablet \
    -device virtio-serial \
    -chardev spicevmc,id=vdagent,name=vdagent \
    -device virtserialport,chardev=vdagent,name=com.redhat.spice.0 \
    "$@"
exec remote-viewer --title Windows spice://127.0.0.1:${SPICE_PORT}