- What does a kernel source tree contain? Is this related to Linux kernel headers?
- 1 Answer 1
- How to View and Browse the Linux Kernel Source?
- What Is the Linux Kernel and How To Browse It
- The Linux Kernel Structure
- Viewing and Browsing the Linux Kernel Source Code
- Conclusion
- What is a kernel-source-tree?
- 2 Answers 2
What does a kernel source tree contain? Is this related to Linux kernel headers?
My question may be naive, but I’m a bit confused. Is (2) what I should expect to see in the Kernel Source Tree; and why do I have the explicit reference to ‘headers’? I needed to install linux-generic-headers a while back for some other software and am unsure if this might be related. I realize there is good reason for the makefiles (eg, to install modules in the /driver sub-directory), but (pretty much) only makefiles?
1 Answer 1
Distribution kernel-header packages contain, as their name implies, only kernel header files (plus the necessary plumbing) that are required to build software like kernel modules.
You shouldn’t expect to find binary files at all in a kernel source directory, except for build output. (If you configure and build a kernel yourself, the kernel source directory will also contain the compiled objects, modules, the built kernel itself and a few other binary bits and pieces that make it work.)
KConfig files are a description of the kernel configuration options (and their dependencies) that are available for a given directory/module.
Apart from that, it’s all (mostly) C source code, header files and Makefile s. There are a few helper scripts here and there, and assembly source too.
Header packages (what you installed) only contain the header part of the above (and not all of that — only the «exported» headers), and some of the build infrastructure. So what you are seeing is expected. Header packages do not contain C source code (except for some stubs and build infrastructure code). The whole point of having this type of package is to save space (and bandwidth) — the whole Linux kernel source tree is rather large, and completely unnecessary if you don’t intend to compile the kernel yourself. The header packages are built and shipped by distributions to provide just the right things necessary to build modules, but no more. (They certainly do not contain the compiled kernel.)
Addressing your comment: header packages don’t relocate anywhere. They are built for specific versions of the kernel, packaged in a specific directory, and that’s that. It’s just a set of files. (Note that the header packages don’t necessarily have the same version as the current stable kernel binary packages — the header packages are generic, and can lag behind the actual kernel you’re running. They should not, however, be from a kernel version that is more recent than the current installed (or target) kernel.)
Installed kernel binaries are usually installed in the /boot directory, along with bootloader binaries and configuration files. (This is sometimes an independent filesystem, not mounted by default.) The exact name of the files depends on the kernel and distribution. (So does the bootloader.)
Installed kernel modules reside in sub-directories of:
So for instance on my system, they are currently in
Full kernel source code: On Ubuntu, if you want the full kernel sources to build a kernel yourself, you should install following the instructions here.
You could also download a source tarball from kernel.org and unpack it somewhere (do not overwrite Ubuntu-installed files if you use this tarball, keep your personal stuff and the stuff managed by RPM separate).
/usr/src/linux is a traditional place to put kernel sources, but nothing prevents you from putting kernel sources elsewhere. This path is also often just a symbolic link to a directory. e.g. I have this on my machine:
$ ls -l /usr/src/linux lrwxrwxrwx 1 root root 18 Dec 7 17:03 /usr/src/linux -> linux-3.1.4-gentoo
The symlink is there to simplify building applications that depend on the kernel source. You link that path to your running (or target) kernel so that you don’t have to specify exact version or path information when you build a module out-of-tree. Helps a bunch for source-based distributions at least.
How to View and Browse the Linux Kernel Source?
This article briefly explains the Linux kernel is and how to browse its source code.
This tutorial is optimized for inexperienced Linux users and programmers who want to read the kernel source code or become Linux kernel developers. The Linux kernel is universal; therefore, the present document is useful for all Linux distributions.
By reading this article, you will understand what the kernel is, how it is structured and how to browse through its components. This article was initially written in 2019 and updated in 2022.
What Is the Linux Kernel and How To Browse It
The Linux kernel is the main component allowing interaction between the software and the hardware with the help of device drivers. You can think about the Linux kernel as a translator allowing communication between programs, the hardware, and the user interface.
Learning how to browse the Linux kernel is the first step for any programmer who wants to become a kernel developer. It is also useful for programmers or users who want to read its source code or modify it.
The Linux Kernel Structure
The Linux kernel is structured with directories and subdirectories known as “kernel tree”, users can browse to access specific sections of the whole code. This allows users and developers to focus on specific parts of the code.
Except for the Slackware Linux distribution, whose kernel is maintained by a single person, the rest of the Linux distributions’ huge developers’ community divide tasks based on the kernel tree. In contrast, several developers focus on drivers, others focus on security, and others on virtualization.
The kernel source tree is the following structure described through which users can browse the source code:
Viewing and Browsing the Linux Kernel Source Code
Usually, when we install Linux, we don’t include the source code. We can install Linux and keep the source code in our system. But most users discard it to download a smaller ISO image. After all, you can download the source code later if you need it.
If the kernel source code is present in your system, you can find it under the /usr/src/linux- directory, where must be replaced with the source code kernel version you are dealing with.
You also can find the last kernel source code at https://github.com/torvalds/linux.
As you can see in the previous screenshot or at the provided URL, within the source code tree, we can find the following main directories and files:
- arch: This directory contains source codes for different system architectures supported by the Linux kernel.
- block: The block I/O layer directory contains code for the management of block devices (such as hard disks, DVDs, floppy disks, etc.) and their requests.
- certs: The certs directory contains certificates and signs files to allow module signatures for the kernel to load signed modules. This can be useful to prevent malicious code from running within kernel modules like rootkits.
- COPYING: This file has information about licenses for the source code
- CREDITS: A partial listing of people who have contributed to the linux kernel source code
- Crypto: This directory contains cryptographic ciphers, which handle cryptographic and compression tasks.
- Documentation: As its name suggests, this directory includes all documentation about all kernel components.
- drivers: The drivers directory contains source code for device drivers to support hardware. Inside this directory, you’ll find subdirectories for each hardware, such as video, Bluetooth, etc. (any supported hardware).
- Fs: This directory contains sources for file systems, including virtual file systems, plus read and write file systems.
- include: This directory contains C headers for kernel files, such as functions to compile code.
- init: The init directory contains source code related to the boot process and the initialization of the Linux kernel. The source code is stored in a file named mainc.c within the /init directory. The code initializes the kernel and some initial processes.
- ipc: Inter-Process Communication, such as signals and pipes.
- Kbuild: The Kbuild file indicates object files for vmlinuz.
- Kconfig: The Kconfig file is used to disable or enable kernel features. Users deal with running the “make menuconfig” command.
- kernel: The kernel directory includes core subsystems, such as scheduler signal handling code.
- lib: This directory includes library routines, common string operations, hardware-dependent operations, debugging routines, and command-line parsing code.
- LICENSES: It contains the licenses to be applied to the kernel source and individual source files, which may have different licenses. Inside this directory, you will find four main subdirectories containing supported licenses like LGPL-X, Apache license, etc.
- MANTAINERS: The MAINTAINERS file shows the kernel maintainers’ list and instructions to submit kernel changes.
- Makefile: This file reads the .config file and builds modules and vmlinux.
- mm: The Linux kernel manages hardware and virtual memory (swap). This is the directory where the source code for memory management is stored.
- net: The net directory contains the network stack. Here, you can find code related to communication protocols, such as IP, TCP, and UP.
- README: The README file refers to guides for kernel developers in the Documentation directory previously described.
- Samples: The samples directory includes kernel and configuration files sample code.
- scripts: Here are stored the necessary scripts to build the Linux kernel.
- security: The security subdirectory contains the Security-Enhanced Linux (SELinux) security framework.
- sound: Here, you can find the sound subsystem, including sound drivers, driver architectures, and code related to sound, such as ALSA.
- tools: Here are tools for compressed kernel development, such as ACPI, cgroup, USB testing tools, vhost test module, GPIO, IIO and SPI tools, and Inter energy policy tool.
- usr: here, usr stores the initramfs, which roots the filesystem and init in the kernel memory cache. Code included allows you to execute code in user space in the boot process when the kernel isn’t fully loaded.
- virt: The virtualization directory contains the KVM (Kernel Virtual Machine) module for hypervisors, including architectures.
These are all top files and directories included in the main kernel source directory. You can browse each section depending on your needs. Once you understand the purpose of all files and directories listed above, subdirectories are pretty intuitive.
Conclusion
As you can see, this article was written for novel and medium users to understand the basics of the Linux kernel. Yet, some knowledge on development under Linux, hardware, and operating system characteristics is necessary for practical use. The previous lecture is excellent as an introduction to the Linux kernel structure and Linux distribution. Keep in mind that the Linux kernel evolves periodically. That’s why we keep the previously described content up-to-date.
We hope you found this article useful. Keep reading LinuxHint for more Linux professional articles.
What is a kernel-source-tree?
What is the Linux kernel source tree? What does it contain and what is its purpose? I’m trying to build an external module and the tutorial I’m using says to make sure that a kernel source tree is available. If it is available, where can I find it in Ubuntu? There is a similar question here: What does a kernel source tree contain? Is this related to Linux kernel headers? but I don’t see the answer to my questions. It would be nice to have this clarified.
2 Answers 2
The source-tree is a directory which contains all of the kernel source. You could build a new kernel, install that, and reboot your machine to use the rebuilt kernel. Other than for learning, people rebuild the kernel to select less-used options, or to add device drivers which are normally not bundled with Linux.
You may not find it in Ubuntu, but would have to download the source tar-file, e.g., from kernel.org . Ubuntu uses Debian packages for many things, and the latter’s website makes it easier to find the packages.
Those consist (in either case) of a «pristine» tar-file (from «upstream») and a «debian» add-on (scripts and packages). You can download both of those from Debian. If you are looking for the source for the kernel package which you have installed, you would download both parts.
You can also install the «linux-source» package: Debian and Ubuntu provide a few source-packages, this is one of the few (a quick check finds only a couple of dozen packages with «-source» in their names, compared to tens of thousands of other packages). The source-package is preferred, since there are many fixes (and customizations) needed, and the source-package has those patches incorporated into the tree.
I used to routinely build kernels until about ten years ago, since the drivers for sound, video and network were lacking.