[RK_2014_0919]Linux Kernel Hacking

KernelBuild

Guide to building the Linux kernel.

Where do I find the kernel?

The latest source code for the Linux kernel is kept on kernel.org. You can either download the full source code as a tar ball (not recommended and will take forever to download), or you can check out the code from the read-only git repositories.

What tools do I need?

To build the Linux kernel from source, you need several tools: git, make, gcc, and (optionally) ctags and/or ncurses-dev. The tool packages may be called something else in your Linux distribution, so you may need to search for the package. The ncurses-dev tools are used if you "make menuconfig" or "make nconfig".

On Ubuntu, you can get these tools by running:

sudo apt-get install libncurses5-dev gcc make git exuberant-ctags

On Red Hat based systems like Fedora, Scientific Linux, and CentOS you can run:

sudo yum install gcc make git ctags ncurses-devel

Which kernel to build?

If you want to test to see if a bug is fixed then test against the latest stable kernel from kernel.org. If you are brave and your system is backed up the latest release candidate from Linus‘s tree is a great target. Sometimes the maintainer may want you to use an experimental branch from their own git tree. You should use the git URL they gave you instead of the git URLs below.

If you‘re doing development for a new feature, or trying to test a bug fix, you should use Linus‘ tree, or the subsystem maintainer‘s -next tree. Most subsystem maintainers keep their git trees on kernel.org. When in doubt, use Linus‘ tree.

If you don‘t understand what a stable or release candidate kernel is, you should read the KernelDevProcess page.

Downloading the latest stable tree

First, checkout the stable kernel git repository:

git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
cd linux-stable

Next, find the latest stable kernel tag by running

git tag -l | less

Find the latest stable kernel by looking for the largest vX.Y.Z values. For example, use the v3.1 tag over the v3.0.46 tag. If v3.1.1 is available, use that instead of v3.1. The kernel tags that end with -rcX are release candidate kernels, not stable kernels.

Now checkout the code associated with that kernel with the command

git checkout -b stable tag

Where tag is the latest vX.Y.Z tag you found.

Downloading the latest -rc tree

Check out Linus‘ tree:

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

Setting up your kernel configuration

Many kernel drivers can be turned on or off, or built as modules. The .config file in the kernel source directory determines which drivers are built. When you download the source tree, it doesn‘t come with a .config file. You have several options on generating a .config file. The easiest is to duplicate your current config.

Duplicating your current config

If you‘re trying to see if a bug is fixed, you probably want to duplicate the configuration on your running kernel. That config file is stored somewhere in /boot/. There might be several files that start with config, so you want the one associated with your running kernel. You can find that by running uname -a and finding the config file that ends with your kernel version number. Copy that file into the source directory as .config. Or just run this command:

cp /boot/config-`uname -r`* .config

Making the default config

Remember, a default config may not have the options you are currently using. Run

make defconfig

Making a minimal config

Compiling a kernel from scratch from a distribution configuration can take "forever" because the distros turn on every hardware configuration possible. For people wanting to do kernel development fast, you want to make a minimal configuration. Steve Rostedt uses ktest.pl make_min_config to get a truely minimum config, but it will take a day or two to build. Warning: make sure you have all your USB devices plugged into the system, or you won‘t get the drivers for them!

Changing your config

If you need to make any changes to your configuration, you can run one of the following commands. These require the curses tools to be installed.

make menuconfig

or

make nconfig

Building the kernel

Run

make

Or, if you have a multi-core processor, run

make -jX

Where X is a number like 2 or 4. If you have a dual core, 2 or 3 might be good. Quad core, 4 or 6. Do not run with really big numbers unless you want your machine to be dog-slow!

Walk away, get some coffee, lunch, or go read some comics.

Installing the kernel

To install a kernel, you will need to either manually update your GRUB configuration file, or have an installkernel script. This script installs the kernel to /boot/, installs modules to /lib/modules/X.Y.Z/ (where X.Y.Z is something like 3.1.5), and updates file /boot/grub/grub.conf. Fortunately, Ubuntu provides an installkernel script in /sbin/installkernel. The grubby RPM provides it for RPM based systems.

If you have an installkernel script, you can just run

sudo make modules_install install

Or if you don‘t have sudo installed, run

su -c "make modules_install install"

Running your kernel

