Partitioning help for multi booting Ubuntu Linux and Windows XP safely

April 3, 2007

A few days back, the Windows installation on my PC at home got corrupted. So I decided to reinstall Windows and multi-boot it with Ubuntu. I backed up whatever data I needed from the desktop hard drive onto my laptop and proceeded to have fun trying out all kinds of acrobatics with no fear of data loss.

I had an Ubuntu 6.06 LTS (Dappper Drake) Live CD which I used to boot/install Ubuntu.

Here are a few observaions that may be interesting and could be of help to anyone who needs to install operating systems or partition hard disks.

The MBR (Master Boot Record) is the first sector(512 bytes) of the Hard disk.

The Partion table is stored within the MBR.

You can back up your MBR into a 512 byte file under Linux by the dd command. Remember that you need to be root to use dd. Or under Ubuntu, you’ll need to add “sudo” to the beginning of the command -

sudo dd if=/dev/hda of=mbr.backup bs=512 count=1

The backup can be restored by -

sudo dd if=mbr.backup of=/dev/hda bs=512 count=1

If you want to completely overwrite your MBR (needed sometimes when MBR gets corrupted or just doesn’t agree with the way Microsoft thinks it should be) -

sudo dd if=/dev/zero of=/dev/hda bs=512 count=1

The boot code is stored in the first 446 bytes of the MBR and the rest is the partition table and the MBR signature. So if you want to clear out the boot code only and leave the partition table intact -

sudo dd if=/dev/zero of=/dev/hda bs=446 count=1

You can restore the stock Windows boot code by -

fdisk /MBR

(here fdisk is the Windows partitioning tool, not the one in Linux)

You may wish to save the MBR backup to a Windows FAT partition. To do so you’ll have to first mount your Windows partition on a directory in Linux. If /dev/hda1 is your FAT partition,

sudo mkdir /mnt/c
sudo mount /dev/hda1 /mnt/c

Now you can access the Window partition in the /mnt/c directory. Remember to unmount it when done by

sudo umount /mnt/c 

The boundaries of the partitions are stored in the partition table in two ways in CHS (Cylinder Head Sector) terms and in LBA (Logical Block Address) terms. CHS doesn’t really mean much for modern harddisks but Windows will have trouble if CHS values are not faked as appropriate. Linux uses only the LBA values.

If extended partitions are made past the 1024th cylinder, DOS fdisk may crash/hang mysteriously. Such a crash happened to me repeatedly at the partitioning screen during the Windows installation.

It is also a good idea to have a “/boot” partition for Linux at the beginning of your drive of about 100MB to circumvent some problems caused in older Firmware. (GRUB Error 18: Selected cylinder exceeds maximum supported by BIOS.)

Extended partitions can be “Linux Extended”, “Windows Extended”, “Windows Extended LBA mapped”. Having Windows and Linux partitions inside the same extended partition can sometimes lead to strange problems. I believe I was stuck with a GRUB stage 1.5 : Error 17 : Cannot mount selected partition which I got rid of by not putting the FAT partition in the same extended partition as the Linux partition.

I managed to prevent Windows setup from crashing at the partitioning screen simply by changing the type of a “Windows Extended Partition” which contained FAT32 as well as an EXT3 and a LinuxSwap partition to a “Linux Extended Partition”. My conclusion, Windows has a sucky partitioning tool.

Under Ubuntu, “cfdisk” is a great curses based tool (console based GUI) for partitioning safely and easily but it can’t change the type of an extended partition in which case you can use “fdisk”. GPartEd and QtPartEd have a full blown friendly interface for partitioning and are usually integrated into the Linux installers but they may not have all the power of the console based tools.

It may be a good idea to save the MBR to a file and email it to yourself just in case it ever gets corrupted by a virus or overwritten by Windows or if you accidently overwrite it when experimenting. ;)

Finally, the best practice is to install Windows first, then Linux. Remember that Linux is built for multi-OS compatibility, not Windows.