Wednesday, April 4, 2012

Gentoo, Grub, and UUID

A guide I wrote so I can remember how to set up UUID booting on my Gentoo system running Grub. It is a simplified version of the Gentoo Initramfs guide. The following assumptions apply:

  • That initramfs is enabled in the kernel.
  • That all modules required to boot the system are compiled into the kernel.

Emerge a statically linked Busybox

$ echo sys-apps/busybox mdev static >> /etc/portage/package.use
$ emerge -a busybox

Create the ramfs image

$ echo sys-apps/busybox ipv6 pam mdev static >> /etc/portage/package.use
$ mkdir /usr/src/initramfs
$ cd /usr/src/initramfs
$ mkdir bin sbin proc sys mnt mnt/root
$ cp /bin/busybox bin/
fail_safe() {
        echo "Entering Shell"
        exec /bin/sh
}

find_and_mount_root() {
    for cmd in $(cat /proc/cmdline) ; do
        case $cmd in
        root=*)
            type=$(echo $cmd | cut -d= -f2)
            if [ $type == "LABEL" ] || [ $type == "UUID" ] ; then
                uuid=$(echo $cmd | cut -d= -f3)
                mount -o ro $(findfs "$type"="$uuid") /mnt/root
            else
                mount -o ro $(echo $cmd | cut -d= -f2) /mnt/root
            fi
            ;;
        esac
    done
}
#!/bin/busybox sh

. /bin/funcs

/bin/busybox --install -s

mount -t proc none /proc
mount -t sysfs none /sys

echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s

find_and_mount_root || fail_safe

umount /proc
umount /sys


exec switch_root /mnt/root /sbin/init
#!/bin/bash

BOOT=/boot/
MY_NAME=`basename $0`

make_it() {

        find .  \( ! -iname $MY_NAME \) -print0 | cpio --null -ov --format=newc | gzip -9 > $BOOT/initramfs.cpio.gz
}


mount $BOOT
cd $(cd  $(dirname $0) ; pwd -P) && make_it
$ chmod +x init
$ chmod +x make_initramfs
$ ./make_initramfs

Grub

If you are using /dev/sda1 as your root file system, you can find its UUID
ls /dev/disk/by-uuid/ -la
total 0
drwxr-xr-x 2 root root 240 Apr  3 19:32 .
drwxr-xr-x 6 root root 120 Apr  2 04:00 ..
lrwxrwxrwx 1 root root  10 Apr  2 04:00 550e8400-e29b-41d4-a716-446655440000 -> ../../sda1
# This is a sample grub.conf for use with Genkernel, per the Gentoo handbook
# http://www.gentoo.org/doc/en/handbook/handbook-x86.xml?part=1&chap=10#doc_chap2
# If you are not using Genkernel and you need help creating this file, you
# should consult the handbook. Alternatively, consult the grub.conf.sample that
# is included with the Grub documentation.

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

title Gentoo Linux kernel-3.2.1-gentoo-r2
root (hd0,0)
kernel /boot/kernel-3.2.1-gentoo-r2 root=UUID=550e8400-e29b-41d4-a716-446655440000
initrd /boot/initramfs.cpio.gz

Ok. All done. Reboot and enjoy!