First, make sure you know how to select a kernel at boot time. If your new kernel is broken, you want a way to boot into your old kernel. The grub bootloader usually presents users with a choice of kernels and you can reboot into a known good kernel if your new compile doesn‘t work. Some distros use a default grub config that hides that menu. You can usually get the menu to appear by mashing the ESC key during boot after the BIOS display disappears.

Ubuntu: To make the grub menu always appear on boot under Ubuntu, remove the GRUB_HIDDEN_TIMEOUT_QUIET line from /etc/default/grub. You may want to increase the GRUB_DEFAULT timeout from 0 to 15 seconds or more. After you‘ve finished editing the grub file you may need to update your grub file.

sudo update-grub2

You will (usually) need to reboot into your new kernel.

Patching your kernel

There are several ways to apply a patch to your kernel. Usually the maintainer will send you a patch as attachment, or inline in the mail. You should either save the file, or copy and paste the patch into a new file.

To apply the patch, go to the base kernel directory and run

git am patchfile

Where patchfile is the file you saved. If patch fails, you can run:

git am --abort
git reset --hard HEAD
git am -3 patchfile

This applies the patch, and attempts to run a three-way merge if the patch application fails. If all else fails, you can attempt to duplicate the patch changes by hand.

Then you need to re-build your kernel and reboot.

(Note: the older way of manually patching the kernel with patch -p1 <patchfile does not create any git history, which makes it hard to revert and retry different patches. You will often have to go through several patches with a maintainer to find the right fix for a bug, so having the git history is useful.)

Reverting a patch

If a maintainer wants you to revert a patch you have applied, and try a different patch, you can use git to reset the history to the point before the patch was applied.

If git log shows the patch to be removed is the first log entry, you can run

git reset --hard HEAD^

If you need to revert several patches, you can use git log to find the commit ID of the first commit before those patches. For instance, say you have applied two patches to the stable tree 3.4.17, and you want to revert those patches. git log will look like this:

$ git log --pretty=oneline --abbrev-commit
8901234 Testing patch 2
1234567 Testing patch 1
5390967 Linux 3.4.17
1f94bd4 drm/i915: no lvds quirk for Zotac ZDBOX SD ID12/ID13
0187c24 x86, mm: Use memblock memory loop instead of e820_RAM
a0419ca staging: comedi: amplc_pc236: fix invalid register access during detach

To reset your tree to 3.4.17, you can run:

git reset --hard 5390967

If you look at the commits with gitk you will notice that the 3.4.17 commit is also tagged as v3.4.17. You can reset by tag as well:

git reset --hard v3.4.17

Tips and Tricks

If you have a driver installed as a module, you can recompile just that driver. This saves time, because the kernel build system doesn‘t have to look for changes across the entire kernel tree or compile any of the built-in code.

All module files end with .ko. The kernel make system will compile just one .ko file if you give it the full path to the file:

make drivers/usb/host/xhci-hcd.ko

Note you will need to be in the base kernel source directory for this to work. You can‘t make from a different directory.

You can also reload drivers without rebooting your kernel. For example, I can remove the xHCI driver and reload it with

sudo rmmod xhci-hcd && sudo insmod drivers/usb/host/xhci-hcd.ko

Make sure that you understand the consequences of unloading your driver! For instance, if you unload the USB core driver in order to try out changes, your USB mouse and keyboard aren‘t going to work until the USB core driver is reloaded.

You may have to unload other drivers that depend on your driver before you can reload it. Use lsmod to find which drivers that are loaded depend on your driver. E.g.

$ lsmod | grep usb
usbnet                 26596  2 rndis_host,cdc_ether
mii                     5198  1 usbnet
btusb                  16575  0
usbhid                 44621  1 hid_logitech
usbcore               191078  9 xhci_hcd,rndis_host,cdc_ether,usbnet,btusb,uvcvideo,usbhid,ehci_hcd
usb_common              1093  1 usbcore

In this case, usbcore is used by xhci_hcd, rndis_host, cdc_ether, usbnet, btusb, uvcvideo, usbhid, and ehci_hcd. I would have to unload all those drivers in order to reload the usbcore module.

http://kernelnewbies.org/KernelHacking

二、网址

http://www.cnblogs.com/tom-and-jerry/articles/3978090.html

时间: 2024-10-12 03:31:59

[RK_2014_0919]Linux Kernel Hacking的相关文章

OK335xS Linux kernel check clock 24M hacking

