Sharing Windows 7 between Boot Camp and VirtualBox

Posted by: admin  :  Category: OS X, VirtualBox, Virtualization, Windows

I always used to have Windows installed through Boot Camp on my MacBook Pro. The reason for it being simple: I need it for some games ;-)
On the other hand, I use OS X for my primary work, but again, sometimes there’s no way around Windows. So I had the same Windows installed again within VirtualBox.

Well, what a waste of valuable disk space. So, after I got my new MacBook Pro, equipped with an SSD, I decided to go for it and share the Boot Camp partition between Windows 7 running natively and VirtualBox.

Actually, there exist a lot of posts on this topic already. So here’s a few I came across:

The best and most complete one is IMHO luckypiplav’s howto, as it already covers most of the issues and caveats.

So my today’s post will be sort of an aggregate of all these posts before. I’m not trying to duplicate, my focus is to show what I had to do to get my setup working for me.

To begin with, here’s a short summary of my specs:

  • Apple MacBook Pro 15″, February 2011 model, equipped with an 120 GiB SSD instead of a 500 GiB hard drive
  • OS X 10.6.6 on HFS partition (80 GiB)
  • Windows 7 SP 1 on Boot Camp partition (40 GiB)

Install Windows 7 through Boot Camp

I’m not going to loose big words on this. I used the Boot Camp assistant to prepare the hard drive for installation.
My hard drive was split into a 80 GiB partition for OS X and a 40 GiB partition for Windows 7.
The process is straight forward. For those not so familiar with it, here’s a howto.

After completing Windows 7 setup I first installed SP1 and all additional updates. I also activated the Windows 7 license. Then I booted right back into OS X.

Setup RAW partition access

Oh well, here’s some tech stuff to go through. This is also one of the things I dislike about all the other posts on this topic (sorry guys, I’m just honest): Everyone tells you to do some shell magic trickery, like write access to the partition device files. However, if your setup is only slightly different, you’re most likely doomed ;-)

So, let’s start on looking up the partition information first. For this you need to launch Disk Utility first.
My screen shots are from a german language OS X, but for the sake of demonstration purpose, this will be sufficient.

On the main window, click your Boot Camp partition first and the “Information” icon second.

This will reveal the partition information. The most important thing for us to work with, is the partition ID, which in my case reads “disk0s3″ (first disk, 3rd slice). In most cases this will be identical for you, but chances are, that it’s not. If it reads something different for you, then use this instead.

Now open up Terminal. Your starting directory in Terminal is usually your home directory, which evaluates to /Users/USERNAME. In my case it’s /Users/Gianpaolo. You can always verify your current location using the “pwd” (Print Working Directory) command.
I store all my VM related stuff in my home directory in a folder called “VirtualBox VMs”, so I change to this directory and create a new folder for the new Windows 7 VM.


cd "VirtualBox VMs"
mkdir Win7onMBP
cd Win7onMBP

Now, we need to grant permissions to the partition we inspected before. Rembember, mine was “disk0s3″? Don’t forget to use the one, that correctly applies to your system.
Note: You’ll be asked to enter your password to run this command:


sudo chmod 777 /dev/disk0s3

You will also need to eject the Boot Camp volume, in case it’s mounted.
Note: If you decided to format your Boot Camp partition with NTFS, then it won’t be accessible from OS X. In this case, it won’t be mounted as well. This is the case for me, so I effectively left this step out.


diskutil umount /Volumes/BOOTCAMP

To have this settings parmenently applied to the system, you will need to add them to the local boot script. For this purpose, we use the “nano” command, a simple text editor which is simpler to use then “vi”. Just type and quit using the “CTRL-X” key stroke.
Chances are that this file does not yet exist, but never mind, it’ll be created automatically.


sudo nano /etc/rc.local

Add the following text to your rc.local file:


# grant VirtualBox permissions to Boot Camp partition
#
chmod 777 /dev/disk0s3
diskutil umount /Volumes/BOOTCAMP

Again, if the Boot Camp volume is NTFS formatted, it won’t be mounted anyway, so the “diskutil” line can be omitted in this case.
Type “CTRL-X” to save and exit.

Now it’s time to create the RAW disk file for use with VirtualBox. For this, you should be inside the VM directory we created previously. If in doubt, check this using the “pwd” command. From here, run the following command:


