OS2:Introduction
From SOFTICE
|
Introduction to SOFTICE's OSC Labs
- Installing the SOFTICE OS Labs virtual appliance
- Recompiling a kernel
- Writing a simple Loadable Kernel Module (LKM)
- Inserting, removing and listing LKMs
- Alessio Gaspar , Clark Godwin (original version, summer 2006)
- Alessio Gaspar (update to 2.6.24.3 & vmware)
Synopsis
This lab's objective is to get you started working on the SOFTICE VMware OS Labs virtual appliance. Then, we will write our first loadable kernel module (LKM) which will simply log messages like "hello world" to the console of the system.
[Briefing] Getting started
Installing the SOFTICE OS Labs virtual appliance
Your first task is to read this page in order to install the necessary software on your machine (running windows or Linux) and then download the virtual appliance in which you will work on the laboratories described on this wiki.
Recompiling your own kernel (optional)
While we are mostly going to use Loadable Kernel Modules during our laboratories, it is interesting for students to rebuild their own kernel.
The sources of the Linux kernel (version 2.6.24.3) are in the directory /usr/src/linux/. You can navigate with a terminal to that directory and issue the following commands;
cd /usr/src/linux/ make menuconfig
This command will make a text-based menu appear in your terminal. The purpose of this application is to help you configure your kernel (e.g. including appropriate features or device drivers directly in the kernel or as LKMs) and generate the /usr/src/linux/.config file
This configuration is saved when you exit this program and is then used when compiling the new modules and kernel with;
make modules make vmlinuz
This will generate the new kernel which can then be installed in /boot/ by issuing;
make install
Feel free to experiment various kernel settings on a copy of your virtual machine but keep the one you first downloaded handy to be able to work on the rest of these labs in case you break something. This experiment is optional but your instructor will assist you should you be interested in learning more about kernels compilations and installations procedures.
[Solved] Hello Kernel World module
Loadable Kernel Modules (LKMs)
Linux allows you to load new code into a running kernel (e.g. device drivers) without need to reboot the machine. This is accomplished by using Loadable Kernel Modules (LKM). LKMs are units of code which are not linked as a complete executable but meant to be dynamically linked into a running kernel. They extend the functionality of the kernel without the need to reboot the system and are typically used to add support for new hardware or filesystems. When a module is no longer needed, it can be removed from the kernel, thus freeing up system resources. This makes for a very flexible and convenient way to add "features" and "capabilities" to the kernel.
Without Linux Loadable Kernel Modules, the Linux kernel would have to be built with all possible anticipated functionality directly into the kernel image. This would result in a very large kernel requiring more system resources. Also, changing directly the kernel code means that it must be re-compiled and re-installed on the system for the new functionality to be available.
Your "softice" user account on the SOFTICE OS Labs virtual appliance server is a regular user account. This means that while you can write and compile LKMs on it, you aren't allowed to load them into the running kernel. To do this, you will have to use the sudo command which will prompt you for your "softice" user account's password and allow you to execute commands as if you were the 'root' user of the system.
Let's start our exploration of LKMs with the following. Here is the source code of a simple LKM which is going to display a "hello world" message when loaded into the kernel and another message when unloaded. Of course, LKMs are not like the programs you have been writing all along. They execute as part of the kernel in what has been defined in the lectures as "kernel land". One of the (many) particularities of programming in "kernel land" vs. "user land" is that your programs can't just print a message on the screen. This is due to the fact that LKMs have no terminals with which to communicate, they are meant to be executed without supervision inside the kernel without any sort of interactions with users.
For this reason, when we want our LKMs to display a message we will have them add an entry to the kernel logs. Fortunately, the syntax of the printk system call, which is available for programs written in the kernel to write to the system's kernel logs, is very similar to the syntax of the printf function which is made available for "user space" programs by the standard Library. It is worth pointing out that other standard library functions (malloc, realloc, ...) are also unavailable to kernel programmers. Think of stdlib as being "a layer" on top of the kernel itself, no way to use it when programming at the kernel level!
Sample program to test
Here's a code sample which uses printk to implement the LKM equivalent of a hello world program:
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
static int hello_init(void)
{
printk( KERN_ALERT "[hello mod] Hi there \n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "[hello mod] all done\n");
}
module_init(hello_init);
module_exit(hello_exit);
[Solved] Playing around with our first LKM
Now that we have a first LKM, we can compile it and insert it inside of the kernel of our Linux virtual appliance.
Compiling a LKM
Navigate to the folder "softice-os-labs" located on your desktop and to the "00_hello" subfolder. There you will find the source of the above LKM as the file solved-01-hello.c.
You can now compile this LKM by issuing the softice-make-lkm command followed by the name of your LKM .c file without the .c suffix.
softice-make-lkm solved-01-hello
Inserting a module in the Kernel
The previous section resulted in the generation of the solved-01-hello.ko. The .ko suffix stands for kernel object and indicates that, unlike a regular executable, it is not meant to be ran standalone but rather inserted into the kernel. Issue the following commands to insert this module into the kernel:
sudo insmod solved-01-hello.ko
You can then verify that the LKM has been loaded into the kernel by listing all loaded LKMs
lsmod
If your module has been loaded, you can then made sure that it did its work by checking the kernel logs
tail /var/log/kern.log
Removing a Module from the UML Kernel
Modules are unlinked from the kernel by using the rmmod program. Issue the following commands to remove your module from the kernel:
sudo rmmod solved-01-hello
Verify that the module was appropriately unloaded by using
lsmod
Also verify that the kernel logs reflect the execution of your second printk
tail /var/log/kern.log
References
[LDD] Linux Device Drivers, Third Edition (safary reference)
[LPMPG] The Linux Kernel Module Programming Guide

