Using Virtualbox with an Existing Windows Partition

Posted by & filed under Apps FTW, HOWTO.

The recursion might blow your mind.

I am not able to help troubleshoot this anymore, as I have switched over to VMWare Player; However, I am still maintaining this document. If you happen to discover anything about making it work, please post in the comments below and I will update the post text with your findings and credit you.

Last week, a friend of mine needed me to do an audio file conversion, but the app that I use is installed on my windows partition. I really don’t ever boot into Windows unless I have a good reason for it — I’m much happier tooling around in Linux — there’s just something satisfying and comfortable about being able to pop open a shell at any time.

Anyways – it got me thinking: I’ve booted into a Windows XP image,  why can’t I use VirtualBox to boot from a whole partition? Surely that is possible…

Tonight I finally got to play with it. And as you can see from the image here, I got success. :)

It’s a little challenging, but it’s doable. I had to spend some time to iron out the kinks, but you can reap the benefits!

UPDATE: Sandeep has submitted screenshots with instructions on getting this to work with Windows 7, see below, at the very end.

UPDATE: If you are getting the error message: Offset must be a number: rce
I have found the fix for it. See the instructions below.

UPDATE: Bogdan (see comments) was able to get Windows Vista working under Virtualbox OSE, using the method below. See his comments for specifics on Windows Vista.

UPDATE: Dan has found some tricks for getting this to work with Win7 if you are getting a BSOD on bootup.

UPDATE: Julian has successfully gotten it to work with Windows 8 Pro, see below, near the end of the post content.

UPDATE: user PCWorld has submitted some suggestions to get this to work with Windows 7

Install Virtual Box

Ok, for starters, you need the CLOSED SOURCE version of VirtualBox. As in — do not install the one from the Ubuntu Software Center. Go directly to Sun Oracle’s website and download the appropriate version for your OS.

Ubuntu users should use the PPA instead of downloading the binaries. Be sure to check the link above for the most recent PPA. This sample applies to 12.04, Precise Pangolin, but there will quite possibly be a newer version on the website when you read this.

  1. Open up Ubuntu Software Center
  2. Click on the Edit top menu, and select “Software Sources
  3. Choose the “Other Software” tab
  4. Click on “Add…
  5. Paste: deb http://download.virtualbox.org/virtualbox/debian precise contrib
  6. Click on “Add Source”

Now you’ll want to update your repo cache and get the key:

1
2
$ wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add -
$ sudo apt-get update

And then install the package of your choice.

1
$ sudo apt-get install virtualbox-4.2

Whether you do binary-based install or package based install — install the most recent version of the personal edition (NOT “OSE”, which is the open-source version.) As of this post, the current version for Precise Pangolin (12.04) is VirtualBox 4.2.

The Virtualbox documentation also indicates that Ubuntu/Debian users should install the dkms package.

1
$ sudo apt-get install dkms

Install “mbr”

In order to successfully trick Windows into booting into a confined space, you need to fake your mbr (no grub). Fortunately this is WAY easier than it sounds:

1
2
$ sudo apt-get install mbr
$ install-mbr --f ~/.VirtualBox/FAKE.mbr

That’s it! We’ll use that later.

Your system will NOT be affected by this — all it does is copy the MBR from your computer, dump it into a file and that’s it.

NOTE: Originally, I had used the full name flag (–force) which uses two dashes. Due to the way my stylesheet renders italicized fonts, it looked like it was a single dash, understandably confusing some people. If you are getting the error message:

1
$ install-mbr: Offset must be a number: rce

Then either use just -f instead, or ensure that you are using two -’s before “force”.

UPDATE: Ken D has debugged an issue he had with install-mbr by using the -e1 option.

1
$ install-mbr -e1 --force ~/.VirtualBox/FAKE.mbr

His windows partition is /dev/sda1, so if yours is not, you need to use -eN where “N” is the number of the partition where windows is installed. Or not. (More info)

Create a VDMK file

This is actually the trickiest part. A VDMK, which I am not sure what that is an acronym for, is essentially a micro-image that contains instructions to tell MBR where it’s booting from. If your computer is modern, your hard drives are likely SATA drives (and thus represented as /dev/sda). If you’re unsure, just go into a shell and type “fdisk -l” (no quotes, and that last part is a “dash lowercase-L” not “dash one”). What you want is the device name for your harddrive… mine is /dev/sda  — depending on how many hard drives you have and what type they are, it might be /dev/hda/ or /dev/sdb/ etc.

