Difference between pages "Building a Kernel from Source" and "Genkernel Quick Start Tutorial"

From Funtoo
(Difference between pages)
Jump to: navigation, search
m (build the better-initramfs)
 
 
Line 1: Line 1:
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 ;-)
+
Genkernel is a tool which help you to build a Linux kernel and deploy it along with a ramdisk image which contains all of the necessary modules to make the kernel being able to mount the different partitions of your harddrives in the VFS at the system startup.
  
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.
+
= Concepts =
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.
+
  
=== Minimum Requirements ===
+
TBC
You should understand the way how things work in a terminal and how to use an editor and tweak config files. This is crucial.
+
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.
+
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 ===
+
= Recompiling the Gentoo kernel sources from a SystemRescue CD chroot =
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.
+
A second case that many Funtoo users will face to is to compile their own kernel when installing a brand new Funtoo instance from a stage 3 archive (the most common scenario is to boot the machine with SystemRescue CD).
If you don't have this setup, don't worry, you just don't need all the modules, but everything else is similar.
+
  
 +
{{fancynote|If you want to use the System rescue CD sources provided by Funtoo ( sys-kernel/sysrescue-std-sources ), the philosophy remains exactly the same}}
  
= Getting everything in order to start =
+
== First step emerging the required packages ==
  
First there is the decision which linux kernel sources we need.
+
The first step is to emerge:
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!
+
# The Gentoo kernel sources
 +
# Genkernel itself
  
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_Linux_Kernels| Funtoo Kernels Tutorial]].
+
This is achieved with:
 
+
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 ==
+
We are going to use the kernel sources from the funtoo git repository.
+
 
+
The source you use on your system is up to you and your needs.
+
For a laptop or desktop system, we recommend the following:
+
 
