Wednesday, August 20, 2008

How to boot an existing Linux or XP installation from virtual box

If you find the post below to be from mars then please read this article first.

The easiest way to run an OS is to install it from the virtual machine. But this is not the case always. In my case I came to know about virtualization only after I had installed all my operating systems.

My system config was something like this, I had three operating systems installed.
1. Windows XP
2. Ubuntu 8.04
3. Custom Built Linux.

My case was that I needed to boot into my custom built linux through Ubuntu using virtualization. Now I had to choose a Virtualization kit. I had three choices before me:
1. VmWare
2. Virtualbox
3. Qemu

I did not try VmWare at all. But between Virtualbox and Qemu, Virtualbox seemed to be way better. I downloaded the virtualbox from the SUN's website. It had a binary for my Ubuntu 8.04. Two double clicks and it was installed. Now my problem is how to make virtualbox boot into my custom built linux or my existing windows xp installation.

To achieve this you have two ways.
1. Create separate entries for each OS you want to boot or
2. Let virtualbox point to your MBR.

In the first case you will be booted into the OS os your choice, whereas in the second case when you start virtualbox you will be greeted with your own grub menu which you see every day when you start your system. It is dangerous to start your system in this way as the guest os(os you are going to boot from virtualbox) has all access to your hard disk and you may potentially damage your Host OS(OS on which Virtualbox is installed.)

I found these methods from the following post: - any credit goes to the author. IMO this is the best post for running an existing XP installation. Caution:These methods require you reactivate your copy of windows. so please decide whether you want to really boot into existing windows installation using virtualbox. Installing Guest Additions(tools that make the guest os run faster and smoother on virtualbox) for XP will not let you boot into your existing windows install any more. so proceed with caution.

Method 1 -- pointing to existing linux install. 

FIrsrtly, we need to download and install a package called mbr.  Run the following code in your linux terminal:
sudo apt-get install mbr && mkdir ~/.VirtualBox && install-mbr ~/.VirtualBox/myboot.mbr --force 
the above command mirrors a copy of your MBR onto a file called as myboot.mbr. We will be using this myboot.mbr in our virtualbox. Then run the following command to create a virtual disk that fakes your current hard disk state - partition status, grub everything.
VBoxManage internalcommands createrawvmdk -filename ~/.VirtualBox/linux.vmdk -rawdisk /dev/sda -partitions 2 -mbr ~/.VirtualBox/myboot.mbr -relative -register 
/dev/sda -- should be replaced with the corresponding disk. The number 2 -should be replaced by the partition number where your linux is residing. To know what is your existing setup from the terminal type:
fdisk -l.
Then replace the entries. If it is /dev/sda1 where you want to boot to then your command should be:
VBoxManage internalcommands createrawvmdk -filename ~/.VirtualBox/linux.vmdk -rawdisk /dev/sda -partitions 1 -mbr ~/.VirtualBox/myboot.mbr -relative -register 
This method may fail in some cases and you just cant boot into the existing installation. In such cases you need to follow the second method, FYI - the second method worked for me.

Method 2 - point virtualbox to your hard disk.

VBoxManage internalcommands createrawvmdk -filename ~/.VirtualBox/SystemHD.vmdk -rawdisk /dev/sda -register 
This creates a virtual disk called as SystemHD. Just create a virtual machine that uses this HD. Your work is done start the VM and run the OS of choice.


Virtualization is one darn hot technology and here's why:

We the end users use the system to access various programs for example a word processor to write documents or the browser to post this blog entry and every program needs access to the hardware. For example, to get this post going I had to make use of the key board and the mouse. So how did the key board know what I was typing should appear in my browser. Did I make friends with my keyboard or did I bribe it. I tell you my key board is very unfriendly, it never talked to me directly inspite of all my valiant efforts to make it talk. So, there should be some mechanism which facilitates this communication between my browser and this keyboard. This is taken care of by the Operating System (OS) or to be very precise the kernel. The kernel has exclusive rights to all our hardware or in other sense only one kernel can run at a time. Imagine if there were two kernels and one keyboard this would be a triangular love story , where each kernel is fighting to win the heart of the keyboard meanwhile the poor guy the end user who is me in this case will never understand why the hell his key presses dont appear on the screen no matter how hard he hits the keys. What does this love story indicate. It says with all dignity that only monogamy is possible or only one operating system can run on your system at any given point of time.

And my friends, Virtualization is a technique which breaks the barries of monogamy, it breaks all your religious laws about computing, ah yes! you have guessed it right we can finally run more than one operating system at the same time. When I first heard about virtualization 6 years ago I thought that these people might have gone crazy otherwise how can one run two operating systems simultaneously and even if this was remotely possible only guys who never cared about their data or who loved reinstalling their OS to the point that their installation CD could no longer bear it and died a fiery death would only do it. And after 6 years I'm fully convinced this is possible and very reliable too and all my asumptions are wrong(all, about the data, the CD and monogamy).

Virtualization is a technique where the virtual machine (a program which runs on your Host OS) allocates system resources to the Guest OS. Here the system resources are Hard disk Space, RAM, Processor etc. The virtual machine tricks your guest OS into beliving that it has access to all these resources. Whereas in reality the virtual machine allocates these resources to our guest OS, the virtual machine inturn requests these resources from our host operating system. So, the virtual machine and the guest OS running inside it is nothing but another process(an open window that can be closed by clicking the 'X' button located at the top right corner) on our host OS.

This is what virtualization has achieved and I wouldn't be surpirised at all if the future of all Operating Systems depends on virtualization.

There are many virtual machines available out there like

1. VmWare -- the first and the most successful company to invest in virtualization.
2. Virtualbox -- a very lightweight VM now owned by SUN.

there are many other opensource VM's available too. I have tried virtualbox and it is pretty robust. In my next post I'm going to show how you can setup virtualbox on your Linux machine.