Tuesday, August 5, 2008

Running your physical Windows XP partition as a VM using Virtualbox on Ubuntu.

Ok, so you've just installed your shiny new Hardy Heron OS but you still have all those pesky Windows apps you need to run.

Currently you're dual booting, but that sucks.

No worries, the latest version of Virtualbox allows to wrap an existing physical partition as a virtual machine.

Here's how to do it.

1. Install Virtualbox
Firstly; do not install the Virtual box which comes standard in the Ubuntu repositories. In order to support this functionality you have to download the closed source version from Sun. BTW I give Kudos to Sun for being .deb friendly.

2. Setup your permissions.
After you've installed virtualbox you will need to assign your user to certain groups in order to access the physical partition and virtualbox for that matter.

The commands are as follows:
- "sudo usermod -a -G disk "
- "sudo usermod -a -G vboxusers "

You will need to log out of your terminal and then log back in for the changes to take effect.

3. Prep your Windows installation.
Since Virtualbox imposes a Virtual hardware environment to the guest operating system, Windows is going to try install all the new drivers for Virtualbox's virtual hardware environment. This is a problem if you wish to boot back into that Windows partition. For this reason you have to create two separate hardware profiles in Windows; one to run physically and one to run as a guest operating system. This can be done under System Properties > Hardware > Hardware profiles.

Next you have to run a utility called MergeIDE which you can get here. As it turns out Windows writes some registry information about your physical IDE controller into the registry. MergeIDE does some funky rewiring of the registry in order to get around this so that you can "move" your Windows installation onto another hardware environment.

4. Create a virtual hard disk for Virtualbox (VMDK file).
The first thing you need to do is determine which partition your MBR (master boot record) lives on. You can determine this using fdisk or checking your Grub settings.

Finally when you have all this information you create the VMDK file using the command:

VBoxManage internalcommands createrawvmdk -filename [Absolute_Path_to_output_File] -rawdisk /dev/sda -register


You then start Virtualbox, create a new Virtual machine and select this created VMDK file as the hard disk for this VM.

5. Modifying your VM settings for Windows.
In the settings for your virtual machine, under "general" you need to enable the IO APIC and VT-x/AMD-V options in the "advanced" tab.

6. Fixing the licensing issues.
The Windows XP volume license registers your license key against the BIOS for that machine. When Virtualbox loads your guest operating system it presents virtual BIOS information to that OS, which obviously invalidates the license you currently have installed for XP since from it's point of view it's now on a different machine.

In order to fix this you have to modify the settings of your VM config file in order to report your physical BIOS information.

First you have to get the information using the dmidecode command. To get the DMI bios information and the the DMI system information run: dmidecode -t0 and dmidecode -t1 respectively.

You then have to append this information as a set of key-value pairs to your virtual machine settings. A list of all the variables can be found in section 9.13 of the VirtualBox user guide.

The user guide explains how to do this using the "VboxManage" command I found it easier to add the variables myself to the config file of your VM which can be found at ~/.VirtualBox/Machines/My_Windows_Machine/My_Windows_Machine.xml and can be added under the <ExtraDataItems> element.

And that should be it, now you can fire it up!

Some final information.
  • When you start the virtual machine it will effectively start the same boot loader you normally use for the dual boot system. Under no circumstances boot into the same OS you are currently running. If you do this I can guarantee you a world of pain.

  • Performance is not fantastic. This seems to mostly stem from lack of decent graphics acceleration. I upped the Video memory and turned off all visual effects in Windows which helped however if you know a way to improve graphics performance, let me know.
  • 12 comments:

    Abhijeet said...

    Clear and simple! I had tried doing this once on Vmware and messed up badly. Feel like trying it again after reading your post. Thanks!

    Matt said...

    Nice instructions, it's just a pain in the butt that you have to do all of that to your machine in order to get it to boot correctly. I did like the howto, though. Thanks!

    julian_za said...

    @Matt, It is a pain, when I originally did it I had to trawl google everytime I hit a problem. I also accidentally booted into the Ubuntu partition I was running which caused havoc with the file system. I had to fsck the disk in order to fix it, luckily it worked, else I wouldn't have been able to write the blog post, since I would still be rewriting all the code I would have lost :-)

    Rory Laitila said...

    Thanks for the post!

    I currently am able to make it through the grub boot screen, but when windows starts to load, I get a BSOD, with no useful error. Any suggestions?

    antych said...

    I followed your instructions and it works. I used Vritual Box 2.0.2. The only problem was booting windows after installing guest tools, but I tried few times and it worked eventually.

    Kristjan Adojaan said...

    Anyone any idea how to do it with Vista? Vista does not support hardware profiles any more, so don't want to mess up the system...

    Adam L. said...

    I was able to make this work on a Mac under Boot Camp on OS X Leopard, as well, using a similar process.

    Although, to be fair, I did it for completely different reasons. My iMac has a broken LCD, which has been removed. I use an external display, which is unsupported during the installation phase of Windows.

    So, by using your process (with BSDish /dev/ entries instead of the Linux ones, fixing device ownership wonkiness), I was able to get my XP install rocking towards my already-created Boot Camp partition, change the IDE oddities using MergeIDE, and boot into it off the bare metal.

    Kind of the bass-ackwards way of doing things, but *real* raw disk support is lacking in either commercial package for OS X - VMware Fusion and Parallels Desktop (both of which I own and use).

    -Adam

    Unknown said...

    Lone

    Can you explain step by step what did you do to use BootCamp with VBox?

    Thanks

    Anonymous said...

    You can use GuestAdditions. The only driver misbehaved on real hardware is mouse class filter. You can disable it by removing from "UpperFilters" in
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E96F-E325-11CE-BFC1-08002BE10318}

    Of course this must be done it in VirtualBox, before you boot from real hardware.

    Rusl Bicycle said...

    This seems great. Thanks! I want try it on my netbook.

    However, using the normal GRUB which might boot you into your host OS by default is really dangerous.

    Has anyone ever tried simply making a 2nd GRUB partition and somehow getting Virtualbox to use that instead? It seems like it should be possible but some complications could arise. I haven't thought it all out yet.

    Anyway, do you know of anyone doing this? I'd rather follow a howto than re-invent the wheel here.

    thanks

    Christian Ramcke said...

    How about Vista? Can anyone give me advice running a Vista partition? Thanks!

    jago said...

    Thanks for this info - I've been looking for this for a very long time.

    It's also good to have a micro copy of XP as well. However, to do this means a lot of work and it would be easier to use one of the preprovided images that are floating around as warez. One is <80mb! (but no longer available AFAIK)