首页 > 代码库 > grub2配置

grub2配置

环境:ubuntu14.04

默认配置文件路径/etc/default/grub

例子:

GRUB_DEFAULT=0 #默认启动那一个
#GRUB_HIDDEN_TIMEOUT=15 #选择菜单是否隐藏,注释掉就直接显示了
GRUB_HIDDEN_TIMEOUT_QUIET=true
GRUB_TIMEOUT=10
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
GRUB_CMDLINE_LINUX=""

更新grup调用

ubuntu@ubuntu-Lenovo-Product:~$ sudo update-grub
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.16.0-031600-generic
Found initrd image: /boot/initrd.img-3.16.0-031600-generic
Found linux image: /boot/vmlinuz-3.13.0-43-generic
Found initrd image: /boot/initrd.img-3.13.0-43-generic
Found linux image: /boot/vmlinuz-3.13.0-41-generic
Found initrd image: /boot/initrd.img-3.13.0-41-generic
Found linux image: /boot/vmlinuz-3.13.0-24-generic
Found initrd image: /boot/initrd.img-3.13.0-24-generic
Found memtest86+ image: /boot/memtest86+.elf
Found memtest86+ image: /boot/memtest86+.bin
done

然后如上所示update-grup会在系统里找编译好的内核,试过放在根目录/也能找到。

update-grup找完内核后,开始更新文件/boot/grub/grub.cfg

这个文件里有开机启动菜单入口信息,如下:

menuentry ‘Ubuntu‘ --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option ‘gnulinux-simple-e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668‘ {
	recordfail
	load_video
	gfxmode $linux_gfx_mode
	insmod gzio
	insmod part_msdos
	insmod ext2
	set root=‘hd0,msdos1‘
	if [ x$feature_platform_search_hint = xy ]; then
	  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1  e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668
	else
	  search --no-floppy --fs-uuid --set=root e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668
	fi
	linux	/boot/vmlinuz-3.16.0-031600-generic root=UUID=e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668 ro  quiet splash $vt_handoff
	initrd	/boot/initrd.img-3.16.0-031600-generic
}
submenu ‘Advanced options for Ubuntu‘ $menuentry_id_option ‘gnulinux-advanced-e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668‘ {
	menuentry ‘Ubuntu, with Linux 3.16.0-031600-generic‘ --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option ‘gnulinux-3.16.0-031600-generic-advanced-e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668‘ {
		recordfail
		load_video
		gfxmode $linux_gfx_mode
		insmod gzio
		insmod part_msdos
		insmod ext2
		set root=‘hd0,msdos1‘
		if [ x$feature_platform_search_hint = xy ]; then
		  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1  e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668
		else
		  search --no-floppy --fs-uuid --set=root e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668
		fi
		echo	‘Loading Linux 3.16.0-031600-generic ...‘
		linux	/boot/vmlinuz-3.16.0-031600-generic root=UUID=e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668 ro  quiet splash $vt_handoff
		echo	‘Loading initial ramdisk ...‘
		initrd	/boot/initrd.img-3.16.0-031600-generic
	}
	menuentry ‘Ubuntu, with Linux 3.16.0-031600-generic (recovery mode)‘ --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option ‘gnulinux-3.16.0-031600-generic-recovery-e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668‘ {
		recordfail
		load_video
		insmod gzio
		insmod part_msdos
		insmod ext2
		set root=‘hd0,msdos1‘
		if [ x$feature_platform_search_hint = xy ]; then
		  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1  e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668
		else
		  search --no-floppy --fs-uuid --set=root e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668
		fi
		echo	‘Loading Linux 3.16.0-031600-generic ...‘
		linux	/boot/vmlinuz-3.16.0-031600-generic root=UUID=e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668 ro recovery nomodeset 
		echo	‘Loading initial ramdisk ...‘
		initrd	/boot/initrd.img-3.16.0-031600-generic
	}
	menuentry ‘Ubuntu, with Linux 3.13.0-43-generic‘ --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option ‘gnulinux-3.13.0-43-generic-advanced-e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668‘ {
		recordfail
		load_video
		gfxmode $linux_gfx_mode
		insmod gzio
		insmod part_msdos
		insmod ext2
		set root=‘hd0,msdos1‘
		if [ x$feature_platform_search_hint = xy ]; then
		  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1  e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668
		else
		  search --no-floppy --fs-uuid --set=root e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668
		fi
		echo	‘Loading Linux 3.13.0-43-generic ...‘
		linux	/boot/vmlinuz-3.13.0-43-generic root=UUID=e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668 ro  quiet splash $vt_handoff
		echo	‘Loading initial ramdisk ...‘
		initrd	/boot/initrd.img-3.13.0-43-generic
	}
	menuentry ‘Ubuntu, with Linux 3.13.0-43-generic (recovery mode)‘ --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option ‘gnulinux-3.13.0-43-generic-recovery-e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668‘ {
		recordfail
		load_video
		insmod gzio
		insmod part_msdos
		insmod ext2
		set root=‘hd0,msdos1‘
		if [ x$feature_platform_search_hint = xy ]; then
		  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1  e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668
		else
		  search --no-floppy --fs-uuid --set=root e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668
		fi
		echo	‘Loading Linux 3.13.0-43-generic ...‘
		linux	/boot/vmlinuz-3.13.0-43-generic root=UUID=e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668 ro recovery nomodeset 
		echo	‘Loading initial ramdisk ...‘
		initrd	/boot/initrd.img-3.13.0-43-generic
	}
	menuentry ‘Ubuntu, with Linux 3.13.0-41-generic‘ --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option ‘gnulinux-3.13.0-41-generic-advanced-e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668‘ {
		recordfail
		load_video
		gfxmode $linux_gfx_mode
		insmod gzio
		insmod part_msdos
		insmod ext2
		set root=‘hd0,msdos1‘
		if [ x$feature_platform_search_hint = xy ]; then
		  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1  e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668
		else
		  search --no-floppy --fs-uuid --set=root e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668
		fi
		echo	‘Loading Linux 3.13.0-41-generic ...‘
		linux	/boot/vmlinuz-3.13.0-41-generic root=UUID=e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668 ro  quiet splash $vt_handoff
		echo	‘Loading initial ramdisk ...‘
		initrd	/boot/initrd.img-3.13.0-41-generic
	}
	menuentry ‘Ubuntu, with Linux 3.13.0-41-generic (recovery mode)‘ --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option ‘gnulinux-3.13.0-41-generic-recovery-e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668‘ {
		recordfail
		load_video
		insmod gzio
		insmod part_msdos
		insmod ext2
		set root=‘hd0,msdos1‘
		if [ x$feature_platform_search_hint = xy ]; then
		  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1  e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668
		else
		  search --no-floppy --fs-uuid --set=root e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668
		fi
		echo	‘Loading Linux 3.13.0-41-generic ...‘
		linux	/boot/vmlinuz-3.13.0-41-generic root=UUID=e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668 ro recovery nomodeset 
		echo	‘Loading initial ramdisk ...‘
		initrd	/boot/initrd.img-3.13.0-41-generic
	}
	menuentry ‘Ubuntu, with Linux 3.13.0-24-generic‘ --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option ‘gnulinux-3.13.0-24-generic-advanced-e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668‘ {
		recordfail
		load_video
		gfxmode $linux_gfx_mode
		insmod gzio
		insmod part_msdos
		insmod ext2
		set root=‘hd0,msdos1‘
		if [ x$feature_platform_search_hint = xy ]; then
		  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1  e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668
		else
		  search --no-floppy --fs-uuid --set=root e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668
		fi
		echo	‘Loading Linux 3.13.0-24-generic ...‘
		linux	/boot/vmlinuz-3.13.0-24-generic root=UUID=e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668 ro  quiet splash $vt_handoff
		echo	‘Loading initial ramdisk ...‘
		initrd	/boot/initrd.img-3.13.0-24-generic
	}
	menuentry ‘Ubuntu, with Linux 3.13.0-24-generic (recovery mode)‘ --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option ‘gnulinux-3.13.0-24-generic-recovery-e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668‘ {
		recordfail
		load_video
		insmod gzio
		insmod part_msdos
		insmod ext2
		set root=‘hd0,msdos1‘
		if [ x$feature_platform_search_hint = xy ]; then
		  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1  e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668
		else
		  search --no-floppy --fs-uuid --set=root e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668
		fi
		echo	‘Loading Linux 3.13.0-24-generic ...‘
		linux	/boot/vmlinuz-3.13.0-24-generic root=UUID=e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668 ro recovery nomodeset 
		echo	‘Loading initial ramdisk ...‘
		initrd	/boot/initrd.img-3.13.0-24-generic
	}
}

