Tuesday, September 21, 2010

Windows 7 + Ubuntu Dual Boot and Virtual Box on a Lenovo T510

Here is how to setup a sensible dual boot Win7 + Linux environment, with support for virtualization. The setup will use Win7 as the primary OS, minimizing the effects of the dualboot on the Linux environment. This guide assumes that Win7 Pro, 64 bit is previously installed. And that the processor supports VT-X (this is important).

Step 1 : Setup disks
1.1 Shrink the active C: and create a 20 gb partition (extended is fine, not just primary partitions)
1.2 Use the Disk Management plugin in Computer management
1.3 Create this partition as a RAW partition, with no drive letter assigned

Step 2 : Installing Linux
2.1 Put dvd of your favorite linux distro in.
2.2 While installing, use the 20gb partition, formatted as ext3, as the "/" or root partition.
2.3 WHEN INSTALLING GRUB, DO NOT INSTALL IT ON THE MBR. INSTALL GRUB ON THE PARTITION WHERE LINUX IS INSTALLED.
2.4 For the rest of this, we assume that Linux is installed on /dev/sda5


Step 3 : Creating the boot sector
3.1 Reboot computer using a livecd. Most install disks for Linux distributions are live CDs.
3.2 Once booted, switch to a root shell.
3.3 Mount the existing Windows 7 boot drive (i.e. C:). This is usually /dev/sda2
3.4 Copy the boot sector of the Linux installation into a file.

dd if=/dev/sda5 of=/media/Win7/LINUX.BOOT bs=512 count=1

This should create a small file C:\LINUX.BOOT which is the stage 1 of GRUB for the install.

Step 4 : Integrating this with the Windows 7 boot menu
4.1 Boot into Windows 7
4.2 Open an administrator shell by rightclicking "cmd" -> Run As Administrator
4.3 Create an entry for linux. NOTE: {ID} is generated after the first command is run. Its a long GUID for the partition entry.

bcdedit.exe /create /d "Linux" /application BOOTSECTOR
bcdedit.exe /set {ID} device partition=c:
bcdedit.exe /set {ID} PATH \LINUX.BOOT

4.4 Set the order and display time

bcdedit.exe /displayorder {ID} /addlast
bcdedit.exe /timeout 5

4.5 Test this works by rebooting and choosing windows or linux.

5. Setting up Virtual Box in Windows
5.1 Download and install virtual box for windows (NOT THE OPEN SOURCE EDITION)
5.2 Reboot the T510. On boot, press F1 to get BIOS options. Enable the option for VT-x under CPU->Virtualization.
5.3 Save, exit, and reboot into windows.
5.4 Start a new administrator shell
5.5 Navigate to the virtualbox installed folder ("C:\Program Files\Oracle\VirtualBox")
5.6 Check the partition number

VboxManage.exe internalcommands listpartions -rawdisk \\.\PhysicalDrive0

Look for the number of the 20 gb drive (alternately, the partition with type 0x83)
5.7 Create the harddisk

VboxManage.exe internalcommands createrawvmdk -filename rawdisk.vmdk -rawdisk \\.\PhysicalDrive0 -partitions 5 -mbr C:\LINUX.BOOT -register

NOTE: Change the number for the partitions argument to the partition numbers from 5.6. For multiple partitions, use a comma separated list.
5.8 Create a new virtual machine, add this hard disk as the storage disk, and all should be well.

One important note : VirtualBox needs admin access to use raw disks, so if you are running as an alternate user, you must use RunAs to run virtualbox as root. (Even the GUI, everytime)

Refs:
Ref1
Ref2
VirtualBox Manual