HOWTO Dual Boot from Windows Bootloader (NTLDR) and why

From Gentoo Linux Wiki

Jump to: navigation, search
This article is part of the Dual Boot series.
Install Linux after Windows Install Windows after Linux Using Windows Loader Using GRUB or LILO coLinux

Contents

[edit] Introduction

Gentoo is one of the very best ways to learn Linux but can also be one of the trickiest. Understandably, you may wish to hang on to your Microsoft Windows XP system to provide a backup in case it all goes wrong or simply because XP is where you feel most comfortable for the time being. Dual-boot, therefore, is the ideal solution providing you with peace of mind and the opportunity to explore Gentoo at your own pace. There are two fundamental approaches to dual-booting:

  1. Have XP and Gentoo on the same physical hard disk
  2. Use separate hard disks for XP and Gentoo, possibly different combinations of SATA and IDE disks

Whichever you choose, keeping XP safe and being able to revert back to it, should you discover Linux is not for you, is probably of concern. Fortunately there is a wealth of information surrounding dual-boot; unfortunately there is a lack of a single, coherent document describing a failsafe process. Hopefully, this is it.

[edit] NTLDR - What is it and why?

You may have heard of GRUB, LILO and NTLDR. What these all have in common is that they are boot loaders; that is to say they initialize your system and haul your operating system into memory ready for use. NTLDR (or NT Loader) is Microsoft’s boot loader for NT based operating systems which include 2000 and XP. It relies upon three key files:

  1. boot.ini – contains configuration options for a boot menu
  2. NTDETECT.com – detects type of hardware in your system
  3. NTLDR – the loader itself

The boot loaders usually reside in the Master Boot Record (MBR) of your hard disk. Your MBR is a 512 byte boot sector of the disk that contains a sequence of commands to boot your operating system. By default XP uses NTLDR, so it would make sense to use this to continue to control which operating system you want to boot. Not only does this mean you avoid overwriting your MBR with GRUB or LILO and potential problems relating to lost partitions and an inability to boot an operating system, but also should you wish to get rid of Gentoo minimal effort is required to get your system back to how it was when you started. So, onto the action. I will assume by this stage you have been following the excellent Gentoo Handbook and arrived at the “Configuring the Bootloader” section.

[edit] GRLDR - A Simpler Approach

grldr is found in the grub4dos distribution, and is a patched version of regular grub. grldr can be pointed to by ntldr, thus removing the need for fiddling with dd, bootsectors and whatnot. grldr supports fat and ntfs, and employs a regular grub-style 'menu.lst'. It is particularly useful if you want to boot a linux kernel and ramdisk from your Windows partition, but may also be useful in other scenarios. Like if you want to install Linux on a machine with Windows, but no usb/floppy/cdrom. To use grldr, put grldr and menu.lst in the root of your windows partition. Then add: C:\grldr="Start Linux Loader (GRUB)" to boot.ini. That's it. Apart from editing menu.lst, that is. See the grub documentation for menu.lst syntax. (menu.lst is the same as grub.conf, but grldr looks for menu.lst.)

It is also worth mentioning that you still have to install grub during your gentoo install (and then select the -do not install to MBR- option). This will put your grub install into /boot/grub/, and from there you can use the grub.conf to add a proper entry into your C:\menu.lst.

[edit] Single disk installation

Scenario: XP and Gentoo installed on the same hard disk; XP in your first partition and Gentoo in subsequent partitions.

(Warning - contrary to what the handbook says, when setting up your partitions, you shouldn't set the /boot partition to be "bootable". You can only have one partition set as bootable - and this should be the Windows partition. If you have more than one partition set as bootable then your partition table will be invalid and you won't be able to boot up. If this happens, however, you can always boot from the livecd and use fdisk to correct the problem).

I will concentrate on using GRUB in this situation although you could equally use LILO; take a look at the separate disk installation section to get an idea of the configuration for LILO but remember you won’t be using separate disks so change the disk references accordingly.

[edit] GRUB Configuration

Firstly emerge grub and then create your configuration file using your favourite editor, in this case I use nano:

 # emerge grub
 # nano -w /boot/grub/grub.conf

