How to Use a MacOS Bootcamp Partition as a VirtualBox Guest

Post Reply
JimBushWhack
Posts: 77
Joined: 1. Sep 2008, 23:07
Primary OS: Mac OS X other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Win XP on iMac; Win 7 on MacBook pro + bootcamp
Location: SF Bay Area; California, USA

How to Use a MacOS Bootcamp Partition as a VirtualBox Guest

Post by JimBushWhack »

Hi, In a recent post, I suggested that it would be good to have a writeup of the subject matter so that other folks could benefit from it. One of the site moderators suggested that since I had just gone through this process, that I would be the perfect one to add such a writeup. So, I am "putting my money where my mouth is" and doing just that. In the post following post following this one, I detail how I was able to add a Bootcamp Windows 7 (and XP) partition as a VirtualBox Virtual Machine.

Site Moderators: Please review this document and and consider it for inclusion in the HowTos and Tutorials section.

Thanks,
JimBushWhack
JimBushWhack
Posts: 77
Joined: 1. Sep 2008, 23:07
Primary OS: Mac OS X other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Win XP on iMac; Win 7 on MacBook pro + bootcamp
Location: SF Bay Area; California, USA

Re: How to Use a MacOS Bootcamp Partition as a VirtualBox Gu

Post by JimBushWhack »

How to Use a MacOS Bootcamp Partition as a VirtualBox Guest Virtual Machine

Background:

When I first got an Intel Mac in the summer of 2008, I decided that I could finally get rid of my Gateway PC running Windows ME. Since the Intel Mac allowed me to run Windows via Bootcamp, I was able to use this to run the Windows version of Quicken, since the Mac version of Quicken was not very good. This worked well for a few months until I got tired of having to reboot the Mac to run Windows via Bootcamp. I started looking at VMWare Fusion and Parallels as a solution. Then I heard about VirtualBox. Since it was OpenSource (and free), I decided to give it a try. But I was disappointed when I tried to setup my existing Bootcamp Windows partition as a VirtualBox Virtual Machine. It did not work and the instructions in the manual were fairly complicated and involved delving into the Mac CLI, as the VBox GUI did not (and still does not) allow you to perform this setup. There are many forum threads on this subject, many of which I have contributed to. Finally in the Spring of 2011, I was finally able to setup a Windows 7 Guest virtual machine from a Bootcamp partition on my MacBook Pro. I never could get a Windows XP Bootcamp based Guest running on my iMac and finally gave up on it.

The Problem:

About a month ago, I updated both of my Macs to run the new MacOS release Lion. When I got around to trying to bring up Windows 7 via VirtualBox on my MacBook Pro, it failed miserably. The bootcamp partition had originally been setup as partition 3 on my hard disk under Snow Leopard. Upon investigation, I found that when Lion was installed, the hard disk partitioning had been shuffled and partition 3 was now the MacOS Recovery partition and the bootcamp partition had been moved to partition 4. Below is a view of the hard disk partitions, using the vboxmanage CLI command:
-----------------------
JBs-MacBook:BC7 Jim$ sudo vboxmanage internalcommands listpartitions -rawdisk /dev/disk0
Number Type StartCHS EndCHS Size (MiB) Start (Sect)
1 0xee 1023/254/63 1023/254/63 200 1
2 0xaf 1023/254/63 1023/254/63 274167 409640
3 0xab 1023/254/63 1023/254/63 619 561903832
4 0x07 1023/254/63 1023/254/63 30258 563173376
------------------------
The Solution:

After downloading and installing the 4.1.6 Mac OS version of VirtualBox and the Extension Pack (I had had problems with the 4.1.8 version before and the 4.1.10 version was not out yet), I tried using the vboxmanage createrawvmdk command to regenerate the vmdk disk description and MBR files, but found that I only had generated the disk description file and not the MBR. After adding a post to the VirtualBox on Mac OS X Hosts forum, one of the site moderators (mpack) showed me the error of my ways and that I had not invoked the createrawvmdk command with the -partitions argument which is required to generate the MBR file. After re-reading “Access to individual physical hard disk partitions” (paragraph 9.8.1.2) in the VirtualBox UserManual, I was able to setup the correct command line like so:

