Grub2

From Gentoo Linux Wiki

Jump to: navigation, search

GRUB 2 has been rewritten from scratch to clean up everything for modularity and portability.

Please feel free to list working or non-working archs here. You can also check bugs.gentoo.org for any relevant bugs according to your arch.

Thanks goes to Spanky for his time spent modifying & finishing the Grub2 ebuild, including, submitting into Portage within a timely manner.

Warning: Using SVN/CVS ebuilds is considered experimental and may break your system. Please read the #Recovering from failed booting prior to upgrading to Grub2.
Note: But if you're even bothering to read this, why worry about breakage? Go for it! This is probably why you're using Gentoo to begin with! ;-)

Contents

[edit] Installing the Grub2 Package

If you already have grub-0.97* stable series installed, keep it installed. And install Grub2 into another slot. This way, if Grub2 fails for you, you can boot with the Gentoo Install CD-Rom and reinstall the MBR using the grub-0.97* working binaries.

# echo "sys-boot/grub multislot" >> /etc/portage/package.use

Decide which version of Grub2 you want. You should use at least version >=1.97.1, especially if you are using EXT4 for your root device, since grub-1.96* is over a year old. Alternatively you can choose the svn version (aka grub-9999). Unmask & mark stable within your portage configuration files as needed.

# echo "=sys-boot/grub-9999" >> /etc/portage/package.unmask # echo "=sys-boot/grub-9999 **" >> /etc/portage/package.keywords
Note: Please remember to mount your /boot partition if it is not mounted.

And then install the Grub package:

# emerge "=sys-boot/grub-9999"

[edit] Grub2 configuration files

[edit] Config syntax changes

Grub2 has has a slightly altered configuration file layout, including renumbering of partitions, which now start at 1 like linux partitions (but not drives, which still start at 0).

So you need to migrate your old configuration from /boot/grub/menu.lst to the new Grub2 /boot/grub/grub.cfg file. Rest assured, you can leave all your previous /boot/grub files from the previous version as they shouldn't interfere with Grub2 and you can easily downgrade if need be.

The easiest method of learning the new configuration syntax is just using grub-mkconfig documented in the next section. And then, just manually convert your old menu.lst file to your own grub.cfg file, after reading the automatically generated grub.cfg and realizing the minor changes.

[edit] Automatic config generator

The following will automagically generate a Grub configuration file from the presence of your built kernels within your /boot folder, using the autoconfiguration scripts in /etc/grub.d.

# /sbin/grub-mkconfig -o /boot/grub/grub.cfg

The default files int /etc/grub.d/ are 00_header, 10_linux (the one that detects your built kernels), 30_os-prober and 40_custom. Note that currently, 10_linux requires that your kernels are named /boot/kernel-* or /boot/vmlinu[xz]-*. 30_os-prober requires that sys-boot/os-prober be installed.

If you want to add some other operative system to the bootlist, you should create a script like 11_Other_OS that prints out your desired configuration, and give it exec permission by executing:

# nano /etc/grub.d/11_Other_OS
# chmod a+x /etc/grub.d/11_Other_OS

The number in the front specifies the booting order for that entry.

Note: A simpler automatic method exists for those who want to test it: sys-boot/os-prober

More grub2 variables are set in /etc/default/grub:

# /etc/default/grub
# If you change this file, run 'grub-mkconfig -o /boot/grub/grub.cfg' afterwards to update
# /boot/grub/grub.cfg.

GRUB_DEFAULT=0
GRUB_HIDDEN_TIMEOUT=0
GRUB_HIDDEN_TIMEOUT_QUIET=true
GRUB_TIMEOUT=10
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
GRUB_CMDLINE_LINUX=""

# Uncomment to disable graphical terminal (grub-pc only)
#GRUB_TERMINAL=console

# The resolution used on graphical terminal
# note that you can use only modes which your graphic card supports via VBE
# you can see them in real GRUB with the command `vbeinfo'
#GRUB_GFXMODE=640x480

# Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux
#GRUB_DISABLE_LINUX_UUID=true