Using the Gentoo Handbook, or the sample below, your conf file will look something like this:

File: /boot/grub/grub.conf
default 0		# Default operating system to load: first entry
timeout 5		# Timeout before loading default: 5 seconds
title=Gentoo Linux	# Title of OS: Gentoo Linux
root (hd0,1)		# Location of the kernel image: second partition on the first disk
# Tell GRUB where the kernel image is before changing root to point to your root partition:
# third partition (now using Linux notation)
kernel /boot/kernel-2.6.16-gentoo-r5 root=/dev/hda3

Don’t forget GRUB references the first partition of your hard disk as hd0,0 and not hd0,1. GRUB also references SATA disks using the same notation i.e. hd0,1. If you are not sure which partitions boot and root lives in then use “fdisk –l” to remind yourself of your disk partitioning scheme and, if you are still unsure, “df –h” which will display your partitions and where they are mounted.

[edit] GRUB Installation

If you have not yet done so, create /etc/mtab which lists all mounted file systems.

 # grep -v rootfs /proc/mounts > /etc/mtab

Now GRUB needs to be installed into the boot sector of the /boot partition and not your disk’s MBR, thus preserving the MBR. The boot sector of a partition is similar to the MBR in that it is the first 512 bytes although boot sectors of partitions can only be loaded by a bootloader from the MBR. To install GRUB use the following commands:

 # grub
 grub> root (hd0,1) 	(Specify where your /boot partition resides)
 grub> setup (hd0,1) 	(Install GRUB in the boot sector of /boot)
 grub> quit 		(Exit the GRUB shell)

adjusting partition numbers if required.

[edit] Fooling Windows

Firstly, make sure you have a medium to transfer a small file from Gentoo to Windows e.g. a FAT formatted floppy disk or a partition readable from Windows and writeable from Gentoo. You can also use Explore2fs[1] from windows and copy the file directly from the linux partion to the windows file system. Now we shall make a copy of the boot sector of the boot partition containing GRUB:

 # mount /dev/fd0 /mnt/floppy 			(mount your floppy disk, if required)
 # dd if=/dev/hda2 of=grub.mbr bs=512 count=1	(create a copy of the boot partition)
 # cp grub.mbr /mnt/floppy			(copy grub.mbr to your floppy)
 # umount /dev/fd0				(un-mount the floppy, if required)

Now reboot your machine into Windows and copy the grub.mbr file to c:\, assuming this is where Windows lives on your system. Cast your mind back to the boot.ini file...it is a hidden file but we can still open it with any text editor. Now open Notepad by selecting Start menu - All Programs - Accessories - Notepad. In Notepad go to file menu - open file "C:\boot.ini" and add this line to the bottom: c:\grub.mbr="Gentoo Linux"

It should now look similar to this example:

File: c:\boot.ini
[boot loader]
timeout=30
default=multi(0)disk(0)rdisk(0)partition(1)\WINDOWS
[operating systems]
multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Microsoft Windows XP Professional" /fastdetect
C:\grub.mbr="Gentoo Linux"

This additional line references the grub.mbr file we created earlier which is simply the instructions to boot Linux. Save the file and close Notepad. Finally reboot your machine; with luck you will see two listings, the second one being “Gentoo Linux”. When you select this GRUB should greet you and you are done.

Any easier solution is to use BOOTPART which will do everything for you. Download the program from http://www.winimage.com/bootpart.htm. This program, when run from Windows, will extract the first 512 bytes of the partition into a file, and make the change to boot.ini.

[edit] Separate disk installation

Scenario: XP and Gentoo installed on separate hard disks; XP in your first partition of your first disk and Gentoo in partitions on your second disk. This arrangement is common and desirable in long-term dual boot systems because each drive can contain a different self-contained, bootable operating system, which are easy to bring alive solo if the other one dies. Plus, if you include a 30GB FAT32 partition on each drive, each operating system can write to the other OS's drive as a local file backup (chances of both drives dying at the same time is about nil).

