CS183 Lab 2: Linux Kernel Compilation

Goal: Assuming you have a very old laptop with limited amount of RAM (such as my 7-year Sony Vaio laptop :), and you would like to have a minimal Linux kernel so that the kernel only supports drivers specialized to your laptop. That way, not only would you get a smaller kernel that could maximize the performance of the laptop, but also more memory can be freed up by the kernel and utilized by your applications.

Details: In order to have a specialized kernel, you will need to recompile a kernel with redundant features turned off or compile unfrequently used features as kernel modules, which can later be loaded/unloaded on demand.

Kernel compilation is not as hard as it seems. Rather, it could be addictive sometimes :) Usually, the steps involve: 1) Download a kernel; 2) Configure the kernel; 3) Compile the kernel; 4) Compile the kernel modules; 5) Install the kernel modules and the kernel ; 6) Make changes to GRUB/LILO.

For the purpose of this lab, you are going to compile a minimal Linux kernel and modules for your virtual machine. Before doing so, you will need to gather most, if not all, of the information about your virtual machine, such as the chipsets used by the motherboard, various controllers used by the graphic card, network card, SCSI, sound card, etc. Those information can be obtained from the manual of your motherboard, vendor's website, BIOS as well as the Internet. Also, the commands such as '/sbin/lspci' and '/bin/dmesg', and log files such as /var/log/messages are also good sources of those information. Write them all down on a piece of paper for your reference later.

1. Download the kernel

Download the newest 2.6 Linux kernel from http://www.kernel.org/. Note: usually, you would store the downloaded files under /tmp directory, and remove them after use.

2. Configure the kernel

Before configuring and compiling the kernel, you will need to install 'ncurses-devel' and 'gcc' packages for the very first time:
  $ yum install ncurses-devel
  $ yum install gcc
Ungzip (if it's a .bz2 extension, try to use 'bzip2' instead of 'gzip' with the same options) and untar the kernel under /usr/src. Then soft-link the newly created directory (usually it looks something like linux-x.x.x.x) to linux. Change directory to /usr/src/linux and run 'make menuconfig'. Go through each options and unselect/uncheck those that do not apply to your virtual machine. Save the config before exiting.

Usually, this is an iterative process. Unchecking too many features might cause your kernel unusable. On the other hand, leaving too many would result a bloated kernel.

HINT: Be sure to include initrd (General setup/Initial RAM filesystem and RAM disk support) and LSI Logic Fusion-MPT driver (Device Drivers/Fusion MPT device support/Fusion MPT ScsiHost drivers for SPI) modules. Without them, you would get "kernel panic" message.

3. Compile the kernel

HINT: It is a good time to use the snapshot feature from the VMWare now, just in case something go wrong and you can revert your VM back to a good condition.

Run 'make bzImage' to compile the kernel. 'make help' will show you all the available alternatives.

4. Compile the kernel modules

It is not enough to compile just the kernel because some components are configured as kernel modules. So, you will need to compile those modules as well. Run 'make modules' to compile the modules.

5. Install the kernel modules and kernel

To install the kernel modules and the kernel, run 'make modules_install' and 'make install' respectively. The kernel will be installed under /boot while the modules will be installed under /lib/modules/x.x.x.x, where x.x.x.x is the kernel's version number.

Compare the size of the new kernel and the current kernel. (Note: use 'uname -a' to find out the version of the current kernel). Also, use command 'du' to find out both the size of the new and old module directories. At last, use '/bin/lsmod' to show the loaded modules in the new and old kernel.

6. Add a new entry in GRUB for your new kernel

Once a new kernel is created, a new entry for the newly created kernel should be added automatically into the GRUB. If not, add the new kernel to GRUB by editing /boot/grub/grub.conf.

Then try to reboot from the new kernel. If everything goes smoothly, ie. the virtual machine boots fine, network works, sound card works, etc., make the new kernel the default kernel in GRUB if you would like your VM to boot off the new kernel over every reboot.

Questions

  1. What are the pros and cons of using a generic and specialized kernel? You can argue this from the perspective of an user and a system administrator.
  2. Explain the following commands: lsmod, insmod, rmmod and modprobe.
  3. Read The UNIX time-sharing system and give a summary of the paper.

Scoring


Notes & Tips: