GRUB

From OSDev Wiki
(Redirected from GRUB2)
Jump to: navigation, search

This article is written like a tutorial. Please edit it to have more information and documentation instead of example code and step by step instructions.

GRUB is the GNU project's bootloader. The current version 2 series have a more complete feature set than GRUB 0.97 (commonly referred to as "GRUB Legacy").

Contents

History

GRUB version 2 started its life as the PUPA (note the pun) research project and was rewritten from the ground up. Since then GRUB 2 (actually at time of update 1.97) has grown more stable and even hobby operating systems use of the new bootloader instead of GRUB Legacy.

Features

  • Basic scripting support
  • GUI (better bootsplash support, custom colors, custom themes, ...)
  • Memory management
  • Cleaner design
  • Better portability
  • Internationalization
  • Rescue mode

A complete list of features can be found on the GNU mailing lists [1]

Using GRUB to boot your OS

Complete example code for booting your operating system with GRUB can be found in the Bare Bones tutorial. The general idea is that you want to create a file that has Multiboot header which GRUB can use to identity your program as a kernel and boot it.

Upgrading from GRUB Legacy

WARNING: These steps have not been tested very well yet. Use at your own risk!

Since modern GRUB 2 is very different from GRUB Legacy, the directions for getting your kernel up and running are different. Modern GRUB differs from GRUB Legacy in that to implement all but the most basic functionality, the user must load so-called "modules": little bits of code that add components (e.g. a different file system or a VGA font). This section gives you an overview of the process you need to go through when you want to have GRUB 2 load your kernel. It's actually rather simple to create a GRUB 2 image (assuming you have GRUB 2 either built or installed):

ISO instructions

There have been a lot of tries to make Grub2 work good with ISOs, but mostly failed. The only combination of commands that seems to work is the following.

First create a directory tree called "iso", where you put your kernel (and any other needed files) somewhere. Then in the boot/grub subdirectory create the grub.cfg file which is your configuration.

Now run:

grub-mkrescue -o bootable.iso iso

Be sure that your grub.cfg is syntactically correct. A common mistake is to put the menuentry brace on newline. It must be like:

menuentry "Place your os name here" {
}

grub-mkrescue depends on program xorriso with version 0.5.6 or higher.

If you cannot get it as binary (possibly from a package named "libisoburn") then get the all-in-one source tarball from GNU xorriso homepage. GNU xorriso can be used where it gets built, without further installation:

 grub-mkrescue --xorriso=/...full.path.../xorriso/xorriso -o bootable.iso iso

Floppy instructions

mkdir tmp
grub-mkimage -p /boot -o tmp/core.img multiboot sh fat # This should work... I hope :D

Explanation

Let's go through those grub-mkimage options:

-p By default, GRUB 2 looks in /boot/grub for its configuration file. -p changes this.
-o Like so many other GNU tools, grub-mkimage uses -o to set the output file. By default, it's stdout.
multiboot This module is required to load multiboot-compliant kernels.
biosdisk This module is required for GRUB 2 to be able to boot from a LiveCD.
iso9660/fat Allows GRUB 2 to look on the image for different files.
sh This module allows GRUB to parse the configuration file.

GRUB 2, like GRUB Legacy, needs a configuration file to find your kernel. In GRUB Legacy it's called menu.lst, but in GRUB2, it's called grub.cfg. The syntax for the configuration file is also a bit different.

Here's a sample configuration file (NOTE: This file should be placed into the /boot/grub folder of your disk image, and be named grub.cfg):

set timeout=15
set default=0 # Set the default menu entry
 
menuentry "OS Name" {
   # The multiboot command replaces the kernel command
   # For multiboot v2, use the multiboot2 command
   multiboot /boot/kernel-file
   boot
}

That's basically it. Copy these files to a disk image, pop it in an emulator, and you're done!

Double check that you put the brace on the same line of "menuentry". It can't be on a new line. This is not C.

USB instructions

