Taming Windows 7 in a VirtualBox VM Using Raw Disk Access

VirtualBox
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]
  • ningbao
    Is there a typo here?
    You said:
    Set the VM to mount the DVD drive and put in your Vista DVD. Start the VM.

    What does a Vista DVD (instead of a Windows 7 DVD? ) have to do with this?

    Or, I missed something here?
  • javatexan
    I can't get mine to work...I have Ubuntu 9.10/VB 3.1.4r57640. I added myself to the disk group and vboxusers group. I have the same setup as you do, a dualboot with windows 7 enterprise and ubuntu9.10. Ubuntu9.10 is my host os. I have all on sda with sda1-win7 100MB part sda2-win7 87G. I get all the way to booting the system and trying the recovery stuff. The recovery claims that it fails and I get these errors. http://imagebin.ca/view/vyhycT.html and http://imagebin.ca/view/yvoZZd.html. Someone on #vbox said that it may mean that win7 doesn't know how to access the vmdk. At this point I am very lost and not sure what to try next. Thanks.
  • Sape
    Note: if you use VBox 3.1.2, then the regression is gone again, no need to downgrade.
    Note: if you use the version which is not fully open source but still freely available (and has USB support as well), then you do not need the insecure chmod-ing above either. You only need to add your user to the vboxusers and disk group in /etc/group (line looks like: vboxusers:x:121:sape). In my system (Ubuntu Karmic) ls -l /dev/sda* shows:
    brw-rw---- 1 root disk 8, 0 2010-02-03 15:54 /dev/sda
    brw-rw---- 1 root disk 8, 1 2010-02-03 15:54 /dev/sda1
    brw-rw---- 1 root disk 8, 2 2010-02-03 16:30 /dev/sda2
    brw-rw---- 1 root disk 8, 3 2010-02-03 15:54 /dev/sda3
    brw-rw---- 1 root disk 8, 5 2010-02-03 15:54 /dev/sda5
    brw-rw---- 1 root disk 8, 6 2010-02-03 15:54 /dev/sda6
  • John
    Hi Rajat,

    Firstly thanks for the post. I am researching this process and your guide/post is the best I have found. I too want to have ubuntu as the host and Windows as the guest. At present I have just windows 7 installed, and I am on the verge of repartitioning the hard disk and installing Ubuntu. That is one to ponder eh? Well, I have a couple of questions for you:

    You dont mention your system specs. I have a 32 bit dual core @ 2.0 laptop with 4 gig of ram. How much did you put aside in Virtualbox for the 7 VM to get comparable performance to when 7 runs in standalone mode?

    I see a few folks have made comments about having to run the 7 repair app on standalone or vm restart. Have you overcome this? D Parker's comments looks great - but I'll need to reread it a few times to follow it. Lastly, how easy was it to downgrade virtualbox?

    Thanks in advance,
    John
  • John - Sorry I have not had a chance to update these steps with the added
    instructions in the comments - I hope to get time this coming weekend to get
    the dual-boot to work without the repair step. In terms of the downgraded
    VirtualBox - you should be able to use 3.0.12 and forward - the fix is
    included in that version.

    Hope this helps and thanks for reading.

    Rajat
  • John
    Thanks for reply Rajat - I look forward to reading the write up.
  • KM
    I used this technique. However, when ever I switch from VBox to Native boot (or the reverse) I must do a repair. In the case of native it does the repair on boot, and then does a second boot. In the case of VBox it requires the DVD image to do the repair and reboot. Either way its painful.

    Is there a way to avoid the repair on each switch?
  • Sape
    Same here. I am not bothered by the native boot self-healing itself, but that would be nice the other way around too. Is it maybe possible to install a recovery console to the same partition too, as it worked with XP?
  • fazan
    I tried following your suggested steps but I'm getting a BSOD when I try to run the Windows partition, even after repairing the Windows 7 install from the DVD. Here's my partition layout according to VBoxManage:

    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 162 /162/2 1200 2048
    2 0x07 162 /162/3 1023/239/63 64913 2459648
    5 0x83 1023/239/63 1023/239/63 44053 135414783
    6 0x82 1023/239/63 1023/239/63 1926 225635823
    3 0x07 1023/239/63 1023/239/63 10000 229586944

    and according to fdisk:

    Disk /dev/sda: 128.0 GB, 128035676160 bytes
    255 heads, 63 sectors/track, 15566 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Disk identifier: 0x02f2e53c

    Device Boot Start End Blocks Id System
    /dev/sda1 * 1 154 1228800 7 HPFS/NTFS
    Partition 1 does not end on cylinder boundary.
    /dev/sda2 154 8429 66471262 7 HPFS/NTFS
    Partition 2 does not end on cylinder boundary.
    /dev/sda3 14292 15566 10240000 7 HPFS/NTFS
    Partition 3 does not end on cylinder boundary.
    /dev/sda4 8430 14291 47083680 5 Extended
    Partition 4 does not end on cylinder boundary.
    /dev/sda5 8430 14046 45110488+ 83 Linux
    /dev/sda6 14046 14291 1973128+ 82 Linux swap / Solaris

    Does it make a difference that my partitions aren't aligned on cylinder boundaries? I've read that this isn't ideal for other performance reasons (it's a 128GB SSD), but I didn't think the partition alignment would affect the ability to load the raw W7 partition.

    I also tried DParker's suggestion of making the Linux partition accessible to VBox, and I did indeed get the grub menu to show up, but selecting the Windows partition caused an immediate crash.

    Any advice?
  • Sape
    Yes, it did work for me like described here!
  • stephan
    Is there any way to do this without going through the 'repair' step? My laptop has no cd/dvd and I need to dual-boot into one OS or the other frequently. It is not looking promising: http://forums.virtualbox.org/viewtopic.php?f=7&...
  • D Parker
    I ran into the same problem (it has to do with how the VirtualBox MBR deals with the drive geometry) and found that if I configured the virtual disk without the -mbr switch it used the MBR on the host. I had Windows on the first partition and Linux on the second partition. If I gave the virtual machine access to both partitions it could load menu.lst from the Linux partition and I could boot cleanly into Windows.

    I was afraid that some day I might accidentally boot into Linux, which would be really ugly since it would be trying the boot the same OS the host was running and almost surely cause corruption. So as a trick I created a boot floppy image with grub for the Windows VM and changed the menu so Windows was the only choice. I attached this image to the VM, made sure it the boot order had the floppy first, and now I can boot Windows cleanly either natively or through VirtualBox.
  • apmsylvain
    Dear rajatarya,

    I follow your tutorial, unfortunately I have trouble to repair Seven. I've got a Blue Screen with no useful information (of course ! So easy !), but Windows boot manager give me some clue : Status 0xc000000e - The boot selection failed because a required device is inaccessible. Do you have any idea ?
    Thank you in advance
    apmsylvain
  • @apmsylvain

    I have also gotten this, but I resolve it by booting up with the Windows 7 DVD and going through the 'Repair' steps. This is an annoying step for actually dual-booting. Since setting this up I have only booted back into Windows 7 a couple times - mostly for testing. I have simply used the Windows 7 instance from within VirtualBox OSE on the Ubuntu installation.

    Hopefully this helps, thanks for your comment.
  • Kevin
    I have been trying to find this sort of detailed guide for a week. Thank you!
  • Glad this was helpful Kevin. Make sure to downgrade from VirtualBox 3.10, since there is a regression in that release related to raw disk access. See the bug report here: http://www.virtualbox.org/ticket/5355. Let me know how the steps turn out for you and I'd love to hear any feedback you have on them.
blog comments powered by Disqus