柴米油盐计划

Kbuild 编译 Linux 内核系列(二)

Author :Wei Yang
作者公众号:杨小伟的世界

第一次总是让人激动,但是也常常伴随着恐惧和害怕。我相信有不少小白希望学习内核,常常第一步没有开始,就直接栽倒在了出发的地方。

这篇文章献给那些向往成为内核大牛的小白们,由我来帮你们破除进入内核世界的第一道障碍。

准备环境

编译内核之前有一些基本的条件。

  • 一台联网的机器(或者虚拟机)。
  • 安装了 Linux 系统。
  • 基本的命令操作技能。

除此之外, Linux 系统还要安装一些软件包(可能不全,在编译过程中遇到提示可以使用 google 搜索是缺了哪个包)。

  • Git (分布式版本控制工具,用它来获得内核源码)
  • GCC (编译器)
  • Make (构建系统)
  • Libncurse-dev (终端图形库)
  • OpenSSL-dev (加密库)
  • BC (计算器)

嗯,差不多了,开始动手吧。

获取内核

感谢 Linus 开创了 Git,自从有了 Git ,获取内核代码变得异常的方便,而且时刻都可以是最新的。

在终端输入以下命令即可:

$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

没有安装 Git 的请重复上一个步骤。

基本配置

和大部分开源软件类似, Linux Kernel 也是要配置之后才能够编译的。配置方法有好几种,我比较偏爱的是 make menuconfig 。

$ cd linux
$ make menuconfig

执行完,你可以看到如下的配置界面。

知道为啥我喜欢用这个了吧,因为这个有图形界面。虽然有好多看不懂,但是毕竟你能猜出个大概意思。

这次只是用来编译第一个内核,所以不需要什么配置,直接按右箭头,走到 Exit 退出保存即可。

注意

在执行 make menuconfig 的时候,可能会遇到一些提示说某些包没有安装导致执行失败。大家不要慌,原因是配置的过程中,内核先编译了一个用户态的配置工具,这个过程就需要依赖的软件包: Make 、 GCC 、 LD 、图形库Libncurse-dev 等。不用紧张,按照提示,缺什么就安装什么,再编译就好了。

开始编译

配置完了之后就可以编译了。

很简单,运行如下命令

$ make -j`grep -c processor /proc/cpuinfo`

如果编译成功,你就会看到目录下有一个文件叫 vmlinux 的文件。
恭喜~

安装内核

安装内核也很简单,只需要这样。

$ sudo make modules_install
$ sudo make install

另外需要注意的是,安装后有些版本可能要调整引导程序的配置。比如在 ubuntu 上,配置文件在 /boot/grub/grub.cfg 。否则有时候下一次重启还是使用旧的内核。

重启机器

执行如下命令。

$ sudo reboot

好了,等下次机器起来,那就是一个崭新的世界了。


本文是 LinuxStory 柴米油盐计划的投稿文章,由 LinuxStory 整理发布。
原作者署名为:Wei Yang
转载请注明出处,否则必究相关责任。
本文链接:https://linuxstory.org/kbuild-compile-linux-2/

对这篇文章感觉如何?

太棒了
0
不错
0
爱死了
0
不太好
0
感觉很糟
0
这里是柴米油盐计划投稿的发布账号。

    You may also like

    5 Comments

    1. Focxus on Fun

      Finally, remember that virtual slots are meant to be fun. While making money is nice,
      it’s not guaranteed. Savor the themes, animations, and
      features with Thepokies106. When yyou emphaskze
      the experience instead of jhst thee payout, the experience becomes far more rewarding. https://it.trustpilot.com/review/premiosabrinasganga.it

    2. It’s an remarkable post in favor oof all the internet people; they will
      get advantage from it I am sure. http://boyarka-inform.com

    3. Happy to join conversations, share experiences, and gain fresh perspectives throughout the journey.
      I like learning from different perspectives and contributing whenever I can. Always open to new ideas and meeting like-minded people.
      Here’s my site:https://automisto24.com.ua/

    4. Here to join conversations, share thoughts, and learn something new throughout the journey.
      I’m interested in understanding different opinions and sharing my input when it’s helpful. Always open to different experiences and meeting like-minded people.
      There is my web-site-https://automisto24.com.ua/

    5. I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.

    Leave a reply

    您的邮箱地址不会被公开。 必填项已用 * 标注

    这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理