Dual Boot Into Windows And Ubuntu With GRUB - Adventures in Switching to Linux

Sunday, December 23, 2007

Dual Boot Into Windows And Ubuntu With GRUB

Dual booting in Linux is pretty easy. All you have to do is install Linux on a separate partition (after installing Windows or there is more work involved since Windows always overwrites your MBR) and setup the GRUB boot manager. Creating a separate partition is also fairly easy with GParted. GParted is part of the Ubuntu Live CD applications. I am not going to get into the details of resizing partitions today though.

In addition to resizing partitions, GParted also is a handy utility to find out what partitions each OS is installed on. Knowing what partition each OS is installed on is necessary to configure GRUB. The image below is from my home desktop:

GParted is not installed by default so you have to add it from Add/Remove programs if you want to use it. Alternatively you can just use fdisk to find out how each partition is configured.

forrest@desktop:~$ sudo fdisk -l /dev/sda

Disk /dev/sda: 160.0 GB, 160000000000 bytes
255 heads, 63 sectors/track, 19452 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x41ab2316

Device Boot Start End Blocks Id System
/dev/sda1 1 8 64228+ 6 FAT16
/dev/sda2 * 9 15756 126495810 7 HPFS/NTFS
/dev/sda3 18996 19452 3670852+ db CP/M / CTOS / ...
/dev/sda4 15757 18995 26017267+ 5 Extended
/dev/sda5 15757 18799 24442866 83 Linux
/dev/sda6 18800 18995 1574338+ 82 Linux swap / Solaris
Earlier this week I commented about some new Ubuntu updates, including a Kernel update. I'm a bit disappointed because the the update wiped out my GRUB configuration, removing my ability to boot into Windows (I still need it sometimes). That is the basis for this post.

So at this point we know from above that Windows is installed on /dev/sda2 and Linux is installed on /dev/sda5. To add the ability to boot into Windows back, you will need to edit your GRUB menu file. From a command line run (gksu is the graphical version of sudo):

gksu gedit /boot/grub/menu.lst &

The file has a lot of options but the ones you are looking for are the ones that have title and root options. My currently configured kernel has this entry in menu.lst:
title  Ubuntu 7.10, kernel 2.6.22-14-generic
root (hd0,4)
kernel /boot/vmlinuz-2.6.22-14-generic root=UUID=2a7aa925-61b3-4890-ab99-baa68b29c76c ro quiet splash
initrd /boot/initrd.img-2.6.22-14-generic
Notice the hd0 part of the root option. That corresponds to the device we want to boot from. The 4 is the partition on that device. In this case, Linux is installed on the first disk and the fifth partition. Since GRUB starts from 0 instead of 1, the first hard disk is hd0 and the first partition is 0. All of the details of finding the correct name of your partition are here in the GRUB manual, including some slight differences when dealing with extended partitions.

Looking ad the output from fdisk or what is show in GParted, we can see that Windows is installed on the second parition so all we have to do to add Windows to the list is add the following to the GRUB menu.lst:
title  Windows XP
root (hd0,1)
chainloader +1
Windows boots a little differently from other OSes. It has to be chain-loaded. That is the reason for the makeactive and chainloader options.

Another example

I also had to do the same thing on my laptop. I think when I initially installed Ubuntu on my laptop it automatically added Windows to the GRUB configuration. Updates have since wiped that out. Here are my partitions on the laptop via GParted and then fdisk:

forrest@laptop:~$ sudo fdisk -l /dev/sda

Disk /dev/sda: 40.0 GB, 40007761920 bytes
255 heads, 63 sectors/track, 4864 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xa0000000

Device Boot Start End Blocks Id System
/dev/sda1 1 4 32098+ de Dell Utility
/dev/sda2 * 5 2567 20587297+ 7 HPFS/NTFS
/dev/sda3 2568 4676 16940542+ 83 Linux
/dev/sda4 4677 4864 1510110 5 Extended
/dev/sda5 4677 4864 1510078+ 82 Linux swap / Solaris

And based on that, my GRUB menu.lst entries for my Windows partition and Linux kernel:
title  Windows XP
root (hd0,1)
chainloader +1

title Ubuntu 7.10, kernel 2.6.22-14-generic
root (hd0,2)
kernel /boot/vmlinuz-2.6.22-14-generic root=UUID=b4741939-10d8-4a31-bfd1-55231e49ebb6 ro splash quiet
initrd /boot/initrd.img-2.6.22-14-generic

I hope those examples help to get your dual boot back up and running. While I am here I also wanted to mention a few other GRUB options that I've taken advantage of. I think all of them are already in the default menu.lst file, just commented out with a #.
  • default - set which OS you want to boot into by default
  • timeout - how long to wait for a user selection before booting into the default OS
  • color - make your default menus fit a tad more to your taste
  • hidemenu - disable the OS selection menu, I prefer to NOT use this option
## default num
# Set the default entry to the entry number NUM. Numbering starts from 0, and
# the entry number 0 is the default if the command is not used.
# You can specify 'saved' instead of a number. In this case, the default entry
# is the entry saved with the command 'savedefault'.
# WARNING: If you are using dmraid do not use 'savedefault' or your
# array will desync and will not let you boot your system.
default 1

## timeout sec
# Set a timeout, in SEC seconds, before automatically booting the default entry
# (normally the first entry defined).
timeout 6

## hiddenmenu
# Hides the menu by default (press ESC to see the menu)
# hiddenmenu

# Pretty colours
color cyan/blue white/blue

One final note on GRUB background images with "splashimage"

And one final, final comment. Thinking about the color configuration reminded me of booting into Fedora or RHEL. They have a nice graphical background as part of the GRUB boot screen. I looked into that months back and that is a feature that is not part of the vanilla GRUB distribution but was added to the Redhat stream of GRUB as far back as Redhat 8 (September 2002). This feature is not included in the Debain stable branch, what Ubuntu is based on, so you can't add a boot background without installing GRUB from the Debian unstable which does have it. I don't think that is worth it. Many details on splashimage in GRUB are here.

No comments: