intro

最近在做ovs的实验,为了保持实验环境的一致性,我这边用了openvswitch-2.3.1这个版本,由于ovs与对应的内核版本有要求:

Open vSwitchLinux kernel
1.4.x2.6.18 to 3.2
1.5.x2.6.18 to 3.2
1.6.x2.6.18 to 3.2
1.7.x2.6.18 to 3.3
1.8.x2.6.18 to 3.4
1.9.x2.6.18 to 3.8
1.10.x2.6.18 to 3.8
1.11.x2.6.18 to 3.8
2.0.x2.6.32 to 3.10
2.1.x2.6.32 to 3.11
2.3.x2.6.32 to 3.14
2.4.x2.6.32 to 4.0
2.5.x2.6.32 to 4.3
2.6.x3.10 to 4.7

这里为了安装ovs2.3.1,我需要让内核版本满足2.6.32到3.14的条件,当前ubuntu系统的内核版本是3.19.0-25-generic,目标内核版本是3.13.0-24-generic

安装3.13.0-24-generic内核

1.显示所有可以获取的内核版本

1
apt-cache search linux

2.安装指定版本version内核
version一般为x.x.x-x的形式。

1
2
3
sudo apt-get install  -y linux-headers-version-generic \
linux-image-version-generic \
linux-image-extra-version-generic

比如这里我要安装version为3.13.0-24的内核,这里就执行

1
2
3
sudo apt-get install  -y linux-headers-3.13.0-24-generic \
linux-image-3.13.0-24-generic \
linux-image-extra-3.13.0-24-generic

[可选]修改GRUB,保证ubunu启动的时候使用的使新内核

修改默认grub启动菜单

这里是一个可选的配置,如果不配置,每次在启动系统的时候,都需按下ESC键,进入GRUB菜单,选中新内核。这一重复的操作,我们可以在GRUB中进行配置。需要修改的文件是/etc/default/grub

1
2
3
4
5
6
7
8
// /etc/default/grub
GRUB_DEFAULT=0
GRUB_HIDDEN_TIMEOUT=0
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=""

这里讲下GRUB_DEFAULT的配置,GRUB_DEFAULT表示了开机的时候的默认选项(包括二级菜单选项)。这边先盗个图说下内核顺序:
下面是一张GRUB的启动菜单
grub_submenu_examples
在配合一段grub的配置说明,如下:
grub_simple_configuration
意思就是一级菜单比如Ubuntu,with Linux 3.2.0-24-generic-paeUbunut,with Linux 3.2.0-24-generic-pae(recovery mode)Previous Linux versionsMemory test(memtest86+)等等,一级菜单按照从0开始编号,如Ubuntu,with Linux 3.2.0-24-generic-pae对应0,Ubunut,with Linux 3.2.0-24-generic-pae(recovery mode)对应1,Previous Linux versions对应2,以此类推。对于二级菜单也是一样的道理,每个菜单项下的子菜单都是从0开始编号。
那么这里如果我要配置Previous Linux versions下的Ubuntu, with Linux 3.2.0-18-generic-pae,我们的内核顺序就应该是2>0,这里注意在填写GRUB_DEFAULT的时候一定要加上有引号,如:

1
GRUB_DEFAULT="2>0"

除了填写数字形式的内核顺序,还可以有其他的写法

1
2
3
GRUB_DEFAULT="Previous Linux versions>Ubuntu, with Linux 3.2.0-18-generic-pae"
GRUB_DEFAULT="Previous Linux versions>0"
GRUB_DEFAULT="2>Ubuntu, with Linux 3.2.0-18-generic-pae"

ps:我这里并没有重新看一遍grub的启动内容,而是查看了/boot/grub/grub.cfg,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-5723676a-712c-4518-a288-feea52c251b0' {
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 5723676a-712c-4518-a288-feea52c251b0
else
search --no-floppy --fs-uuid --set=root 5723676a-712c-4518-a288-feea52c251b0
fi
linux /boot/vmlinuz-3.19.0-25-generic root=UUID=5723676a-712c-4518-a288-feea52c251b0 ro quiet splash $vt_handoff
initrd /boot/initrd.img-3.19.0-25-generic
}
submenu 'Advanced options for Ubuntu' $menuentry_id_option 'gnulinux-advanced-5723676a-712c-4518-a288-feea52c251b0' {
menuentry 'Ubuntu, with Linux 3.19.0-25-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.19.0-25-generic-advanced-5723676a-712c-4518-a288-feea52c251b0' {
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 5723676a-712c-4518-a288-feea52c251b0
else
search --no-floppy --fs-uuid --set=root 5723676a-712c-4518-a288-feea52c251b0
fi
echo 'Loading Linux 3.19.0-25-generic ...'
linux /boot/vmlinuz-3.19.0-25-generic root=UUID=5723676a-712c-4518-a288-feea52c251b0 ro quiet splash $vt_handoff
echo 'Loading initial ramdisk ...'
initrd /boot/initrd.img-3.19.0-25-generic
}
menuentry 'Ubuntu, with Linux 3.19.0-25-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.19.0-25-generic-recovery-5723676a-712c-4518-a288-feea52c251b0' {
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 5723676a-712c-4518-a288-feea52c251b0
else
search --no-floppy --fs-uuid --set=root 5723676a-712c-4518-a288-feea52c251b0
fi
echo 'Loading Linux 3.19.0-25-generic ...'
linux /boot/vmlinuz-3.19.0-25-generic root=UUID=5723676a-712c-4518-a288-feea52c251b0 ro recovery nomodeset
echo 'Loading initial ramdisk ...'
initrd /boot/initrd.img-3.19.0-25-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-5723676a-712c-4518-a288-feea52c251b0' {
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 5723676a-712c-4518-a288-feea52c251b0
else
search --no-floppy --fs-uuid --set=root 5723676a-712c-4518-a288-feea52c251b0
fi
echo 'Loading Linux 3.13.0-24-generic ...'
linux /boot/vmlinuz-3.13.0-24-generic root=UUID=5723676a-712c-4518-a288-feea52c251b0 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-5723676a-712c-4518-a288-feea52c251b0' {
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 5723676a-712c-4518-a288-feea52c251b0
else
search --no-floppy --fs-uuid --set=root 5723676a-712c-4518-a288-feea52c251b0
fi
echo 'Loading Linux 3.13.0-24-generic ...'
linux /boot/vmlinuz-3.13.0-24-generic root=UUID=5723676a-712c-4518-a288-feea52c251b0 ro recovery nomodeset
echo 'Loading initial ramdisk ...'
initrd /boot/initrd.img-3.13.0-24-generic
}
}

根据我这段从grub.cfg中摘取出来的菜单项,那么我的对应的菜单项为

1
2
3
4
5
6
|-----Ubuntu
|-----Advanced options for Ubuntu
|-----Ubuntu, with Linux 3.19.0-25-generic
|-----Ubuntu, with Linux 3.19.0-25-generic (recovery mode)
|-----Ubuntu, with Linux 3.13.0-24-generic
|-----Ubuntu, with Linux 3.13.0-24-generic (recovery mode)

为了使用3.13.0-24版本的内核,修改GRUB_DEFAULT为"1>2"
如下:
etc-default-grub

使上面的配置生效

修改完/etc/default/grub之后,要使配置生效,还需要执行sudo update-grub,使得配置项可以更新到/boot/grub/grub.cfg文件中

验证

reboot,在终端中执行uname -r看看内核有没有改过来。

参考http://blog.mtkfan.com/?p=431