sudo VBoxManage internalcommands createrawvmdk -rawdisk /dev/disk0 -filename win7raw.vmdk -partitions 3

Please note that this time, you won’t need to give the whole “disk0s3″ string to the command above. Instead leave the last two characters away, just stating the physical disk name, which is “disk0″. You will need however to supply the partition number “3″ taken from the “disk0s3″ string to the “-partitions” argument as seen above.

Create the VM

Simply follow the instructions of luckyviplav’s howto.

Important to note however: As of VirtualBox 4.x, the disk drives are always attached to a SATA controlled, as shown on the screenshot below.
It is effectively required to remove the disk from the SATA controller and attach it to the IDE controller as primary master instead. Also do not forget to set the IDE controller type to “ICH6″, otherwise you’ll get a “STOP: 0x0000007B” BSOD (Inaccessible Boot Device)”.

I strongly recommend to keep the SATA controller in the VM profile. This will have the SATA drivers automatically installed during your first boot.

First Boot and driver installation

So you’re now ready to boot your VM off the Boot Camp partition.
This will cetainly take a while as all the drivers need to be installed. Please take the chance to also install the VirtualBox guest additions.

Note: If you want 3D acceleration, you’ll need to install the VirtualBox drivers from safe mode (press F8 during boot to get into safe mode).

Optional: Re-attach boot drive to SATA controller

One of the most dramatic performance improvements for VirtualBox is the use of the virtual SATA controller in favor of the IDE controller.
So after you successfully installed all the drivers including the VirtualBox guest additions to your VM shut it down first. Then open your VM settings in VirtualBox and navigate to the storage controller section. Remove the hard drive from the IDE controller and re-attach it to the SATA controller. I asked you to keep it there in the first place so the drivers could be installed, remember?

During the next boot, Windows will automatically pick up the disk from the SATA controller and use this for booting.

As a result overall performance in VirtualBox will be a little snappier as with the IDE controller, which also used a bit more CPU power than the SATA controller.

So here’s finally our Windows 7 VM bootet off the Boot Camp partition. And yet, it’s still possible to run the very same Windows 7 natively by booting directly into Boot Camp.

30 Responses to “Sharing Windows 7 between Boot Camp and VirtualBox”

  1. Rick Says:

    I seemed to be aware about this previously, however there were some helpful bits which concluded the image for me personally, thanks for your time!

  2. AlexL Says:

    Brilliant! Thanks for all the tips. It works for me!
    How about if I now wish to cancel the script to unmount BootCamp every time. I want to do so only when I call upon VirtualBox. Can you advise me how to reverse that nano command? Many thanks!

  3. phaq » Blog Archive » Refining application shortcut for shared BootCamp / VirtualBox Windows VM Says:

    [...] a follow-up to my previous posts on sharing Windows 7 in BootCamp and VirtualBox and launching a VirtualBox VM directly through an icon [...]

  4. admin Says:

    Hello Alex,

    I don’t know about on-startup hooks in VirtualBox itself to get this behaviour, but you can still achieve this if you create an application shortcut to launch the VM directly.
    I tried to summarize the necessary steps at http://phaq.phunsites.net/?p=634.

    Cheers,
    Gianpaolo

  5. Bob Wall Says:

    I very much appreciated your post “Sharing Window 7 between Boot Camp and VirtualBox.” It is by far the clearest explanation of how to go about this I have found on the Web. Unfortunately, I got hung up at the “sudo chmod /// /dev/disk0s3” step.

    I have a mid-year 2011 iMac (SSD primary drive and 1T spinning secondary drive) running Lion. Windows 7 is installed on the Bootcamp partition which is on the SSD. Bootcamp is the third partition on the SSD (Mac OS Recovery, Mac OS and Win7). I performed the Disk Utility step you suggested to revel the Disk Identifier of my Bootcamp partition and discovered it was “disk0s4”. So naturally I substituted 4 for 3 in your sudo … example. I got the following error message
    “Invalid file mode: ///”

    Could you please suggest what I should do to correct this problem?

    Also, I discovered using Disk Utility that even though my Bootcamp partition is NTFS formatted it was mounted. I also got the above error message if I unmounted the disk before I typed the sudo command.

    Though I can’t image it makes any difference, I had renamed the BOOTCAMP drive “Win SSD”

    I am new to the Mac world, so any suggestion you can offer would be appreciated.

  6. admin Says:

    Hi Bob,

    Sorry for the long delay, there was some issues with the comments function on my blog recently.

    After reading through your comment, I understand there is a typo in what you have typed in.

    Actually, it is NOT sudo chmod /// /dev/disk0s3 with three slashes in it, that’s three 7 (as in number Seven).

    This is why you get the Invalid file mode: /// message.

    Cheers,
    Gianpaolo

  7. Running Windows 7 from the Bootcamp partition in VirtualBox | Wouter Says:

    [...] over at phaq has written a great and easy-to-follow tutorial to get VirtualBox up and running your Bootcamp partition. I’m not going to plagiarize and [...]

  8. efstajas Says:

    I followed all these steps, but this is what I get:
    VBoxManage: error: Cannot open the raw disk ‘/dev/disk0′: VERR_ACCESS_DENIED
    VBoxManage: error: The raw disk vmdk file was not created
    I typed all the permission commands though and also unmounted the disk! Is it right that there isn’t a message like “Done”?

  9. Lorenzo Says:

    Ma la macchina virtuale utilizza il vero disco rigido o uno virtualizzato? se io scrivo un file da windows nativo e poi accendo la macchina virtuale, me lo ritrovo li o devo creare una cartella condivisa?

  10. Joe Says:

    I couldn’t get the raw disk file to create, I got an error about access denied to disk0. my bootcamp is on partition 4.

    I entered:
    VBoxManage internalcommands createrawvmdk -rawdisk /dev/disk0 -filename win7raw.vmdk -partitions 4

    result:
    VBoxManage: error: Cannot open the raw disk ‘/dev/disk0′: VERR_ACCESS_DENIED
    VBoxManage: error: The raw disk vmdk file was not created

    i followed all the steps before including unmounting bootcamp, not sure why i can’t create the raw disk.

  11. gdelmatto Says:

    You probably missed the “chmod” command.

    Please check back with the partition ID you looked up from Disk Manager.
    Depending on your setup, it may be something like ‘disk0s3′ or ‘disk0s4′ or similar.

    As stated in the single steps, you need to apply write permissions to the partition ID, like this:
    “sudo chmod 777 /dev/disk0s3″ or “sudo chmod 777 /dev/disk0s4″ or whatever may be right for your system.

    If you don’t run the “chmod” command, you WILL get the VERR_ACCESS_DENIED. Also, keep in mind that you
    need to add the startup script as described, which sets these permissions during every startup, or you will
    get VERR_ACCESS_DENIED again. This change does NOT survice a reboot!

    The check the permissions of your bootcamp partition, run this command from the Terminal: “ls -l /dev/disk0s3″ (or use /dev/disk0s4 or whatever applies to you).

    You may get an output like this:

    brwxrwxrwx 1 root operator 14, 3 22 Jan 17:29 /dev/disk0s3

    Note the “brwxrwxrwx” at the begging? This is what it must look like. It simply wont work otherwise.

    And yes, some come commands simply don’t give you a “Done” or “OK” message.

    You could always try to run a commands like this: “sudo chmod 777 /dev/disk0s4 ; echo $?” by adding ” ; echo $?” (that’s “semicolon echo dollar question mark”)

    This would give you a number back, where ’0′ (zero) usually means “everything ok” while any other number denotes an error running the command.
    A few commands also have a “-v” switch, which means “be verbose”, but that’s not a universal rule. But in general, if you run a command that does not give you
    any output, then everything it’s fine. Most of them bail out with a terrible error message if something was really that wrong.

  12. gdelmatto Says:

    You most likely missed to run the “chmod” command line: chmod 777 /dev/disk0s4

    Have a look at my reply to “efstajas” for a long explanation as well.

  13. gdelmatto Says:

    Ciao Lorenzo,

    Ma si, la macchina virtuale usa la partizione fisica del disco rigido. Non si sta creando un disco rigido virtuale, solo un riferimento alla partizione raw.
    Allora quarda le dimensioni dei file sul mio computer:

    du -h *.vmdk
    4,0K bootcamp-pt.vmdk
    4,0K bootcamp.vmdk

    Si riferiscono alla partizione raw.
    Adesso Windows nel Boot Camp e Windows nel VirtualBox può accedere alla stessa partizione fisica. C’è una sola installazione di Windows :-)

    Ciao,
    Gianpaolo

  14. Val Says:

    Gianpaolo,

    Just a quick and heartfelt THANKS for this excellent walkthrough!

    Everything went smoothly for me with one exception, as mentioned in the comments in Viplav’s howto: On restarting, I got a black character-mode display with the error that Windows couldn’t start because of a hardware or software change.

    The fix: Shut down the VM, and from the VirtualBox menu pick Settings> System and on the Motherboard tab, check Enable IO APIC.

    I expected to have much more trouble and your guide made it easy — grazie mille!

  15. s Says:

    I keep getting in virtual box:

    Failed to open the hard disk /Users/xxxx/win7raw.vmdk.

    The medium ‘/Users/xxxx/win7raw.vmdk’ can’t be used as the requested device type.

    Result Code: NS_ERROR_FAILURE (0×80004005)
    Component: Medium
    Interface: IMedium {53f9cc0c-e0fd-40a5-a404-a7a5272082cd}
    Callee: IVirtualBox {c28be65f-1a8f-43b4-81f1-eb60cb516e66}

  16. gdelmatto Says:

    Make sure that you granted write permissions on your device file (the /dev/disk0s3 or whatever applies to your system):

    sudo chmod 777 /dev/disk0s3

    Check also the permissions of the VMDK files created:

    ls -l /path/to/your/*.vdmk
    -rw——- 1 USERNAME staff 512 23 Jul 2011 bootcamp-pt.vmdk
    -rw——- 1 USERNAME staff 747 22 Jan 16:44 bootcamp.vmdk

    They should belong to your user account. So if you don’t see your username in the listing,
    change it like this:

    chown YOURUSERNAME /path/to/your/*.vmdk

    If multiple users on your system are going to use the virtual machine, then do this as well: chmod 777 /path/to/your/*.vmdk

  17. s Says:

    I redid everything and now it says
    Failed to access USB subsystem
    could not load the usb proxy service
    verr_general_failure

    Result Code:
    NS_ERROR_FAILURE (0×00004005)
    Component:
    Host
    Interface:
    IHost {dab4a2b8-c735-4f08-94fc-9bec84182e2f}
    Callee:
    IMachine {5eaa9319-62fc-4b0a-843c-0cb1940f8a91}

  18. s Says:

    I did sudo chmod 777 /dev/disk0s4 because i’m 4
    then i eject boot camp
    i type in only the first 3 lines in the nano screen
    and hit control x
    i then use the line sudo VBoxManage internalcommands createrawvmdk -rawdisk /dev/disk0 -filename win7raw.vmdk -partitions 4

    i have done all of that successfully the i put

    sudo chown Admin-Name /Users/Name/*.vmdk

    and thats where i get lost I’m not sure how to determine the path.

  19. rghazarian Says:

    I followed all the instructions on preparing the rawdisk, even checked using “ls -l /dev/disk0s3″. The response I got is:

    brwxrwxrwx 1 root operator 14, 3 25 Jan 11:35 /dev/disk0s3

    Yet when I try running the VBoxManage command I get the errors:

    VBoxManage: error: Cannot open the raw disk ‘/dev/disk0′: VERR_ACCESS_DENIED
    VBoxManage: error: The raw disk vmdk file was not created

    I’m using VirtualBox version 4.1.8 r75467.

    What am I missing?

    Thanks for your time and effort.

    Rafi

  20. rghazarian Says:

    An update. I just tried the command again preceding it with SUDO and that work for me. So the command I used was:

    sudo VBoxManage internalcommands createrawvmdk -rawdisk /dev/disk0 -filename win7raw.vmdk -partitions 3

  21. test123 Says:

    The reason these people are getting permissions errors is because you suggested they run VBoxManage as their user account instead of as sudo.

    VBoxManage requires write access to the raw block device /dev/disk0 for the raw disk creation process. In the future it’s not needed but the VBoxManage command needs to be run with sudo to work. If you look at the tutorials you cited they did correctly use sudo in this step.

    Also rc.local is run as root during boot so there is no reason for the sudo in front of the chmod command.

  22. gdelmatto Says:

    Oh yes, I see … I checked with earlier revisions and saw, that the missing “sudo” slipped away when I moved the text blocks around. :-/
    Thanks for pointing that one out :-)

    And yes, the “sudo” in the rc.local is of little use, because bootstrap is run from root.
    But … if you ever want to run rc.local from an unprivileged user account, you can just do so:

    bash /etc/rc.local

    the ‘sudo’ in place will make sure, you get asked for the root password, which you otherwise wouldn’t.
    This is mainly in there because I’m doing some other stuff from rc.local, which I sometimes need to rerun during testing.
    Since I sometimes forget to ‘sudo bash /etc/rc.local’, it’s mainly there for my convenience ;-)

    Nevertheless, I removed it, because it’s not best practice.

  23. gdelmatto Says:

    Can’t help you too much about that particular error, but on the fast track I found a possible resolution for you in this post:

    https://forums.virtualbox.org/viewtopic.php?f=7&t=41005

    Maybe that’s gonna help you with the USB issue.

  24. Raja Says:

    I followed the instructions, but when I select the win7raw.vmdk file in VirtualBox (New VM, Use Existing Disk) the file is selected and the disk shows up as 689GB. My bootcamp partition size is only 90 GB. What am I doing wrong?

    Your help will be very much appreciated

  25. admin Says:

    Your partition should show up with its real size in VirtualBox.
    From the size you state, I suppose you have accidentally selected the OS X Lion recovery partition.
    On a default system, disk0s3 tends to be the recovery partition.
    Thus your Bootcamp will likely be disk0s4, or even different.

    Please check with disk manager that you selected the correct partition.

    Make also sure, that you use the correct partition with the VBoxManage command as well.

    disk0s3 must be “partitions 3″
    disk0s4 must be “partitions 4″
    etc.

  26. Sherri Says:

    I followed everything and I have the right partition according to disk utility but the size of my virtual drive shows that it is taking the entire disk minus the size of my OS X partition. I have a new Macbook Air with an SSD. I’ve tried several times and keep getting the same result. Any suggestions?

  27. Raja Says:

    Thanks for your reply. My BOOTCAMP partition is labelled disk0s4. I unmounted the drive. In the VBoxManage command I used partitions 4. I changed the owner to admin user. But when I create a new VM and select te vmdk file it shows as 698 GB!

    Not sure what is going wrong here. Your help is very much appreciated.

    Regards
    Raja

  28. gdelmatto Says:

    I checked back at my VirtualBox. Indeed, the VMDK file shows up with the full size of the HARDDRIVE and not the partition.
    After some reading I found that the VMDK raw partition access limits access to a certain partition (or multiple partitions if instructed so), BUT it will always refer to the whole disk in terms of geometry and some other meta data.
    This is why the size of the hard drive is shown, and not the partition size.

    In my case (I have a 120 Gig SSD installed) it reads “bootcamp.vmdk (normal, 111.79 GB)”

    So if your harddrive is some 750 Gig in size, which would essentially translate to some 697 Gigs in binary byte calculation, then it should be all fine.

    So please ignore my previos statement, I was in error there.

  29. gdelmatto Says:

    To sum my latest findings please note, that the size of the hard drive seen in VirtualBox _must_ be the real size of the _complete_ hard drive, and not the size of the the single bootcamp partition.

    So even if your setup may look like a 120 GB SSD (decimal notation) of which let’s say 30 GB Bootcamp, 90 GB OS X are allocated, creating the ‘vmdk’ file will show up in VirtualBox with a size of approx 112 GB (binary notation), so to speak the complete hard disk.

    Despite of that, VirtualBox will only be able to access the single Bootcamp partition (the one given by the ‘–partions __n__’ statement).

  30. Danny O'Brian Says:

    I was having trouble booting a 64bit Bootcamp install with VIrtualbox. It was stuck in a reboot loop and kept saying a recent hardware change may have caused the problem.

    Then I changed to the number of processors in use by the VM at:

    Settings->System->Processor

    To match the number of cores on my iMac, Bingo! booted just fine.

Leave a Reply