编译Debian内核源码

参考:

《鸟哥的Linux私房菜》第26章

http://hi.baidu.com/wg_wang/item/f9375c2f00ca75c0ee10f1db

http://www.linuxdiyf.com/bbs/thread-193762-1-1.html

环境:

Wind7主机+Debian6虚拟机,虚拟机软件是VirtualBox

第一步、下载Linux源码

首先需要看一下当前Linux使用的版本

pengdl@debian:~$ uname -r

2.6.32-5-686

可以看到,当前使用的Linux内核版本是:2.6.32.5

为了说明问题,我们也从网上下载linux-2.6.32.5(当然也可以下载最新的版本,但是有风险)。下载地址如下:

ftp://ftp.kernel.org/pub/linux/kernel/v2.6/

我下载的是:linux-2.6.32.5.tar.xz

第二步、解压Linux内核

将来把内核源码解压到/usr/src/kernels下

mkdir -p /usr/src/kernels

 

tar -xJf linux-2.6.32.5.tar.xz -C /usr/src/kernels/

第三步、编译Linux内核

清理垃圾:

cd /usr/src/kernels/linux-2.6.32.5

make mrproper

make clean

编译内核前需要配置,为了说明问题,直接将本机自带的配置config-2.6.32-5-686拷贝到刚才解压的目录下(如果下载了比较新的内核,就需对照着config-2.6.32-5-686自己配置了),并重命名为.config

cp     /boot/config-2.6.32-5-686     /usr/src/kernels/linux-2.6.32.5/.config

cd   /usr/src/kernels/linux-2.6.32.5

make menucongfig

修改General setup内的Local version为pengdl,目的是防止执行make install和make modules_install的时候跟旧的发生冲突。

make bzImage  -j4

make modules  -j4

make modules_install

make install

制作initrd镜像

mkinitramfs     2.6.32.5pengdl   -o    /boot/initrd.img-2.6.32.5pengdl

其中2.6.32.5pengdl 就是执行完make modules_install后,在/lib/modules下新生成的文件夹

此时/lib/modules和/boot下的内容如下(其中带有686的是系统原先就有的,带pengdl的是我们新生成的,

其中之所以有以.old结尾的文件,是因为执行了两次make install,实际上执行一次就行了。手动将*.old删除):

debian:/usr/src/kernels/linux-2.6.32.5# ls /lib/modules/

2.6.32-5-686  2.6.32.5pengdl

debian:/usr/src/kernels/linux-2.6.32.5# ls /boot/

config-2.6.32-5-686        System.map-2.6.32-5-686

config-2.6.32.5pengdl      System.map-2.6.32.5pengdl

config-2.6.32.5pengdl.old  System.map-2.6.32.5pengdl.old

grub                       vmlinuz-2.6.32-5-686

initrd.img-2.6.32-5-686    vmlinuz-2.6.32.5pengdl

initrd.img-2.6.32.5pengdl  vmlinuz-2.6.32.5pengdl.old

debian:/usr/src/kernels/linux-2.6.32.5# 

第四步、更新grub

grub-mkconfig >/boot/grub/grub.cfg

可以看看新生成的grub.cfg的内容:

#

# DO NOT EDIT THIS FILE

#

# It is automatically generated by 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

set default="2"

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 load_video {

  insmod vbe

  insmod vga

  insmod video_bochs

  insmod video_cirrus

}

 

insmod part_msdos

insmod ext2

set root='(hd1,msdos5)'

search --no-floppy --fs-uuid --set 87115ddc-75d2-4f74-b8c4-e3796e6a244a

if loadfont /share/grub/unicode.pf2 ; then

  set gfxmode=640x480

  load_video

  insmod gfxterm

fi

terminal_output gfxterm

insmod part_msdos

insmod ext2

set root='(hd0,msdos1)'

search --no-floppy --fs-uuid --set 2c7658ba-7c5c-45f2-8117-d70fcadcdda6

set locale_dir=($root)/boot/grub/locale

set lang=en

insmod gettext

set timeout=5

### END /etc/grub.d/00_header ###

 

### BEGIN /etc/grub.d/05_debian_theme ###

insmod part_msdos

insmod ext2

set root='(hd1,msdos5)'

search --no-floppy --fs-uuid --set 87115ddc-75d2-4f74-b8c4-e3796e6a244a

insmod png

if background_image /share/images/desktop-base/spacefun-grub.png; then

  set color_normal=light-gray/black

  set color_highlight=white/black

else

  set menu_color_normal=cyan/blue

  set menu_color_highlight=white/blue

fi

### END /etc/grub.d/05_debian_theme ###

 

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

menuentry 'Debian GNU/Linux, with Linux 2.6.32.5pengdl' --class debian --class gnu-linux --class gnu --class os {

    insmod part_msdos

    insmod ext2

    set root='(hd0,msdos1)'

    search --no-floppy --fs-uuid --set 2c7658ba-7c5c-45f2-8117-d70fcadcdda6

    echo    'Loading Linux 2.6.32.5pengdl ...'

    linux    /boot/vmlinuz-2.6.32.5pengdl root=UUID=2c7658ba-7c5c-45f2-8117-d70fcadcdda6 ro  quiet

    echo    'Loading initial ramdisk ...'

    initrd    /boot/initrd.img-2.6.32.5pengdl

}

