kernel source code location in linux
I am working in Windows and running fedora through VMware. By default, the kernel source tree should be in usr/src/ directory. Instead I could find only an empty directory called kernels. The actual source code is missing there. When browsing I thought it might be in other directories like /boot . But I couldn’t find source tree there as well. I need to modify system call tables for which I should be able to access those source codes.
3 Answers 3
Kernel source is not installed by default. You can follow e.g. the instructions here to install the source and build your kernel.
You probably need to install the kernel-devel package.
On Fedora, all of the software is managed through a system called the Red Hat Package Manager (RPM). Every file (other than user data and other transient files like logs and such) is «owned» by a package. The ls command is from coreutils , the kernel is probably from kernel and BZFlag (the greatest Linux game ever) is provided by the bzflag package.
In the good old days, you would try to install software by using the rpm command (type » man rpm » for more information) and giving it the path to an RPM file that you had downloaded. The rpm command would then dutifully respond by informing you that you first need to satisfy 33 prerequisites. Then you would go download those 33 prerequisites and try to install them, only to find that each of those had their own prerequisites. And so on.
These days, Fedora has a nice set of online repositories of packages (and you can add others for third party software), and a program called yum that can download databases of all the packages, and you tell yum «install package foo for me» (by typing something like » yum install kernel-devel «) and it goes and figures out that in order to install that package, you need a bunch of other prerequisite packages, and so on. It then automatically downloads and installs everything you need, and away you go. For more information on the various incantations that make yum go, see » man yum «).
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.
Where can I find the source code of Ubuntu?
I would like to know where to find the source code of Ubuntu. I’d like to see how far it is «open source».
Despite being «open» there are things called binary blobs in the kernel as far as I’m aware. It’s open, but not decipherable in some places.
5 Answers 5
apt-get source linux-source-3.2.0
sudo apt-get build-dep $package
sudo apt-get build-dep abiword apt-get source abiword
The source code for every package in the main and universe archives is in Launchpad, or you can get it by enabling Sources in the Software Properties dialog, and then doing apt-get source $packagename in a terminal, after refreshing the package information.
Source code for packages in the partner repositories is not generally available, as they are mostly not open source applications. This is true for some items available in the Software Center as well.
Software in linux distributions is organized into packages. Each package either contains user facing software like Firefox, or libraries like libasound2 which is required by firefox. If you install Firefox, the apt system will figure out what prerequisite packages exist, will download and install them. Now, these packages are generally distributed as binary files that have already been compiled for a given processor architecture, but the exact source used to compile that version of a package is available via apt-get source firefox .
The vast majority of things in Ubuntu’s repositories of packages are open source. But Ubuntu does have a list of approved proprietary software. While this software doesn’t satisfy the freedom goals of Ubuntu, they are greatly desired by the community, and provide a lot of value. Examples of this are Skype or Sun Java (now removed). These packages are not listed on the Ubuntu packages site.
You can download the complete source code ISOs from the Ubuntu download servers:
- http://cdimage.ubuntu.com/releases//release/source/ for currently-supported releases. For example, http://cdimage.ubuntu.com/releases/12.04/release/source/ for 12.04.
- http://old-releases.ubuntu.com/releases//release/source/ for obsolete/EOL releases. For example, http://old-releases.ubuntu.com/releases/12.10/source/ for 12.10. However, source ISOs seem to be available only for 12.10 onwards.