------------------------
JBs-MacBook:BC7 Jim$ sudo vboxmanage internalcommands createrawvmdk -rawdisk /dev/disk0 -filename BC7.vmdk -partitions 4
RAW host disk access VMDK file BC7.vmdk created successfully.
------------------------
I then set open access to these two files and dumped out the contents of the BC7-pt.vmdk (i.e. MBR) file:
------------------------
JBs-MacBook:BC7 Jim$ sudo chmod 777 BC7*
JBs-MacBook:BC7 Jim$ ls -l
total 16
-rwxrwxrwx 1 root Jim 512 Mar 13 13:40 BC7-pt.vmdk
-rwxrwxrwx 1 root Jim 638 Mar 13 13:40 BC7.vmdk

JBs-MacBook:BC7 Jim$ hexdump BC7-pt.vmdk
0000000 33 c0 8e d0 bc 00 7c fb 50 07 50 1f fc be 1b 7c
<<>>
00001c0 ff ff ee fe ff ff 01 00 00 00 27 40 06 00 00 fe Partition 1 Type 0xEE = Legacy MBR
00001d0 ff ff af fe ff ff 28 40 06 00 d8 a0 2c 19 00 fe Partition 2 Type 0xAF = Mac OS X HFS
00001e0 ff ff ab fe ff ff 00 e1 32 19 28 5f 13 00 80 fe Partition 3 Type 0xAB = Mac OS X Boot
00001f0 ff ff 07 fe ff ff 28 40 46 19 20 19 d6 03 55 aa Partition 4 Type 0x07 = Windows NT NTFS
JBs-MacBook:BC7 Jim$ sudo chmod 777 /dev/disk0s4
JBs-MacBook:BC7 Jim$
------------------------
Note the MBR partition table starting at hexadecimal address 00001c0. The 3rd hex character of each 16 byte partition table entry is the partition type. I have expanded the partition type to the right of the hexdump output.

At this point, I went back to the GUI and setup my virtual machine as follows:

1. Selected New and setup the Virtual Machine Name as BC7.
2. Operating System as Windows and version as Windows 7, since it is a 32bit version of Windows 7. For a 64bit version, select Windows 7 (64 bit).
3. Selected 1024 MB of memory. My Mac has 4 GB so, this works about right.
4. At the Virtual Hard Disk window, I check Start-up disk and select Use Existing Hard Drive. I clicked the arrow token on the right side and put the file path of the BC7.vmdk file generated above.
5. Review the summary window and hit Create. A new virtual machine named BC7 will show up on the left side of the VirtualBox window and be in the state of Powered Off.
6. Select this new virtual machine and then go to Settings. Review each setting category and use the defaults unless otherwise noted.
7. Under Storage, you will see that the Storage Tree shows that you have 2 controllers. An IDE controller with a CD/DVD drive showing empty and a SATA controller with the BC7 virtual disk under it. We need to get rid of the SATA controller and setup the IDE controller so that the BC7 virtual disk and the CD/DVD drive are both controlled by it:
a) Select the Empty CD/DVD drive and click on the delete attachment button below it (the one with an orange minus sign, second from the left).
b) Select the SATA Controller and click on remove controller button below it (the one with the orange minus sign, on the far right). This will also remove the BC7 virtual disk, but we will add it back in the next step.
c) Select the IDE Controller and click the Add Hard drive button to the far right of the IDE Controller line. This will bring up a dialog box where you may now select the path of the BC7 virtual disk once again, after clicking on choose existing disk.
d) Select the IDE Controller again and then click the Add CD/DVD button to the left of the Add Hard drive button. Click the Leave Empty button on the next dialog box.
e) Under Attributes, change the type to ICH6.
8. Complete the Virtual Machine setup with network and USB ports as needed. Also setup a Shared Folder so you can pass files back and forth between Windows and Mac OS. When Windows is up, you can assign a Drive letter to this shared folder for easy access. Then hit OK.

Before trying to start a Bootcamp Virtual Machine, open access to the Bootcamp partitions Device Special File must be set and if the Bootcamp partition is mounted on the live invocation of Mac OS, it must be unmounted. Since these are repetitive tasks, they are best done with a script. Below is a screen shot of a compiled applescript file that will perform these tasks as well as start the Virtual Machine:
Screen Shot 2012-03-24 at 1.38.51 PM.jpg
Screen Shot 2012-03-24 at 1.38.51 PM.jpg (52.46 KiB) Viewed 98063 times
Revisit Bootcamp Windows XP Virtual Machine:

Since I got my Bootcamp Windows 7 Virtual Machine running with VirtualBox, I decided to give Windows XP another shot. So, I installed VirtualBox 4.1.6 on my iMac. I then displayed the partition table and generated the vmdk description and MBR files. After this, I set open access for these files and then I used hexdump to display the contents of the partition table in the generated MBR file:
------------------------
JBs-iMac:BCXP Jim$ sudo vboxmanage internalcommands listpartitions -rawdisk /dev/disk0
Number Type StartCHS EndCHS Size (MiB) Start (Sect)
1 0xee 1023/254/63 1023/254/63 200 1
2 0xaf 1023/254/63 1023/254/63 206228 409640
3 0xab 1023/254/63 1023/254/63 619 422764800
4 0x0c 1023/254/63 1023/254/63 31427 424034344
JBs-iMac:BCXP Jim$ sudo vboxmanage internalcommands createrawvmdk -rawdisk /dev/disk0 -filename BCXP.vmdk -partitions 4
RAW host disk access VMDK file BCXP.vmdk created successfully.
JBs-iMac:BCXP Jim$ sudo chmod 777 BCXP*
JBs-iMac:BCXP Jim$ ls -l
total 16
-rwxrwxrwx 1 root Jim 512 Mar 13 14:02 BCXP-pt.vmdk
-rwxrwxrwx 1 root Jim 638 Mar 13 14:02 BCXP.vmdk

JBs-iMac:BCXP Jim$ hexdump BCXP-pt.vmdk
0000000 33 c0 8e d0 bc 00 7c fb 50 07 50 1f fc be 1b 7c
<<>>
00001c0 ff ff ee fe ff ff 01 00 00 00 27 40 06 00 00 fe Partition 1 Type 0xEE = Legacy MBR
00001d0 ff ff af fe ff ff 28 40 06 00 d8 a0 2c 19 00 fe Partition 2 Type 0xAF = Mac OS X HFS
00001e0 ff ff ab fe ff ff 00 e1 32 19 28 5f 13 00 80 fe Partition 3 Type 0xAB = Mac OS X Boot
00001f0 ff ff 0c fe ff ff 28 40 46 19 20 19 d6 03 55 aa Partition 4 Type 0x0C = Win95 FAT32
JBs-iMac:BCXP Jim$
------------------------

I went through the exercise described above of using the GUI to setup the Virtual Machine. But when I tried to start it, it would not start. I puzzled over this for a bit and then went back and re-read some of the posts associated with setting up Bootcamp Virtual Machines. I found references to setting the partition type field in each of the MBR partition table entries, except the one describing the Bootcamp partition, to an Unused type (e.g. 0x2d). I did this using the dd CLI command and then displayed the MBR partition table contents with the hexdump command once again below:

------------------------
JBs-iMac:BCXP Jim$ echo -en "\x2d" | dd of=BCXP-pt.vmdk bs=1 count=1 seek=0x1c2 conv=notrunc
1+0 records in
1+0 records out
1 bytes transferred in 0.000020 secs (49932 bytes/sec)
JBs-iMac:BCXP Jim$ echo -en "\x2d" | dd of=BCXP-pt.vmdk bs=1 count=1 seek=0x1d2 conv=notrunc
1+0 records in
1+0 records out
1 bytes transferred in 0.000021 secs (47663 bytes/sec)
JBs-iMac:BCXP Jim$ echo -en "\x2d" | dd of=BCXP-pt.vmdk bs=1 count=1 seek=0x1e2 conv=notrunc
1+0 records in
1+0 records out
1 bytes transferred in 0.000020 secs (49932 bytes/sec)
JBs-iMac:BCXP Jim$ hexdump BCXP-pt.vmdk
0000000 33 c0 8e d0 bc 00 7c fb 50 07 50 1f fc be 1b 7c
<<>>
00001c0 ff ff 2d fe ff ff 01 00 00 00 27 40 06 00 00 fe Partition 1 Type 2D = Unused
00001d0 ff ff 2d fe ff ff 28 40 06 00 d8 a0 2c 19 00 fe Partition 2 Type 2D = Unused
00001e0 ff ff 2d fe ff ff 00 e1 32 19 28 5f 13 00 80 fe Partition 3 Type 2D = Unused
00001f0 ff ff 0c fe ff ff 28 40 46 19 20 19 d6 03 55 aa Partition 4 Type 0C = Win95 FAT32
JBs-iMac:BCXP Jim$ sudo chmod 777 /dev/disk0s4
JBs-iMac:BCXP Jim$
------------------------

After patching the partition table and starting the Virtual Machine once again, everything worked fine.
mpack
Site Moderator
Posts: 39134
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Mostly XP

Re: How to Use a MacOS Bootcamp Partition as a VirtualBox Gu

Post by mpack »

Discussion of this tutorial should be continued here.
Post Reply