One down-side is that some problems booting Gentoo/XP from a separate disk are common, insidious, and go un-resolved. One example I've come across is putting Windows on the Secondary drive. LILO can boot this up, making Windows think it's on the IDE Primary by intercepting BIOS disk I/O calls. But eventually, some application doesn't play nice, and directly does I/O to one or both disks. As a general rule, Windows does not like to boot from a Secondary drive.

Fortunately, the Wiki HOWTO you're reading provides a definitive method of booting Gentoo on a Secondary disk, allowing XP to reside happily on the Primary disk. I will use LILO here as it has the best support for Windows partitions. You could also use GRUB although I have not had any success with it.

[edit] LILO Configuration

Firstly set your boot partition as active by running these commands:

# fdisk /dev/hdb
# Command (m for help):   <enter ‘a’>
# Partition number (1-4): <enter /boot partition number>
# Command (m for help):   <enter ‘p’ and check your /boot partition has an asterisk next to it.
# Command (m for help): w (Save the new schema)

If you are not sure which partition boot lives in then use “fdisk –l” to remind yourself of your disk partitioning scheme and, if you are still unsure, “df –h” which will display your partitions and where they are mounted. Make a note of root’s location while you are there as you will need this later.

Next, emerge lilo and then create your configuration file using your favourite editor, in this case I use nano:

 # emerge lilo
 # nano -w /etc/lilo.conf

Using the Gentoo Handbook, or the sample below, your conf file will look something like this:

File: /etc/lilo.conf
# Global LILO settings
boot=/dev/hdb1          # Install LILO in the /boot partition
prompt                  # Give the user the chance to select another section
timeout=50              # Wait 5 (five) seconds before booting the default section
default=Gentoo Linux 	# When the timeout has passed, boot the "Gentoo Linux" section

# Kernel specific LILO settings
image=/boot/kernel-2.6.14-gentoo-r5	# Location of your kernel image
  label= Gentoo Linux		# Name of this section
  read-only               	# Start with a read-only root. Do not alter!
  root=/dev/hdb3          	# Location of the root filesystem

Remember to alter the root, boot and image locations according to your own setup. SATA disks will be referenced using sda (first SATA disk).

One important note: the above configuration file sets where LILO will write to. LILO needs to be installed into the boot sector of the boot partition (/dev/hdb1) and not your disk’s MBR (/dev/hdb). The boot sector of a partition is similar to the MBR in that it is the first 512 bytes although boot sectors of partitions can only be loaded by a bootloader from the MBR. So you should set “boot=/dev/hdb1”, assuming this is where your boot partition is located.

If the boot partition is part of an already-used Linux system, chock full of files, don't worry. The boot sector of any partition is not used for normal file storage. It is resident and reserved space of any partition whether or not the partition has been used or is populated with files.

[edit] LILO Installation

LILO will probably make a backup of an existing boot sector before it writes over it. From Section 3 of the LILO (v 21) manual, "Whenever the map installer updates a boot sector, the original boot sector is copied to /boot/boot.number, where number is the hexadecimal device number. If such a file already exists, no backup copy is made." The easy answer is to inspect the /boot directory and rename any existing files so LILO will create a new one. With a backup copy of the boot sector, should you make a mistake at any point, just use “lilo -u device-name” and it will restore the saved boot sector. To quote from the To install LILO:

 # /sbin/lilo

As a side note, notice LILO writes to the boot device in the configuration file (or -b command line option, if it were to be used). LILO gets other critical information from the current root mount. For instance, if you ever salvage a hard drive installation after booting from an emergency LILO floppy, you'll probably chroot to the hard drive first. Note that the to and from could be the same on a simple installation.

[edit] Fooling Windows

Firstly, make sure you have a medium to transfer a small file from Gentoo to Windows e.g. a FAT formatted floppy disk or a partition readable from Windows and writeable from Gentoo. I will assume Gentoo is located on hdb, so just change this reference in the examples, if required.

Although LILO will probably do this for us (see caveat above for pre-existing files, quoted from the LILO manual), just to be sure initially we back up the MBR of the disk Gentoo is installed onto:

 # dd if=/dev/hdb of=mbr.save bs=512 count=1

before installing LILO into the MBR of the Gentoo disk (hdb) using a special LILO command:

 # lilo -M /dev/hdb