+
* '''sys-kernel/pf-sources'''
+
* '''sys-kernel/ck-sources'''
+
* '''sys-kernel/gentoo-sources'''
+
* '''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.
+
It doesn't harm to just copy and paste and do some steps again.
+
 
+
First, we look into our <code>/etc/make.conf</code>:
+
  
 
<console>
 
<console>
###i## nano /etc/make.conf
+
###i## emerge sys-kernel/gentoo-sources sys-kernel/genkernel
#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"
+
 
</console>
 
</console>
  
Next, we set the package keywords:
+
Once the Gentoo kernel sources are deployed, you should find a directory named '''linux-''version''-gentoo''' (e.g. linux-2.6.39-gentoo) under '''/usr/src'''. Update the '''linux''' symlink to point on this directory:
 
<console>
 
<console>
###i## nano /etc/portage/package.use/dracut
+
###i## cd /usr/src
sys-kernel/dracut dm net device-mapper crypt lvm
+
###i## rm linux
 +
###i## ln -s linux-2.6.39-gentoo linux
 
</console>
 
</console>
  
{{Note}} If you don't have lvm over encrypted LUKS you probably just add the "net" keyword here, or "selinux".
+
== Second step: Grabbing and tweaking a configuration file ==
  
 +
How to start your kernel configuration? Simply by using the same configuration template the running System Rescue CD kernel had been built with! Before chrooting in your Funtoo instance, you did something like:
  
After that we are going to 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## mount -o bind /proc /mnt/gentoo/proc
 
</console>
 
</console>
 
+
Or:
Finished? Well, then let's go on and
+
 
+
== Preparing the kernel ==
+
 
+
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## mount -t proc none /mnt/gentoo/proc
###i## 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
+
###i## zcat /proc/config.gz > /usr/src/linux/.config
+
 
</console>
 
</console>
  
<console>
+
In your chroot environment (or from a System Rescue CD virtual terminal) if you look what /proc contains you will notice a file named config.gz:
###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 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)
 
#
 
# configuration written to .config
 
#
 
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)
 
root@[~src/linux] #
 
 
</pre>
 
 
Now comes the most adventurous part!
 
 
= Building the Kernel =
 
 
<console>
 
<console>
###i## make -j8  bzImage
+
###i## ls /proc
###i## make -j8 modules
+
###i## make modules_install
+
###i## make install
+
</console>
+
  
 
+
...
'''Building an initramfs or not?'''
+
dr-xr-xr-x  7 root      root                    0 May 23 03:13 952
 
+
dr-xr-xr-x  7 root      root                    0 May 23 03:13 953
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.
+
dr-xr-xr-x 7 root      root                    0 May 23 18:42 9834
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''.
+
...
 
+
-r--r--r--  1 root      root                16024 May 23 22:27 config.gz
= Option one: Initrd with dracut =
+
-r--r--r--  1 root      root                    0 May 23 22:27 consoles
 
+
-r--r--r--  1 root      root                    0 May 23 22:27 cpuinfo
To build the initrd we just execute
+
...
 
+
<console>
+
# ##i##dracut -f --fstab --xz /boot/initramfs-3.2.6-pf.img 3.2.6-pf
+
 
</console>
 
</console>
  
Generally, this really should be enough!
+
config.gz holds the running kernel (System Rescue CD) configuration, just copy the gunziped content in the Gentoo sources directory:
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!
+
  
 
<console>
 
<console>
###i## nano /etc/boot.conf
+
###i## cd /usr/src/linux
 
+
###i## zcat /proc/config.gz > .config
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
+
}
+
 
</console>
 
</console>
  
That's it almost!
+
Next, run make oldconfig to set all newly added options:
  
Now write to the <code>grub.cfg</code> with the new handy boot-update script from funtoo:
 
 
<console>
 
<console>
###i## boot-update -v
+
###i## make oldconfig
 
+
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.
+
 
</console>
 
</console>
  
Okay,... here you go..! :)
+
Next, tweak the kernel configuration in the way you prefer (manually edition of the .config file, make nconfig, make menuconfig....) if you wish. You are not ready yet! A final step is required: '''you ''must'' either set CONFIG_INITRAMFS_SOURCE to a blank value (CONFIG_INITRAMFS_SOURCE="") either delete the statement in the .config file'''. Forgotting to do that will make Genkernel abort the compilation process with a message like:
 
+
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 independant.
+
 
+
For using this you just do the following steps:
+
<pre>
+
1. download sources
+
2. build kernel with "make bzImage"
+
3. download better-initramfs
+
4. run better-initramfs
+
5. adjust /etc/boot.conf
+
</pre>
+
 
+
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 ===
+
  
 
<console>
 
<console>
###i## cd /usr/src/;
+
/usr/src/linux-2.6.39-gentoo/scripts/gen_initramfs_list.sh: Cannot open '/var/tmp/genkernel/initramfs-2.6.32.14-std155-i386.cpio.gz'
###i## git clone https://github.com/slashbeast/better-initramfs.git
+
make[1]: *** [usr/initramfs_data.cpio.lzma] Error 1
###i## /src #  cd better-initramfs
+
###i## better-initramfs git:(master) ls
+
AUTHORS    LICENSE  README.rst  bootstrap  output  sourceroot
+
ChangeLog  Makefile  TODO        examples  scripts
+
 
</console>
 
</console>
  
=== Build Better-Initramfs ===
+
== Third step: Building and installing the kernel ==
<console>
+
###i## better-initramfs git:(master) sudo bootstrap/bootstrap-all
+
###i## sudo make prepare
+
###i## sudo make image
+
###i## sudo mv output/initramfs.cpio.gz /boot
+
</console>
+
  
=== adjust grub ===
+
This is simply achieved by:
 
+
Taking the above setup we edit the /etc/boot.conf
+
as I installed genkernel first, and dracut after - you see this setup:
+
 
+
<pre>
+
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
+
}
+
</pre>
+
Okay,... here you go..! :)
+
 
+
update the grub.cfg, then reboot and see how it works!
+
  
 
<console>
 
<console>
root@[~src/linux-3.2.6-pf] # boot-update -v   
+
###i## genkernel --no-mrproper all
 
+
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@[~src/linux-3.2.6-pf] #
+
 
+
root@[~src/linux-3.2.6-pf] # reboot
+
 
+
System going down for reboot!
+
 
+
 
</console>
 
</console>
  
 +
The same remarks written in the [[Genkernel_Quick_Start_Tutorial#Third_step:_Building_and_installing_the_kernel|third paragraph]] of the first use case are still valid here.
  
[[Category:HOWTO]]
 
[[Category:Featured]]
 
 
[[Category:Kernel]]
 
[[Category:Kernel]]

Revision as of 18:23, 9 January 2014

Genkernel is a tool which help you to build a Linux kernel and deploy it along with a ramdisk image which contains all of the necessary modules to make the kernel being able to mount the different partitions of your harddrives in the VFS at the system startup.

Contents

Concepts

TBC

Recompiling the Gentoo kernel sources from a SystemRescue CD chroot

A second case that many Funtoo users will face to is to compile their own kernel when installing a brand new Funtoo instance from a stage 3 archive (the most common scenario is to boot the machine with SystemRescue CD).

Note: If you want to use the System rescue CD sources provided by Funtoo ( sys-kernel/sysrescue-std-sources ), the philosophy remains exactly the same

First step emerging the required packages

The first step is to emerge:

  1. The Gentoo kernel sources
  2. Genkernel itself

This is achieved with:

# emerge sys-kernel/gentoo-sources sys-kernel/genkernel

Once the Gentoo kernel sources are deployed, you should find a directory named linux-version-gentoo (e.g. linux-2.6.39-gentoo) under /usr/src. Update the linux symlink to point on this directory:

# cd /usr/src
# rm linux
# ln -s linux-2.6.39-gentoo linux

Second step: Grabbing and tweaking a configuration file

How to start your kernel configuration? Simply by using the same configuration template the running System Rescue CD kernel had been built with! Before chrooting in your Funtoo instance, you did something like:

# mount -o bind /proc /mnt/gentoo/proc

Or:

# mount -t proc none /mnt/gentoo/proc

In your chroot environment (or from a System Rescue CD virtual terminal) if you look what /proc contains you will notice a file named config.gz:

# ls /proc

...
dr-xr-xr-x  7 root       root                     0 May 23 03:13 952
dr-xr-xr-x  7 root       root                     0 May 23 03:13 953
dr-xr-xr-x  7 root       root                     0 May 23 18:42 9834
...
-r--r--r--  1 root       root                 16024 May 23 22:27 config.gz
-r--r--r--  1 root       root                     0 May 23 22:27 consoles
-r--r--r--  1 root       root                     0 May 23 22:27 cpuinfo
...

config.gz holds the running kernel (System Rescue CD) configuration, just copy the gunziped content in the Gentoo sources directory:

# cd /usr/src/linux
# zcat /proc/config.gz > .config

Next, run make oldconfig to set all newly added options:

# make oldconfig

Next, tweak the kernel configuration in the way you prefer (manually edition of the .config file, make nconfig, make menuconfig....) if you wish. You are not ready yet! A final step is required: you must either set CONFIG_INITRAMFS_SOURCE to a blank value (CONFIG_INITRAMFS_SOURCE="") either delete the statement in the .config file. Forgotting to do that will make Genkernel abort the compilation process with a message like:

/usr/src/linux-2.6.39-gentoo/scripts/gen_initramfs_list.sh: Cannot open '/var/tmp/genkernel/initramfs-2.6.32.14-std155-i386.cpio.gz'
make[1]: *** [usr/initramfs_data.cpio.lzma] Error 1

Third step: Building and installing the kernel

This is simply achieved by:

# genkernel --no-mrproper all

The same remarks written in the third paragraph of the first use case are still valid here.