Fewer and fewer systems have a floppy disc controller these days, but USB ports are found on all. Modern BIOSes can boot from a USB device, usually by pressing some special key during startup.

Putting GRUB 2 on a bootable USB storage device is a nice way to experiment with your OS on different computers. Here's how you set this up (using Linux):

1. Create a FAT32-formatted USB disk, without partitions:

Warning: the following command uses superuser privileges (sudo). E.g. just typing the wrong character for X could cause severe troubles for your current system

sudo mkfs.vfat -F 32 -n YourLabel -I /dev/sdX
(where sdX is your USB device)

The "-I" option is needed because we are targeting a partition-less device

2. Remove your USB device, and plug it back in. The auto-mounter on your OS should detect it now.

3. Invoke grub-install (on some systems this command is called grub2-install, located under /usr/sbin or /usr/local/sbin)

sudo grub-install --root-directory=/media/YourLabel --no-floppy --recheck --force /dev/sdX

It is important to do this as root (or sudo), else the generated device.map listing available boot devices can be empty. /media/YourLabel is the mount point under Fedora 16, it may be different for other distributions.

4. Create a grub.cfg for your kernel (see above), and copy it to your new bootable USB disk

Disk image instructions

For a more detailed tutorial, see bootable disk creation.

Hobby operating systems don't have to use real devices when running on virtual machines (although it may be, and it usually is faster). Creating bootable GRUB disk image is similar to installing it on USB devices, but here you're working with image itself and partition device at once.

1. Create new disk image file

$ dd if=/dev/zero of=disk.img bs=512 count=131072
131072+0 records in
131072+0 records out
67108864 bytes (67 MB) copied, 0.349436 s, 192 MB/s

2. Create new DOS partition table with bootable entry for your filesystem

$ fdisk disk.img

Add new partition, starting at 1MB (2048th sector). This is more space than GRUB actually needs.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-131071, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-131071, default 131071): 
Using default value 131071

Make it bootable

Command (m for help): a 
Partition number (1-4): 1

Write the new partition table to disk

Command (m for help): w
The partition table has been altered!

Syncing disks.

3. Setup two loop devices. One will be used for writing GRUB and its additional codes to MBR, and the second will be used for mounting filesystem of your operating system.

$ sudo losetup /dev/loop0 disk.img
$ sudo losetup /dev/loop1 disk.img -o 1048576

-o option defines offset from start of the file. Number 1048576 is actually 1024^2 = 1MB and that's the start of your partition.

4. Format your partition You can simply use [any supported filesystem] like ext2 or FAT32.

$ sudo mke2fs /dev/loop1
$ sudo mkdosfs -F32 -f 2 /dev/loop1

5. Mount your newly formatted partition

$ sudo mount /dev/loop1 /mnt

Note that if you tried to mount your first loop device which doesn't have any offset set, you would be requested to specify filesystem and even if you did it, you wouldn't get the expected result.

7. Install GRUB using grub-install

sudo grub-install --root-directory=/mnt --no-floppy --modules="normal part_msdos ext2 multiboot biosdev" /dev/loop0

If you mistyped /dev/loop1 (pointing on your partition) instead of /dev/loop0 (pointing on your MBR), you would receive message that grub-install can't use 'embedding' (because there's no space for it) and that it would like to use 'block lists', which are not recomended.

Don't forget to flush the filesystem buffers when manipulating with files on mounted disk image. On a Unix-like system, this can be simply done by executing the sync program in your shell.

HDD Image Instructions for OS X users

It might be useful to create an image file for a HDD; The following instructions help you create a HDD with an MBR partition map. Based on information from <palk> on #osdev, it is slightly more complicated to create a HDD image than you'd expect.

This information might not be applicable to Linux users, who will most probably want to use a loopback device. This is for developers on OS X, which doesn't have a loopback device and has a finicky image mounter.

1. First, create a blank, raw image with DD, with the required size. Here, I'll make a 80MB image -- 163840 sectors of 512 bytes.

