linux —— 编译linux内核

目录:     0、测试环境 

                1、获得最新内核源代码

      2、编译源代码

                3、运行新的内核


0、测试环境:

我的系统         :   ubuntu 16.04 LTS (内核版本:4.4.0-36-generic )
下载源代码内核版本 :   Linux 4.8-rc6
测试时间         :   2016/9/16  

1、获得最新内核源代码

获取方法可以有以下两种:

     1.1 在linux 内核官方主页http://www.kernel.org,你永远可以获得 当前最新的linux内核源完整的tarball(一种用tar命令生成的 归档/压缩 文件)以及不断发布的 补丁(patch) .

     1.2  直接从git上面获取: https://github.com/torvalds/linux。具体是:Terminal 进入自己想要同步下来的目录后。clone下来:

cd  /同步下来/的/目录
git clone https://github.com/torvalds/linux.git        #这个应该有2G多

 1.3 编译准备

连同源代码和编译后的产生的文件,请准备好20G的硬盘空间!(从git上clone下来的源代码 2.6 G; 经过make后另外生成的编译文件 13.5G 这加起来有17G的空间)

/ 根目录(具体来说是 /lib,没挂载/lib的话就默认是使用/目录) 要有至少4G的空余空间

/ boot   要有至少 400M 的空余空间

所以请在编译前查看自己是否有对应充足的空间。(我就是不知道这一点重装了两次系统:一次是 /home 不足,一次是/boot不足)

 

2、编译源代码

编译内核:(根据github README 的说明)

2.0、在编译前仔细看README,有一些细节得注意例如说:

DOCUMENTATION:

 这个说明项里面提到,可以参考的文档网站:帮助文档项目  还有git项目的目录里document里有部分项目文档的说明等。

SOFTWARE REQUIREMENTS

 而这个提到要去看一下 项目目录里的  “Documentation/Changes” 的软件要求最低要求(不然后面遇到版本问题会很头大),我使用的时ubuntu 16.04,里面提到的软件版本都符合要求,就是有一些没软件我没有,因为刚接触还不确定需不需要所以没有的我都自己下载了。

 还有,在开始编译前:要 Make sure you have no stale .o files and dependencies lying around:

     cd linux              #进入git clone 下来的linux 目录(最顶目录)
     make mrproper

具体还是自己刻苦啃一下 README 比较好。

2.1、执行 make menuconfig 

我决定自定义编译的路径:( 根据 “BUILD directory for the kernel”)

执行:

make O=/home/bensonlaur/build/kernel menuconfig

结果报错:

/home/bensonlaur/gproject/linux/scripts/kconfig/lxdialog/dialog.h:38:20: fatal error: curses.h: 没有那个文件或目录

解决:

#因为缺少包,安装之,然后重新 make menuconfig
sudo apt-get install libncurses5-dev

2.2、开始按上面的配置结果来 执行: make

执行:(由于github 项目里的README说,一旦自定义了build路径,选择了O选项,所有的make都得制定这个路径)

 make O=/home/bensonlaur/build/kernel 

(注意:刚才执行 make O=/home/bensonlaur/build/kernel menuconfig 成功时,会显示图形界面给你配置,save 配置为 .config 。

   我试过别的名字,这样的话在make这一步会提示:

***
*** Configuration file ".config" not found!
***
*** Please run some configurator (e.g. "make oldconfig" or
*** "make menuconfig" or "make xconfig").
***

,我也不知道改了配置名字后,去哪里制定我的别的名字的配置文件,就重新使用make O=/home/bensonlaur/build/kernel menuconfig生成 名为.config的配置文件,于是就没报这个错了 )

继续make 时继续报错:

/home/bensonlaur/gproject/linux/scripts/sign-file.c:25:30: fatal error: openssl/opensslv.h: 没有那个文件或目录

解决:

#还是因为缺少包,安装之,然后重新 执行make
sudo apt-get install libssl-dev

解决缺少的文件问题后,接着就是漫长的编译等待了

