2017年2月24日 星期五

[Linux 文章收集] Guide to running Windows 7 in QEMU

Source From Here 
Introduction 
I was trying to get this working earlier and found it a bit confusing, so here's a little guide that might come in handy for people who are forced to use Windoze for work or school. For those who are unfamiliar, QEMU is a pretty simple, straightforward virtual machine manager that works with the Kernel-based Virtual Machine hypervisor built into Linux. 

First install the QEMU package. 
# yum search qemu // Search qemu packages
# yum -y install qemu-kvm.x86_64

How-To 
Next create the disk image that Windows will be installed into. I would recommend keeping the disk image and install ISO in the same folder, mine is ~/machines/windows. I created a 30GB image, which will plenty for what I need, but you might want more or less. I would also use the qcow2 format, which doesn't automatically take up all the space you've allotted before the virtual machine uses it. 
# qemu-img create -f qcow2 disk.qcow2 30G
Formatting 'disk.qcow2', fmt=qcow2 size=32212254720 encryption=off cluster_size=65536 lazy_refcounts=off

Next you'll need to either copy a Windows 8 ISO from a disc or download a legal ISO from Microsoft (Or windowsiso.net). If the former, insert your Windows disc and, preferably in the machine-specific folder you created for the disk image, copy the iso to your hard drive. The device name of your ODD will likely be /dev/sr0, but if it's something different make sure to modify the command. 
# dd if=/dev/sr0 of=install.iso

If you don't have a CD, you'll need to download an official ISO from Microsoft. Make sure you download the version that matches your serial key. If you don't have a key, there's something you can use called Daz Loader, but I won't condone that in my post (wink, wink). Download the ISO here. Rename it to something sane like install.iso and move it to the folder you have the disk image saved in. Next you need to enable KVM, which will allow the virtual machine to access your hardware much more directly than software emulation. To use KVM, you need an Intel processor with VT-x capability or an AMD processor with AMD-V. If the former, enter: 
# modprobe kvm_intel
Or
# modprobe kvm_amd

We also probably want these modules to load at boot. To set this up, make a .conf file in modprobe.d called something that describes what it loads. Mine is vt-x.conf, but it doesn't matter what it's called. 
# vi /etc/modprobe.d/kvm.conf

Or kvm_amd. depending on your processor. Now the modules will be present after a reboot. 

From here on out I'm going to assume your disk image is called disk.qcow2, your ISO is called install.iso, and you are in the folder containing both of these files. Now we can start up the virtual machine. I'm assuming that you're running this on a 64-bit system (if not, try "qemu-i386" instead of "qemu-system-x86_64"). I set the memory that the virtual machine can use to 4096MB, which is around half the total RAM of my system. Again, modify this based on your system and how much RAM you think the VM needs (keeping in mind that Windows requires 1GB of RAM for i386 and 2GB for x86_ 64). 
// -enable-kvm: enable KVM full virtualization support
// -m megsset virtual RAM size to megs MB [default=128]
// -cdrom file: use 'file' as IDE cdrom image (cdrom is ide1 master)
// -boot [order=drives][,once=drives][,menu=on|off]
// [,splash=sp_name][,splash-time=sp_time][,reboot-timeout=rb_time][,strict=on|off]
// 'drives': floppy (a), hard disk (c), CD-ROM (d), network (n)

# qemu-system-x86_64 -enable-kvm -m 4096 -cdrom install.iso -boot d disk.qcow2

This will start the VM in a new window. Go through the install process. Once everything is running, you can begin the hellish Windows Update process. You'll probably have to check and install updates three or four times before they're all covered. Now you've got Windows up and running on your machine, quarantined from your pure, virginal Linux system: 

To start your VM after shutting it down, you will need to specify all the attributes again, besides for the install ISO. I have a script called windows.sh with the following contents: 
  1. qemu-system-x86_64 -enable-kvm -m 4096 -vga std -daemonize ~/machines/windows/disk.qcow2  
This adds a few commands that make the script more useful. "-vga std" lets the machine's resolution be adjusted beyond 1024x768. "-daemonize" detaches the QEMU process from the terminal, so that you don't have to leave it open while your machine is running. Some more handy tips: press Ctrl-Alt to free the mouse from the virtual machine, and Ctrl-Alt-2 to open the QEMU command line, which allows commands like "quit"

Some Further Notes 
If you using a tiling window manager, I would recommend setting qemu-system-x86_64 to float automatically, or else Windows will look blurry and distorted due to it's been resized. 

SOUND: So I've been playing around with sound for a while, and I believe I've found the solution. I'm going to do a full reinstall before I confirm anything, of course. After trying a bunch of things, the necessary steps seem to be as follows. First you need to set the QEMU audio driver to ALSA, since it defaults to OSS and makes sound not work. 
# export QEMU_AUDIO_DRV=alsa

You probably want this to be persistent, so add a line to your .bash_profile, .profile, or whatever autostart file you use. Now that the QEMU audio is set right, boot Windows 7 with the -soundhw ac97 command: 
# qemu-system-x86_64 -enable-kvm -m 4096 -vga std -daemonize -soundhw ac97 ~/machines/windows/disk.qcow2

Download the Realtek AC97 driver for Windows 7 from this page. Download this file on the Windows machine, not the host. 
Right click > "Extract all" from the .zip you just downloaded 

Run the executable named "setup" in the folder you just extracted This will run the whole install process, then ask you to reboot. After you reboot you should hear the ear-grating Windows startup noise and sound should work fine. 

Supplement 
Linux: Find Out If CPU Support Intel VT and AMD-V Virtualization Support

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...