# Uncomment to disable generation of recovery mode menu entrys
#GRUB_DISABLE_LINUX_RECOVERY="true"

[edit] Windows entry for Grub2 config

Currently, a lot of documentation omits Windows configuration.

[edit] Automatic Configuration

For automatic setup of windows chainloading, see: os-prober

[edit] Manual Configuration

As an example, let's consider Windows XP being installed on the first partition of the second harddrive. Remember, Grub2 has renumbered its drive scheming making drive numbering more confusing.

hd0,1 = First Hard Drive, First Partition. (/dev/sda1)
hd0,2 = First Hard Drive, Second Partition. (/dev/sda2)
hd1,1 = Second Hard Drive, First Partition. (/dev/sdb1)
hd1,2 = Second Hard Drive, Second Partition. (/dev/sdb2)

The following has been reported to work:

Code: /boot/grub/grub.cfg
menuentry "Microsoft Windows XP" {
        set root=(hd1,1)
        devicemap -s hd0 hd1
        chainloader +1
}
Note: devicemap might be drivemap, same command line.
Note: The remapping isn't needed if Windows is installed on the first hard drive's first partition.
Note: You may get a disappointing 'command not found' error for 'chainloader'. That's because you're discovering that Grub2 is modularized. Use the 'insmod chain' command if you need to chain-load any external bootloader.
Note: If Windows is installed on its own disk with its own MBR, then target the drives MBR with
Code: /boot/grub/grub.cfg
set root=(hd1)

[edit] Booting Memtest86+

Because memtest86+ behaves like a netbsd kernel, we have to load it this way, e.g.:

Code: /boot/grub/grub.cfg
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:

Code: /boot/grub/grub.cfg
menuentry "Memtest86+" {
        linux16 /boot/memtest86plus/memtest.bin
}

[edit] os-prober

Users of grub2 on debian or ubuntu variants may recall that in these systems, grub2 can automagically detect other OS you may have installed on your computer. This functionality depends on the os-prober package. sys-boot/os-prober is currently in testing for amd64 and x86 (~) so you will need to unmask it to use it. for x86:

# echo "sys-boot/os-prober ~x86" >> /etc/portage/package.use

for amd64:

# echo "sys-boot/os-prober ~amd64" >> /etc/portage/package.use

Since grub2 installs a script to hook into os-prober by default, no more configuration should be needed for it to work. Now, when grub-mkconfig is run, it should call os-prober and create entries for any OS it can find.

Note: In order for os-prober to detect an OS, it must be able to read the filesystem on which it lives. For example, if you want it to find a windows install, you need to be able to read NTFS which can be accomplished through use of ntgs3g

[edit] Chainloading GRUB2 from GRUB

GRUB2 includes a boot image that's loadable from GRUB Legacy, so you can try it out without wiping out your existing, working MBR. (Original info here).

To set up GRUB2 without actually writing to the MBR, run grub2-install like this:

grub2-install --grub-setup=/bin/true /dev/sda

This uses /bin/true in place of the command that would write the MBR, so it's prevented from doing so.

After this is done, you can point GRUB to /boot/grub/core.img as though it was a normal Linux kernel:

title=GRUB 2 test
root (hd0,0)
kernel /boot/grub/core.img

[edit] Installing Grub2 into MBR

As usual, you probably want Grub2 handling booting for all your operating systems on all your drives:

# grub2-install /dev/sda
Note: If you get the following error:
/usr/sbin/grub-probe: error: unknown filesystem type
Auto-detection of a filesystem module failed.
Please specify the module with the option '--modules' explicitly.

Then you may be helped by applying this patch: [PATCH] Fix a special case when accessing partition on linux.

Fix me: Remove this when the patch is merged.

[edit] Verify installed bootload using Qemu

If you have qemu installed, you can test whether your boot loader will load or not using:

# sync ; echo 3 > /proc/sys/vm/drop_caches ; qemu -hda /dev/sda

[edit] Recovering from failed booting

[edit] Normal recovery with Multislot USE Flag enabled