menuentry 'Debian GNU/Linux, with Linux 2.6.32.5pengdl (recovery mode)' --class debian --class gnu-linux --class gnu --class os {

    insmod part_msdos

    insmod ext2

    set root='(hd0,msdos1)'

    search --no-floppy --fs-uuid --set 2c7658ba-7c5c-45f2-8117-d70fcadcdda6

    echo    'Loading Linux 2.6.32.5pengdl ...'

    linux    /boot/vmlinuz-2.6.32.5pengdl root=UUID=2c7658ba-7c5c-45f2-8117-d70fcadcdda6 ro single 

    echo    'Loading initial ramdisk ...'

    initrd    /boot/initrd.img-2.6.32.5pengdl

}

menuentry 'Debian GNU/Linux, with Linux 2.6.32-5-686' --class debian --class gnu-linux --class gnu --class os {

    insmod part_msdos

    insmod ext2

    set root='(hd0,msdos1)'

    search --no-floppy --fs-uuid --set 2c7658ba-7c5c-45f2-8117-d70fcadcdda6

    echo    'Loading Linux 2.6.32-5-686 ...'

    linux    /boot/vmlinuz-2.6.32-5-686 root=UUID=2c7658ba-7c5c-45f2-8117-d70fcadcdda6 ro  quiet

    echo    'Loading initial ramdisk ...'

    initrd    /boot/initrd.img-2.6.32-5-686

}

menuentry 'Debian GNU/Linux, with Linux 2.6.32-5-686 (recovery mode)' --class debian --class gnu-linux --class gnu --class os {

    insmod part_msdos

    insmod ext2

    set root='(hd0,msdos1)'

    search --no-floppy --fs-uuid --set 2c7658ba-7c5c-45f2-8117-d70fcadcdda6

    echo    'Loading Linux 2.6.32-5-686 ...'

    linux    /boot/vmlinuz-2.6.32-5-686 root=UUID=2c7658ba-7c5c-45f2-8117-d70fcadcdda6 ro single 

    echo    'Loading initial ramdisk ...'

    initrd    /boot/initrd.img-2.6.32-5-686

}

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

 

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

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

 

### BEGIN /etc/grub.d/30_os-prober ###

### 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 ###

 

### BEGIN /etc/grub.d/41_custom ###

if [ -f  $prefix/custom.cfg ]; then

  source $prefix/custom.cfg;

fi

### END /etc/grub.d/41_custom ###

其中已经有了我们新添加的内核了:

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

Found linux image: /boot/vmlinuz-2.6.32.5pengdl

Found initrd image: /boot/initrd.img-2.6.32.5pengdl

menuentry 'Debian GNU/Linux, with Linux 2.6.32.5pengdl' --class debian --class gnu-linux --class gnu --class os {

    insmod part_msdos

    insmod ext2

    set root='(hd0,msdos1)'

    search --no-floppy --fs-uuid --set 2c7658ba-7c5c-45f2-8117-d70fcadcdda6

    echo    'Loading Linux 2.6.32.5pengdl ...'

    linux    /boot/vmlinuz-2.6.32.5pengdl root=UUID=2c7658ba-7c5c-45f2-8117-d70fcadcdda6 ro  quiet

    echo    'Loading initial ramdisk ...'

    initrd    /boot/initrd.img-2.6.32.5pengdl

}

menuentry 'Debian GNU/Linux, with Linux 2.6.32.5pengdl (recovery mode)' --class debian --class gnu-linux --class gnu --class os {

    insmod part_msdos

    insmod ext2

    set root='(hd0,msdos1)'

    search --no-floppy --fs-uuid --set 2c7658ba-7c5c-45f2-8117-d70fcadcdda6

    echo    'Loading Linux 2.6.32.5pengdl ...'

    linux    /boot/vmlinuz-2.6.32.5pengdl root=UUID=2c7658ba-7c5c-45f2-8117-d70fcadcdda6 ro single 

    echo    'Loading initial ramdisk ...'

    initrd    /boot/initrd.img-2.6.32.5pengdl

}

menuentry 'Debian GNU/Linux, with Linux 2.6.32-5-686' --class debian --class gnu-linux --class gnu --class os {

    insmod part_msdos

    insmod ext2

    set root='(hd0,msdos1)'

    search --no-floppy --fs-uuid --set 2c7658ba-7c5c-45f2-8117-d70fcadcdda6

    echo    'Loading Linux 2.6.32-5-686 ...'

    linux    /boot/vmlinuz-2.6.32-5-686 root=UUID=2c7658ba-7c5c-45f2-8117-d70fcadcdda6 ro  quiet

    echo    'Loading initial ramdisk ...'

    initrd    /boot/initrd.img-2.6.32-5-686

}

