Tuesday, October 11, 2011

How to add a new entry into grub2 of ubuntu

Latest versions of ubuntu use grub2 instead of menu.lst to manage boot options. In grub2, all the boot items are stored in the directory "/etc/grub.d/". Open this directory, we can find some default boot items like: 00_header        10_linux       30_os-prober, etc. Following the instructions in the README file, we can add our own boot item just use the prefix "40_" or larger number. The number of this prefix determines in which order our boot item will be loaded. For eg. we can create a new file titled "40_linux-2.6.34" which means a boot item using the kernel of version 2.6.34.

In the new created file, we can add the boot information following the instruction of the default "40_custom". It is like this,

#!/bin/sh
exec tail -n +3 $0
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.
echo "Adding Custom Kernel & SystemRescue" >&2
cat << EOF
menuentry "linux 2.6.34" {
set root=(hd0,1)
linux /boot/vmlinuz-2.6.34 root=/dev/sda1
initrd /boot/initrd.img-2.6.34
}
There are three highlighted lines of this file.
  • In the first line, we specify the location of the kernel image. "hd0" here is the drive name and "1" indicates the image is stored in the first (NOT SECOND) partition.
  • In the second line, we specify the directory in which the image file are located and again, specify the root location. Note here we use /dev/sda1 instead of hd0,1 again. If you are compiling a new kernel, use the command "make bzImage" to create a image for your compiled kernel.
  • In the third line, we specify the location of the initrd file, which includes information of all drivers the new kernel uses. Upon finishing compiling your kernel, use the command "mkintrd -o /boot/initrd.img-kernelname" to make an initrd file. In some new version of ubuntu, this command does not work and then you might have use "mkiniramfs" instead of "mkinitrd". These two commands work in most cases but sometimes while trying boot from your own item, you might get error like this "modprobe: FATAL: Could not load /lib/modules/2.6.8.custem/modules.deb, no such file or directory.". Then you could use another wonderful command "update-initramfs -c -k 2.6.20" by which you could update your owned created initrd file.
  • -c says: create (a inintramfs)
    -k XYZ says, for whitch Kernel the initramfs should be created.
After fixing all annoying booting problems, you could boot your ubuntu from your own boot item.


No comments:

Post a Comment