Pages

Saturday, August 31, 2013

Replace lilo with grub2

As a Slackware user I use lilo as bootloader by default. But I also like to change things and try out new/other things. One is grub2 which I'll explain a little in this article. The topics of this article are:

Create a backup of your current boot loader
Download, compile and install grub2
Generate a basic configuration and install grub2 into MBR
Define a new timeout
Change the distributor from GNU/Linux to Slackware
Loading the kernel in framebuffer
Changing menu colors
Add a initrd

grub2 is really nice. But after all I switched back to lilo (no offense). I'm just too lazy to install grub2 on every Slackware system I own. Also I don't show everything that grub2 really can do. Take this article as a introduction to grub2 on Slackware.

Create a backup of your current boot loader

First create a backup of your current boot loader with dd:

# dd if=/dev/sda of=/root/mbr.bak bs=512 count=1

In case of everything fails, restore the boot load with dd again:

# dd if=/root/mbr.bak of=/dev/sda

Download, compile and install grub2

Start by creating a place to store the sources and change into the directory:

# mkdir /usr/src/grub2
# cd /usr/src/grub2


Then download the sources:

# wget -c "ftp://ftp.gnu.org/gnu/grub/grub-2.00.tar.gz"

Extract the source package, compile and install the binaries:

# tar xf grub-2.00.tar.gz
# cd grub-2.00
# ./configure --prefix=/ --libdir=/lib64 --datarootdir=/usr/share
# make && make install


That's it, the grub2 binaries have been installed.

Generate a basic configuration and install grub2 into MBR

The grub2 configuration file is saved in /boot/grub/grub.cfg but the directory does not exist. Just create it first:

# mkdir /boot/grub

Then run the grub-mkconfig command to auto generate a basic configuration:

# grub-mkconfig -o /boot/grub/grub.cfg
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-huge-3.2.29
Found linux image: /boot/vmlinuz-generic-3.2.29
done


To install grub2 into MBR run the grub-install command:

# grub-install /dev/sda
Installation finished. No error reported.


If you reboot now then grub2 will show up as your boot loader.
Beware: if you try to switch the boot loader from lilo to grub2 on a VM then turn off the VM. Don't just reboot! I had a little trouble with my Virtual Box VM when I just rebooted the VM. For a reboot run:

# shutdown -r now
...


And for turning off your machine run:

# shutdown -h now
...


After that grub should load and you should get a similiar menu like lilo:

...
GNU/Linux
Advanced options for GNU/Linux
...


The first entry GNU/Linux is auto selected and pressing enter will load your Slackware again. Otherwise just wait 5 seconds to auto load your Slackware.
In most cases grub-install has not be run again. grub2 has a configuration file /boot/grub/grub.cfg which can be changed various ways. While executing grub2 during boot it will read the contents of /boot/grub/grub.cfg. In normal cases changes will be made via editing /etc/default/grub. This file does not exist by default (it will be created during the next step). Depending on the options set in /etc/default/grub it will be parsed by the scripts in /etc/grub.d/ when running grub-mkconfig.

Define a new timeout

The default timeout is set to 5 seconds. To change the time to eg. 10 seconds edit /etc/default/grub and add the GRUB_TIMEOUT options:

# vi /etc/default/grub
GRUB_TIMEOUT=10


Then regenerate the configuration file:

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


Check that the new timeout is set in grub.cfg:

# grep timeout /boot/grub/grub.cfg
set timeout=10


After a reboot the timeout is set to 10 seconds.

Change the distributor from GNU/Linux to Slackware

I'm pretty sure that you've noticed that grub only offers GNU/Linux instead of Slackware for booting. To change it back to Slackware edit /etc/default/grub one more time and add the following line:

# vi /etc/default/grub
GRUB_TIMEOUT=10
GRUB_DISTRIBUTOR=Slackware


Then regenerate the configuration file:

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


After a reboot grub will offer you to boot Slackware GNU/Linux.
If you insist only to see the Slackware entry then edit /etc/grub.d/10_linux and change it like this:

# vi /etc/grub.d/10_linux
...
if [ "x${GRUB_DISTRIBUTOR}" = "x" ] ; then
  #OS=GNU/Linux
  OS=Slackware
else
  #OS="${GRUB_DISTRIBUTOR} GNU/Linux"
  OS="${GRUB_DISTRIBUTOR} Slackware"
  CLASS="--class $(echo ${GRUB_DISTRIBUTOR} | tr 'A-Z' 'a-z' | cut -d' ' -f1) ${CLASS}"
fi
...


Just comment out the two lines starting with OS and add two new lines with Slackware instead of GNU/Linux.
Then regenerate the configuration file:

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


After a reboot you will only see Slackware for booting.

Loading the kernel in framebuffer

With the above configurations the kernel will load in normal console mode. To load it in framebuffer again add the GRUB_GFXPAYLOAD_LINUX to /etc/default/grub:

# vi /etc/default/grub
GRUB_TIMEOUT=10
GRUB_DISTRIBUTOR=Slackware
GRUB_GFXPAYLOAD_LINUX=1024x786x32


Then regenerate the configuration file:

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


After a reboot the kernel will load in framebuffer displaying penguin(s) in the upper left corner again.

Changing menu colors

For changing the menu colors there is no default script shipped in /etc/grub.d/. What you need to do is to add the appropiate lines directly to eg. /etc/grub.d/40_custom:

# vi /etc/grub.d/40_custom
...
# Menu colors
set color_normal=blue/white
set menu_color_highlight=white/blue
set menu_color_normal=blue/white


Then regenerate the configuration file:

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


When you reboot then grub will appear with white background and blue font.

Add a initrd

If y ou need a initrd during boot then create one first like this:

# mkinitrd -o /boot/initrd-$(uname -r).img $(uname -r)
18761 blocks
/boot/initrd-3.2.29.img created.
Be sure to run lilo again if you use it.


This will create the initrd in /boot/initrd-3.2.29.img. A file like this (/boot/initrd-kernel.version.img) will be recognized by grub-mkconfig automatically. And again, regenerate the configuration file:

# grub-mkconfig -o /boot/grub/grub.cfg
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-huge-3.2.29
Found initrd image: /boot/initrd-3.2.29.img
Found linux image: /boot/vmlinuz-generic-3.2.29
Found initrd image: /boot/initrd-3.2.29.img
done


Note the lines above starting with Found initrd image... After a reboot grub2 will load the initrd.

Links:

https://wiki.archlinux.org/index.php/GRUB