GRUB Legacy: Difference between revisions

From OSDev.wiki
Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content added Content deleted
No edit summary
Line 164: Line 164:


NOTE: This tutorial required the mkisofs application to generate a CD image. This tutorial
NOTE: This tutorial required the mkisofs application to generate a CD image. This tutorial
also requires the stage2_eltorito file.
also requires the stage2_eltorito file.


This tutorial can be run on windows (mkisofs is available under the cygwin distribution) or under linux.
This tutorial can be run on windows (mkisofs is available under the cygwin distribution) or under linux.

Revision as of 20:43, 21 September 2008

What is GRUB

GRUB stands for GRand Unified Bootloader and is a GNU bootloader that can boot a variety of operating systems from Linux, Mach4, vSTA, DOS, NT 3.51, the *BSD variants and any generic OS you can add to it with its easy-to-use boot menu.

NOTE: This FAQ concentrates on the recently renamed to "GRUB legacy". GRUB 2 doesn't sound 'released' enough at the time of writing...

GRUB takes away all the complexities out of trying to boot your OS by doing it for you. GRUB can handle a variety of filesystems from MS-DOS FAT systems over Linux ext2fs (Second Extended File System) to BSD FFS, and can load kernels in various binary formats, including "generic" ELF, FreeBSD a.out, flat binary, and "generic" executable's (any file with a valid multiboot header). One major advantage of GRUB (or more precisely, a multiboot standard bootloader) is that the kernel will be entered in a known state, which includes the A20 Line having been enabled, and Protected mode having been entered. This takes a lot of the pain out of writing a kernel, rendering GRUB a very useful tool for the amateur, or anyone who wants to spend more time on the intricacies of the kernel rather than worrying about these generic start-up procedures.

Check out informations about the multiboot standard if you want to know what GRUB expects and what you can expect from it ...

GRUB HOWTO

This tutorial will show you how to install GRUB and your self-made kernel on a floppy disk, to avoid screwing up your hard drive. Installing GRUB as your system bootloader depends heavily on your system setup and is beyond the scope of this tutorial.

GRUB cannot be installed directly to the intended boot device. Once an operating system is up and running, GRUB's probing the BIOS for available drives might yield wrong results. Thus, a four-step process is used (using two floppy disks: the boot disk to-be, and an auxiliary disk).

NOTE: creating your own GRUB bootdisk requires a unix-like environment. If you're working under MS Windows (or don't feel like setting up a GRUB disk with linux), check out our Disk Images Under Windows page to get a ready-to-use image.

  1. prepare the boot disk;
  2. create a rudimentary GRUB installation on an auxiliary disk;
  3. boot from the auxiliary disk, and complete the GRUB installation on the boot disk.

You will find the GRUB homepage at [1]; the source as well as precompiled binaries can be found at [2]. (Windows users will want the grub-0.9x-i386-pc.tar.gz package.) Download and unpack the archive.

Step one: Preparing the boot disk

Format a floppy disk; this will be your boot disk. Create a directory "boot" on that disk, and copy the files "stage1" and "stage2" from the tarball (or your build directory) to "boot". Note that, after the installation, "stage2" must not be relocated (e.g. by Windows' disk defragmentation), or the bootloader will be unable to find it.

Remove the disk from the floppy drive.

Step two: Auxiliary GRUB install

Now insert the auxiliary disk into the drive. The files "stage1" and "stage2" from the GRUB distribution must be written to this disk, to sector 0 and sector 1+ respectively. In your shell (for Windows users, this means the Cygwin shell!), type:

$> dd if=<stage1> of=/dev/fd0 bs=512 count=1
$> dd if=<stage2> of=/dev/fd0 bs=512 seek=1

Now GRUB will be able to boot stage1 and stage2 from this disk. We cannot use it as our boot disk, though, since writing stage2 to sector 1 destroyed the floppy's format structure (you can't write files to the disk anymore, and any previously written "boot" directory could no longer be found). That's why we will now set up the other disk to boot GRUB and have a valid structure.

Leave the auxiliary disk in the drive, and boot from that floppy.

Step three: Completing the install

GRUB will boot into its command shell. (If your system does boot from hard drive instead, check your BIOS boot drive settings.) When the "grub >" prompt appears, remove the auxiliary disk from the drive and replace the boot disk. From the GRUB shell, you can now complete the installation on the boot disk:

install (fd0)/boot/stage1 (fd0) (fd0)/boot/stage2 (fd0)/boot/menu.cfg

The parameters tell GRUB:

  • where to find the stage1 file;
  • which device to install GRUB on (the floppy drive) - this writes stage1 to the boot sector, you can delete boot/stage1 afterwards;
  • where to find the stage2 file (hardcoding the sector number of that file into the boot sector stage1; that's why stage2 must not be moved after this procedure);
  • where stage2 should look for the configuration file.

Remove the boot disk from the drive, and reboot your operating system so you can edit /boot/menu.cfg.

Step four: Configuring GRUB

When you boot from the just-created boot floppy, GRUB stage1 is read from the floppy's boot sector. This stage1 "knows" where to find stage2 (it was told so during installation), and loads that. The stage2 does lots of interesting stuff (we'll come to that), but most importantly, it "knows" how to read file systems, so it can find the configuration file, "boot/menu.cfg".

