Wednesday, May 25, 2011

GRUB2 howto

GRUB2 has a lot of new functions. His configuration has changed compared to the earlier version.
Here is described how to set up few basic things.


Very "confusing" feature the Grub2 has gained is the configuration change. Instead of changing the file /boot/grub/menu.lst is now used the file grub.cfg
If you open this file you will seen warning DO NOT EDIT THIS FILE. Despite this warning you can edit this file, the only problem is that this file will be overwritten everytime the new configuration will be generated (e.g. after kernel update).

The first configuration file we'll look at is the file /etc/default/grub. Here you can setup the following:

GRUB_DEFAULT=0 > this sets the default entry to bootin the menu
GRUB_TIMEOUT=3 > time the boot menu will be shown
GRUB_GFXMODE=800x600 > here you can setup the screen resolution for grub menu

Another interesting configuration file is /etc/grub.d/05_debian_theme. Here you can set the colors of text and background (if the monochromatic theme is used) and  you can set the backgroundpicture.
This file looks like this:

#!/bin/bash -e

source /usr/lib/grub/grub-mkconfig_lib

set_mono_theme()
{
    cat << EOF
    set menu_color_normal=white/black # set the text and background color
    set menu_color_highlight=yellow/black # set the color for highlighted entry in GRUB
EOF
}

# check for usable backgrounds
use_bg=false
if [ "$GRUB_TERMINAL_OUTPUT" = "gfxterm" ] ; then
    for i in {/boot/grub,/usr/share/images/desktop-base}/ubuntu2_fgh.{png,tga} ; do
    # here you can set the background picture
    # pick a picture (png or tga) copy it to the folder /boot/grub
    # or /usr/share/images/desktop-base. In my case it is /boot/grub
    # picture is named ubuntu2_fgh.png, this name is used in the GRUB configuration above
if is_path_readable_by_grub $i ; then
    bg=$i
    case ${bg} in
        *.png) reader=png ;;
        *.tga) reader=tga ;;
        *.jpg|*.jpeg) reader=jpeg ;;
    esac
if test -e /boot/grub/${reader}.mod ; then
    echo "Found Debian background: `basename ${bg}`" >&2
    use_bg=true
    break
fi
fi
done
fi

# set the background if possible
if ${use_bg} ; then
    prepare_grub_to_access_device `${grub_probe} --target=device ${bg}`
    cat << EOF
    insmod ${reader}
    if background_image `make_system_path_relative_to_its_root ${bg}` ; then
        set color_normal=black/black        # set the colors when the background
        set color_highlight=light-red/black # picture is used
else
EOF
fi

# otherwise, set a monochromatic theme for Ubuntu
if ${use_bg} ; then
    set_mono_theme | sed -e "s/^/ /g"
    echo "fi"
else
    set_mono_theme
fi

Next I show you how to add own entries to the menu. This is useful if you'd like to have your system on the first place.
In the directory /etc/grub.d/ create new file 06_custom (numbers at the beginning determine the order of executing the scripts).

This is my file:
#!/bin/sh -e
echo "Adding custom boot entry(s)" >&2
cat << EOF
menuentry "Windows 7" {
set root=(hd0,1)
chainloader +1
}
menuentry 'Ubuntu, with Linux 2.6.32-21-generic TEXT MODE' --class ubuntu --class gnu-linux --class gnu --class os {
recordfail
insmod xfs
set root='(hd0,3)'
search --no-floppy --fs-uuid --set 356e2afa-9792-4e62-8b28-286ff3eebd1a
linux /boot/vmlinuz-2.6.32-21-generic root=UUID=356e2afa-9792-4e62-8b28-286ff3eebd1a ro vga=792 quiet splash text
initrd /boot/initrd.img-2.6.32-21-generic
}
EOF


In the GRUB menu will now be Windows 7 entry on the first place and it will boot from the first partition. On the second place will be UBUNTU booting from the 3rd partition, and it'll boot into text mode (defined by the text parameter).
-menuentry Windows a Ubuntu are basically copied from the existing GRUB configuration (/boot/grub/grub.cfg).

At the end, when all is set up, you have to generate new configuration:
sudo update-grub
or sudo grub-mkconfig -o /boot/grub/grub.cfg
The output of this command:
Generating grub.cfg ...
Found Debian background: ubuntu2_fgh.png > the background was found
Adding custom boot entry(s) > output from the 06_custom file, we've created
Found linux image: /boot/vmlinuz-2.6.32-21-generic
Found initrd image: /boot/initrd.img-2.6.32-21-generic
Found memtest86+ image: /boot/memtest86+.bin
Found Windows 7 (loader) on /dev/sda1
done


NOTE: the file /boot/grub/grub.cfg has the attribute READ ONLY! in vim editor you can save the changes with command: ":w!"
When editing and creating scripts in /etc/grub.d and in /etc/default make sure that all scripts are executable (that they have 755 rights).

No comments:

Post a Comment