Search This Blog

Powered By Blogger

Sunday, December 19, 2010

Running an existing OS in VirtualBox under Fedora Linux

(Copied precisely from http://blog.amhill.net/2010/01/27/linux-ftw-using-virtualbox-with-an-existing-windows-partition/ )

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’s website and download the appropriate version for your OS.
If you’re clever, you can load their PPA into your software sources list (System->Admin->Software Sources), load the key, and that way you’ll get updates automatically! (plus you can apt-get install it ;) )
Either way — 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 Karmic (64-bit) is VirtualBox 3.1.2.

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:
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:
install-mbr: Offset must be a number: rce
Then either use just -f instead, or ensure that you are using two -’s before “force”.

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:
VBoxManage internalcommands listpartitions -rawdisk /dev/sda
For the boldfaced 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:
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       0×83  0   /1  /1   1023/254/63         53348           63
2       0×82  1023/254/63  1023/254/63          4000    109258065
3       0×83  1023/254/63  1023/254/63        155998    117451215
4       0×07  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:
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!)
sudo usermod -a -G disk yourusername
Ok, now you’re ready to actually create the VDMK file. Get ready because this is a handful.
VBoxManage internalcommands createrawvmdk -filename ~/.VirtualBox/winxp.vmdk -rawdisk /dev/sda -partitions 4 -mbr ~/.VirtualBox/FAKE.mbr -relative -register
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!”
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 tells VirtualBox to register it in its database of available images. Not absolutely necessary, but it’s a nice shortcut.
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 in Applications -> System Tools -> Sun Virtual Box. 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 have a Core 2 Duo, so I can 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. :)
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.

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:

No comments:

Post a Comment