### END /etc/grub.d/10_linux ###

### BEGIN /etc/grub.d/20_linux_xen ###

### END /etc/grub.d/20_linux_xen ###

### BEGIN /etc/grub.d/20_memtest86+ ###
menuentry ‘Memory test (memtest86+)‘ {
	insmod part_msdos
	insmod ext2
	set root=‘hd0,msdos1‘
	if [ x$feature_platform_search_hint = xy ]; then
	  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1  e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668
	else
	  search --no-floppy --fs-uuid --set=root e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668
	fi
	knetbsd	/boot/memtest86+.elf
}
menuentry ‘Memory test (memtest86+, serial console 115200)‘ {
	insmod part_msdos
	insmod ext2
	set root=‘hd0,msdos1‘
	if [ x$feature_platform_search_hint = xy ]; then
	  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1  e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668
	else
	  search --no-floppy --fs-uuid --set=root e96bea5c-cd18-4b7d-b4a6-1f7ad59b6668
	fi
	linux16	/boot/memtest86+.bin console=ttyS0,115200n8
}

可以看到地一个menuentry有submenu ‘Advanced options for Ubuntu‘,submenu里边嵌套menuentry(结构一样)

之前提到的配置文件/etc/default/grub的DEFAULT_ENTRY指的是第一层的menuentry而不是submenu的。在上面的例子如果DEFAULT_ENTRY=1就表示默认启动menuentry ‘Memory test (memtest86+)。

在submenu里看到有很多不同内核,update-grub会选能检测到的版本最新的内核作为默认启动项目,如果要换内核启动可以:

  1. 开机时手工选择submenu的启动项。

  2. 让update-grub选择为默认内核,可以把不用的高版本内核备份起来(update-grub搜索的时候就找不到该内核),或者可能有命令选择内核(暂时没找到)。



grub2配置