It’s worth noting that if you are currently mounting your windows partition in Linux (I do) so that you can access your Windows filesystem while in Linux, you will need to unmount (eg. sudo umount /windows) it first.

You’ll want to determine which partition contains Windows, so we can restrict Windows to ONLY using its own partition — this is actually a Linux-exclusive ability (the Windows version of Vbox can’t do this, because Windows is a wuss). So to do this, first you need to know what partition(s) Windows is on. This command will tell you:

1
$ VBoxManage internalcommands listpartitions -rawdisk /dev/sda

For the last part, use whatever you determined from above. It’s PROBABLY /dev/sda for you too, that’s pretty common.

The command will produce output that looks something like this:

1
2
3
4
5
6
7
8
9
VirtualBox Command Line Management Interface Version 3.1.2
(C) 2005-2009 Sun Microsystems, Inc.
All rights reserved.

Number  Type   StartCHS       EndCHS      Size (MiB)  Start (Sect)
1       0x83  0   /1  /1   1023/254/63         53348           63
2       0x82  1023/254/63  1023/254/63          4000    109258065
3       0x83  1023/254/63  1023/254/63        155998    117451215
4       0x07  1023/0  /1   1023/254/63         91895    436935870

I have to admit, I feel a little naked showing the whole Internet my partition table…

The last line is the one you want to look for, although yours may not be your last line. Whichever one(s) have 0×07 as their “Type” are the one(s) you want. Jot down the number(s) for those. (In this case, my number is just “4″).

Now there’s one last thing we have to do that may make you a little uncomfortable, if you’re a paranoid person. We need to change the permissions on the hard drive device nodes. If you don’t know what that means, then you are probably not paranoid about that. :)

Fortunately, we only need to change the permissions slightly, and only on the Windows partitions. So it’s not THAT big of a deal.

In a terminal, type:

1
2
$ sudo chmod 666 /dev/sda
$ sudo chmod 660 /dev/sda4

That second line should reflect whichever hard drives you are using (remember that fdisk -l we did?) You’ll need to repeat the command for each hard drive that you want Windows to be able to access.

For those of you that are paranoid: You should be able to change the permissions back to 600 after we’ve created the VDMK file — VirtualBox needs access to your partition table so it can do its job.

There’s one last thing we need to do, and that’s adding you to the “disk” group, so you can access the partitions you just opened up. You’ll need to logout and log back in after doing this, so that your permissions are reset. (Previously forgot to include the username — thanks Ken!)

1
$ sudo usermod -a -G disk yourusername

Ok, now you’re ready to actually create the VDMK file. Get ready because this is a handful.

1
$ VBoxManage internalcommands createrawvmdk -filename ~/.VirtualBox/winxp.vmdk -rawdisk /dev/sda -partitions 4 -mbr ~/.VirtualBox/FAKE.mbr -relative

First off — the “sda” is whatever you determined you’re using, from earlier. It’s probably “sda”, like I mentioned earlier.

The number next to the partitions is the numbers you jotted down from the previous “listpartitions” command (the one where I said I felt naked). If you are using more than one partition, you will need to list them as comma-separated values. So if you wanted partitions 1 and 2 for Windows, you would replace the “4″ I wrote there with “1,2″. Partitions 2, 3 and 4 would be “2,3,4″ and so on.

The “partitions” parameter is what tells VirtualBox “only give Windows access to these partitions, and no where else!”

UPDATE: In the comments below, Heix writes:

Got a Win7 64bit Home Professional, with an ACER Recovery Partition as partition 1, the “boot loader”(?) partition as 2, and the actual win7 partition as 3, so i did a “-partitions 1,2,3″. Had to use the .iso of the win7 recovery disc afterwards.

Just to break down what’s going on here:

VBoxManage internalcommands createrawvmdk
this portion runs the program to “Create Raw VMDK”. Self-explanatory. DO NOT USE SUDO HERE (h/t to Javier, below!)