dd if=/dev/zero of=disk.img count=163840 bs=512

2. For an explaination on calculating the CHS values from the LBA / size see Floppy_Disk_Controller#CHS

   Since we have a 80MB disk, the CHS values are 78, 32 and 63 respectively.

3. Fire up FDISK (or your tool of choice) -- I'm using the OS X version here, so commands may differ. The concept is essentially identical.

What will show on your screen (in OS X anyway) is on the left, what you enter is on the right.

fdisk -e disk.img
 
 
The signature for this MBR is invalid.
Would you like to initialize the partition table? [y]           Yes
fdisk:*1>                                                       disk
Disk: disk.img	geometry: 650/4/63 [16340 sectors]
Change disk geometry? [n]                                       No
 
 
 
fdisk:*1>                                                       edit 1
Partition id ('0' to disable)  [0 - FF]: [B] (? for help)       0B
Do you wish to edit in CHS mode? [n]                            No
Partition offset [0 - 163840]: [63]                             2047
Partition size [1 - 161793]: [161793]                           <Enter>
 
 
fdisk:*1>                                                       write
fdisk:*1>                                                       quit

4. Now that the MBR Partition Table is initialised, you'll want to make a Filesystem on the disk. But first.

   Here, we separate the MBR bit, and the actual FS bit.
dd if=disk.img of=mbr.img bs=512 count=2047
dd if=disk.img of=fs.img bs=512 skip=2047

5. Because we're on OS X, we need to attach the disk image first, without actually mounting it.

hdiutil attach -nomount fs.img

6. Use 'diskutil list' to find out which device your image is, use that below.

7. Now, make a FAT12/16/32 filesystem on the FS.img disk. Remember, use FS.img -- not disk.img

newfs_msdos -F 32 /dev/diskX

8. Now you'll want to unmount it, then recombine the two images, then install GRUB.

hdiutil detach /dev/diskX
cat mbr.img fs.img > disk.img
 
hdiutil attach disk.img
# note the mount point here (/Volumes/NO NAME, probably)
 
grub-install --modules="part_msdos biosdisk fat multiboot configfile" --root-directory="/Volumes/NO NAME" disk.img
Installation finished. No error reported.

And there it is! You know have a disk.img, that will have GRUB 2 installed, ready to go. It should be mountable in OS X simply by double clicking (or with the mount command). Enjoy!

Additional useful options

Whatever device you are using, you may want to have a PC partition table and create a partition you format in one of the filesystems supported by GRUB. If you do, be sure to add the following option the grub-install arguments:

--modules="part_msdos"

In general, if GRUB 2 happens to fail to do what you want and you suspect that it needs some missing functionality, just try to add a module name you believe has the functionality you need to the --modules argument. The module files generally are in /boot/grub/i386-pc/.

Multiboot

Some versions of GRUB 2 like to put Multiboot modules in relatively high physical memory addresses, in contrast to GRUB-legacy which loaded them into low memory. Be careful when making your kernel work with GRUB 2 that it is not making any assumptions about where the Multiboot modules will appear.

When your kernel gets control, the machine state is defined as follows: Multiboot machine state. Your code should have minimal dependency on this initial state; for example, define your own GDT instead of relying on the GDT setup by GRUB.

Header

As the GRUB 2 manual puts it:

The primary requirement for GRUB is that it be compliant with the Multiboot Specification.

But the Multiboot header as used by older versions of GRUB 2 (field is present in GRUB 1.99 and newer) did not include the header_length field that is specified in the Multiboot 1.6 specification.

GRUB 2 also supports the old Multiboot 0.6.96 specification. It is possible to include both headers.

Installing GRUB 2 on OS X

The installation of GRUB 2 on OS X is a little tricky. The latest released version 2.00 (as of 7th of October, 2014) doesn't seem to work with any configuration. The developer team fixed this in the newer revisions.