The configuration file tells GRUB where to look for the actual kernel image(s). You can set it up so it offers you a selection of various different kernels / operating systems, even allowing you to boot your "real" OS from hard drive after booting GRUB from floppy. (In the end, you will probably want to install both GRUB and your own OS kernel on the hard drive, but we'll stick with the floppy solution for now.)

Add an entry like this to the configuration file:

title   MyOS
root    (fd0)
kernel  /boot/kernel.bin

Now all we have to do is to compile a kernel image and save it as /boot/kernel.bin. GRUB will find, load, and start it. For more details, menu options etc., refer to the GRUB manuals.

Network booting with GRUB

You can also use grub's builtin network support to increase your build/test turn around time. You can boot another machine with a kernel directly from your build directory without having to copy it over manually.

Geting Grub to support your card

First you need a version of grub that supports your network card. Most likely you will have to build one from source, so grab it from [3] and extract it to a work directly.

Run

configure --help

to see a list of options. In my case, I use Netgear tulip cards, so I execute:

configure --enable-tulip
make
make install

You may want to consider using the --prefix option if you don't want to overwrite your existing grub install or do not have root access.

NOTE: this was done on a Linux box, I assume using Cygwin under Windows would work as well.

Set up a TFTP server

You will need a tftp server running on your build machine and pointing to your build directory. I've been using the 3CServer utility from 3COM, [4], but there are several out there for most platforms.

Make sure any firewalls you have installed allow incoming connections on the tftp ports.

Install as you would normally.

On your target machine install the grub binary you just built as documented above.

Configure GRUB

I use the following entry in my menu.lst to boot my kernel.

title Network Boot
# obtain an ip address for this card
dhcp
# I don't have dhcp setup to deliver the tftp server's ip address, so I set it manually
# this is my build machine
tftpserver 192.168.0.50
# the root device is the network
root (nd)
# my kernel is directly under the tftp directoy
kernel /kernel.exe
# I also load a module for testing out multitasking
module /module.exe

And that should be it. If you're not running dhcp you can use ifconfig to manually set addresses, gateways, etc. Your can also use the bootp or rarp protocols.

Booting from USB with GRUB

NOTE: I have tried many tools to install GRUB onto a USB device under windows, however I have not been able to figure out how. So, to install, either use a Linux distro on your PC of use a Linux Live CD.

Putting grub onto a USB Pen/Disk is as easy as following the above example of installing to a statndard hard disk.

There is one extra step involved, finding out under which mount point you USB device is located. You may even have to mount it yourself if it is not already mounted.

To install use the following steps:

If you need to mount your USB device: (Exchange 'sdb1' with your device name)

mkdir /mnt/sdb1
mount /dev/sdb1 /mnt/sdb1

Create required directory's:

mkdir /mnt/sdb1/boot
mkdir /mnt/sdb1/boot/grub

Locate the stage1, stage2, and ???_stage1_5 files:

find / -name stage1

Copy stage1 and stage2 files: NOTE: /usr/lib/grub/ is an example location, use the location returned by the find command.

cp /usr/lib/grub/stage1 /mnt/sdb1/boot/grub/stage1
cp /usr/lib/grub/stage2 /mnt/sdb1/boot/grub/stage2

For FAT formatted USB device use:

cp /usr/lib/grub/fat_stage_1_5 /mnt/sdb1/boot/grub/fat_stage_1_5

For Ext2fs formatted USB device use:

cp /usr/lib/grub/e2fs_stage_1_5 /mnt/sdb1/boot/grub/e2fs_stage_1_5

Then execute the grub install application:

sudo grub

Enter the following commands:

root(sdb1,1)
setup(sdb1)
quit

Booting from CD with GRUB

NOTE: This tutorial required the mkisofs application to generate a CD image. This tutorial also requires the stage2_eltorito file.

This tutorial can be run on windows (mkisofs is available under the cygwin distribution) or under linux.

To install use the following steps:

Create the required directory structures...

Windows:

mkdir c:\grubcd
mkdir c:\grubcd\iso
mkdir c:\grubcd\iso\boot
mkdir c:\grubcd\iso\boot\grub

Linux:

mkdir -p /grubcd/iso/boot/grub

Copy the stage2_eltorito file:

Windows:

NOTE: The path c:\downloads\grub\ should be changed with the actual location of stage2_eltorito

copy c:\downloads\grub\stage2_eltorito c:\grubcd\iso\boot\grub

Linux:

cp /lib/grub/i386-pc/stage2_eltorito /grubcd/iso/boot/grub

Next, create the iso image:

Windows:

mkisofs -R -b boot\grub\stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table -o grub.iso C:\grubcd\iso\

Linux:

mkisofs -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table -o grub.iso grubcd/iso/

Edit this image to include your menu.lst and kernel, then burn to a CD.

Other Loaders that are multiboot-compliant

Chris Giese's MBload also works with multiboot-compliant kernels. Check out related thread for more info.

See Also

Tutorials

Threads

External Links