Note: Recommend a hard copy of this section prior to rebooting.

If booting fails, boot using the Gentoo Install CD-Rom.

/dev/sda2 == My partition containing my root filesystem containing the /boot folder. (For those with a separate /boot folder, you'll need to also mount it once the root file system is mounted.)

# mount /dev/sda2 /mnt/gentoo # grub-install --root-directory=/mnt/gentoo /dev/sda # reboot

If you still encounter errors installing, try using the Grub command line tool. There are notes within the next section documenting it's simple usage and usually always works for me if the above fails.

[edit] Recovery with multislot USE Flag disabled

Note: 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 grub-9999 and then install of grub-0.97* using the Gentoo Install CD-Rom.

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*)

# quickpkg grub

Boot with the Gentoo Install CD-Rom and setup a chroot. Then, unmerge grub and install a stable version. Finally, install it

(chroot) # emerge -C grub (chroot) # emerge =sys-boot/grub-0.97-r6 (chroot) # grub-setup /dev/sda

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.

# emerge /usr/portage/packages/All/grub-0.97-r6.tbz2

Still fails?

# cd / # tar -jxvf /usr/portage/packages/All/grub-0.97-r6.tbz2

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 the bootloader into MBR:

# grub

And then you're going to issue two commands into the Grub commandline tool:

grub> root (hd0,1)  (You're designating the partition containing your your /boot folder or /dev/sda2 here. /dev/sda1 is swap on my drive.)
grub> setup (hd0)  (This tells grub to install the bootloader into the first drive, usually called /dev/sda nowadays.)

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!

If you get any other errors, you're SOL or hopefully somebody adds to this wiki!

[edit] Setting up a graphical theme

Grub2 lets you set up backgrounds with few image file format restrictions by using modules like png, etc.

Note: I had some problems to get the fonts via portage. media-fonts/unifont and media-fonts/pcf2bdf did not work for me. Perhaps somebody knows a better way.

The first thing you need is to get the fonts. The Unifont.bdf file is downloadable at Unifoundry.

You can find a detailed description of converting BDF-font to PF2 at Grub Wiki. For converting all glyphs from unifont.bdf to pf2 format use the following commands

grub-mkfont --output=unifont.pf2 unifont.bdf

and for converting only ASCII characters use

grub-mkfont --output=ascii.pf2 --range=0x0-0x7f unifont.bdf

Now you can copy the generated PF2-fonts to your /boot/grub directory.

Next thing is editing the grub configuration file to

  • load the font
  • setting resultions with some fallback
  • loading the nessessary modules
  • starting the graphic mode also with fallback
Code: /boot/grub/grub.cfg
if loadfont /boot/grub/unifont.pf2 ; then
  set gfxmode="1280x1024,1024x768,800x600,640x480"
  insmod gfxterm
  insmod vbe
  terminal_output gfxterm
  if terminal_output gfxterm; then true ; else
    terminal gfxterm
  fi
fi

Finally you can set your prefered background image as jpeg, png or tga by loading the corresponding module.

insmod png
background_image /boot/grub/gentoo.png 

[edit] Other Problems

[edit] Broken Ebuild

If the Ebuild is broken, you may have to modify the current ebuild to show the following. (2008.01.03)

Keep in mind, any changes to the Ebuild require making a new digest:

# ebuild /usr/portage/sys-boot/grub/grub-9999.ebuild digest && emerge =sys-boot/grub-9999

[edit] Using grub-setup still causes booting into rescue mode

File: /usr/portage/sys-boot/grub/grub-9999.ebuild
- QA_EXECSTACK="sbin/grub-probe sbin/grub-setup"


[edit] SVN ./configure not executable error

You may have trouble with the configure script not being executable. If this is the case, modify the ebuild:

File: /usr/portage/sys-boot/grub/grub-9999.ebuild
src_compile() {
    use custom-cflags || unset CFLAGS CPPFLAGS LDFLAGS
    use static && append-ldflags -static

+   chmod +x configure

[edit] External links

Personal tools