Note

The Funtoo Linux project has transitioned to "Hobby Mode" and this wiki is now read-only.

Difference between revisions of "Building a Kernel from Source"

From Funtoo
Jump to navigation Jump to search
 
(Added a less lengthy version of building a custom kernel.)
(36 intermediate revisions by the same user not shown)
Line 2: Line 2:


Even though many of us are using enterprise-ready kernels in datacenters, there is almost nobody who hasn't at least considered building a kernel for his laptop / PC.
Even though many of us are using enterprise-ready kernels in datacenters, there is almost nobody who hasn't at least considered building a kernel for his laptop / PC.
We are showing here how an intermediate Linux user can use an alternative to the standard beginners "genkernel" approach,  to compile a custom kernel,  in a relatively still speedy and easy set up.
We are showing here how an intermediate Linux user can use an alternative to the standard beginners "genkernel" approach,  to compile a custom kernel,  in a relatively speedy and easy set up.


'''minimum requirements'''
== Minimum Requirements ==
You should understand the way how things work in a terminal and how to use an editor and tweak config files. This is crucial.
* '''Understand the command line'''
You don't need much knowledge about the linux kernel and it's internals. Nevertheless, you have to know at least where the files are located, how they are used and what is the file logic behind the overall structure. Otherwise you should consider using a non source based linux distribution.
* '''Know where the kernel files are located'''
If you are scared now, don't worry - we are going to build a kernel the Funtoo way and you will pick up everthing necessary to accomplish this challenge, step by step, so the next time you do it yourself and become a real Funtoo knight!


'''assumptions'''
== Assumptions ==
You start from an installed Funtoo system on the disk, or at least, you are on stage3 in a chrooted environment from a live cd, following somehow the Funto [[Installation (Tutorial)|Installation Tutorial]].
You start from an installed Funtoo system on the disk, or at least, you are on stage3 in a chrooted environment from a live cd, following somehow the Funto [[Installation (Tutorial)|Installation Tutorial]].


In this case we are building a kernel that is booting root in LVM over encrypted LUKS container.
== Less advanced version ==
If you don't have this setup, don't worry, you just don't need all the modules, but everything else is similar.
=== Emerging the kernel sources ===
To begin, we have to figure out which kernel sources we will use. If you are unsure about which sources are available and what their benefits and drawbacks are, check out the [[Funtoo_Linux_Kernels| Kernels]] page.


After you have made a decsion as to which kernel you want to install, emerge it:
<console>
###i## emerge vanilla-sources
</console>
Portage will now go about installing the sources to ''/usr/src''. It will also symlink the kernel-version directory to a directory called ''linux''.


= Getting everything in order to start =
=== Configuring the kernel ===
Now that the kernel sources are on your system, you should configure them. To do this, change your directory to ''/usr/src/linux''
<console>
###i## cd /usr/srclinux
</console>
As we are now in the kernel sources directory, we can run a script that allows us to modify them. Run:
<console>
###i## make menuconfig
</console>
While you edit the sources, keep the following in mind:
* To build something into your kernel, press y when you have it selected.
* To exclude something from your kernel, press n when you have it selected.
* To build something as a module, press m.


First there is the decision which linux kernel sources we need.
Things that you may need to include in your kernel:
* Wireless/LAN drivers
* Support for your graphics card
* Support for your audio card
* Support for USB devices
{{fancynote| Many pages on the wiki will tell you the kernel requirements for the application that they are about. Keep your eyes open for the blue background, white text sections of pages. Like on this one: [[uvesafb| uvesafb]]}}
 
=== Building and installing the kernel sources ===
After you finish configuring your kernel sources, you will need to build them. To build your sources, run the following:
<console>
###i## make
</console>
{{fancytip| You can add -j<number of processing cores + 1> after make to build the kernel more quickly.}}
 
