Grub2 on gentoo


Grub2 is finally declared as stable in gentoo portage. While optional, migration from grub:0 to grub2 is straightforward.

Installation of grub2 is done in four steps.

First inspect your existing grub.conf file and make sure naming is correct. Grub2 has strict naming requirements and wants kernel images to be named as kernel-${version} or vmlinuz-${version} and an initramfs must be named initramfs-${version}.img, initramfs-genkernel-${version}, initramfs-genkernel-${arch}-${version}, initrd-${version}.img, initrd.img-${version}, initrd-${version}.gz, or initrd-${version}. The kernel filenames must match a corresponding kernel that is available in /boot. See gentoo migration guide.

nano -w /boot/grub/grub.conf

default 0
timeout 5
splashimage=(hd0,0)/boot/grub/splash.xpm.gz

title GRUB2 Chainload
root (hd0,0)
kernel /boot/grub/i386-pc/core.img
boot

title Gentoo Linux 3.10.7-r1
root (hd0,0)
kernel /boot/kernel-3.10.7-gentoo-r1 root=/dev/sda2

title Windoz7
map (hd0,0) (hd1,0)
map (hd1,0) (hd0,0)
rootnoverify (hd1,0)
makeactive
chainloader +1

GRUB2 Chainload section is added in order to allow testing of GRUB2 configuration before adopting it permanently.

Secondly, install grub2 files on your /boot partition.

# grub2-install --grub-setup=/bin/true /dev/sda
Installation finished. No error reported.

Thirdly, scan for the existing kernels.

To probe for Windows, grub needs os-prober and ntfs3g packages
emerge -av os-prober ntfs3g


# grub2-mkconfig -o /boot/grub/grub.cfg
Generating grub.cfg ...
Found linux image: /boot/kernel-3.10.7-gentoo-r1
No volume groups found
Found Windows 7 (loader) on /dev/sdb1
done

grub2-mkconfig will create /boot/grub/grub.cfg. This file is automatically generated using templates from /etc/grub.d and settings from /etc/default/grub. If you intend to use automatic configuration, /boot/grub/grub.cfg shouldn’t be manually modified since it will be overwritten next time you run grub2 config.
NOTE: grub.cfg can be also manually configured on your own risk.

Finally, test the configuration by rebooting and selecting GRUB2 Chainload. If everything works as supposed to install grub2 and remove the grub:0.

mount /boot
grub2-install /dev/sda
grub2-mkconfig -o /boot/grub/grub.cfg
emerge -Ca sys-boot/grub:0

Upgrading/modifying kernel

Whenever you wish to make changes to kernel grub2 has to be reconfigured. Add kernel image to /boot/ and run grub2 configuration.

grub2-mkconfig -o /boot/grub/grub.cfg

Chanloading iso image with custom kernel arguments is described at . It involves creating /etc/grub.d/ rule.

nano -w /etc/grub.d/50_isofile

#!/bin/sh
exec tail -n +3 $0

menuentry "GRUB4DOS" {
	linux /grub4dos-0.4.4/grub.exe --config-file=/menu.lst
}

menuentry "SYSRESCUECD" {
	loopback loop /sysrecuecd-x86-2.3.1.iso
	linux  (loop)/isolinux/rescue64 nomodeset vga=791 docache setkmap=fr isoloop=/sysrecuecd-x86-2.3.1.iso
	initrd (loop)/isolinux/initram.igz
}

menuentry "STG3-AMD64" {
	set cmdline="dokeymap looptype=squashfs loop=/image.squashfs cdroot"
	loopback loop /stg3-amd64-.iso
	linux  (loop)/isolinux/gentoo $cmdline root=/dev/ram0 init=/linuxrc initrd=gentoo.igz
	initrd (loop)/isolinux/gentoo.igz
}

Make this rule file executable.

Grub2 with raid-1 disk

Prepare kernel usual way

cd /usr/src/linux
make && make modules_install
make install

To boot from raid, grub2 need additional stuff: add additional parameters to kernel and pass mdadm support via initramfs
nano -w /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT=”domdadm rootfstype=ext4″

Initframes

# emerge genkernel

To configure genkernel add your mdadm.conf file to your initrd with the “MDADM_CONFIG=” option in /etc/genkernel.conf

nano -w etc/genkernel.conf

MDADM_CONFIG="/etc/mdadm.conf"

If you need specific support in the initramfs, such as lvm or raid, add in the appropriate options to genkernel. See genkernel –help for more information, or the next example which enables support for LVM and software raid (mdadm):

# genkernel --lvm --mdadm --install initramfs

grub2 setup
grub2-mkconfig -o /boot/grub/grub.cfg
check result in /boot/grub/grub.cfg and compare raid device id with

blkid | grep /dev/md3