在ubuntu10.04中,启动菜单是由/boot/grub/grub.cfg文件决定的。打开该文件,在此文件的开头可以看见这样一句话# it is automatically generated by /usr/sbin/grub-mkconfig using templates from /etc/grub.d and setting from /etc/default/grub。这句话的大致意思是,/boot/grub/grub.cfg是通过 /etc/grub.d 作为模板、/etc/default/grub 作为配置,被 grub-mkconfig
命令自动生成的。在我之前发表的ubuntu10.04内核升级博文中所说的自动设置grub方法,即运行update-grub命令,本质上就是根据/etc/default/grub配置文件,用grub-mkconfig命令自动生成的。那么怎么修改配置文件呢?下图是/etc/default/grub文件中常用的变量:

其中的GRUB_DEFAULT表示默认的菜单项,各启动菜单的菜单号是从0开始依次加1的。
GRUB_HIDDEN_TIMEOUT=0被#注释掉后,系统在启动时会显示所有的启动菜单。
GRUB_TIMEOUT 为引导项列表自动选择超时时间。
根据自己的需要设置好这些变量以后,必须运行update-grub命令,这些配置参数才会更新到/boot/grub/grub.cfg文件中。
我在虚拟机中用这种方法进行grub配置以后,重启,系统在启动升级内核的时候,显示:VFS:Unable to mount fs on unknown-blocking(0,0),然后无法进入系统。这个错误一般都是因为找不到内存盘ramdisk的镜像文件。于是,我从原来版本的ubuntu内核进入系统,打开/boot/grub/grub.cfg文件,看到update后的该文件的升级内核菜单项:
menuentry 'Ubuntu,Linux 2.6.34.11' --class ubuntu --class gnu-linux --class gnu --class os {
recordfail
insmod ext2
set root='(hd0,1)'
search --no-floppy --fs-uuid --set 2e78226c-16d3-4da2-a35a-77e2146d6427
linux /boot/vmlinuz-2.6.34.11 root=/dev/sda1 ro quiet splash
}
我们只需加上一个initrd项(粗体显示),修改后的升级内核菜单项为:
menuentry 'Ubuntu,Linux 2.6.34.11' --class ubuntu --class gnu-linux --class gnu --class os {
recordfail
insmod ext2
set root='(hd0,1)'
search --no-floppy --fs-uuid --set 2e78226c-16d3-4da2-a35a-77e2146d6427
linux /boot/vmlinuz-2.6.34.11 root=/dev/sda1 ro quiet splash
initrd /boot/initrd-linux2.6.34.11
}
其中,initrd项就是指定内存盘ramdisk镜像文件路径。
PS:在添加initrd项之前,要先修改/boot/grub/grub.cfg的读写权限,才能对该文件进行修改。修改保存之后,新内核就能够正常启动啦啦啦~
另一种方法是手动更改/boot/grub/grub.cfg文件,打开该文件:
# DO NOT EDIT THIS FILE
#
# It is automatically generated by /usr/sbin/grub-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#
### BEGIN /etc/grub.d/00_header ###
if [ -s $prefix/grubenv ]; then
load_env
fi
#该default设置系统默认的菜单项
set default="0"
if [ ${prev_saved_entry} ]; then
set saved_entry=${prev_saved_entry}
save_env saved_entry
set prev_saved_entry=
save_env prev_saved_entry
set boot_once=true
fi
function savedefault {
if [ -z ${boot_once} ]; then
saved_entry=${chosen}
save_env saved_entry
fi
}
function recordfail {
set recordfail=1
if [ -n ${have_grubenv} ]; then if [ -z ${boot_once} ]; then save_env recordfail; fi; fi
}
insmod ext2
set root='(hd0,1)'
search --no-floppy --fs-uuid --set 2e78226c-16d3-4da2-a35a-77e2146d6427
if loadfont /usr/share/grub/unicode.pf2 ; then
set gfxmode=640x480
insmod gfxterm
insmod vbe
if terminal_output gfxterm ; then true ; else
# For backward compatibility with versions of terminal.mod that don't
# understand terminal_output
terminal gfxterm
fi
fi
insmod ext2
set root='(hd0,1)'
search --no-floppy --fs-uuid --set 2e78226c-16d3-4da2-a35a-77e2146d6427
set locale_dir=($root)/boot/grub/locale
set lang=zh
insmod gettext
if [ ${recordfail} = 1 ]; then
set timeout=-1
else
#此处的timeout设置菜单项显示时间
set timeout=10
fi
### END /etc/grub.d/00_header ###
### BEGIN /etc/grub.d/05_debian_theme ###
set menu_color_normal=white/black
set menu_color_highlight=black/light-gray
### END /etc/grub.d/05_debian_theme ###
### BEGIN /etc/grub.d/10_linux ###
#以下几个menuentry都是启动菜单
menuentry 'Ubuntu, with Linux 2.6.32-21-generic' --class ubuntu --class gnu-linux --class gnu --class os {
recordfail
insmod ext2
set root='(hd0,1)'
search --no-floppy --fs-uuid --set 2e78226c-16d3-4da2-a35a-77e2146d6427
#linux后的粗体路劲为内核镜像文件
linux /boot/vmlinuz-2.6.32-21-generic root=UUID=2e78226c-16d3-4da2-a35a-77e2146d6427 ro quiet splash
#initrd后的粗体文件为内存盘ramdisk镜像文件
initrd /boot/initrd.img-2.6.32-21-generic
}
menuentry 'Ubuntu, with Linux 2.6.32-21-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os {
recordfail
insmod ext2
set root='(hd0,1)'
search --no-floppy --fs-uuid --set 2e78226c-16d3-4da2-a35a-77e2146d6427
echo 'Loading Linux 2.6.32-21-generic ...'
linux /boot/vmlinuz-2.6.32-21-generic root=UUID=2e78226c-16d3-4da2-a35a-77e2146d6427 ro single
echo 'Loading initial ramdisk ...'
initrd /boot/initrd.img-2.6.32-21-generic
}
### END /etc/grub.d/10_linux ###
### BEGIN /etc/grub.d/20_memtest86+ ###
menuentry "Memory test (memtest86+)" {
insmod ext2
set root='(hd0,1)'
search --no-floppy --fs-uuid --set 2e78226c-16d3-4da2-a35a-77e2146d6427
linux16 /boot/memtest86+.bin
}
menuentry "Memory test (memtest86+, serial console 115200)" {
insmod ext2
set root='(hd0,1)'
search --no-floppy --fs-uuid --set 2e78226c-16d3-4da2-a35a-77e2146d6427
linux16 /boot/memtest86+.bin console=ttyS0,115200n8
}
### END /etc/grub.d/20_memtest86+ ###
#下面的从“### BEGIN /etc/grub.d/30_os-prober ###”到“### END /etc/grub.d/30_os-prober ###”之间的内容如果注释掉,启动过程才会显示各启动菜单。如果不注释掉,启动过程是不会显示各启动菜单的。
### BEGIN /etc/grub.d/30_os-prober ###
if [ ${timeout} != -1 ]; then
if keystatus; then
if keystatus --shift; then
set timeout=-1
else
set timeout=0
fi
else
if sleep --interruptible 3 ; then
set timeout=0
fi
fi
fi
### END /etc/grub.d/30_os-prober ###
### BEGIN /etc/grub.d/40_custom ###
# 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.
### END /etc/grub.d/40_custom ###
OK,了解了这些,我们就可以根据自己需要添加启动菜单,设置grub参量了。