LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Blogs > druuna
User Name
Password

Notices

- - - - - As of February 1st 2014 this blog is no longer updated - - - - -
Rate this Entry

LFS: Building a kernel

Posted 12-05-2012 at 07:53 AM by druuna
Updated 01-31-2014 at 03:22 AM by druuna (Added links to tools sourcecode)
Tags kernel, lfs

# -------------------------------------------------------------------------- #
# General information
# -------------------------------------------------------------------------- #


The chapter that deals with setting up the kernel (Linux-x.y.x) is too limited in my opinion. I do realise that my extension is out-of-scope for the LFS developers, but I do think that many will benefit from it, especially if you want to tinker with kernels and their settings.

The following will be described in this article:
  • Creating the first kernel
  • Recompiling the kernel
  • Adding a new kernel version
  • Cleanup
  • Tools to gather information
  • Using a .config file created by your host.

Out-of-scope: Grub and setting up the boot entries are not part of this article.

# -------------------------------------------------------------------------- #
# Creating the first kernel
# -------------------------------------------------------------------------- #


This step is done to create an initial bootable kernel.

The location where the kernel should be build is mentioned in the book, but it is not obvious. The de-facto location is /usr/src/. This isn't a hard rule, if you want/need to you can choose a different location.

Code:
# copy, untar and set permissions
cp /sources/linux-3.0.0.tar.xz /usr/src/
cd /usr/src
tar linux-3.0.0.tar.xz
chmod -R 0:0 linux-3.0.0

# build the kernel
cd linux-3.0.0
make mrproper
make defconfig
make menuconfig

# install the kernel and associated files
make
make modules_install
cp -v arch/x86/boot/bzImage /boot/vmlinuz-3.0.0-lfs-7.0
cp -v System.map /boot/System.map-3.0.0
cp -v .config /boot/config-3.0.0
There are many ways to start with a new, clean kernel. I choose to generate a starting point by using make defconfig. This will create a .config file that is relevant for the architecture the kernel is build upon. This quote from the README file:
Code:
"make defconfig"   Create a ./.config file by using the default
                           symbol values from either arch/$ARCH/defconfig
                           or arch/$ARCH/configs/${PLATFORM}_defconfig,
                           depending on the architecture.
Another way to create a starting point would be using the .config file from your host (or a previous, working LFS version). Instead of the make defconfig step, you need to do the following:
Code:
.
.
make mrproper
make defconfig
cp /location/of/working/.config-file .config
make silentoldconfig  # just press enter if any questions are asked
make menuconfig
.
.
NOTE: When using a .config file that was produced by your host to build the initial kernel you should have a look at the last entry in this article!!

The make menuconfig step: You might want to check to see if all the relevant options for your system/programs are (un)set. You could also decide to skip this step for now.

When all the above steps are done, continue with the next chapter in the LFS book (Using GRUB to Set Up the Boot Process) or edit the grub.cfg entry that came with your host.

Once you have a bootable kernel and are able to boot into LFS have a look at the next entry.

# -------------------------------------------------------------------------- #
# Recompiling the kernel
# -------------------------------------------------------------------------- #


Once you have a kernel that boots, you might want to fine-tune or add certain options (some packages in the BLFS book need kernel changes). To make sure you have a working kernel to fall back on if something goes wrong the following steps are needed.

Code:
# create a fall-back entry for the current, working kernel
cd /boot
cp vmlinuz-3.0.0-lfs-7.0 vmlinuz-3.0.0-lfs-7.0.prev
cp System.map-3.0.0 System.map-3.0.0.prev
cp config-3.0.0 config-3.0.0.prev

# recompile the current kernel
cd /usr/src/linux-3.0.0
make mrproper
cp /boot/config-3.0.0 .config
make menuconfig  # make the wanted/needed changes
make
make modules_install
cp -v arch/x86/boot/bzImage /boot/vmlinuz-3.0.0-lfs-7.0
cp -v System.map /boot/System.map-3.0.0
cp -v .config /boot/config-3.0.0
Add the vmlinuz-3.0.0-lfs-7.0.prev entry to your grub.cfg.

