Taming Windows 7 in a VirtualBox VM Using Raw Disk Access

Image via Wikipedia

(This is even more technical than my other technical posts. Consider yourself warned.)

I wanted a dual-boot system between Windows 7 and Ubuntu 9.10 64-bit.  I wanted both systems to be fully functional as 64-bit operating systems and have full access to the computer’s hardware.  Accomplishing this has been well documented and I won’t bother discussing it.  I simply repartitioned the hard drive into two partitions, and then let Windows 7 install on one of them and then installed Ubuntu 9.10 on the other.  Grub gives me a choice when the machine boots and defaults to Linux, which is what I wanted.  Then I decided I want to run Windows 7 in a VM on the Linux machine, and that I really wanted the VM to run from the Windows 7 partition already created.  I had heard this was possible with earlier versions of Windows, so I figured Windows 7 should be no exception (at least if Vista can support such a scenario then Windows 7 should be the same).

I started by reading this, which gave me good guidance, but was the other configuration – Windows as the host and Ubuntu as the guest – I wanted the opposite – Ubuntu as the host and Windows as the guest.  That led me to the VirtualBox User’s Guide, which does a great job of describing the process of using Raw Disk access.  And finally, I did some perusing of the VirtualBox forums to find a couple specific answers to getting the setup working.

In an effort to help document this scenario, here are the things I did to get it working:

  1. Read the VirtualBox User’s Guide on Raw Disk Access (here)
  2. user@computer:$ VBoxManage internalcommands listpartitions -rawdisk /dev/sda
    VirtualBox Command Line Management Interface Version 3.0.8
    (C) 2005-2009 Sun Microsystems, Inc.
    All rights reserved.

    Number Type StartCHS EndCHS Size (MiB) Start (Sect)
    1 0x07 0 /32 /33 12 /223/19 100 2048
    2 0x07 12 /223/20 1023/254/63 99900 206848
    3 0x07 1023/254/63 1023/254/63 70000 204802048
    5 0x83 1023/254/63 1023/254/63 65624 348176808
    6 0x82 1023/254/63 1023/254/63 2839 482576598
  3. Notice how there is a 100MB partition (partition 1) – that is the boot partition for Windows 7. This, along with partition 2 are the Windows 7 partitions. Partition 3 is my ‘data’ partition, which is shared between both OSes. All of these partitions need to be enabled for read/write access by me in order for VirtualBox to load them up. I did this by:
    user@computer:$ chmod 666 /dev/sda1
    user@computer:$ chmod 666 /dev/sda2
    user@computer:$ chmod 666 /dev/sda3

    I know this is not the most secure way of doing things, but it works for me.

  4. Now it is also important that there is a place for Master-Boot Record (MBR) to get loaded from VirtualBox. This is necessary so that when the VM starts up it has an MBR to use – otherwise it will try to use Grub and will fail miserably. To get a ‘dummy’ MBR created I read a couple forum posts (here), and then did the following:
    user@computer:$ sudo apt-get install mbr
    user@computer:$ install-mbr -e12 --force ~/vm.mbr

    The -e12 argument means I want the first and second partition enabled in the MBR. This is critical to getting it all to work – otherwise the VM won’t know which partition to enable.

  5. Now we are ready to actually create the raw disk for VirtualBox to handle, I typed in the following:
    user@computer:$ VBoxManage internalcommands createrawvmdk -filename /home/rajat/win7.vmdk -rawdisk /dev/sda -partitions 1,2 -mbr /home/rajat/vm.mbr -relative -register
  6. Go through VirtualBox, create a new VM, mark it Windows 7 (in my case 64-bit) and save. The VM is ready to be started, but it won’t work entirely yet.
  7. Set the VM to mount the DVD drive and put in your Vista DVD. Start the VM. Press F12 and select the DVD drive to start (c). Let Win7 setup start, pick a language, and then click the ‘Repair installation’ option. Go through automatic repair, and then let the VM restart. This time it should go into Win7 running off the raw disk.

Let me know if you have any trouble with these instructions, or would like to add to them. Drop me a line to know if these worked for you as well. I can’t wait to use these steps on my other boxes and put Windows in a box while I’m using it.

Update: There is a regression in VirtualBox 3.10 regarding raw disk access. Any machine that boots with raw disk access stops booting using VirtualBox 3.10. Read more about it in the documented bug report. As a workaround simply downgrade to VirtuablBox 3.08 or install the OSE edition. I got hit with this the day after this post went live, downgrading solved the problem for me.

Reblog this post [with Zemanta]
This entry was posted in technical, website and tagged , , , . Bookmark the permalink.