-filename ~/.VirtualBox/winxp.vmdk
this is the output file that actually holds the micro-image. You can call it whatever you like, and you can put it whereever you like. If you have multiple Virtual Machines, you may like to file them away in a separate folder. Whatever you like.

-rawdisk /dev/sda -partitions 4
This tells it that the raw Windows installation is located on device /dev/sda and that you only want to use partition 4 (yours may be different, as noted above)

-mbr ~/.VirtualBox/FAKE.mbr -relative
the -mbr parameter tells it to use that Fake MBR we created earlier. It won’t work without this. -relative is a parameter that works in conjunction with the -partitions parameter, to allow you to explicitly specify which partition you want to use.

-register 
This used to tell VirtualBox to register it in its database of available images, but has since been deprecated. Do not use this with version 4 of VBox. (Thanks, Ken D!)

That’s it. Chances are, if you are going to get errors anywhere, it’s likely to be here.

Possible causes, based on my experience and what I’ve read about on the Internet, is that you didn’t set permissions correctly, didn’t unmount your device, are using the Open-source edition (which doesn’t have createrawvdmk) instead of the closed-source personal edition. If you get an error, you can post it in a comment and I’ll see if I can help you, but google around — there are a LOT of resources online for this. Below, I’ll include links to the places I looked.

Load your new VDMK into Virtual Box

Load up Virtualbox, if you installed it through the automated installation (rather than compiling it manually), you should see it by opening dash (click on the Unity icon) and typing “Virtual”. If you don’t see it there, try logging out and logging back in.

In VirtualBox, click on “New”, and follow the instructions for the first couple of screens. Name it whatever you like and select your OS (these instructions have all applied to Windows XP so far — caveat operor on any other versions / OSs). Once you get to the “Virtual Hard Disk” screen (pictured here) select “Use existing hard disk” and you should see the VDMK file we just created. If you don’t, click the little folder icon next to the drop down list. It will allow you to “add” your vdmk file that you created.

Once that’s done, you’ll be back at the main screen.

Click once on your new Virtual Machine, and click the “Settings” icon at the top. You need to do a few more things first.

Configuring your Virtual Machine

In the “System” area, be sure to check “Enable IO APIC” in the “Motherboard” tab. In “Processor”, specify how many CPUs you want to use (ie. I had a Core 2 Duo, so I could choose more than one). You really only need 1 — if you’re doing stuff that requires more, you should probably boot into Windows natively.

In the Acceleration tab — some CPUs have the native instructions built for virtualization — modern Intel CPUs do. If you paid a little extra for your CPU and you bought it in the past year or two, you might have these. I forget the specific models that have it, but it’s something that’s easy to overlook. :)

Under “Display” area, adjust the slider to specify how much Video RAM you want to give your VM. I picked “64MB” for mine… very modest. You can also check “Enable 2d Acceleration” and “Enable 3d acceleration” simply because it won’t hurt to do so… but again, if you’re doing things that require hardware acceleration, a VM probably isn’t the best way to do it. :) (sidebar: with computing power as strong as it is nowadays, you can realistically run 3D accelerated games in a VM, provided you can devote enough resources to it)

That’s it — the rest of the stuff is just icing. You can tinker with it if you want, but you’re good to go.

Click on “OK” to get out of there, then click “Start” (green arrow at the top). The first time you boot up, it may take a little while longer than usual.

This is the other point that is prone to errors. The first time I did this, I had errors because I didn’t do the -partitions parameter in my VDMK creation, which gave me “disk read” errors. You might get a blue screen of death or some other nonsense. If you get any errors — google it. Like I said earlier, there’s lots of support for it.

UPDATE (Additional Notes for Windows 7 Users)

To anyone struggling to get this running with Windows 7, I might be able to save you some time:

If Windows bombs straight to a blue screen of death on boot, in your VM go to settings->storage. If your .vmdk file is listed under SATA, then remove the attachment, then add a new hard disk under IDE, selecting your vmdk file. Next – and this is the crucial bit, in the storage settings again, select the IDE controller, then change its type to ICH6.

Save your settings, and your Windows 7 partition should boot – given you’ve followed all the previous instructions (particularly the windows 7 disc repair option part, which will get you past the ‘MBR 1FA’ problem.)

Thanks to Dan (@lazydan) for the feedback!

User-submitted Walkthrough for Windows 7