/****************************************************************************** * OK335xS Linux kernel check clock 24M hacking * 声明: * 由于需要确认kernel中的时钟和引脚配置的时钟是否一致,于是需要去跟踪内核 * 中的代码是如何对引脚配置时钟进行识别,并对其进行相关配置的额. * * 2016-1-5 深圳 南山平山村 曾剑锋 ****************

Linux Kernel - Debug Guide (Linux内核调试指南 )

http://blog.csdn.net/blizmax6/article/details/6747601 linux内核调试指南 一些前言 作者前言 知识从哪里来 为什么撰写本文档 为什么需要汇编级调试 ***第一部分:基础知识*** 总纲:内核世界的陷阱 源码阅读的陷阱 代码调试的陷阱 原理理解的陷阱 建立调试环境 发行版的选择和安装 安装交叉编译工具 bin工具集的使用 qemu的使用 initrd.img的原理与制作 x86虚拟调试环境的建立 arm虚拟调试环境的建立 arm开发板调试环

#26 Linux kernel(内核)详解与uname、lsmod、modinfo、depmod、insmod、rmmod、modprobe...命令用法

Linux kernel: 内核设计流派: 单内核设计,但是充分借鉴了微内核体系设计的优点,为内核引入了模块化机制,内核高度模块化: 内核被模块化之后,一些最为基本最为重要的内容,被编译到内核核心:而其他更多的功能则以模块的方式来提供:而且支持动态装载和卸载各内核模块: 内核的组成部分: kernel:内核核心文件,一般为bzimage,经过压缩处理的镜像文件:通常内核核心文件保存在/boot/目录下,名称为vmlinuz-version-release kernel object(ko):内核

Eclispe+qemu+gdb调试linux Kernel

单步调试kernel说明 恩,这个文档的目标是单步调试内核,从每一个工具软件的版本号到每一个命令,都有一个说明 ubuntu1204,32位 http://www.ubuntu.org.cn/download/desktop 用vmware虚拟机安装该系统. 用64位系统时,gdb有bug.报错信息为:xxx太长.所以建议用32位系统 编译kernel 3.5.4 下载内核的地址,北京交通大学的映像地址:http://mirror.bjtu.edu.cn/kernel/linux/kernel/

linux kernel menuconfig【转载】

原文网址:http://www.cnblogs.com/kulin/archive/2013/01/04/linux-core.html Linux内核裁减 (1)安装新内核: i)将新内核copy到/usr/src下, #tar xzvf linux-2.6.38.4.tar.gz -----解压缩. ii) 将名为linux的符号链接删掉,这是旧版本内核的符号链接. #ln -s linux-2.6.38.4 linux ------建立linux-2.6.38.4的符号链接linux. (

linux kernel启动跟踪

说明,本文为我学习孟宁老师的linux内核课的一点总结,同时作为上课的作业. 作者:唐建,<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.准备工作:将编译linux内核,并将调试信息也编译进去. 使用在linux 根目录执行make menuconfig命令进入配置界面:向下找到kernel hacking 选项并进入: 然后找到"compile-time checks and compiler

ARM32 Linux kernel virtual address space

http://thinkiii.blogspot.jp/2014/02/arm32-linux-kernel-virtual-address-space.html The 32-bit ARM CPU can address up to 2^32 = 4GB address*. It's not big enough in present days, since the size of available DRAM on computing devices is growing fast and

Linux Kernel 开发报告 25 周年版

Linux基金会发布 2016 年度 Linux 内核开发报告,这次恰逢 Linux 内核 25 周年(腾云科技ty300.com),所以相比往年又更多的回顾性内容,值得一读. Linux 内核开发报告 2016 版 一些有趣的信息: 自 3.18 内核以来,合并自Linux Kernel内核的新功能覆盖面更为广泛,且涉及安全性的新功能越来越多 4.0 系列的引入仅仅是由于 Linus (基础教程qkxue.net)觉得小版本号已经超越正常人手指和脚趾数量的总和了.每一个内核版本发布都是传统意义

linux kernel 字符设备详解

有关Linux kernel 字符设备分析: 参考:http://blog.jobbole.com/86531/ 一.linux kernel 将设备分为3大类,字符设备,块设备,网络设备. 字符设备是指只能一个字节一个字节读写的设备, 常见的外设基本上都是字符设备. 块设备:常见的存储设备,硬盘,SD卡都归为块设备,块设备是按一块一块读取的. 网络设备:linux 将对外通信的一个机制抽象成一个设备, 通过套接字对其进行相关的操作. 每一个字符设备或块设备都在/dev目录下对应一个设备文件.l