From OSDev Wiki
Jump to: navigation, search

GRUB stands for GRand Unified Bootloader and is a bootloader and the reference implementation of the Multiboot Specification. GRUB can boot any Multiboot-compliant OS (e.g. Hurd, among others), however it can also boot some non-Multiboot-compliant OSes, e.g. Windows, via a chain-loading function, and some others via special support, e.g. Linux. mbchk can be used to determine if a file is Multiboot-compliant.

This article concentrates on GRUB Legacy. Information regarding GRUB 2 can be found in a separate article.



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" executables (any file with a valid Multiboot header).

One major advantage of GRUB (or more precisely, a Multiboot-compliant 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.

Consult the Multiboot Specification if you want to know what GRUB expects and what you can expect from it.

Installation and booting

This section describes how to install GRUB on your actual computer, if you are using some kind of emulator (e.g. Bochs) to test your OS in you're probably more interested in the setup described in e.g. Booting the kernel or Environment setup. However, you might still be interested in how to load your kernel over TFTP.

Installing to floppy

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 Windows (or don't feel like setting up a GRUB disk with Linux), check out our Disk Images 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 source as well as precompiled binaries at the GRUB web page. (Windows users will want the grub-0.9x-i386-pc.tar.gz package.) Download and unpack the archive.

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.

Auxiliary GRUB install

Remove the boot disk from the floppy drive and insert the auxiliary disk. 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.

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.

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, 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.

Installing to USB

UPDATE: You may wish to look into grub-install if using GRUB2.

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 device is as easy as following the above example of installing to a standard 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 directories:

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, /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_stage1_5 /mnt/sdb1/boot/grub/fat_stage1_5

For ext2 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 (hd1,1)
setup (hd1)

if you don't know where your kernel is, you can use the command "find /boot/grub/e2fs_stage_1_5" to get the parameters which would be used in the command root (sdb1,1) and setup (sdb1).

Installing to hard disk

See Installation in the official GRUB manual.

Installing to CD

See Bootable El-Torito CD with GRUB Legacy.

Load your kernel over network (TFTP)

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. See also, How to set up your network in the official GRUB manual.

Getting 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 and extract it to a work directly.


./configure --help

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

./configure --enable-tulip
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, 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
# I don't have DHCP setup to deliver the TFTP server's IP address,
# so I set it manually this is my build machine
# The root device is the network
root (nd)
# My kernel is directly under the TFTP directory
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 protocol.

Grub for EFI

This section will tell you how to compile, build and install Grub2 on an EFI system. This have been tested on an MacBook Pro6,1


First you need a x86_64 linux with gcc and binutils. I downloaded a Ubuntu 10.4 LiveCD, booted it in VirtualBox and installed it to disk.

You need to make sure gcc, bison, flex etc are installed. Do this with the apt package manager. apt-get install gcc bison flex is what i did.

You need the grub2 source code. I got that from

Download grub-1.9-rc2.tar.gz and unpack it.

Next thing to do is
./configure --with-platform=efi --target=x86_64

next step is to compile:


Now you should have compiled grub2 efi if everything went ok.

Build Grub EFI binary (bootx64.efi)

grub.efi / bootx64.efi is a bundle of tools including grub itself. You need to make the efi image by selecting what modules you want.

For Grub to be able to read disk partitions you need a module for that. Either you want plain-old bootsector and msdos style partition map or you want GPT (new fancy one) you need to select correct module when bundling grub.efi.

For your grub to be functional you should include as many filesystems as possible and also linux,multiboot,multiboot2 etc so that you can read any type of kernel from any type of filesystem.

So.. Lets build grub.efi / bootx64.efi

Go to "grub-core" folder.

Execute: ../grub-mkimage -d . -o bootx64.efi -O x86_64-efi -p /efi/boot [list of modules without .mod]

where "-p /efi/boot" tells where "grub.cfg" is after booting and it will be loaded from this path on booting device.

A easy way of adding all modules is:

../grub-mkimage -d . -o bootx64.efi -O x86_64-efi -p /efi/boot `find *.mod | xargs | sed -e 's/\.mod//g'`

That will build "bootx64.efi" in the subfolder and this can now be copied to a USB-disk, harddisk etc.

From UEFI specification the EFI bios will try to load & boot from /efi/boot/bootx64.efi on a FAT formated drive. This is for x86_64 plattform.


Add a "grub.cfg" file to /efi/boot/gruf.cfg or wherever you like. Make sure to put it where you told grub to look for its files "-p option".

To use graphical menu you must also provide a font file. I used unicode.pf2 that i found in /usr/share/grub2 or something.

I did like this:

set timeout=30
set default=0
#set debug=all

loadfont /efi/boot/unicode.pf2
#set gfxmode=1024x768
set gfxmode=auto
set gfxpayload=keep
terminal_output gfxterm

background_image -m normal /efi/boot/background.jpg

menuentry "OSKernel" {
set debug=all
multiboot /kernel32.exe
module /null.sys
module /console.sys
module /serial.sys
module /keyboard.sys
module /random.sys
sleep 5 

Some nice to know info is that grub2 (1.99rc2 atleast) parses the video mode information in the multiboot header. It also tries to sattisfy your request. This means that you can request text/video, WxHxD from the multiboot header. Video information about mode, framebuffer address etc should be available in the multiboot info aswell. Sweet isnĀ“t it? :)


I partitioned my usbdisk with MacOSX diskutil, but any partition tool should/would do. I created a small 100mb partition as first partition and created a huge secondary partition for files. I formated the partition to HFS+, but i guess FAT should be more compatible since i dont think other EFI firmwares have HFS+ filesystem support like the Mac..

Anyway i created "/efi/boot/" folder and copied bootx64.efi to /efi/boot/bootx64.efi on my disk. I then created a grub.cfg file alongside with it.

To make OSX find the bootx64.efi image on boot i did;

sudo bless --verbose --folder=/Volumes/EFI --file=/Volumes/EFI/efi/boot/bootx64.efi --setBoot

where /Volumes/EFI is the first partition on my usb disk..

Now i saved my work and rebooted.. Press/hold "option" key during boot will bring up boot menu. I now get an option to boot en EFI DISK. I select this and now Grub2 shows like any other Grub..

Configuration of grub and config file is like any other grub2 syntax.

The "blessing" procedure should not be needed on UEFI compatible EFI versions. These will always look for /efi/boot/bootx64.efi.

Other loaders that are Multiboot compliant

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

See Also

Wiki Pages



External Links

Personal tools
In other languages