menuentry 'Debian GNU/Linux, with Linux 2.6.32-5-686 (recovery mode)' --class debian --class gnu-linux --class gnu --class os {

    insmod part_msdos

    insmod ext2

    set root='(hd0,msdos1)'

    search --no-floppy --fs-uuid --set 2c7658ba-7c5c-45f2-8117-d70fcadcdda6

    echo    'Loading Linux 2.6.32-5-686 ...'

    linux    /boot/vmlinuz-2.6.32-5-686 root=UUID=2c7658ba-7c5c-45f2-8117-d70fcadcdda6 ro single 

    echo    'Loading initial ramdisk ...'

    initrd    /boot/initrd.img-2.6.32-5-686

}

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

如果按照上面的配置,启动时,Linux 2.6.32.5pengdl 会放在第一个,如果不手动按上下键进行选择,那么启动的将是我们刚编译出来的内核,如何让系统默认启动旧的内核呢?这里有几种方法:

1、手动修改grub.conf,交换旧内核和新内核的位置:

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

menuentry 'Debian GNU/Linux, with Linux 2.6.32-5-686' --class debian --class gnu-linux --class gnu --class os {

    insmod part_msdos

    insmod ext2

    set root='(hd0,msdos1)'

    search --no-floppy --fs-uuid --set 2c7658ba-7c5c-45f2-8117-d70fcadcdda6

    echo    'Loading Linux 2.6.32-5-686 ...'

    linux    /boot/vmlinuz-2.6.32-5-686 root=UUID=2c7658ba-7c5c-45f2-8117-d70fcadcdda6 ro  quiet

    echo    'Loading initial ramdisk ...'

    initrd    /boot/initrd.img-2.6.32-5-686

}

menuentry 'Debian GNU/Linux, with Linux 2.6.32-5-686 (recovery mode)' --class debian --class gnu-linux --class gnu --class os {

    insmod part_msdos

    insmod ext2

    set root='(hd0,msdos1)'

    search --no-floppy --fs-uuid --set 2c7658ba-7c5c-45f2-8117-d70fcadcdda6

    echo    'Loading Linux 2.6.32-5-686 ...'

    linux    /boot/vmlinuz-2.6.32-5-686 root=UUID=2c7658ba-7c5c-45f2-8117-d70fcadcdda6 ro single 

    echo    'Loading initial ramdisk ...'

    initrd    /boot/initrd.img-2.6.32-5-686

}

 

menuentry 'Debian GNU/Linux, with Linux 2.6.32.5pengdl' --class debian --class gnu-linux --class gnu --class os {

    insmod part_msdos

    insmod ext2

    set root='(hd0,msdos1)'

    search --no-floppy --fs-uuid --set 2c7658ba-7c5c-45f2-8117-d70fcadcdda6

    echo    'Loading Linux 2.6.32.5pengdl ...'

    linux    /boot/vmlinuz-2.6.32.5pengdl root=UUID=2c7658ba-7c5c-45f2-8117-d70fcadcdda6 ro  quiet

    echo    'Loading initial ramdisk ...'

    initrd    /boot/initrd.img-2.6.32.5pengdl

}

menuentry 'Debian GNU/Linux, with Linux 2.6.32.5pengdl (recovery mode)' --class debian --class gnu-linux --class gnu --class os {

    insmod part_msdos

    insmod ext2

    set root='(hd0,msdos1)'

    search --no-floppy --fs-uuid --set 2c7658ba-7c5c-45f2-8117-d70fcadcdda6

    echo    'Loading Linux 2.6.32.5pengdl ...'

    linux    /boot/vmlinuz-2.6.32.5pengdl root=UUID=2c7658ba-7c5c-45f2-8117-d70fcadcdda6 ro single 

    echo    'Loading initial ramdisk ...'

    initrd    /boot/initrd.img-2.6.32.5pengdl

}

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

2、或者,修改 set default="0"  ,将0改为2。直接在这里改,如果再更新grub.conf,default的值又变为了0,其中在grub.conf的最上面给出了方法:

#

# DO NOT EDIT THIS FILE

#

# It is automatically generated by grub-mkconfig using templates

# from /etc/grub.d and settings from /etc/default/grub

#

我们应该修改/etc/default/grub这个文件,将GRUB_DEFAULT=0 改为GRUB_DEFAULT=2,然后重新执行

grub-mkconfig >/boot/grub/grub.cfg

下面是启动测试:(可以用上下箭头选择有启动的内核)

编译Debian内核源码

注意:

因为我是用的是VirtualBox虚拟机,更新完内核后,需要重新安装VirtualBox增强工具,增强工具的安装方法参见:

http://www.cnblogs.com/pengdonglin137/p/3366765.html#a8

安装增强工具的时候,会把上次安装的增强工具给删掉。

完!!

上一篇:javascript-添加 class 类 和 移出 class 类 方法


下一篇:CentOS 7 更改网卡名为eth0