[rk_2014_0919] Linux Kernel Hacking

Source: Internet
Author: User
Tags git clone

Kernelbuild

Guide to building the Linux kernel.

Where do I find the kernel?

The latest source code for the Linux kernel are kept on kernel.org. You can either download the full source code as a tar ball (not recommended and would take forever to download), or 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 is called something else in your Linux distribution, so if you are need to search for the package. The Ncurses-dev tools is used if you ' make menuconfig ' or ' Make nconfig '.

On Ubuntu, you can get the 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 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 was fixed then test against the latest stable kernel from kernel.org. If you're brave and your system is backed the-latest release candidate from Linus's tree is a great target. Sometimes the maintainer may want your to use a 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 m Aintainer ' 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 are, 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.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 is release candidate kernels, not stable kernels.

Now checkout the code associated with this 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

Many kernel drivers can is turned on or off, or built as modules. The. config file in the kernel source directory determines which drivers is built. When you download the source tree, it doesn ' t come with a. config file. You have the 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. The config file is stored somewhere in/boot/. There might be several files as start with config, so you want the one associated with your running kernel. You can find this by running uname-a and finding the config file, the ends with your kernel version number. Copy the 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 has the options you are currently using. Run

Make Defconfig
Making a minimal config

Compiling a kernel from scratch to a distribution configuration can take "forever" because the distros turn on every har Dware configuration possible. For people wanting to does 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'll take a day or both to build. Warning:make sure you has all your USB devices plugged to 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 had a dual core, 2 or 3 might be good. Quad Core, 4 or 6. Do don't 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'll need to either manually update your GRUB configuration file, or has an Installkernel script . This script installs the kernel to/boot/, installs modules to/lib/modules/x.y.z/(where x.y.z was 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 a 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 your know how to select a kernel at boot time. If your new kernel is broken, you want a by-boot into the your old kernel. The Grub bootloader usually presents users with a choice of kernels and can reboot into a known good kernel if your NE W 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/defa Ult/grub. You could want to increase the Grub_default timeout from 0 to + seconds or more. After you ' ve finished editing the grub file, need to update your grub file.

sudo update-grub2

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

Patching your kernel

There is several ways to the apply a patch to your kernel. Usually the maintainer would send you a patch as attachment, or an 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--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, 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'll often has to go through several patches with a maintainer to find the right fix for a bug that so has the git his Tory is useful.)

Reverting a patch

If a maintainer wants you to revert a patch you had applied, and try a different patch, you can use Git to reset the hist Ory to the point before the patch was applied.

If git log shows the patch to being removed is the first log entry and 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 has applied, patches to the stable tree 3.4.17, and your want to revert those patches. git log would look like this:

$ git log--pretty=oneline--abbrev-commit8901234 testing patch 21234567 Testing patch 15390967 Linux 3.4.171f94bd4 drm/i9 15: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 are in the commits with gitk , you'll notice that the 3.4.17 commits 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 had a driver installed as a module, you can recompile just that driver. This saves time, because the kernel build system doesn ' t has 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 would compile just one. ko file if you give it the full path to the file:

Make Drivers/usb/host/xhci-hcd.ko

Note you'll need to is in the base kernel source directory for this to work. You can ' t do 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 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 W Ork until the USB core driver is reloaded.

Unload other drivers that depend on your driver before you can reload it. Use Lsmod to find which drivers is 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 are used by XHCI_HCD, Rndis_host, Cdc_ether, Usbnet, Btusb, Uvcvideo, Usbhid, and EHCI_HCD. I would the reload module with unload all those drivers on order to Usbcore.

Http://kernelnewbies.org/KernelHacking

Second, the Web site

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

[rk_2014_0919] Linux Kernel Hacking

Related Article

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.