The -M option, available for the first time in LILO ver 22.3, is not the same as the -m option. Instead, the -M option creates a Master boot sector that we only need to copy to Windows once, and never need to update, no matter what other changes lilo makes. Specifically, "lilo -M" installs a default MBR onto the target that is obtained from a static copy compiled into /sbin/lilo, which will search for an active partition on the target disk and load that partitions's boot sector. (-M details obtained from hyc and LILO man pages).

Cull a copy of this new MBR using:

 # dd if=/dev/hdb of=lilo.mbr bs=512 count=1

and finally restore the old mbr:

 # dd if=mbr.save of=/dev/hdb bs=512

The next step is to transfer lilo.mbr to your Windows disk, in this case using a floppy, so:

 # mount /dev/fd0 /mnt/floppy 			(mount your floppy disk, if required)
 # cp lilo.mbr /mnt/floppy			(copy lilo.mbr to your floppy)
 # umount /dev/fd0				(un-mount the floppy, if required)

Now reboot your machine into Windows and copy the lilo.mbr file to c:\, assuming this is where Windows lives. Cast your mind back to the boot.ini file...we shall now add an entry to boot Gentoo but first we need to make the file viewable, writeable and unhidden. From command prompt (start > run, type “cmd”) enter:

 attrib -s -h -r c:\boot.ini

You can now browse to this file, open it and add this line to the bottom: c:\lilo.mbr="Gentoo Linux"

It should now look similar to this example:

File: c:\boot.ini
[boot loader]
timeout=30
default=multi(0)disk(0)rdisk(0)partition(1)\WINDOWS
[operating systems]
multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Microsoft Windows XP Professional" /fastdetect
C:\lilo.mbr="Gentoo Linux"

This additional line references the lilo.mbr file we created earlier which is simply the instructions to boot Linux. Save the file and reapply the attributes you removed by running:

attrib +s +h +r c:\boot.ini

at command prompt. Finally reboot your machine; with luck you will see two listings, the second one being “Gentoo Linux”. When you select this LILO should greet you and you are done.

[edit] Installing Grub

