Building a Kernel from Source
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.
- Understand the command line
- Know where the kernel files are located
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.
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.
Getting ready to start
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.
The source you use on your system is up to you. For a laptop or desktop system, the following are recommended:
- sys-kernel/pf-sources (package not on wiki - please add)
- sys-kernel/ck-sources (package not on wiki - please add)
- sys-kernel/gentoo-sources (package not on wiki - please add)
- sys-kernel/git-sources (package not on wiki - please add)
- sys-kernel/sysrescue-std-sources (package not on wiki - please add)
- Package:Debian-sources, Package:Debian-sources/pt-br
gentoo-sources. That's always a safe bet for a general system. For more information on available kernels, check out: Funtoo Linux Kernels
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
Next, we set the package keywords by adding the following to
# 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:
# cd /usr/src/linux/ # 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 # 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.
# 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 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 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 TOMOYO Linux Support (SECURITY_TOMOYO) [Y/n/?] y Default maximal count for learning mode (SECURITY_TOMOYO_MAX_ACCEPT_ENTRY)  2048 Default maximal count for audit log (SECURITY_TOMOYO_MAX_AUDIT_LOG)  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 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)
Now comes the most adventurous part!
Building the Kernel
# make -j8 bzImage # make -j8 modules # make modules_install # make install
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:
# 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 going to tell now grub how to boot off correctly!
That's it -- almost!
Now write to the
grub.cfg with the handy Boot-Update script:
# 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:
- download sources
- build kernel with "make bzImage"
- download better-initramfs
- run better-initramfs
- 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:
# cd /opt/ # git clone https://github.com/slashbeast/better-initramfs.git # cd better-initramfs # ls AUTHORS LICENSE README.rst bootstrap output sourceroot ChangeLog Makefile TODO examples scripts
# bootstrap/bootstrap-all # make prepare # make image # mv output/initramfs.cpio.gz /boot
Taking the above setup we edit the /etc/boot.conf:
Okay,... here you go..! :)
update the grub.cfg with boot update, then reboot and see how it works!
root@[~src/linux-3.2.6-pf] # 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. # reboot