Grub2
From Gentoo Linux Wiki
GRUB2 has been rewritten from scratch to clean up everything for modularity and portability.
Contents |
[edit] Installation
Make sure that your /boot partition is mounted before proceeding. If you already have sys-boot/grub-0.97* version installed, you can keep it and install GRUB2 into another slot. This way, if GRUB2 fails, you can boot with a LiveCD and reinstall the MBR using the sys-boot/grub-0.97* working binaries. To do this GRUB needs be emerged with the multislot USE flag enabled.
As the GRUB2 ebuild are masked ** you first need to unmask it(the live(-9999) ebuild is also masked ** but not hardmasked so you need to make sure that isn't included as below):
Now emerge GRUB2:
To have GRUB2 do OS probing you also need to emerge sys-boot/os-prober. See #os-prober below.
[edit] Configuration
For starters, the config files and syntax of GRUB2 is not compatible with GRUB1. GRUB2 does however come with a nice automatic configuration generating tool, grub-mkconfig.
[edit] Syntax
GRUB2 has has a slightly altered configuration syntax, including the numbering of partitions, which now start at 1, like linux partitions, but not drives, which still start at 0. So /dev/sdb3 in a GRUB2 configuration file would be (hd1,3).
[edit] grub-mkconfig
The following will automatically generate a GRUB2 configuration file including kernels images within your /boot folder, using the auto configuration scripts in /etc/grub.d, the -o specifices an output file, here the default, /boot/grub/grub.cfg:
The default files in /etc/grub.d/ are 00_header, 10_linux, 30_os-prober and 40_custom. Note that currently, the 10_linux script requires that your kernels are name start with kernel- or vmlinu[xz]. 30_os-prober requires sys-boot/os-prober to be installed, if not it does nothing.
[edit] Theme
Grub2 lets you set up backgrounds with few image file format restrictions by using modules like png, etc. and fonts. For fonts to work you need to emerge GRUB2 with the truetype USE flag enabled.
In this example we'll use the UniFont located at the Unifoundry. You can find a detailed description of converting BDF-font to PF2 in the Grub Wiki. For converting all glyphs from unifont.bdf to pf2 format use the following commands
For converting only ASCII characters use:
Now you can copy the generated PF2-fonts to your /boot/grub directory.
In the snippet below we tell GRUB2 to try to load the font /boot/grub/unifont.pf2 (the font we created earlier), if it succeeds, set the resolution to 1280x1024, failing that it should try the next in line and finally set the terminal to graphical mode. Note that the snippet begin with loading the required modules, here vbe and gfxterm.
... insmod gfxterm insmod vbe if loadfont /boot/grub/unifont.pf2 ; then set gfxmode="1280x1024,1024x768,800x600,640x480" terminal_output gfxterm if terminal_output gfxterm; then true ; else terminal gfxterm fi fi ...
You can also set your preferred background image as jpeg, png or tga by loading the corresponding module.
insmod png background_image /boot/grub/my_background.png
[edit] Microsoft Windows Entry
As an example, let's consider Windows XP being installed on the first partition of the second harddrive:
menuentry "Microsoft Windows XP" {
insmod chain
set root=(hd1,1)
devicemap -s hd0 hd1
chainloader +1
}
The remapping isn't needed if Windows is installed on the first hard drive's first partition(that's /dev/sda1 or in GRUB2 terms, (hd0,1). Also note that if Windows has its own MBR on a disk you should only set the root to the disk, e.g:
set root=(hd1)
[edit] Windows in Hibernation Entry
The scripting features of grub2 make it possible to do various checks before displaying the grub menu. It is for example possible to determine on a dual boot computer if Windows is in hibernation (which is useful if some partition are shared between Linux and Windows: Windows partitions should be mounted read-only when it is in hibernation, to avoid data corruption).
To tell if Windows is in hibernation, one simply has to check the 4 first bytes of the {{Path|hiberfil.sys} file on the Windows system drive: they are equal to "hibr" if windows is in hibernation. Unfortunately, there does not seem to be any feature in grub2 to store a part of a file in a variable, but a simple patch to the hexdump function can do the trick! Unpack the grub-1.98 sources:
Edit the file lib/hexdump.c to insert the following lines at the very begining of the hexdump function:
char savebuf[5];
savebuf[0] = buf[0];
savebuf[1] = buf[1];
savebuf[2] = buf[2];
savebuf[3] = buf[3];
savebuf[4] = 0;
grub_env_set ("savebuf", savebuf);
Now recompile the modified grub-1.98:
And install it:
Now, everytime you call the hexdump function from grub.cfg, the savebuf variable will contain the 4 first bytes of the dump. Now simply replace your Windows menuentry with the following lines:
insmod ntfs
insmod hexdump
set name="Windows XP"
hexdump -s 0 -n 4 (hd0,2)/hiberfil.sys
if [ $savebuf == hibr ] ; then
name="Windows XP (in hibernation)" ;
fi
menuentry $name {
insmod chain
set root=(hd0,2)
chainloader +1
}
Of course, you should replace (hd0,2) with the name of your system Windows partition.
[edit] Memtest86+ Entry
Because memtest86+ behaves like a netbsd kernel, we have to load it this way, e.g.:
menuentry "Memtest86+" {
insmod bsd
netbsd /boot/memtest86plus/memtest.bin
}
If this does not work and you obtain a message such as "error: invalid a.out header", try:
menuentry "Memtest86+" {
linux16 /boot/memtest86plus/memtest.bin
}
[edit] Finalizing
As usual, you probably want Grub2 handling booting for all your operating systems on all your drives:
[edit] Verifying (qemu)
If you have qemu installed, you can test whether your boot loader will load or not using:
[edit] Troubleshooting
[edit] Chainloading to GRUB2
GRUB2 includes a boot image that's loadable from GRUB Legacy, so you can try it out without wiping out your existing, working MBR. To set up GRUB2 without actually writing to the MBR, run grub-install (or grub2-install if you installed grub2 with the multislot USE flag) like this:
This uses /bin/true in place of the command that would write the MBR, which fools GRUB into thinking it succeeded in doing so, thus preventing GRUB from ever actually writing anything.
After this is done, you can create a GRUB boot entry that points to /boot/grub/core.img as though it was a normal Linux kernel:
title=GRUB2 root (hd0,0) kernel /boot/grub/core.img
[edit] Recovery
If booting fails, boot using a LiveCD that ships with GRUB1, like the Gentoo LiveCD.
Mount the partition containing the root filesystem which contains the /boot folder (for those with a separate /boot partition, you'll need to also mount that once the root file system is mounted).
From the LiveCD, install GRUB1 into the MBR with the --root-directory pointing to your Gentoo systems root:
Upon success, reboot.
If you still encounter errors installing, try using the GRUB1 command line tool. There are notes within the next section documenting it's simple usage and usually always works if the above fails.
[edit] Without "multislot"
This is here in case you didn't use multislot, hence, keeping the stable grub-0.97* series installed. And you also didn't make a bootable Grub rescue floppy. Please enable multislot USE Flag for an easy & safe fall back option!
This scenario details a manual uninstall of GRUB and then the installation of grub-0.97* using the Gentoo LiveCD.
If booting fails, boot using the Gentoo Install CD-Rom. (Or, if you still have another boot option such as an old floppy drive, create a bootable floppy using /bin/grub-mkrescue prior to rebooting.)
As noted above prior to upgrading, it is recommended you make a binary package of grub-0.97*, it will be located within /usr/portage/packages/All/grub-0.97*)
Boot with the Gentoo Install CD-Rom and setup a chroot. Then, unmerge grub and install a stable version.
Finally, install it while in the chroot:
If emerge fails to install grub-0.97, but the tarball is present, the you can try to either emerge it directly or just untaring it from the top folder.
Still fails?
If you used tar, you'll need to re-emerge grub-0.97 version as tar doesn't perform any Portage Package operations including entering the package into the system database.
If grub-setup fails, try manually installing the bootloader into MBR:
And then you're going to issue two commands into the Grub command line, we here assume the root(/) in which /boot resides is /dev/sda2, if /boot is on its on partition it should point to that:
grub> root (hd0,1) grub> setup (hd0)
And, you should see the following:
grub> root (hd0,1) Filesystem type is ext2fs, partition type 0x83 grub> setup (hd0) Checking if "/boot/grub/stage1" exists... yes Checking if "/boot/grub/stage2" exists... yes Checking if "/boot/grub/e2fs_stage1_5" exists... yes Running "embed /boot/grub/e2fs_stage1_5 (hd0)"... 16 sectors are embedded. succeeded Running "install /boot/grub/stage1 (hd0) (hd0)1+16 p (hd0,1)/boot/grub/stage2 /boot/grub/menu.lst"... succeeded Done. grub> quit
Reboot and you should be back to using the old Grub and ready for another round of Grub2 attempt!
