[RK_2014_0919] Linux Kernel Hacking, hacking

Source: Internet
Author: User

[RK_2014_0919] Linux Kernel Hacking, 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 shoshould 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 shoshould 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.gitcd 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.gitcd linux
Setting up your kernel configuration

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. config file. you have several options on generating. 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 runningUname-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 INSTALLThe kernel to/boot/, installmodules to/lib/modules/X.Y. z/(where X.Y. Z is something like 3.1.5), and updates file/boot/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 shoshould 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 --abortgit reset --hard HEADgit 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 kernelPatch-p1 <patchfileDoes 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.

IfGit logShows 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 logWill look like this:

$ git log --pretty=oneline --abbrev-commit8901234 Testing patch 21234567 Testing patch 15390967 Linux 3.4.171f94bd4 drm/i915: no lvds quirk for Zotac ZDBOX SD ID12/ID130187c24 x86, mm: Use memblock memory loop instead of e820_RAMa0419ca 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 commitsGitkYou 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 when SS 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

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 usbusbnet                 26596  2 rndis_host,cdc_ethermii                     5198  1 usbnetbtusb                  16575  0 usbhid                 44621  1 hid_logitechusbcore               191078  9 xhci_hcd,rndis_host,cdc_ether,usbnet,btusb,uvcvideo,usbhid,ehci_hcdusb_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 wowould have to unload all those drivers in order to reload the usbcore module.

Http://kernelnewbies.org/KernelHacking

Ii. Website

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


Linux Kernel configuration is required

Kernel configuration considerations

If you want to compile the kernel yourself (the kernel source code can be downloaded to the ftp://ftp.kernel.org/pub/kernel/, domestic download can be to the ftp://ftp.cn.kernel.org/pub/kernel/ so that download faster), before the compilation usually need to use make menuconfig or make xconfig to configure the kernel. My system does not have xconfig, so I can only use menuconfig. Note the following when compiling the 2.6.23.x Kernel on my Compaq Presario V3414TX laptop:

1. Networking -->

Wireless LAN -->

[M] Generic IEEE 802.11 Networking Stack (mac80211)

This is the network stack module currently used by Linux. If you want to use a wireless network adapter (I use Intel PRO/3945 ABG), you need to compile this option into a module (or compile it into the kernel, the mac80211 module will be automatically loaded at startup ). Otherwise, you need to download and install the module at intellinuxwireless.org.

2. Device Drivers -->

Network Drivers -->

Wireless LAN -->

[M] Intel PRO/Wireless 3945ABG Network Connection

Intel PRO/Wireless 3945ABG Network Connection can be replaced with any Wireless Network adapter. Similarly, if you want to use a wireless network card, this item also needs to be compiled as a module. However, the 2.6.23.14 kernel I compiled last does not contain this item. Therefore, you must download the 3495ABG driver from intellinuxwireless.org.

3. File System -->

DOS/FAT/NT Filesystems -->

<*> VFAT (Windows-95) fs support

(437) Default codepage for FAT (NEW)

(Utf8) Default iocharset for FAT (NEW)

Selecting VFAT (Windows-95) fs support as y is to enable inner nuclear energy to support the mounting of FAT hard disks. Here codepage needs to use 437; many articles on the Internet say it should use 936, so that the file name of the FAT hard disk can be displayed in Chinese, but after I do this, when a FAT partition is mounted, It is prompted by the new kernel that it cannot be mounted. the system log shows that codepage 936 cannot be found. However, I have compiled codepage 936 into the kernel (as described below ), so I am quite confused about this. Later, it was found that whether the Chinese File Name of the FAT partition can be correctly displayed depends on the Default iocharset for FAT item, and the character encoding must use utf8. The reason may be that the default character encoding of Windows FAT partition is ascii or gb2312, while that of Linux is utf8 by default. do not recognize gb2312 ...... I don't quite understand this place.

4. File System -->

Native Language Support -->

[M] Simplified Chinese charset (CP936, GB2312)

If you want to support Chinese characters, you must select this option (... the remaining full text>
 
How to compile Linux Kernel

Kernel is the core of an operating system. It manages system processes, memory, device drivers, files, and network systems, and determines system performance and stability. Linux as a free software,
With the support of many enthusiasts, kernel versions are constantly updated. The new kernel fixes bugs in the old kernel and adds many new features. If you want to use these new features, or choose your own system
To create a more efficient and stable kernel, You need to recompile the kernel. This document uses RedHat Linux 6.0 (kernel
2.2.5) This section describes how to compile the kernel on Linux.

1. Download the source code of the new kernel

Currently, there are many websites that provide Linux source code on the Internet. You can select a site that is faster to download. .

Ii. Release the kernel source code

Because the source code is placed in a compressed file, you must release the source code to the specified directory before configuring the kernel. Log on to the/usr/src subdirectory with the root account. If you install the source code of the kernel when installing Linux, you will find a subdirectory of the linux-2.2.5. The source code of kernel 2.2.5 is stored in this directory. In addition, a linux link pointing to this directory is also found. Delete the connection and copy the source file of the new kernel to the/usr/src directory.

(1) Use the tar command to release the kernel source code

# Cd/usr/src

# Tar zxvf Linux-2.3.14.tar.gz

After the file is released successfully, a linux subdirectory is generated under the/usr/src directory. It contains all the source code of kernel 2.3.14.

(2) link/usr/include/asm,/usr/inlude/linux,/usr/include/scsi to the corresponding directory under/usr/src/linux/include.

# Cd/usr/include

# Rm-Rf asm linux

# Ln-s/usr/src/linux/include/asm-i386 asm

# Ln-s/usr/src/linux/include/linux

# Ln-s/usr/src/linux/include/scsi

(3) Delete the. o files and other subordinate files left in the source code directory.

# Cd/usr/src/linux

# Make mrproper

3. Configure the kernel

(1) Start the Kernel configuration program.

# Cd/usr/src/linux

# Make config

In addition to the preceding command, you can also use the make menuconfig command to start a menu mode configuration interface. If you have installed the X window System, you can also execute... the remaining full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.