When the kernel and its modules finish building, install them:
<console>
###i## make install modules_install
</console>
Now that you have installed your kernel and modules, it is a good idea to install an initramfs. If your system has a separate ''/usr'' partition, is encrypted, or uses some other non-standard configuration, it will probably not boot without an initramfs. See [[Building_a_Kernel_from_Source#Initramfs| Initramfs]].
 
== Advanced version ==
=== Getting ready to start ===
 
{{fancynote| In this case we are building a kernel that is booting root in LVM over encrypted LUKS container.
If you don't have this setup, don't worry, you just don't need all the modules, but everything else is similar.}}
First, there is the decision which linux kernel sources we need.
There are plenty of them in the repositories around, often it is not easy to distinguish between them.
There are plenty of them in the repositories around, often it is not easy to distinguish between them.


Line 27: Line 69:
From there you should have a running system booting nicely from your own build (just little bit bloated) kernel. This is more than you can expect from any other ready to go distribution.
From there you should have a running system booting nicely from your own build (just little bit bloated) kernel. This is more than you can expect from any other ready to go distribution.


'''Attention'''
{{fancynote| We are using RedHat's dracut in order to build a nice initramfs (containing all the necessary tools and extra drivers our kernel might need to start the system). Although dracut is the way to go, more sophisticated and not as buggy as gentoo's genkernel approach, more and more funtoo geeks start using slashbeast's better-initramfs, which we will cover at the end of this howto! So after having set up a genkernel from debian or gentoo sources we are going to build a kernel with either (or both) dracut or/and better-initramfs. So gentoo sources with genkernel is always my backup if anything is not working correctly on my system. For the slightly more geeky approach with my own initram I am using pf-sources, ck-sources or any other more or less heavily patched sources.}}
We are using RedHat's dracut in order to build a nice initramfs (containing all the necessary tools and extra drivers our kernel might need to start the system).
 
Although dracut is the way to go, more sophisticated and not as buggy as gentoo's genkernel approach, more and more funtoo geeks start using slashbeast's better-initramfs, which we will cover at the end of this howto!
 
So after having set up a genkernel from debian or gentoo sources we are going to build a kernel with either (or both) dracut or/and better-initramfs.
So gentoo sources with genkernel is always my backup if anything is not working correctly on my system. For the slightly more geeky approach with my own initram I am using pf-sources, ck-sources or any other more or less heavily patched sources.


Let's go!
Let's go!


== kernel sources ==
=== Kernel Sources ===
We are going to use the kernel sources from the funtoo git repository.
The source you use on your system is up to you. For a laptop or desktop system, the following are recommended:
 
* '''{{Package|sys-kernel/pf-sources}}'''
Which sources you like to use is up to you, and your needs.
* '''{{Package|sys-kernel/ck-sources}}'''
For my laptop or desktop system I am recommending any one of:
* '''{{Package|sys-kernel/gentoo-sources}}'''
 
* '''{{Package|sys-kernel/git-sources}}'''
  sys-kernel/pf-sources
* '''{{Package|sys-kernel/sysrescue-std-sources}}'''
  sys-kernel/ck-sources
* '''{{Package|sys-kernel/debian-sources}}'''
  sys-kernel/gentoo-sources
{{fancynote| If you are unsure of which sources you would like to use, emerge <code>gentoo-sources</code>. That's always a safe bet for a general system. For more information on available kernels, check out: [[Funtoo Linux Kernels]]}}
  sys-kernel/git-sources
  sys-kernel/sysrescue-std-sources
  sys-kernel/debian-sources
 
Please, have a look in the ebuild description, look onto their homepage and take the one that suits you best!
If you are unsure for now, use sys-kernel/gentoo-sources. That's always a safe bet for a general system.
 
It is not a problem to have various kernels installed parallel, so go on with any one of them.
 
I am going to use the sys-kernel/pf-sources now, as I already had the gentoo-sources installed.
 
== prerequisites ==


I don't know which tools you have already installed, so some information here might be redundant.
=== Prerequisites ===
It doesn't harm to just copy and paste and do some steps again.


First, we look into our /etc/make.conf:
Regardless of the tools you already have installed, it is recommended to follow the steps below, even if you find them to be redundant.
First, we edit our <code>/etc/portage/make.conf</code>:


<pre>
<pre>
Line 68: Line 92:
CFLAGS="-O2 -pipe -march=native -ftracer -fforce-addr"
CFLAGS="-O2 -pipe -march=native -ftracer -fforce-addr"
CXXFLAGS="${CFLAGS} -fpermissive -fomit-frame-pointer"
CXXFLAGS="${CFLAGS} -fpermissive -fomit-frame-pointer"
KDIR=/usr/src/linux
KDIR=/usr/src/linux
KERNEL="symlink build"
KERNEL="symlink build"
USE="$KERNEL ....here are your use flags...."
USE="$KERNEL ....here are your use flags...."
## These modules are available:
## These modules are available:
## DRACUT_MODULES="dracut_modules_biosdevname dracut_modules_btrfs dracut_modules_caps dracut_modules_crypt dracut_modules_crypt-gpg dracut_modules_dmraid dracut_modules_dmsquash-live dracut_modules_gensplash dracut_modules_iscsi dracut_modules_livenet dracut_modules_lvm dracut_modules_mdraid dracut_modules_multipath dracut_modules_nbd dracut_modules_nfs dracut_modules_plymouth dracut_modules_ssh-client dracut_modules_syslog"
## DRACUT_MODULES="dracut_modules_biosdevname dracut_modules_btrfs dracut_modules_caps dracut_modules_crypt dracut_modules_crypt-gpg dracut_modules_dmraid dracut_modules_dmsquash-live dracut_modules_gensplash dracut_modules_iscsi dracut_modules_livenet dracut_modules_lvm dracut_modules_mdraid dracut_modules_multipath dracut_modules_nbd dracut_modules_nfs dracut_modules_plymouth dracut_modules_ssh-client dracut_modules_syslog"
## We are using this ones for LVM / LUKS
## We will use these modules for LVM / LUKS:
DRACUT_MODULES="crypt lvm plymouth biosdevname dmraid crypt-gpg dmsquash-live ssh-client syslog"
DRACUT_MODULES="crypt lvm plymouth biosdevname dmraid crypt-gpg dmsquash-live ssh-client syslog"
</pre>
</pre>


Now we are setting the package keywords:
Next, we set the package keywords by adding the following to <code>/etc/portage/package.use</code>:
 
<pre>
<pre>
/etc/portage/package.use/dracut:
<console>
sys-kernel/dracut dm net device-mapper crypt lvm
sys-kernel/dracut dm net device-mapper crypt lvm
</console>
</pre>
</pre>
If you don't have lvm over encrypted LUKS you probably just add the "net" keyword here, or "selinux".
 
{{fancynote| If you don't have lvm over encrypted LUKS you just add the "net" keyword here, or "selinux".}}




After that we are going to build our packages:
Next, we build our packages:
<console>
<console>
# ##i##emerge -av app-portage/gentoolkit sys-kernel/pf-sources sys-kernel/dracut sys-boot/plymouth sys-boot/plymouth-openrc-plugin
###i## emerge -av app-portage/gentoolkit sys-kernel/pf-sources sys-kernel/dracut sys-boot/plymouth sys-boot/plymouth-openrc-plugin
</console>
</console>


Finished? Well, then let's go on and
=== Preparing the kernel ===
 
== preparing the kernel ==


We go now to the sources directory and enter the following commands to update the kernel's  .config  file:
We go now to the sources directory and enter the following commands to update the kernel's  .config  file:
<console>
<console>
# ##i##cd /usr/src/linux/
###i## cd /usr/src/linux/
# ##i##make clean
###i## make clean
   CLEAN  .
   CLEAN  .
   CLEAN  arch/x86/kernel/acpi/realmode
   CLEAN  arch/x86/kernel/acpi/realmode
Line 124: Line 143:
   CLEAN  .tmp_versions
   CLEAN  .tmp_versions
   CLEAN  vmlinux System.map .tmp_kallsyms2.S .tmp_kallsyms1.o .tmp_kallsyms2.o .tmp_kallsyms1.S .tmp_vmlinux1 .tmp_vmlinux2 .tmp_System.map
   CLEAN  vmlinux System.map .tmp_kallsyms2.S .tmp_kallsyms1.o .tmp_kallsyms2.o .tmp_kallsyms1.S .tmp_vmlinux1 .tmp_vmlinux2 .tmp_System.map
# ##i##zcat /proc/config.gz > /usr/src/linux/.config
###i## zcat /proc/config.gz > /usr/src/linux/.config
</console>
</console>


Next, we run <tt>make localmodconfig</tt>. You will get some questions which you can answer mostly with either M (compiled as a module) or Y (compiled directly into the kernel). If you are not sure what to choose, press enter, and the default option will be selected.
<console>
<console>
# ##i##make localmodconfig
###i## make localmodconfig
</console>
 
You will get some questions which you can answer mostly with either M (compiled as a module) or Y (compiled directly into the kernel).
<pre>
Enable different security models (SECURITY) [Y/n/?] y
Enable different security models (SECURITY) [Y/n/?] y
Enable the securityfs filesystem (SECURITYFS) [Y/?] y
Enable the securityfs filesystem (SECURITYFS) [Y/?] y
Line 169: Line 185:
warning: (MEDIA_TUNER) selects MEDIA_TUNER_TEA5761 which has unmet direct dependencies (MEDIA_SUPPORT && VIDEO_MEDIA && I2C && EXPERIMENTAL)
warning: (MEDIA_TUNER) selects MEDIA_TUNER_TEA5761 which has unmet direct dependencies (MEDIA_SUPPORT && VIDEO_MEDIA && I2C && EXPERIMENTAL)
warning: (ACPI_HOTPLUG_CPU) selects ACPI_CONTAINER which has unmet direct dependencies (ACPI && EXPERIMENTAL)
warning: (ACPI_HOTPLUG_CPU) selects ACPI_CONTAINER which has unmet direct dependencies (ACPI && EXPERIMENTAL)
root@[~src/linux] #
</console>
 
</pre>


Now comes the most adventurous part!
Now comes the most adventurous part!


= Building the Kernel =
=== Building the Kernel ===
<console>
<console>
# ##i##make -j8  bzImage
###i## make -j8  bzImage
# ##i##make -j8 modules
###i## make -j8 modules
# ##i##make modules_install
###i## make modules_install
# ##i##make install
###i## make install
</console>
</console>


 
== Initramfs ==
'''Building an initramfs or not?'''
=== Initramfs, or not? ===
 
The reason to build a kernel with an initramfs is mostly for interoperability (e.g. live-cd's) and special features like an included busybox, ssh, etc.  But mostly, and that's why we are doing this here now, to have a proper kernel up and running quick'n dirty in a reasonable time without fighting hours and days until a more or less exotic hardware is perfectly run by the kernel.  
The reason to build a kernel with an initramfs is mostly for interoperability (e.g. live-cd's) and special features like an included busybox, ssh, etc.  But mostly, and that's why we are doing this here now, to have a proper kernel up and running quick'n dirty in a reasonable time without fighting hours and days until a more or less exotic hardware is perfectly run by the kernel.  
After having a proper basic kernel running with the help of an initramfs, I really recommend you to go a step further and build a true kernel with all features includes without an initramfs. But this could be pain in the ass and very time consuming - so we do it the funtoo way here - at least in the second example when we stick to better-initramfs instead of Red-Hat's ''dracut''.
After having a proper basic kernel running with the help of an initramfs, I really recommend you to go a step further and build a true kernel with all features includes without an initramfs. But this could be pain in the ass and very time consuming - so we do it the funtoo way here - at least in the second example when we stick to better-initramfs instead of Red-Hat's ''dracut''.


= Option one: Initrd with dracut =
=== Option one: Initrd with dracut ===
 
To build the initrd we just execute


To build the initrd with dracut, we just execute:
<console>
<console>
# ##i##dracut -f --fstab --xz /boot/initramfs-3.2.6-pf.img  3.2.6-pf
# ##i##dracut -f --fstab --xz /boot/initramfs-3.2.6-pf.img  3.2.6-pf
</console>
</console>


Generally, this really should be enough!
Generally, this should be enough!
If you experience booting problems like missing modules / drivers then just boot from the genkernel section and fix the initrd building. You can look into the man page to tweak the command a bit (e.g. --add-drivers "xz dm_crypt" etc...).
If you experience booting problems like missing modules / drivers then just boot from the genkernel section and fix the initrd building. You can look into the man page to tweak the command a bit (e.g. --add-drivers "xz dm_crypt" etc...).


Ok let's go on and finish the taks, we are going to tell now grub how to boot off correctly!
Ok let's go on and finish the taks, we are now going to tell grub how to boot off correctly! Edit <tt>/etc/boot.conf</tt>:


<pre>
<pre>
root@[~src/linux] # nano /etc/boot.conf


boot {
boot {
Line 227: Line 238:
         params += lvm root=/dev/mapper/vg-root
         params += lvm root=/dev/mapper/vg-root
}
}
</pre>
</pre>


That's it almost!
That's it -- almost!


Now write to the grub.cfg with the new handy boot-update script from funtoo:
Now write to the <code>grub.cfg</code> with the handy [[Boot-Update]] script:
<console>
<console>
root@[~src/linux] # boot-update -v
###i## boot-update -v
</console>
<pre>
root@[~src/linux] # sudo boot-update -v


  boot-update 1.5.2 / Copyright 2009-2011 Funtoo Technologies
  boot-update 1.5.2 / Copyright 2009-2011 Funtoo Technologies
Line 249: Line 256:


  * Completed successfully.
  * Completed successfully.
</pre>
</console>


Okay,... here you go..! :)
Okay,... here you go..! :)
Line 255: Line 262:
Reboot and see how it works!
Reboot and see how it works!


= Option two: using better-initramfs =
=== Option two: using better-initramfs ===


Piotr's better-initramfs is another approach that is tiny, nice and shiny and seems to become more and more a favourite among funtoo'ers. The biggest plus is that, once built it is kernel version independant.
Piotr's better-initramfs is another approach that is tiny, nice and shiny and seems to become more and more a favourite among funtoo'ers. The biggest plus is that, once built it is kernel version independent.


For using this you just do the following steps:
To use better-initramfs, follow these steps:
<pre>
# download sources
1. download sources
# build kernel with "make bzImage"
2. build kernel with "make bzImage"
# download better-initramfs
3. download better-initramfs
# run better-initramfs
4. run better-initramfs
# adjust <tt>/etc/boot.conf</tt>
5. adjust /etc/boot.conf
</pre>


Here is how in detail:
''Here is how in detail:''


Assuming you did install already a genkernel backup or at least you have a working bzImage + modules installed, we rush forward to step 3:
Assuming you did install already a genkernel backup or at least you have a working bzImage + modules installed, we rush forward to step 3:


=== download better-initramfs ===
=== Downloading Better-initramfs ===


<console>
<console>
# cd /usr/src/;
###i## cd /opt/
# git clone https://github.com/slashbeast/better-initramfs.git
###i## git clone https://github.com/slashbeast/better-initramfs.git
 
###i## cd better-initramfs
# /src # cd better-initramfs
###i## ls
# better-initramfs git:(master) ls
AUTHORS    LICENSE  README.rst  bootstrap  output  sourceroot
AUTHORS    LICENSE  README.rst  bootstrap  output  sourceroot
ChangeLog  Makefile  TODO        examples  scripts
ChangeLog  Makefile  TODO        examples  scripts
</console>
</console>


=== build the better-initramfs ===
=== Build Better-Initramfs ===
<console>
<console>
# better-initramfs git:(master) sudo bootstrap/bootstrap-all
###i## bootstrap/bootstrap-all
Passwort: xxx
###i## make prepare
# from here go and grab a coffee
###i## make image
# sudo make prepare
###i## mv output/initramfs.cpio.gz /boot
# sudo make image
# sudo mv output/initramfs.cpio.gz /boot
</console>
</console>


=== adjust grub ===
=== Adjust grub ===


Taking the above setup we edit the /etc/boot.conf
Taking the above setup we edit the <tt>/etc/boot.conf</tt>:
as I installed genkernel first, and dracut after - you see this setup:


<pre>
<pre>
Line 339: Line 340:
Okay,... here you go..! :)
Okay,... here you go..! :)


update the grub.cfg, then reboot and see how it works!
update the <tt>grub.cfg</tt> with boot update, then reboot and see how it works!


<console>
<console>
root@[~src/linux-3.2.6-pf] # boot-update -v     
###i## boot-update -v     


  boot-update 1.5.2 / Copyright 2009-2011 Funtoo Technologies
  boot-update 1.5.2 / Copyright 2009-2011 Funtoo Technologies
Line 355: Line 356:


  * Completed successfully.
  * Completed successfully.
 
###i## reboot
root@[~src/linux-3.2.6-pf] #
 
root@[~src/linux-3.2.6-pf] # reboot
 
System going down for reboot!
 
</console>
</console>



Revision as of 02:23, August 2, 2014

Setting up a proper kernel yourself - lean, mean and tailored to your hardware, is the challenge by which a linux user can graduate to becoming a Funtoo knight ;-)

Even though many of us are using enterprise-ready kernels in datacenters, there is almost nobody who hasn't at least considered building a kernel for his laptop / PC. We are showing here how an intermediate Linux user can use an alternative to the standard beginners "genkernel" approach, to compile a custom kernel, in a relatively speedy and easy set up.

Minimum Requirements

  • Understand the command line
  • Know where the kernel files are located

Assumptions

You start from an installed Funtoo system on the disk, or at least, you are on stage3 in a chrooted environment from a live cd, following somehow the Funto Installation Tutorial.

Less advanced version

Emerging the kernel sources

To begin, we have to figure out which kernel sources we will use. If you are unsure about which sources are available and what their benefits and drawbacks are, check out the Kernels page.

After you have made a decsion as to which kernel you want to install, emerge it:

root # emerge vanilla-sources 

Portage will now go about installing the sources to /usr/src. It will also symlink the kernel-version directory to a directory called linux.

Configuring the kernel

Now that the kernel sources are on your system, you should configure them. To do this, change your directory to /usr/src/linux

root # cd /usr/srclinux

As we are now in the kernel sources directory, we can run a script that allows us to modify them. Run:

root # make menuconfig

While you edit the sources, keep the following in mind:

  • To build something into your kernel, press y when you have it selected.
  • To exclude something from your kernel, press n when you have it selected.
  • To build something as a module, press m.

Things that you may need to include in your kernel:

  • Wireless/LAN drivers
  • Support for your graphics card
  • Support for your audio card
  • Support for USB devices
   Note
Many pages on the wiki will tell you the kernel requirements for the application that they are about. Keep your eyes open for the blue background, white text sections of pages. Like on this one:  uvesafb

Building and installing the kernel sources

After you finish configuring your kernel sources, you will need to build them. To build your sources, run the following:

root # make
   Tip
You can add -j<number of processing cores + 1> after make to build the kernel more quickly.

When the kernel and its modules finish building, install them:

root # make install modules_install

Now that you have installed your kernel and modules, it is a good idea to install an initramfs. If your system has a separate /usr partition, is encrypted, or uses some other non-standard configuration, it will probably not boot without an initramfs. See Initramfs.

Advanced version

Getting ready to start

   Note
In this case we are building a kernel that is booting root in LVM over encrypted LUKS container.

If you don't have this setup, don't worry, you just don't need all the modules, but everything else is similar.

First, there is the decision which linux kernel sources we need. There are plenty of them in the repositories around, often it is not easy to distinguish between them.

I would always trust my distribution of choice and take what is has to offer - and funtoo has a lot to offer!

I really do recommend (especially if it is your first time) to build a debian-sourced genkernel like described in chapter 5 "Using Debian-Sources with Genkernel" in the Funtoo Kernels Tutorial.

From there you should have a running system booting nicely from your own build (just little bit bloated) kernel. This is more than you can expect from any other ready to go distribution.

   Note
We are using RedHat's dracut in order to build a nice initramfs (containing all the necessary tools and extra drivers our kernel might need to start the system). Although dracut is the way to go, more sophisticated and not as buggy as gentoo's genkernel approach, more and more funtoo geeks start using slashbeast's better-initramfs, which we will cover at the end of this howto! So after having set up a genkernel from debian or gentoo sources we are going to build a kernel with either (or both) dracut or/and better-initramfs. So gentoo sources with genkernel is always my backup if anything is not working correctly on my system. For the slightly more geeky approach with my own initram I am using pf-sources, ck-sources or any other more or less heavily patched sources.

Let's go!

Kernel Sources

The source you use on your system is up to you. For a laptop or desktop system, the following are recommended:

   Note
If you are unsure of which sources you would like to use, emerge gentoo-sources. That's always a safe bet for a general system. For more information on available kernels, check out: Funtoo Linux Kernels

Prerequisites

Regardless of the tools you already have installed, it is recommended to follow the steps below, even if you find them to be redundant. First, we edit our /etc/portage/make.conf:

#These compiler flags are just tweaking (optimazation) and NOT necessary:
CFLAGS="-O2 -pipe -march=native -ftracer -fforce-addr"
CXXFLAGS="${CFLAGS} -fpermissive -fomit-frame-pointer"
KDIR=/usr/src/linux
KERNEL="symlink build"
USE="$KERNEL ....here are your use flags...."
## These modules are available:
## DRACUT_MODULES="dracut_modules_biosdevname dracut_modules_btrfs dracut_modules_caps dracut_modules_crypt dracut_modules_crypt-gpg dracut_modules_dmraid dracut_modules_dmsquash-live dracut_modules_gensplash dracut_modules_iscsi dracut_modules_livenet dracut_modules_lvm dracut_modules_mdraid dracut_modules_multipath dracut_modules_nbd dracut_modules_nfs dracut_modules_plymouth dracut_modules_ssh-client dracut_modules_syslog"
## We will use these modules for LVM / LUKS:
DRACUT_MODULES="crypt lvm plymouth biosdevname dmraid crypt-gpg dmsquash-live ssh-client syslog"

Next, we set the package keywords by adding the following to /etc/portage/package.use:

sys-kernel/dracut dm net device-mapper crypt lvm
   Note
If you don't have lvm over encrypted LUKS you just add the "net" keyword here, or "selinux".


Next, we build our packages:

root # emerge -av app-portage/gentoolkit sys-kernel/pf-sources sys-kernel/dracut sys-boot/plymouth sys-boot/plymouth-openrc-plugin

Preparing the kernel

We go now to the sources directory and enter the following commands to update the kernel's .config file:

root # cd /usr/src/linux/
root # make clean
  CLEAN   .
  CLEAN   arch/x86/kernel/acpi/realmode
  CLEAN   arch/x86/kernel/cpu
  CLEAN   arch/x86/kernel
  CLEAN   arch/x86/vdso
  CLEAN   arch/x86/lib
  CLEAN   drivers/gpu/drm/radeon
  CLEAN   drivers/net/wan
  CLEAN   drivers/scsi/aic7xxx
  CLEAN   drivers/tty/vt
  CLEAN   drivers/video/logo
  CLEAN   firmware
  CLEAN   kernel
  CLEAN   lib/raid6
  CLEAN   lib
  CLEAN   security/apparmor
  CLEAN   security/selinux
  CLEAN   usr
  CLEAN   arch/x86/boot/compressed
  CLEAN   arch/x86/boot
  CLEAN   .tmp_versions
  CLEAN   vmlinux System.map .tmp_kallsyms2.S .tmp_kallsyms1.o .tmp_kallsyms2.o .tmp_kallsyms1.S .tmp_vmlinux1 .tmp_vmlinux2 .tmp_System.map
root # zcat /proc/config.gz > /usr/src/linux/.config

Next, we run make localmodconfig. You will get some questions which you can answer mostly with either M (compiled as a module) or Y (compiled directly into the kernel). If you are not sure what to choose, press enter, and the default option will be selected.

root # make localmodconfig
Enable different security models (SECURITY) [Y/n/?] y
Enable the securityfs filesystem (SECURITYFS) [Y/?] y
Socket and Networking Security Hooks (SECURITY_NETWORK) [Y/?] y
Security hooks for pathname based access control (SECURITY_PATH) [Y/?] y
Low address space for LSM to protect from user allocation (LSM_MMAP_MIN_ADDR) [65536] 65536
NSA SELinux Support (SECURITY_SELINUX) [Y/n/?] y
  NSA SELinux boot parameter (SECURITY_SELINUX_BOOTPARAM) [N/y/?] n
  NSA SELinux runtime disable (SECURITY_SELINUX_DISABLE) [N/y/?] n
  NSA SELinux Development Support (SECURITY_SELINUX_DEVELOP) [Y/n/?] y
  NSA SELinux AVC Statistics (SECURITY_SELINUX_AVC_STATS) [Y/n/?] y
  NSA SELinux checkreqprot default value (SECURITY_SELINUX_CHECKREQPROT_VALUE) [1] 1
  NSA SELinux maximum supported policy format version (SECURITY_SELINUX_POLICYDB_VERSION_MAX) [Y/n/?] y
    NSA SELinux maximum supported policy format version value (SECURITY_SELINUX_POLICYDB_VERSION_MAX_VALUE) [19] 19
TOMOYO Linux Support (SECURITY_TOMOYO) [Y/n/?] y
  Default maximal count for learning mode (SECURITY_TOMOYO_MAX_ACCEPT_ENTRY) [2048] 2048
  Default maximal count for audit log (SECURITY_TOMOYO_MAX_AUDIT_LOG) [1024] 1024
  Activate without calling userspace policy loader. (SECURITY_TOMOYO_OMIT_USERSPACE_LOADER) [Y/n/?] y
AppArmor support (SECURITY_APPARMOR) [Y/n/?] y
  AppArmor boot parameter default value (SECURITY_APPARMOR_BOOTPARAM_VALUE) [1] 1
Integrity Measurement Architecture(IMA) (IMA) [Y/n/?] y
EVM support (EVM) [N/y/?] (NEW)
Default security module
  1. SELinux (DEFAULT_SECURITY_SELINUX)
  2. TOMOYO (DEFAULT_SECURITY_TOMOYO)
  3. AppArmor (DEFAULT_SECURITY_APPARMOR)
> 4. Unix Discretionary Access Controls (DEFAULT_SECURITY_DAC)
choice[1-4?]: 4
warning: (ACPI_HOTPLUG_CPU) selects ACPI_CONTAINER which has unmet direct dependencies (ACPI && EXPERIMENTAL)
warning: (MEDIA_TUNER) selects MEDIA_TUNER_TEA5761 which has unmet direct dependencies (MEDIA_SUPPORT && VIDEO_MEDIA && I2C && EXPERIMENTAL)
root #
root # configuration written to .config
root #
warning: (GFS2_FS) selects DLM which has unmet direct dependencies (EXPERIMENTAL && INET && SYSFS && CONFIGFS_FS && (IPV6 || IPV6=n))
warning: (IMA) selects TCG_TPM which has unmet direct dependencies (HAS_IOMEM && EXPERIMENTAL)
warning: (MEDIA_TUNER) selects MEDIA_TUNER_TEA5761 which has unmet direct dependencies (MEDIA_SUPPORT && VIDEO_MEDIA && I2C && EXPERIMENTAL)
warning: (ACPI_HOTPLUG_CPU) selects ACPI_CONTAINER which has unmet direct dependencies (ACPI && EXPERIMENTAL)

Now comes the most adventurous part!

Building the Kernel

root # make -j8  bzImage
root # make -j8 modules
root # make modules_install
root # make install

Initramfs

Initramfs, or not?

The reason to build a kernel with an initramfs is mostly for interoperability (e.g. live-cd's) and special features like an included busybox, ssh, etc. But mostly, and that's why we are doing this here now, to have a proper kernel up and running quick'n dirty in a reasonable time without fighting hours and days until a more or less exotic hardware is perfectly run by the kernel. After having a proper basic kernel running with the help of an initramfs, I really recommend you to go a step further and build a true kernel with all features includes without an initramfs. But this could be pain in the ass and very time consuming - so we do it the funtoo way here - at least in the second example when we stick to better-initramfs instead of Red-Hat's dracut.

Option one: Initrd with dracut

To build the initrd with dracut, we just execute:

root # dracut -f --fstab --xz /boot/initramfs-3.2.6-pf.img  3.2.6-pf

Generally, this should be enough! If you experience booting problems like missing modules / drivers then just boot from the genkernel section and fix the initrd building. You can look into the man page to tweak the command a bit (e.g. --add-drivers "xz dm_crypt" etc...).

Ok let's go on and finish the taks, we are now going to tell grub how to boot off correctly! Edit /etc/boot.conf:


boot {
        generate grub
        default "Funtoo Linux dracut"
        timeout 3
}

"Funtoo Linux genkernel" {
        kernel kernel-genkernel[-v]
        initrd initramfs-genkernel[-v]
        params = quiet rootfstype=ext4
        params += luks enc_root=/dev/sda3
        params += lvm root=/dev/mapper/vg-root
}

"Funtoo Linux dracut" {
        kernel vmlinuz[-v]
## this is the better-initramfs generated initrd
        initrd initramfs[-v].img
        params  = quiet rootfstype=ext4
        params += luks enc_root=/dev/sda3
        params += lvm root=/dev/mapper/vg-root
}

That's it -- almost!

Now write to the grub.cfg with the handy Boot-Update script:

root # boot-update -v

 boot-update 1.5.2 / Copyright 2009-2011 Funtoo Technologies

 [use option "-l" for license info, "-h" for help]

 * Generating config for grub...

 DEFAULT > Funtoo Linux - vmlinuz-3.2.6-pf
           Funtoo Linux genkernel - kernel-genkernel-x86_64-3.2.6-pf

 * Completed successfully.

Okay,... here you go..! :)

Reboot and see how it works!

Option two: using better-initramfs

Piotr's better-initramfs is another approach that is tiny, nice and shiny and seems to become more and more a favourite among funtoo'ers. The biggest plus is that, once built it is kernel version independent.

To use better-initramfs, follow these steps:

  1. download sources
  2. build kernel with "make bzImage"
  3. download better-initramfs
  4. run better-initramfs
  5. adjust /etc/boot.conf

Here is how in detail:

Assuming you did install already a genkernel backup or at least you have a working bzImage + modules installed, we rush forward to step 3:

Downloading Better-initramfs

root # cd /opt/
root # git clone https://github.com/slashbeast/better-initramfs.git
root # cd better-initramfs
root # ls
AUTHORS    LICENSE   README.rst  bootstrap  output   sourceroot
ChangeLog  Makefile  TODO        examples   scripts

Build Better-Initramfs

root # bootstrap/bootstrap-all
root # make prepare
root # make image
root # mv output/initramfs.cpio.gz /boot

Adjust grub

Taking the above setup we edit the /etc/boot.conf:

boot {
        generate grub
        default "Funtoo Linux"
        timeout 3
}

# Rootfs over lvm over luks
# /dev/sda3 - encrypted lvm's pv
# /dev/mapper/vg-root - rootfs's lv

"Funtoo Linux" {
        kernel bzImage[-v]
## this is the better-initramfs generated initrd
        initrd initramfs.cpio.gz
        params  = quiet rootfstype=ext4
        params += luks enc_root=/dev/sda3
        params += lvm root=/dev/mapper/vg-root
}

"Funtoo Linux dracut" {
        kernel vmlinuz[-v]
## this is the dracut generated initrd
        initrd initramfs[-v].img
        params  = quiet rootfstype=ext4
        params += luks enc_root=/dev/sda3
        params += lvm root=/dev/mapper/vg-root
}


"Funtoo Linux genkernel" {
        kernel kernel-genkernel[-v]
        initrd initramfs-genkernel[-v]
        params = quiet rootfstype=ext4
        params += luks enc_root=/dev/sda3
        params += lvm root=/dev/mapper/vg-root
}

Okay,... here you go..! :)

update the grub.cfg with boot update, then reboot and see how it works!

root # boot-update -v     

 boot-update 1.5.2 / Copyright 2009-2011 Funtoo Technologies

 [use option "-l" for license info, "-h" for help]

 * Generating config for grub...

 DEFAULT > Funtoo Linux better-initramfs - vmlinuz-3.2.6-pf
           Funtoo Linux dracut - vmlinuz-3.2.6-pf
           Funtoo Linux genkernel - kernel-genkernel-x86_64-3.2.6-ck

 * Completed successfully.
root # reboot