Linux kernel
What is a Kernel?
A kernel is a computer program that constitutes the central core of a computer's operating system. It has complete control over everything that occurs in the system. As such, it is the first program loaded on startup, and then manages the remainder of the startup, as well as input/output requests from software, translating them into data processing instructions for the central processing unit. It is also responsible for managing memory, and for managing and communicating with computing peripherals, like printers, speakers, etc. The kernel is a fundamental part of a modern computer's operating system.
Why the Word 'kernel'?
In the German language, the term der Kern translates to the English equivalent the core.
According to Dictionary.com, the term kernel is defined as:
It was first used in the 16th century to mean nucleus or core so it's application to Linux would seem to be a good description of that part of the system.
The Linux Kernel
The Linux kernel is a Unix-like computer operating system kernel. It is used world-wide: the Linux operating system is based on it and deployed on both traditional computer systems such as personal computers and servers, usually in the form of Linux distributions, and on various embedded devices such as routers and NAS appliances. The Android operating system for tablet computers, smartphones and smartwatches is also based atop the Linux kernel.
A Modern Monolithic Kernel
Linux is considered to implement a modern monolithic kernel-wide design approach. Linux falls into the category of a Unix-like operating system, featuring the ability to load modules at runtime, thereby allowing easy extension of the kernel's capabilities as required, while helping to minimize the amount of code running in kernel space. There are some advantages to utilitizing the monolithic kernel. They are:
- Since there is less software involved, it is faster.
- As it is one single piece of software, it should be smaller both in source and compiled forms.
- Less code generally means fewer bugs which can translate to fewer security problems.
The main disadvantages of monolithic kernels are the dependencies between system components - a bug in a device driver might crash the entire system - and the fact that large kernels can become very difficult to maintain.
The Linux kernel API, the application programming interface (API) through which user programs interact with the kernel, is meant to be very stable and to not break userspace programs (some programs, such as those with GUIs, rely on other APIs as well). As part of the kernel's functionality, device drivers control the hardware; "mainlined" device drivers are also meant to be very stable. However, the interface between the kernel and loadable kernel modules (LKMs), unlike in many other kernels and operating systems, is not meant to be very stable by design.
The Linux Kernel During Startup
The kernel in Linux handles all operating system processes, such as memory management, task scheduling, I/O, interprocess communication, and overall system control. This is loaded in two stages - in the first stage the kernel (as a compressed image file) is loaded into memory and decompressed, and a few fundamental functions such as basic memory management are set up. Control is then switched one final time to the main kernel start process. Once the kernel is fully operational - and as part of its startup, upon being loaded and executing - the kernel looks for an init process to run, which (separately) sets up a user space and the processes needed for a user environment and ultimate login. The kernel itself is then allowed to go idle, subject to calls from other processes.
Kernel Loading Stage
The kernel is typically an image file, compressed into either
zImage or bzImage formats with zlib. A routine at the head of it does a
minimal amount of hardware setup, decompresses the image fully into high
memory, and takes note of any RAM disk
if configured. It then executes kernel startup via
./arch/i386/boot/head
and the startup_32()
(for x86 based processors) process.
Kernel Startup Stage
The startup function for the kernel (also called the swapper or process 0)
establishes memory management (paging tables and memory paging), detects
the type of CPU and any additional functionality such as floating point
capabilities, and then switches to non-architecture specific Linux kernel
functionality via a call to start_kernel()
.
start_kernel()
executes a wide range of initialization
functions. It sets up interrupt handling (IRQs), further configures memory,
starts the init process (the first user-space process), and then starts the
idle task via cpu_idle()
. Notably, the kernel startup process
also mounts the initial RAM disk through a call to initrd
that
was loaded previously as the temporary root file system during the bootloader
phase. The initrd
allows driver modules to be loaded directly
from memory, without reliance upon other devices (e.g. a hard disk) and the
drivers that are needed to access them (e.g. a SATA driver). This split of
some drivers statically compiled into the kernel and other drivers loaded
from initrd
allows for a smaller kernel. The root file
system is later switched via a call to pivot_root()
which
unmounts the temporary root file system and replaces it with the use of the
real one, once the latter is accessible. The memory used by the temporary
root file system is then reclaimed.
Thus, the kernel initializes devices, mounts the root filesystem specified
by the boot loader as read only, and runs an init process.
There are several different init processes that can be utilized. Linux init
processes can be handled by init
, systemd
, or
upstart
. Although there are a number of init process that can
be used, only one init processes can be used at any one time. In other
words, there can be one and only one init process running. The init process,
regardless of the process implemented, is designated as the first process run
by the system (PID = 1). Use the ps
command to locate and identify the init
process (i.e. ps -p 1
).
A message is printed by the kernel upon mounting the file system, and by init upon starting the Init process. It may also optionally run Initrd to allow setup and device related matters (RAM disk or similar) to be handled before the root file system is mounted.
According to Red Hat, the detailed kernel process at this stage is therefore summarized as follows:
At this point, with interrupts enabled, the scheduler can take control of the overall management of the system, to provide pre-emptive multi-tasking, and the init process is left to continue booting the user environment in user space.
Releases
The maintenance releases can be found on the Wiki (general) page. Major releases as old as 2.0 (officially made obsolete with the kernel 2.2.0 release in January 1999) are maintained as needed, although at a very slow pace.
What Version do I Have?
There are a number of methods to determine the version number of the kernel on a particular Linux system.
uname
The first method is to use the uname
command with the
-r
option:
uname -r
hduser> uname -r 3.10.0-327.13.1.el7.x86_64
/proc/version file
A second way is to look at the /proc/version
file. This can be
easily accomplished by using the cat
command:
cat /proc/version
hduser> cat /proc/version Linux version 3.10.0-327.13.1.el7.x86_64 ([email protected]) (gcc version 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC) ) #1 SMP Thu Mar 31 16:04:38 UTC 2016