(时间需要很长,也消耗空间,第一次都没底,可怜我12G的/home空间本来已经剩下9G,没想到还爆满,也不知道还会生成多少,为了分20G给/home,还重装了一遍系统,还好这次够用)。

此过程有很多warning,不知道是什么情况,我先记录在这里: 

......
/home/bensonlaur/gproject/linux/arch/x86/kernel/cpu/microcode/intel.c: In function ‘save_mc_for_early’:
/home/bensonlaur/gproject/linux/arch/x86/kernel/cpu/microcode/intel.c:521:1: warning: the frame size of 1032 bytes is larger than 1024 bytes [-Wframe-larger-than=]
 }
 ^
/home/bensonlaur/gproject/linux/arch/x86/kernel/cpu/microcode/intel.c: In function ‘load_microcode.isra.3.constprop’:
/home/bensonlaur/gproject/linux/arch/x86/kernel/cpu/microcode/intel.c:144:1: warning: the frame size of 1032 bytes is larger than 1024 bytes [-Wframe-larger-than=]
 }
 ^
/home/bensonlaur/gproject/linux/arch/x86/kernel/cpu/microcode/intel.c: In function ‘get_matching_model_microcode.isra.4.constprop’:
/home/bensonlaur/gproject/linux/arch/x86/kernel/cpu/microcode/intel.c:357:1: warning: the frame size of 1080 bytes is larger than 1024 bytes [-Wframe-larger-than=]
 }
 ^
/home/bensonlaur/gproject/linux/arch/x86/kernel/cpu/microcode/intel.c: In function ‘save_microcode_in_initrd_intel’:
/home/bensonlaur/gproject/linux/arch/x86/kernel/cpu/microcode/intel.c:681:1: warning: the frame size of 1032 bytes is larger than 1024 bytes [-Wframe-larger-than=]
 }
 ^
......
这个不是warning,也记录下吧,(反正现在还在等着)
  CC [M]  kernel/torture.o
  CC      certs/system_keyring.o
  EXTRACT_CERTS   
Generating X.509 key generation config
###
### Now generating an X.509 key pair to be used for signing modules.
###
### If this takes a long time, you might wish to run rngd in the
### background to keep the supply of entropy topped up.  It
### needs to be run as root, and uses a hardware random
### number generator if one is available.
###
Generating a 4096 bit RSA private key
..........................................................................................++
.................++
unable to write 'random state'
writing new private key to 'certs/signing_key.pem'
-----
###
### Key pair generated.
###
  EXTRACT_CERTS   certs/signing_key.pem
  AS      certs/system_certificates.o
  LD      certs/built-in.o
  CC      mm/filemap.o
  CC      mm/mempool.o

......
  CC [M]  fs/jffs2/xattr.o
/home/bensonlaur/gproject/linux/fs/jffs2/xattr.c: In function ‘jffs2_build_xattr_subsystem’:
/home/bensonlaur/gproject/linux/fs/jffs2/xattr.c:887:1: warning: the frame size of 1128 bytes is larger than 1024 bytes [-Wframe-larger-than=]
 }
 ^
  CC [M]  fs/jffs2/xattr_trusted.o
......

(呀,等了那么久,不小心复制 ctrl+shift+c 按错成 取消的ctrl+c,还好重新make好像可以从上次继续,吓死我了,以为又要等那么久,算了不贴了,warning应该都是类似的那个warning: the frame size of 1128 bytes is larger than 1024 bytes [-Wframe-larger-than=])
......
......
make 过程中的 warning

2.3、执行 :sudo make modules_install install

我前面指定了O选项,这里同样指定执行:

sudo make O=/home/bensonlaur/build/kernel modules_install install

 此过程会在 /lib 生成接近4G的所编译内核的文件,在/boot生成400M的文件(包括initrd.img-4.8.0-rc6,/boot/grub/menu.lst 等)。

 2.4 配置kernel

CONFIGURING the kernel:

在README 的“CONFIGURING the kernel”里面提到,配置是必要的,如果想要以最小的工作量把已有的自己系统的配置搬过来 ,执行make oldconfig:
sudo make O=/home/bensonlaur/build/kernel oldconfig

关于make oldconfig: 不必要的驱动会使内核变得臃肿,不同于386的处理器类型的设置会试内核无法运行在386上,建议不熟悉配置的使用make oldconfig。

 3、运行新的内核

 - Booting a kernel directly from a floppy without the assistance of a
   bootloader such as LILO, is no longer supported.
   If you boot Linux from the hard drive, chances are you use LILO, which
   uses the kernel image as specified in the file /etc/lilo.conf.  The
   kernel image file is usually /vmlinuz, /boot/vmlinuz, /bzImage or
   /boot/bzImage.  To use the new kernel, save a copy of the old image
   and copy the new image over the old one.  Then, you MUST RERUN LILO
   to update the loading map! If you don't, you won't be able to boot
   the new kernel image.

上面说现在启动新的内核需要一个叫 LILO (linux loader)的软件,下面安装并让其自动配置。

( 安装之后执行 liloconfig会自动帮我们检测并生成配置文件 /etc/lilo.conf 和/boot/map等, 这个文件里是具体的配置和说明)

bensonlaur@b-lenovo:~$ lilo
程序“lilo”尚未安装。 您可以使用以下命令安装:
sudo apt install lilo
bensonlaur@b-lenovo:~$ sudo apt install lilo
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
下列【新】软件包将被安装:
  lilo
升级了 0 个软件包,新安装了 1 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。
需要下载 252 kB 的归档。
解压缩后会消耗 671 kB 的额外空间。
获取:1 http://cn.archive.ubuntu.com/ubuntu xenial/main amd64 lilo amd64 1:24.2-1 [252 kB]
已下载 252 kB,耗时 1秒 (166 kB/s)     
正在预设定软件包 ...
正在选中未选择的软件包 lilo。
(正在读取数据库 ... 系统当前共安装有 215673 个文件和目录。)
正准备解包 .../lilo_1%3a24.2-1_amd64.deb  ...
正在解包 lilo (1:24.2-1) ...
正在处理用于 man-db (2.7.5-1) 的触发器 ...
正在设置 lilo (1:24.2-1) ...

bensonlaur@b-lenovo:~$ sudo liloconfig
2 images '/boot/vmlinuz*' found.
New file created as: /etc/lilo.conf 
Now you must execute '/sbin/lilo' to activate this new configuation!

bensonlaur@b-lenovo:~$ sudo /sbin/lilo
Added Linux  +  *
Added Linux_Old  +
bensonlaur@b-lenovo:~$ 
安装lilo 并配置

配置完之后从启便可进入我们自己编译的内核了。 

 重启前查看下自己的当前内核版本:

uname -r

重启后菜单里有两个选项:linux    linux_old   (T_T我的windows不见了,算了后面再解决)(解决办法:linux——启动引导程序 LILO 与 grub 中的第5点)

选择第一个【linux】,等待一定时间就启动了,所有一切看上去好像没什么变化,再执行一遍:

uname -r

 如果看到的版本是最新的你编译的版本,那么恭喜,你正在体验的就是世界上最新鲜的linux内核。

那么,接下来想办法去玩一下这个内核吧。:)

 

获得帮助

订阅Linux内核邮件列表 :http://www.wowotech.net/linux_application/lkml.html


 工具介绍:

Oprofile 工具:一个功能强大的性能分析工具,在这里 被推荐下载,下载地址:在这里 (好像直接系统下载跟方便:

sudo apt install oprofile

Oprofile的安装和使用:

安装说明:这里 

安装时缺少2个库:

configure: error: popt library not found
相应的安装包:sudo apt install popt-devel

configure: error: liberty library not found
相应的安装包:sudo apt install libiberty-dev

测试时间:2016/9/16

 oprofile 库的 安装和使用 2

posted @ 2016-09-16 03:43  BensonLaur  阅读(3314)  评论(0编辑  收藏  举报