Installing Grub works much in the same way as LILO. But Stage 1 of Grub has to be told where to find Stage 2.

  • Install Grub on your /boot partition (not the MBR of the first bootable disk!)
  • Normally one would use grub-install, but this assumes the /boot partition is on the same drive as the MBR / Stage 1 of Grub. (The grub-install script should be fixed...)
  • You have to fire up the grub shell and install grub using the command install of grub with the option "d" (see grub manual: http://www.gnu.org/software/grub/manual/html_node/install.html#install)
  • Copy the Grub Stage 1 from the first sector of the /boot partition
# dd if=/dev/[boot partition here] of=grub.boot bs=512 count=1
  • Copy the file to the Windows partition (c:\linux_boot\grub.boot)
  • Add Grub (Gentoo/Linux) to the Windows bootloader NTLDR. Add a line to the file c:\boot.ini (remember the attributes!)
c:\linux_boot\grub.boot="Linux (Grub)"
  • You should see Linux in the boot menu, next time you boot
Warning: Make sure that you repeat the first five steps everytime GRUB gets updated. If you do not, then Gentoo maybe won't boot at the next restart after a GRUB update. If you've forgotten to do it but you can start Windows, you still can use GRUB4DOS to boot to Gentoo (you probably will need access to your /boot partition to obtain menu.lst, you can use Explore2fs to copy this file). Your other option would be SuperGrubDisk to start Gentoo. You can then repeat steps 1-5 to make sure that Gentoo will boot next time.

[edit] Grub Hack

Do not use this hack! Use the install command of the grub shell insead! See section above!

A hack that has worked on my machine because 'install' could not identify that the separate disks where completely isolated from one other, that is only one disk was available to the machine at a time by removing one of them prior to boot. The isolation was to guarantee that windows MBR was never over written. While this hack was successfully applied in this instance it cannot be confirmed that it will work for all.

After following the LILO process but using GRUB instead open the C:\*.mbr file with a hex-editor and change the value at offset 0x40 to 0x8? , where ? is the drive number grub is installed on. Following on from the example the new value will be 0x81 as drive numbers start from from 0x80 and GRUB is installed on the 2nd drive.

More information and the inspiration for the hack can be found at http://mirror.href.com/thestarman/asm/mbr/GRUB.htm (An interesting read)

[edit] Using Grub as bootloader (replacing Windows NTLDR)

Here is a way to have Windows on the first hard disk and Gentoo on the other and be able to boot them both.

Install Windows to the first disk.

Install Gentoo to the other.

Edit grub's config file menu.lst.

File: /boot/grub/menu.lst
title=Windows
root (hd1,0)
chainloader +1
map (hd1) (hd0)
map (hd0) (hd1)

title=Gentoo Linux
root (hd0,0)
kernel /kernel-2.6.16-gentoo-r9

Install grub (0.96 works, others not confirmed) to 2nd disk (Gentoo disk).

Make the 2nd disk the disk to boot from in BIOS.

The trick is to make Windows think that it's the first disk although it's actually the 2nd disk because we boot from the physical 2nd disk first. The map commands do this.

Good thing in this setup is that you have 2 separate disks that you can boot from. If there is something wrong with your Gentoo system you can change the drive to boot from in your BIOS and boot to Windows.

[edit] Reverting back to Windows

So you have given Gentoo a go and decided it is not for you and want to undo the changes we have made or you decided you love Gentoo and just want to boot your windows drive differently. Luckily, it is a simple matter of correcting your boot.ini and deleting the Linux partitions. You can delete all your Linux partitions using Linux's fdisk, or use the Disk Management snapin from "Control Panel -> Administration -> Computer Management -> Disk Management". If you choose to go with the linux fdisk, you might like to use the Gentoo LiveCD you installed Gentoo from, another LiveCD or any other source that has Linux fdisk on it.

 # fdisk <device e.g. /dev/hdb>
 # Command (m for help):   <enter ‘d’>
 # Partition number (1-4): <enter partition number>
 # Command (m for help): w (Save the new schema)

Repeat steps 2 and 3 until you have deleted all your Linux partitions. This will of course erase all data in those partitions.

Now remove the one line from your boot.ini file, located in your c:\ drive, that we modified earlier. Modify the permissions as before and remove:

 c:\grub.mbr="Gentoo Linux" or
 c:\lilo.mbr="Gentoo Linux"

depending on which route you chose, and reapply the permissions. You may also like to remove the *.mbr file left behind for tidiness.

[edit] Boot sector & MBR problems

These can, and will, happen so always make backups of important data before you start anything like altering your boot sectors. Should you find your Windows partition has become unbootable, you can repair the MBR and/or the boot sector of a partition using your XP installation disk. Simply boot from the installation disk, select 'R' to repair the current installation, enter the Recovery Console and then type:

 fixmbr <device> and/or
 fixboot <drive> (this wipes the boot sector)

Depending on the damage you may lose your data on this disk.

If you find you have hosed your partitions and apparently lost your data, help is at hand. Generally, your data will be safe and can be recovered simply by restoring the partition. One such tool that always works for me is Active Partition Recovery[2], although there are of course others.

[edit] Changes for users of Windows Vista

Windows Vista has done away with the boot.ini file. The new system is called "Boot Configuration Data" (BCD), and it is accessed through the BCDEdit.exe command. Running "BCDEdit /?" gives a list of commands and syntax. However, this program is hopelessly obtuse and difficult to use. Instead, a program like EasyBCD makes editing the Windows boot order a much easier task, and includes tools to automate the Windows-Linux dual-boot procedure.

[edit] Glossary

[edit] See also

[edit] External links

  • GNU GRUB homepage [3]
  • LILO homepage [4]
  • Detailed explanation of the boot.ini file and various configuration [5]
  • A detailed tutorial of using GRUB to dual-boot with Linux and Windows [6]
  • A tutorial of how to dual-boot with Linux and Windows on the same drive [7]
  • An explanation of using GRUB to boot Linux from a separate drive [8]
  • An explanation of using LILO to boot Linux from a separate drive [9]
  • Restoring your MBR [10]
Personal tools