Important: To build GRUB 2 so that it can produce a bootloader for your output target you need to have a compiler for that target. So for example, if you want a bootloader for i386-elf (as suggested in the bare bones) you'll need Binutils + a compiler for that target. This is required on OS X because the built-in LLVM doesn't know how to make i386-elf binaries. So you can either build a cross-compiler or create an OS-specific toolchain (recommended) for your target platform. You will need the cross-versions of GCC, objcopy, strip, nm and ranlib in step 4.

1. Clone the developer version of the sources:

git clone git://git.savannah.gnu.org/grub.git

(This was tested on revision: 77063f4cb672f423272db7e21ca448cf3de98dcf)

2. A tool named objconv is required, get it from:

https://github.com/vertis/objconv

Download sources, compile (see website for details) and make available in your PATH.

3. Run "autogen.sh" in the GRUB sources folder

4. Create a seperate build directory, switch to it, and run GRUB's configure script (insert your target-specific tools!):

../grub/configure --disable-werror TARGET_CC=i386-elf-gcc TARGET_OBJCOPY=i386-elf-objcopy \
TARGET_STRIP=i386-elf-strip TARGET_NM=i386-elf-nm TARGET_RANLIB=i386-elf-ranlib --target=i386-elf

5. Run "make" and "make install"

Now you have a working GRUB 2 that has the required files to build an image that boots on i386 platforms.

GRUB for UEFI

Compiling GRUB

Older GRUB versions are riddled with nasty bugs. As this probably includes the version from your package management, you should be compiling GRUB from source. As we're compiling for UEFI, you should pass the appropriate flags to configure. An example invocation might look something like this:

../grub-2.02~rc2/configure --prefix="$HOME/opt/grub" --target=x86_64 --with-platform=efi

After completing the build, GRUB refused to do anything as it was missing a font file. To fix this, run

bin/grub-mkfont -o share/grub/unicode.pf2 /usr/share/fonts/truetype/unifont/unifont.ttf

GRUB might warn you about share/locale/ missing. To solution is to create the missing directory.

Building a GRUB UEFI binary (BOOTX64.EFI)

This method builds a standalone GRUB binary you can copy to a FAT partition. However, note that some UEFI implementations assume that it is located at /EFI/BOOT/BOOTX64.EFI for x86_64 platforms.

Generally, all compiled modules are included in this binary; if you want to cut down on its size, you can specify what modules to include.

This method uses two separate configuration files; this is needed as all GRUB data is located within the binary, but we'll be working around that. The binary contains a memdisk, which also serves as the prefix. As far as I'm aware, there's no way of working around this fact directly. We can, however, use the memdisk grub.cfg to load a configuration file from the disk. This way, we don't have to recreate the binary for every configuration change.

Start off with creating the memdisk grub.cfg. I saved it at build/grub.cfg (we'll need this path later on):

insmod part_msdos
configfile (hd0,msdos1)/boot/grub/grub.cfg

All this does is loading the module for reading the disk partitions (line 1) and loading the configuration file from the disk (line 2). Note that it doesn't have to be located at /boot/grub/grub.cfg; you can change this path on the disk to whatever you like, just remember to apply the changes to the memdisk grub.cfg.

The second grub.cfg (the one on the disk) is pretty much up to you, except that you have to load the part_msdos module and set the root (which by default is memdisk):

insmod part_msdos
set root=(hd0,msdos1)

Add your regular GRUB2 configuration below the set root line.

Finally, all that's left is to create the binary. Note that you have to specify what file to include at what path in the memdisk:

bin/grub-mkstandalone -O x86_64-efi -o BOOTX64.EFI "boot/grub/grub.cfg=build/grub.cfg"

Blessing the binary on macOS

Macs require bootable binaries to be 'blessed' by a utility:

bless --verbose --folder=/Volumes/EFI --file=/Volumes/EFI/EFI/BOOT/BOOTX64.EFI --setBoot

See Also

Articles

External Links

Personal tools
Namespaces
Variants
Actions
Navigation
About
Toolbox
In other languages