At this point you should have 2 kernel related entries in /boot:
Code:
# main 3.0.0 kernel (the one that is normally used to boot)
vmlinuz-3.0.0-lfs-7.0
System.map-3.0.0
config-3.0.0

# previous "fall-back" 3.0.0 kernel
vmlinuz-3.0.0-lfs-7.0.prev
System.map-3.0.0.prev
config-3.0.0.prev
# -------------------------------------------------------------------------- #
# Adding a new kernel version
# -------------------------------------------------------------------------- #


All the previous entries deal with a kernel that has a specific version (3.0.0 in this example). If you want to try a kernel with a higher version you need to do the following:

Code:
# copy, untar and set permissions
cp /sources/linux-3.5.0.tar.xz /usr/src/
cd /usr/src
tar linux-3.5.0.tar.xz
chmod -R 0:0 linux-3.5.0

# build the kernel
cd linux-3.5.0
make mrproper
cp /boot/config-3.0.0 .config # use the .config file from your current working kernel
make silentoldconfig
make menuconfig # make the wanted/needed changes
make
make modules_install
cp -v arch/x86/boot/bzImage /boot/vmlinuz-3.5.0-lfs-7.0
cp -v System.map /boot/System.map-3.5.0
cp -v .config /boot/config-3.5.0
Add the vmlinuz-3.5.0-lfs-7.0.prev entry to your grub.cfg.

# -------------------------------------------------------------------------- #
# Cleanup
# -------------------------------------------------------------------------- #


I do realize that you might end up with a lot of kernel related entries and I do advise to remove those entries that are no longer needed.

# -------------------------------------------------------------------------- #
# Tools to gather information
# -------------------------------------------------------------------------- #


Explaining all the possible options that need to be set, which are or are not relevant for your specific hardware is not possible. There is no out-of-the-box template that will work for all the different hardware and software combo's.

The make defconfig steps does a good job when it comes to setting up a base you can work from, but from there you need to find out which components are used by your box and set options accordingly.

The following tools can be used to gather information about your system:

The above tools might or might not yet be installed (sourcecode available from links).

If you are not interested in tuning or setting up kernel options which are specific for your box you can decide to mark just about every option as modular ([M]). Doing this will have consequences that you need to be aware of:
  • Do have a look at the last entry I wrote (Using a .config file created by your host.),
  • Although the LFS book mentions the kernel size to be anywhere between 540 and 800 Mb during the build, this isn't true when all the (default) options are enabled. The size increases dramatically. A test I performed with kernel version 3.5.2 showed that the kernel build tops at 5.8 Gb!

# -------------------------------------------------------------------------- #
# Using a .config file created by your host.
# -------------------------------------------------------------------------- #


A possible problem might arise if you use the .config file from your host to build the initial kernel.

Most distro's make their kernel as modular as possible, which is also true for those entries that are needed early in the boot process. To make sure that the kernel is able to function, these distro's use initrd. This is a problem when dealing with LFS: No initrd is used (out-of-scope, an old lfs hint is available).

If you do use a .config file from a distro that uses initrd, make sure to make the appropriate entries static instead of modular ([*] vs [M]) during the make menuconfig step.

I like my kernels as static as possible and never used a .config file that was based on a system that uses initrd, so I cannot point out all the entries that need to be changed. Looking at the entries present I can assume that you need to look at the following and change them if they are relevant for your system:
  • [SP]ATA support,
  • SCSI support,
  • RAID / LVM,
  • Input device support (not sure about this one),
  • USB support,
  • File systems (at least those needed to boot, in most cases that will be ext[34]).
There might be more.......
Posted in General
Views 1265 Comments 0
« Prev     Main     Next »

  



All times are GMT -5. The time now is 09:23 PM.

Main Menu

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration