为Ubuntu换颗“心”

    对于现在的Linux发行版操作系统,都默认配置好相应的Kernel,但其版本远比最新的要旧,而最新的Kernel除了会修复已发现的BUG,有时还会更新部分框架以及新增功能模块代码,为了确保系统的稳定,还有体验下新功能,我们只好对操作系统的进行换“心”手术,这手术可不简单,首先要获得制作“心”的原材料和工具(即GNU/Linux Kernel源码和编译工具),然后将这些原材料加工成可用的“心”(即可执行的Kernel文件),最后再将这颗“心”更换到操作系统上。经过这么看似简单的3步,就完成了操作系统的核心更换大动作。下面让我们一一体验这些惊心动魄的大动作吧。
0.现有环境
    Ubuntu13.10(3.11.0-12-generic),64位。
1.获取原材料和工具
    刚装好的Ubuntu13.10系统默认使用的GNU/Linux Kernel版本是3.11.0-12-generic,而现在(2014-3-10)从https://www.kernel.org/网站查看到最新的版本为3.13.6,那么就下载它了:
    cd ~
    wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.13.6.tar.xz
    上面步骤从Kernel官网下载了最新的稳定源码包,由于安装好的Ubuntu发行版系统里默认安装好了编译Kernel所需要的编译链接等加工工具(GCC、LD等),我们仅需确认安装进行menuconfig时需要的包(后面的步骤暂不需要,但会讲述到,使用sudo apt-get install libncurses5-dev安装)。
2.加工制作可用的“心”
    获得源码后,我们先将源码包解压:
    tar xvf linux-3.13.6.tar.xz
    解压后需要配置下Kernel,首先进入解压后的源码目录:
    cd linux-3.13.6
    然后执行make help命令来了解我们有哪些配置模式可使用,执行该命令后有如下内容:
    Configuration targets: 
        config - Update current config utilising a line-oriented program 
        nconfig - Update current config utilising a ncurses menu based program 
        menuconfig - Update current config utilising a menu based program 
        xconfig - Update current config utilising a QT based front-end 
        gconfig - Update current config utilising a GTK based front-end 
        oldconfig - Update current config utilising a provided .config as base 
        localmodconfig - Update current config disabling modules not loaded 
        localyesconfig - Update current config converting local mods to core 
        silentoldconfig - Same as oldconfig, but quietly, additionally update deps 
        defconfig - New config with default from ARCH supplied defconfig 
        savedefconfig - Save current config as ./defconfig (minimal config) 
        allnoconfig - New config where all options are answered with no 
        allyesconfig - New config where all options are accepted with yes 
        allmodconfig - New config selecting modules when possible 
        alldefconfig - New config with all symbols set to default 
        randconfig - New config with random answer to all options 
        listnewconfig - List new options 
        olddefconfig - Same as silentoldconfig but sets new symbols to their default value
    从上面的提示,我们需要用到的配置模式是silentoldconfig,接下来我们将现有的Ubuntu里的内核配置文件复制到当前的Kernel源码目录下:
    cp /boot/config-3.11.0-12-generic .config
    接下来执行make silentoldconfig命令更新.config文件,此时有些重要项还是会提示我们进行配置,我们全部按Eenter键默认配置即可,最终会有如下提示已更新.config文件:
    # 
    # configuration written to .config 
    # 
    更新配置文件后,我们需要开始编译生成目标文件了,此时执行make -j4命令进行编译生成即可(-j后面的数字表示开启4个线程来编译,一般是CPU核心数的2倍)。
3.更换制作好的新“心”
    上面编译好目标文件后,先执行如下命令将生成的模块(驱动KO文件、固件FW和BIN文件)安装到相应目录下:
    sudo make modules_install
    安装好后再安装Kernel:
    sudo make install
    此时会生成相应的Ram Disk镜像文件并更新GRUB的配置,有如下内容输出:
    Generating grub.cfg ... 
    Found linux image: /boot/vmlinuz-3.13.6 
    Found initrd image: /boot/initrd.img-3.13.6 
    Found linux image: /boot/vmlinuz-3.11.0-12-generic 
    Found initrd image: /boot/initrd.img-3.11.0-12-generic 
    Found memtest86+ image: /boot/memtest86+.bin 
    done 
4.体验新“心”
    上面已完成了换“心”的全部工作,接下来是开始试用了,将电脑重启后不操作,会默认以最新的Kernel启动进入Ubuntu。
    在原配Kernel的Ubuntu运行uname -a命令有如下输出:
    xinu@slam:~$ uname -a
    Linux slam 3.11.0-12-generic #19-Ubuntu SMP Wed Oct 9 16:20:46 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux 
    在更换新“心”的Ubuntu里运行相同命令有如下输出:
    xinu@slam:~$ uname -a
    Linux slam 3.13.6 #1 SMP Thu Mar 27 19:44:58 CST 2014 x86_64 x86_64 x86_64 GNU/Linux
    至此,换“心”工程实施很顺利,接下来是好好的体验和处理发现的新问题了。
5.后记
    本文是在Ubuntu这一Linux发行版操作系统环境下进行,其他的发行版系统环境也可参考实施。
6.参考网址
    http://lesca.me/archives/config-and-install-kernel.html

评论

此博客中的热门博文

I/O映射之I/O端口

通过Netlink检测网线插拔

使用seq_file