GRUB2
Kyle Rankin
Systems Architect
QuinStreet Inc.
Agenda
- Reinventing the Wheel
- What is GRUB?
- Why GRUB2?
- GRUB2 Features
- GRUB Legacy
- What's Changed?
- Syntax
- Configuration Files
- Scripts
- Group Therapy
Reinventing the Wheel
- The Open Source Promise:
- Code is shared
- Minimal duplication of effort
- Many eyes make bugs shallow
- The Open Source Reality:
- "That code is old and unmaintainable"
- "Let's start from scratch"
- Many eyes don't understand the original coder's program
- Kyle's Law: "Given enough time, the probability that an Open Source project will stick to its original code base goes to zero."
- Some examples: BIND, sendmail, GNOME, KDE, Enlightenment (still in beta), AmaroK, Galeon...
- and GRUB
What is GRUB?
- GRand Unified Bootloader
- Bootloader transitions from your BIOS to your operating system
- GRUB typically stored in MBR
- Can understand and navigate file systems
- Reads menu and configuration information from the file system
- GRUB displays a menu and ultimately loads your kernel and initrd or some other OS
- All configuration can be changed at boot
Why GRUB2?
- According to the maintainers:
- New feature requests against an old code base
- Newer hardware, boot environments to be supported
- Original GRUB code a decade old
- Unmaintainable going forward
GRUB2 Features
- New rescue mode
- Enhanced graphical menu and boot splash
- Themes
- Booting LiveCD ISOs
- Scripting support
- Full UUID support
- Support for non-x86 platforms (like PowerPC)
GRUB Legacy
- /boot/grub/menu.lst -- Core GRUB config file
- /usr/sbin/grub -- Core tool to install GRUB to boot media
- /usr/sbin/grub-install -- Front-end to ease GRUB installation
- /usr/sbin/update-grub -- Script to automate menu.lst changes
- At boot, hit Esc to edit GRUB configuration on the fly
What's Changed
- Everything?
- Kidding. Almost everything
- New config file: /boot/grub/grub.cfg
- Config file automatically generated by a series of scripts (no hand-editing)
- grub.cfg uses brand new syntax
- User configuration of grub.cfg split into a few files and scripts
- At boot, hit Shift to edit GRUB2 configuration on the fly
Syntax
- Completely new syntax in grub.cfg
- Partitions are numbered starting from 1 now
- Disks are still numbered starting from 0
- Confused yet?
- GRUB1: /dev/sda1 = (hd0,0)
- GRUB2: /dev/sda1 = (hd0,1)
- No I don't know why...
Configuration Files
/boot/grub/grub.cfg
- Core config file that GRUB2 reads when it boots
menuentry 'Ubuntu, with Linux 2.6.31-20-generic' --class ubuntu \
--class gnu-linux --class gnu --class os {
recordfail
insmod ext2
set root='(hd0,2)'
search --no-floppy --fs-uuid --set c7b6836f-ac57-47ed-9e7c-b16adbf8abed
linux /boot/vmlinuz-2.6.31-20-generic \
root=UUID=c7b6836f-ac57-47ed-9e7c-b16adbf8abed ro quiet splash
initrd /boot/initrd.img-2.6.31-20-generic
}
Compare to GRUB menu.lst:
title Ubuntu karmic (development branch), kernel 2.6.31-14-generic
uuid c7b6836f-ac57-47ed-9e7c-b16adbf8abed
kernel /boot/vmlinuz-2.6.31-14-generic \
root=UUID=c7b6836f-ac57-47ed-9e7c-b16adbf8abed ro quiet splash
initrd /boot/initrd.img-2.6.31-14-generic
quiet
Configuration Files
/etc/default/grub
- Controls common options a user might want to change:
GRUB_DEFAULT=0
GRUB_HIDDEN_TIMEOUT=0
GRUB_HIDDEN_TIMEOUT_QUIET=true
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
GRUB_CMDLINE_LINUX=""
Run update-grub after any changes
Configuration Files
/etc/grub.d/
- Various scripts to build grub.cfg:
$ ls /etc/grub.d/
00_header 10_linux 30_os-prober README
05_debian_theme 20_memtest86+ 40_custom
/etc/grub.d/ scripts execute in numerical order
Each script performs various tasks and outputs a grub.cfg-compatible configuration
Scripts
/etc/grub.d/20_memtest86+
if test -e /boot/memtest86+.bin ; then
MEMTESTPATH=$( make_system_path_relative_to_its_root "/boot/memtest86+.bin" )
echo "Found memtest86+ image: $MEMTESTPATH" >&2
cat << EOF
menuentry "Memory test (memtest86+)" {
EOF
prepare_boot_cache="$(prepare_grub_to_access_device ${GRUB_DEVICE_BOOT} | \
sed -e "s/^/\t/")"
printf '%s\n' "${prepare_boot_cache}"
cat << EOF
linux16 $MEMTESTPATH
}
menuentry "Memory test (memtest86+, serial console 115200)" {
EOF
printf '%s\n' "${prepare_boot_cache}"
cat << EOF
linux16 $MEMTESTPATH console=ttyS0,115200n8
}
EOF
fi
Scripts
- Order of script execution determines where that section appears
- Number custom scripts accordingly
- Ubuntu provides /etc/grub.d/40_custom for any custom scripts
- Could be as simple as a series of echo statements
- Run update-grub after any changes
Scripts
- Also other GRUB2 scripts on the system to manage GRUB
- /usr/sbin/grub-install -- Recommended way to install GRUB2 onto boot medium
- /usr/sbin/update-grub -- Recommended way to update grub.cfg. Really just a shell script that points to grub-mkconfig
- /usr/sbin/grub-mkconfig -- Does the heavy lifting to build grub.cfg. Runs grub.d scripts
Some Final Words
- Ubuntu defaults to GRUB2 since 9.10
- Presumably all distros will use it going forward
- GRUB2 no longer has stage 1.5 in the boot process
- New Ubuntu installs with no other OSes will hide GRUB2 menu
- Don't forget Shift instead of Esc to access GRUB2 menu