http://wiki.ubuntuusers.de/Dualboot-Windows_virtualisieren describes a solution for Windows 7 so that you won’t need to do a “Repair Installation” each time you want to switch from native to the VM and vice versa (you won’t need any Windows 7 Installation Disk at all)

First, run

1
sudo fdisk -l

on your host system, you’ll need the value behind “Disk identifier:” of the hard drive your Windows 7 is on.
After you’ve created the VM (leave out the step telling you to boot from the Windows 7 Installation Disk), attach an Ubuntu (or any other Linux) live CD image to the VM:
Settings -> Storage -> Controller: IDE -> Add CD Device (first icon) -> Choose Disk -> select the image of your live CD
Then boot the VM, it should boot the live CD image. Once you’re in there, run

1
sudo fdisk /dev/sda

to start fdisk’s interactive mode (replace /dev/sda with the hard drive your Windows 7 is on).
Enter

1
x

, then enter

1
i

Put in the disk identifier you’ve gathered in the first step, then press

1
w

to rewrite the MBR and quit fdisk.

After this, shut down the VM and remove the live CD image from the VM’s storage devices.
And note that the device name (e.g. /dev/sda) you have to use on the guest system might differ from its name on the host system.

Now you should be able to boot Windows 7 both natively and through the VM without Windows complaining.

To everyone else that has problems, I suggest you to read the wiki article linked at the top of my comment, it’s very comprehensive and has many troubleshooting tips at the end.

UPDATE: For Windows 8 Professional users

Julian says:

I’ve managed to get Windows 8 Pro (64bit) to boot in my 64bit version of Ubuntu 12.04.

I had a few issues getting it booting to start with, but using the windows 8 installation disk to get into command prompt and run

1
2
3
> bootrec.exe /fixboot
> bootrec.exe /fixmbr
> bootrec.exe /rebuildBCD

got this working. I couldn’t access my other SATA HDD that carries all my media, (And adding this as a separate vmdk in my virtual machine’s options made it borky and not detect the original Windows installation), but I’ll try and work it out.

Once You’re In Windows

Some of the sites I’ve read have suggested creating an alternate hardware profile for when you boot into Vbox. This is a good idea if you plan on booting into it normally once in a while (for games or whatnot). To create a new Hardware Profile, right click on “My Computer” and click “Properties”. Select “Hardware” then at the bottom, select “Hardware Profiles”. Create a new one and name it “Raw Boot”, and rename the current one to “VBox Boot”.

Windows will go through some growing pains this first boot sequence, as it maps all the hardware drivers to the VirtualBox extensions provided — You will likely need to restart. No guarantees on all the hardware working as it does in a native boot. You should be able to use things like Word, browse the web, use Photoshop (if you allocated enough RAM…you can always change that later), etc. Anything relatively lightweight should be fine.

Working with Windows 7 (Update!)

One of the commenters below, Sandeep (“Sandy”), has figured out how to get a Windows 7 partition to work with this method. There are a couple small steps that need to be done, and he was cool enough to take screen shots for this blog:

  1. Before you begin, be sure your VDMK is configured, per the instructions above [screenshot]. You will also need to have the Windows 7 Rescue ISO loaded, which can be done within the VirtualBox configuration  (before starting a VM instance)
  2. Once that’s ready, boot up the Windows  7 VM [screenshot] Sandeep is using a newer version of VirtualBox, apparently, but AFAIK, it should still work even if yours says “Sun” instead of “Oracle”
  3. When prompted, boot to the rescue CD by pressing the appropriate key. [screenshot]
  4. At the menu, select “Repair Installation” [screenshot]
  5. Select “Repair & Restart” [screenshot]

I have not personally done this, but based on the comments below, this seems to work for most users. I gather that the problem is an issue of Windows 7 not liking being second banana in the MBR.

I am pretty sure that if you alternately boot raw and boot virtual, you will have to do this rescue disk procedure each time. Be careful with doing a repair on a raw boot, though, if you use Grub. Currently, I do not know of any way to use the partition for both Windows 7 raw and virtual boots without using the rescue disc fix.

Congratulations!

That’s it! Tinkering and optimizing aside, you’re done.

Further Reading

I would not have been able to do this without help from many other bloggers and hackers out there. Here are some of the resources I used to get this working: