Skip to content
ImagineWorld

Linux Kernel Programming – A Quick Introduction

node_idnode-book_linux-kernel-programming_chapter-01-linux-kernel-programming-a-quick-introduction
node_typechapter-node
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_num1
chapter_titleLinux Kernel Programming – A Quick Introduction
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

Chapter 1 — Linux Kernel Programming – A Quick Introduction

Section titled “Chapter 1 — Linux Kernel Programming – A Quick Introduction”

Hello, and welcome to this book on learning Linux kernel programming! This book, now in its second edition, has been explicitly written with a view to helping you learn Linux kernel development in a practical, hands-on fashion, along with the necessary theoretical background to give you a well-rounded view of this vast and interesting topic. It deliberately focuses on kernel development via the powerful Loadable Kernel Module (LKM) framework. This is because many, if not most, real-world/industry projects and products that involve kernel feature and/or device driver development, are done in this manner.

The focus is kept on both working hands-on with, and understanding at a sufficiently deep level, the internals of the Linux OS. In this regard, we cover everything from building the Linux kernel from source through to understanding and working with complex topics such as synchronization within the kernel.

To guide you on this exciting journey, we have divided this book into three sections. The first section covers the basics of setting up an appropriate workspace for kernel development, building the modern kernel from source, and writing your first kernel module.

The next section, a key one, will help you understand the essential details of kernel internals. Its coverage includes the Linux kernel architecture, the task structure, user and kernel-mode stacks, and memory management. Memory management is a key and interesting topic to which we have devoted three whole chapters (covering the internals to a sufficient extent, and importantly, how exactly to efficiently allocate and free kernel memory). The internal workings and deeper details of CPU (task) scheduling on the Linux OS round off this section.

The last section of the book deals with the more advanced topic of kernel synchronization – a necessity for professional design and code on the Linux kernel. We devote two whole chapters to covering key topics within this.

The book uses the kernel community’s 6.1 Long Term Support (LTS) Linux kernel. It’s a kernel that will be maintained (with both bug and security fixes) from December 2022 right through December 2026. Moreover, the Civil Infrastructure Project (CIP) has adopted 6.1 as an SLTS (Super LTS) release and plans to maintain it for 10 years, until August 2033. This is a key point, ensuring that this book’s content will remain current and valid for years to come.

We very much believe in a hands-on approach, with some 40 kernel modules (besides several user apps and shell scripts, double that of the first edition!) on this book’s GitHub repository to bring the learning to life, making it fun, practical, interesting, and useful. (Link: https://github.com/PacktPublishing/Linux-Kernel-Programming_2E)

We highly recommend you also make use of this book’s companion guide, Linux Kernel Programming Part 2 – Char Device Drivers and Kernel Synchronization: Create user-kernel interfaces, work with peripheral I/O, and handle hardware interrupts. It’s an excellent industry-aligned beginner’s guide to writing misc character drivers, performing I/O on peripheral chip memory, and handling hardware interrupts. You can get the eBook version for free along with your print copy, or alternately you can also find this eBook in the GitHub repository at: https://github.com/PacktPublishing/Linux-Kernel-Programming/tree/master/Linux-Kernel-Programming-(Part-2).

We really hope you learn from and enjoy this book. Right, let’s get on to the topic at hand now and study how to set up an appropriate kernel workspace, so that you can get the most out of this book with some practical hands-on sessions.

To get the most out of this book, it is very important that you first set up the workspace environment that we will be using throughout the book. This chapter will teach you exactly how to do this and get started.

Keeping size constraints in mind, the full and enhanced content of this chapter can be found within this book’s GitHub repository, here: I request that you download and read it in full. What follows here is an introduction to this first chapter of ours. Thanks!

In this chapter, we will install a recent Linux distribution (or “distro”) as a Virtual Machine (VM), and set it up to include all the required software packages. We will also clone this book’s code via its GitHub repository. Furthermore, this chapter also introduces a few useful projects that will help you along this exciting journey into the Linux kernel.

Right at the outset, something I would like to emphasize is that the best way to learn something is to do so empirically, hands-on, not taking anyone’s word on anything at all but trying it out and experiencing it for yourself. Hence, this book gives you many hands-on experiments and kernel code examples that you can and indeed must try out yourself. This will greatly aid you in making real progress, learning deeply, and understanding the various aspects of Linux kernel and driver development. So, remember:

Be empirical! Also, be bold, be daring, and try things out!

We will cover the following topics in this chapter in detail, which will help you to set up your working environment:

  • Running Linux as a guest VM
  • Installing an x86_64 Linux guest
  • Additional useful projects

You will need a modern and preferably powerful desktop PC or laptop. Ubuntu Desktop specifies some recommended minimum system requirements for the installation and usage of the distribution here: https://help.ubuntu.com/community/Installation/SystemRequirements. I would definitely suggest you go with a system well beyond the minimum recommendations – as powerful a system as you can afford to use. This is because performing tasks such as building a Linux kernel from source is a very memory- and CPU-intensive process. It should be pretty obvious that the more RAM, CPU power, and disk space the host system has, the better!

Like any seasoned kernel developer, I would say that working on a native Linux system is best. However, for this book, we cannot assume that you will always have a dedicated native Linux box available to you. So, we shall assume that you are working on a Linux guest. Working within a guest VM also adds an additional layer of isolation and thus safety. Of course, the downside is performance; working on a high-spec native Linux box can be up to twice as fast as compared to working on a VM!

The complete source code for this book is freely available on GitHub at https://github.com/PacktPublishing/Linux-Kernel-Programming_2E. You can clone and work on it by cloning the git tree, like so:

git clone https://github.com/PacktPublishing/Linux-Kernel-Programming_2E

The source code is organized chapter-wise. Each chapter is represented as a directory – for example, ch1/ has the source code for this chapter. The root of the source tree has some code that is common to all chapters, such as the convenient.h and klib.c source files, among others.

For efficient code browsing, I would strongly recommend that you always index the code base(s) with ctags and/or cscope. For example, to set up the ctags index on a source tree, just cd to the root of the source tree and type ctags -R. (If you haven’t already, please invest the time in learning code browsing tools like cscope and ctags.)

Unless noted otherwise, the code output we show in the book is the output as seen on an x86_64 Ubuntu 22.04 LTS guest VM (running under Oracle VirtualBox 7.0). You should realize that due to (usually minor) distribution differences – and even within the same distributions but differing versions – the output shown in the book may not perfectly match what you see on your Linux system.

With that, I now leave it to you to download (from its GitHub repo) and read the full Chapter 1 of this book, then move forward with the kernel workspace setup described therein.

Once you’re done, I’ll assume you have the work environment for the coming chapters ready. Awesome. So, next, let’s move on and explore the brave world of Linux kernel development; your kernel journey is about to begin! The next two chapters will teach you how to download, extract, configure, and build a Linux kernel from source.

Get This Book’s PDF Version and Exclusive Extras