If you are looking to gain knowledge on the Linux kernel and the different aspects associated with it, then you have landed at the right place. In this tutorial, we are going to look into the details of various topics that are associated with the Linux kernel.
Before going into the details of the kernel tutorial, let's have a glance over the basics which would help us in acquiring a better understanding of the entire topic.
An operating system is a software platform that creates an environment where a user can run different applications on a computing device. The operating system acts as a bridge between the software programs and the hardware components of a system. It is utilized by different devices which such as Mobiles, Tabs, desktop, web servers, video game consoles, etc. There are various Operating Systems available in the market, and Windows, Linux, Unix, and Mac OS X are some of the examples.
Let's have a look at the components which are required for the functioning of an operating system.
A kernel is the critical component of an operating system. It works as a bridge between the applications and data processing at the hardware level with the help of its interprocess communication and system calls.
Whenever an operating system is loaded into memory, firstly, the kernel is loaded, and stays there till the operating system gets shut down. The kernel is responsible for taking care of low-level tasks such as task management, memory management, risk management, etc.
The kernel is responsible for:
Linux operating system
Linux is an open-source platform developed in 1991 by Linus Torvalds. It is used in a wide variety of devices as an operating system, like in computers, servers, mobiles, mainframes, and other embedded devices. As it is an open source software, users can customize this operating system according to their needs. It supports almost every major computer platform such as ARM, x86, SPARK, etc. The most common usage of Linux is for Server, but it is also used in Desktop computers, ebook readers, smartphones, etc.
The Linux Kernel
A Kernel is the core component of any Linux based operating system. It represents the core aspect of the Linux distributions for Desktop computers and servers. It has a monolithic architecture, and the operating system operates entirely in the kernel space. The monolithic kernel not only encircles the Central Processing Unit, IPC, and memory, but also has system server calls, device drivers, and file system management. Linux kernel works as a layer between the software and hardware of a device.
The kernel is termed as a heart of any operating system as it controls all other programmes in a system. When a device starts, the kernel goes through a process called initialization function, such as checking memory. It takes care of the memory allocation part and creates an environment for running the applications without any disturbances.
Kernel works as a service provider, so the programs can request the kernel for accomplishing multiple tasks such as requesting the use of the disk, network card, or other pieces of hardware, and also, kernel sets interrupt for the CPU to enable multitasking. It protects the computational environment by not letting the faulty programmes enter into the operational functions of others. It stops the unauthorised programs at the entrance by not allowing the memory space and limits the CPU time that they consume.
In general, we have three types of kernels, and they are,
It is a widely used kernel by the operating systems. In a Monolithic architecture, the kernel consists of various modules which can dynamically be loaded and unloaded. This kind of architecture would extend the capabilities of the OS and allows easy extensions to the kernel.
Maintenance of a kernel becomes easy with monolithic architecture because it allows a concerned module to load and unload when there is a need to fix a bug in a particular module. So, it eliminates the tedious task of bringing down and recompiling the whole kernel for little changes. It is easier in the monolithic kernel to unload the module that is no more in usage.
Microkernel has evolved as an alternative to the monolithic kernel to address the issue of the ever-growing size of kernel code which monolithic kernel failed to do. This architecture allows some basic services like protocol stack, device driver management, file system, etc., to run in userspace. This could enhance the capability of OS with minimum code, improved security, and ensures the stability.
It restricts the damages to the impacted areas by leaving the rest of the system to function correctly without any interruptions. In Microkernel architecture, all the basic OS services are available to programmes via interprocess communication (IPC). Microkernel allows direct interaction between the device drivers and hardware.
The hybrid kernel can decide what it wants to run in user mode, and in supervisor mode. In hybrid kernel environment, usually, things like device drivers, file system I/O would run in user mode, whereas server calls and IPC are kept in supervisor mode. It gives the best experience of both the worlds.
In the Linux operating system, the system memory is divided into two different regions: kernel space and user space. Let's look into each region of memory and know the functionalities of both.
Kernel space is found in an elevated state which provides full access to the hardware devices and protects the memory space. This memory space and user space together called as Kernel-space. In a kernel space environment, core access to the system services and hardware are maintained and provided as a service to the rest of the system.
The userspace or userland is a code which runs outside operating system kernel environment.
A userspaceis defined as various applications or programmes or libraries that an operating system uses to connect with the kernel. Because of the complicated process to access the memory, malicious functions can be restricted only to the user system.
It is nothing but kernel userspace ABI (application binary user interface). It exists between program modules. ABIs are used to access the codes that are compiled and ready for usage. ABI is an interface between two binary program modules: one of these modules is an operating system facility or library, and the second one is a program run by a user.
The Linux Loadable Kernel Module
If you want to add code to the Linux kernel, the first thing you need to do is to add some source files to the kernel source tree. There may be situations where you are required to add code to the kernels while it is running, this process is called as loadable kernel module.
The benefits of LKMs (Linux Loadable Kernel Module)
Linux kernel interfaces:
The Linux Kernel provides different interfaces to the user space applications that execute different tasks and have different properties. It consists of two separate Application Programming Interfaces (APIs): one is kernel userspace, and the other is kernel internal. Kernel user space is the Linux API userspace, and allows the programs in the user space into system services and resources of the kernel.
Below mentioned are the some of the subsystems of the Linux kernel. Let's discuss them one by one in detail.
System call interface:
A system call is a programmatic process in which a program requests a service from the kernel of an operating system. It includes various hardware services such as connecting with hardware devices, and creating a communication interface among the integral parts of the Kernel. System call creates an efficient interface between an operating system and a process.
The Kernel takes care of creating and destroying the different processes, and monitors their connection to the outside world such as input and output. It handles the communication between different methods via signals, interprocess communication primitive, or pipes. In addition to all these, it also has a scheduler which controls the processes in sharing the CPU.
Memory is a vital component of an Operating system and kernel takes care of it. Linux manages the available memory and hardware mechanisms for virtual and physical mappings.
Memory management isn't just managing 4KB buffers, and it is much more than that. Linux also provides abstractions other than 4kb buffers, known as a slab allocator. Slab allocator uses the 4kb buffer as its base but then allocates structures from inside by monitoring things like, which pages are full, empty, and partially used. This allows the scheme to grow dynamically and in supporting the more significant needs of the system.
Virtual file system:
Virtual file system (VFS) is an important integral part of the kernel and facilitates common interface abstraction for the file system. The VFS creates a switching layer between the file system supported by the kernel and SCI (System Call Interface).
In addition to the above things, Linux supports various types of file systems that require different ways of organizing data to store in physical format. For instance, a disk can be formatted with the commonly used FAT file system, or Linux standard ext3 file system, or several others.
A vast part of the source code of kernel is stored in the device drivers, and that makes a specific hardware device usable. The Linux provides a driver subdirectory that is further divided into various devices that are supported, such as I2C, Bluetooth, serial, etc.
Even though much of the Linux runs on its independent architecture, some elements should be considered for the architecture efficiency and normal operation. Linux has many subdirectories, and each architecture subdirectory has many numbers of other subsidiaries. And, these subdirectories focus on the specific tasks of the kernel such as memory management, boot, kernel, etc.
Upgrading the Kernel:
As we are aware of the concept called update, we do have that option in the kernel to update it from the older version to a newer one. The retention of old configurations is significant, and to achieve this, one has to back up the configuration file in the kernel source directory. If anything goes wrong while updating the kernel, follow the below steps.
The files and systems in the Linux kernel system are where most of the users find difficulties, majorly because it is hard to tell which files are in which directories if you don’t have the knowledge. For this reason, we shall try to look at the organization of the file systems. We shall also learn how to create, delete, move, and rename directories. Additionally, we shall learn how to edit files and change permissions.
The UNIX file system can aptly fit into a one-line description; “Everything on a UNIX system that is not a process is a file”. This statement holds true for files that are a little bit more than just files. Therefore, a Linux system does not differentiate between a file and directory mainly because the directory, in essence, is a file containing names of other files, services, texts, images, and programs. Additionally, a Linux system also treats input and output devices as files. The general understanding is that the files are a sort of in a tree structure on the main hard drive; this is for easy management and order. Most of the files on a Linux system are regular files, regardless of the data they hold, be it programs, executable files or normal data.
While we have already said that everything in a Linux system is a file, there is a general understanding that there are some exceptions. For instance:
Directories: A file list of other files
Special files: These are the mechanisms used for input and output. Special files are in /dev.
Links: This is a system to make a file, including directory visible in various parts of the “system tree”.
Domain (sockets): These are special types of files similar to the IP/TCP sockets. These files are protected by the file system access control and they provide inter-process networking.
Named pipes: These types of files are the bridge between processes. They are more or less the same as sockets and enhance communication between processes without the use of networks or sockets semantics.
Remember that I had indicated that most computer users generalize that the file system is more or less like a tree, here is a good example of a Linux file system tree.
It is important to note that depending on the UNIX system in use, the file system tree may change; some files and directories may change.
The file system tree starts at the slash or the trunk, which, if you look at our table is the (/) forward slash. This is what we call the root directory; it is the underlying directory for all files. Directories are one level below the slash or root directory often have the slash in their proceeding names to indicate their position and to prevent confusion with other files or directories with similar names.
A question that plagues most Linux users is where programs and program files are stored when they are installed on the system. Let us examine this for a minute. Linux uses two partitions: The data partition where the system data, including the root directories and all system resources required to start the system are located, and the swap partition, which is an expansion of the physical memory on the computer. All files (including programs) are stored in this root directory in accordance to the Linux tree file system we have already looked at.
To show file names, properties, date of creation, permission, type, size, link files and owners, the Is command is the easiest way.
Creating and deleting files and directories on your system is very important when you want to create new files or delete redundant directories to free up space. Because the graphical interface is much or less that of MS DOS, creating files is not that difficult. Deleting files on the other hand is a moderately difficult. There are some popular file managers for the GNU/Linux, with most of them being executable files that are accessible from the desktop manager, home directory icon or the command line using the following commands.
Nautilus: This is the default file manager in the Gnome GNU desktop. There are very useful resources on how to use this tool online.
Konqueror: This file manager is typical in KDE desktops.
MC: Code named Midnight Commander is fashioned from the Norton Commander.
For easier file management, the above applications are worth the time of reading through the documentation and the effort. It is also important to note that there are many more file management applications, but these are the most popular and have a moderate difficulty level. Additionally, these tools optimize the UNIX commands in a specific manner.
To keep files and things in one place, you must allocate specific file default locations by creating directories and subdirectories for them. You can do this by using the mkdir command. For instance:
john:~> mkdir archive john:~> ls -ld archive drwxrwxrwx 2 johnjohn 4096 Jan 13 14:09 archive/
Additionally, you can create subdirectories easily in one-step by using the – p option. For instance:
john:~> cd archive john:~/archive> mkdir 1999 2000 2001 john:~/archive>ls 1999/ 2000/ 2001/ john:~/archive>mkdir 2001/reports/Suppliers-Industrial/ mkdir: cannot create directory `2001/reports/Suppliers-Industrial/':
No such file or directory
john:~/archive>mkdir -p 2001/reports/Suppliers-Industrial/ john:~/archive>ls 2001/reports/ Suppliers-Industrial/:
In some instances, you will find that the file needs more or other permission not included in the file creation permission; this is called as an access right.
Access rights are set using the same mkdir command. It is important to note that there are rules on how to name a directory. For instance, in one directory, you cannot have two files with the same name. However, it is important to note that Linux as well as UNIX are case sensitive systems (you can have two file names with you and YOU in the same directory). Additionally, there are no limits to the length of a filename, so naming files should be a breeze. You can also use special characters in the file names as long as those characters do not hold a special meaning to the shell.
Moving unclassified files uses the mv command.
john:~/archive> mv ../report[1-4].doc reports/Suppliers-Industrial/
The same command is also in use when we are renaming files
The command can also come in handy, if you want to rename files:
In the above example, we can see that only the file name changes and all the other properties doesn’t change.
The cp command is used to copy directories and files. There is also a very useful option of copying all underlying subdirectories and files (recursive copy) which uses the –R. Here is a look at the general syntax.
cp [-R] fromfile tofile
Removing or deleting files
The command rm comes into play when you want to remove single files, while the command rmdir plays its role in removing empty directories. It is important to note that some directories are undeletable (.dot and ..dot dot) because they are necessary for the proper ranking of a directory in the tree hierarchy. Like UNIX, Linux does not have a garbage can (recycle bin) and once you remove a file, that is it, it is gone and you cannot get it back unless you have a backup. To protect against this, sometimes “mistake delete”, you can activate the interactive behavior of the cp, mv, and rm commands by using the -i option. When the –i option is active, the system does not execute command such as delete immediately; instead, it prompts for confirmation, which needs that stroke of a key or an additional click to execute the command fully. For instance as shown below:
The Linux Kernel plays an important role in resource allocation to different applications. The kernel acts as a centralised place to connect the hardware and software and runs the applications in a system. Linux kernel has got more popularity due to its open source nature. Users can customize this OS according to their requirements. Therefore, it’s been used by a wide variety of devices.
The modular characteristic of the Linux kernel allows a wide range of modifications without rebooting the system. The Flexibility of the kernel enables its users to perform their level best. Moreover, the monolithic nature of this kernel has greater computational power than the microkernel.
Free Demo for Corporate & Online Trainings.