Building and Installing the Linux Kernel
What is a kernel?
- Essentially, the "coremost" level of the OS; the outside world should not see/touch it.
- Optimally, the system wouldn't let you into the kernel. But people end up doing it... and that's one way a virus can infect a computer.
- What goes in your kernel is essentially up to you -- Windows includes a spell checker.
- As computers became faster, the the kernel became more bloated. This lead to people creating "microkernels" and "nanokernels" -- small "kernel within a kernel"
- When we move to embedded systems, we'll want only the trimmed down, lightweight, core w/o extraneous apps
- When your machine boots, the kernel explores its environment and prints notes
Root of filesystem
/ /etc /bin /tmp ... /boot
LILO options LILO = LInux LOader and is a boot loader for linux. It is equivalent to GRUB which several people in the class had used in the past. The main configuration file for the boot loader is available at /boot/lilo.conf. This was automatically generated when we first installed the system, but we can also edit it later on for further customization.
- Built automatically for you when you set up LILO
- Different configuration options:
- Display custom message
- Wait for user to pick a different OS? How long? (timeout)
- Screen resolution
- When you boot, where to get the kernel? (/boot/vmlinuz)
- Important when the system...boots.
config -> config-huge-18.104.22.168 is a symbolic link (symlink), like a shortcut on Windows (only not quite. A symlink is handled at the filesystem level. any application will see the symlink as the actually file to which it points whereas a shortcut is an ordinary file that applications can't detect as anything more than an ordinary file)
- we remove the symlinks and rename the files for the sake of screen space (you normally wouldn't want to)
- LILO looks for the vmlinuz file based on physical location on disk not where the hard-link is on the file system. When we moved the location of the file on disk, LILO wasn't able to find it and we weren't able to start up pass LILO. When we moved the file back on the FS though, the physical address of the computer was incorrect. Therefore we needed to re-run the LILO configuration process so that it would re-detect the physical location.
Let's mess with the kernel:
- vmlinuz is the compiled kernel
- Move it to /tmp and reboot...system still works because LILO looks for vmlinuz based on its physical location on the disk, not its logical location on the filesystem
- Zip it and remove the original: now the system is "bricked" because LILO can't find what it's expecting at the old disk address
- How do we save the system? Use a live CD to access the filesystem again. In our case, Slackware.
- Mount the hard drive:
mount /dev/hda1 cd /mnt/tmp
- Unzip the kernel and move it back to /boot:
gunzip oldVmlinuz mv oldVmlinuz /mnt/boot/vmlinuz
- Tell LILO to find its new location on disk:
chroot . /sbin/lilo
- Make sure all work we've done gets written from buffers before we reboot
Getting the Linux source code:
- Raw, uncompiled source code goes here
- Linux source would be here, but we didn't install it
- We'll get it from CD2/slackware/k
- The packages are compressed using the XZ algorithm. It's possible to install the package using "installpkg name_of_package".
- Copy kernel-source ... .txz to /usr/src (this isn't mandatory but may help with speed)
- Install with "installpkg kernel-source... .txz" command; it will put everything where it needs to be inside /usr/src
- /usr/src/linux is a symlink to the current version with long name
- Hidden file .config is important -- don't edit by yourself
- "make oldconfig" checks .config file to make sure everything is OK; questions you otherwise with new entries that are available or invalid entries.
- Gives up the option to set new settings that weren't around when we made our old .config
- "make menuconfig" gives you an ASCII menu interface for changing settings.
- "make xconfig" gives a GTK-based interface
- "make mrproper" essentially restores factory settings. Warning: this includes removing the .config file you may have previously configured.
- once you've configured everything, run "make"
- will compile the source into a new vmlinuz, which we could copy to /boot
run make modules_install. This will compile and install any of the modules that the kernel loads and place these in /lib/modules. *
- finally, "make install" . This will copy vmlinuz into /boot and then run /sbin/lilo so that the boot loader knows where to find the kernel.
If you manually decide to copy your vmlinuz file to /boot, you'll also need to run /sbin/lilo to update LILO with the new location of the kernel.
Once you have a kernel that you like working, you can copy your .config file to /boot/config so that you have it in an easy location next time you want to compile the kernel.
- gzip and its partner gunzip (g-un-zip) which are used to compress and uncompress files.
When attempting to compile the new kernel and boot from it the first time, it is recommended that you first make a backup of /lib/modules/uname -r before you run make modules_install so that if you need your modules you can restore them. If there is only one kernel, it is possible to just backup /lib/modules wholesale. The same idea goes for /boot.
When you are attempting to test whether a USB stick is mounting properly and need to troubleshoot, it may be helpful to run dmesg | tail to look at the recent system messages.
Some notes that were sent out to the class
Hi All, I wanted to send out a note on a couple things we found were useful today and might help others. If you are compiling a kernel with the same revision as the kernel you're currently running (can be found by running `uname -r`) when you run `make modules_install`, you will overwrite any of your current modules for the stable kernel you were previously running. As Prof. Herman mentioned, it's possible to make a copy of /lib/modules and restore these if you run into problems by using a boot CD. Another option which allows you to boot up from the previous stable kernel with no effort is to instead re-name the new kernel you're compiling while you're testing. To make a custom name for the new kernel, you can edit the MakeFile within /usr/src/linux. In the file, you should see a definition for EXTRAVERSION. You can append any string (containing letter, number, dash, period but no spaces or other symbols or you'll be in worlds of pain later) to EXTRAVERSION -- we did something like try1, etc. When you boot from your new kernel, the kernel name will include anything in EXTRAVERSION at the end of the kernel revision name. Make sure you have the `prompt` keyword in your lilo.conf file though so that you can choose the old kernel instead of just booting to the new default. One other note. If you for some reason, mess up your .config file or believe the file /boot/configure is incorrect, you can find the .config file of the currently running kernel in /proc/config.gz.