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
 
(47 intermediate revisions by 11 users not shown)
Line 9: Line 9:


== 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 Funtoo [[Installation (Tutorial)|Installation Tutorial]].


== Less advanced version ==
== Less advanced version ==
Line 15: Line 15:
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.
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:
After you have made a decision as to which kernel you want to install, emerge it:
<console>
{{console|body=
###i## emerge vanilla-sources
###i## emerge sys-kernel/[kernel-name]
</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''.
 
Portage will now go about installing the sources to <code>/usr/src/</code>. This is default directory, where kernel sources installed.
 
You may use
{{console|body=
###i## eselect kernel list
}}
to display a list of kernels currently installed.
In case, you have more than one kernel installed, switching between them is easy with <code>eselect</code>. Below, we will choose the first kernel in the list. This step also creates <code>/usr/src/linux</code> symlink, which points to the actual directory with kernel sources inside <code>/usr/src</code> directory.
 
{{console|body=
###i## eselect kernel set 1
}}
 
For example
 
{{console|body=
oleg2 tmp # eselect kernel list
Available kernel symlink targets:
  [1]  linux-stable *
oleg2 tmp # ls -l /usr/src
total 4.0K
drwxr-xr-x 26 root root 4.0K Aug 16 09:13 linux-stable/
lrwxrwxrwx  1 root root  13 Sep  9 03:22 linux -> linux-stable//
oleg2 tmp #
}}
 
Note that portage can also set this up for you automatically, if the "symlink" USE flag enabled for preferred kernel ebuild.


=== Configuring the kernel ===
=== 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''
Now that the kernel sources are on your system, you should configure them. To do this, change your directory to ''/usr/src/linux''
<console>
{{console|body=
###i## cd /usr/src/linux
###i## cd /usr/src/linux
</console>
}}
As we are now in the kernel sources directory, we can run a script that allows us to modify them. Run:
 
<console>
We are now in the kernel sources directory, next sanitize the sources:
{{warning|this will delete your .config}}
{{console|body=
###i## make distclean
}}
Copy configurations if you have previous configurations to copy:
{{console|body=
###i## cp /boot/config-3.13.7 .config
}}
{{important|Notice, when updating to newer kernel, a copied config file is for older version of kernel! Run:
{{console|body=
###i## make oldconfig
}}
}}
This will provide an interactive menu, and a user asked to review newly added features, driver support and config changes. Do not dismiss this part. If no changes found between older and newer kernel configurations "oldconfig" script will write new .config in /usr/src/linux automatically.
We can now run a script that allows us to modify the configuration. Run:
{{console|body=
###i## make menuconfig
###i## make menuconfig
</console>
}}
 
or
 
{{console|body=
###i## make nconfig
}}
 
{{kernelop||desc=here is where you insert kernel configs, or press / to search for kernel modules by name}}


While you edit the sources, keep the following in mind:
While you edit the sources, keep the following in mind:
Line 49: Line 100:
as '''built in''':
as '''built in''':
* scsi sata & or ata controllers
* scsi sata & or ata controllers
* file system used
* file system


{{fancynote|1=
{{note|1=
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]]}}
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 ===
=== Building and installing the kernel sources ===
If you decided to use LZO compression in kernel settings, install lzop package first. It is needed for decompressing lzo kernel image and not included in default stage3 images.
{{console|body=
###i## emerge lzop
}}
After you finish configuring your kernel sources, you will need to build them. To build your sources, run the following:
After you finish configuring your kernel sources, you will need to build them. To build your sources, run the following:
<console>
{{console|body=
###i## make
###i## make
</console>
}}
{{fancytip|1=
{{tip|1=
You can add -j<number of processing cores + 1> after make to build the kernel more quickly.}}
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:
When the kernel and its modules finish building, install them:
<console>
{{console|body=
###i## make modules_install && make install
###i## make modules_install && make install
</console>
}}
Now that you have installed your kernel and modules, it is a good idea to install an [[Building_a_Kernel_from_Source#Initramfs| Initramfs]].
Now that you have installed your kernel and modules, it is a good idea to install an [[Building_a_Kernel_from_Source#Initramfs| 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.
* 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.
Line 73: Line 128:
=== Getting ready to start ===
=== Getting ready to start ===


{{fancynote|In this case we are building a kernel that is booting root in LVM over encrypted LUKS container.
{{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.}}
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.
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.


I would always trust my distribution of choice and take what is has to offer - and funtoo has a lot to offer!
We 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]].


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]].
From there you should have a running system booting nicely from your own build (just a 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.
 
{{fancynote|1=
We are using Red Hat'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!
Line 91: Line 141:
=== Kernel Sources ===
=== Kernel Sources ===
The source you use on your system is up to you. For a laptop or desktop system, the following are recommended:
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}}'''
* sys-kernel/pf-sources
* '''{{Package|sys-kernel/ck-sources}}'''
* sys-kernel/ck-sources
* '''{{Package|sys-kernel/gentoo-sources}}'''
* sys-kernel/gentoo-sources
* '''{{Package|sys-kernel/git-sources}}'''
* sys-kernel/git-sources
* '''{{Package|sys-kernel/sysrescue-std-sources}}'''
* sys-kernel/debian-sources
* '''{{Package|sys-kernel/debian-sources}}'''
* sys-kernel/vanilla-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]]}}
{{note|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]]}}
 
=== 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 <code>/etc/portage/make.conf</code>:
 
<pre>
#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"
</pre>
 
Next, we set the package keywords by adding the following to <code>/etc/portage/package.use</code>:
 
<pre>
sys-kernel/dracut dm net device-mapper crypt lvm
</pre>
 
{{fancynote|If you don't have lvm over encrypted LUKS you just add the "net" keyword here, or "selinux".}}


 
Please see discussion page for listings of [[Talk:Building_a_Kernel_from_Source|sys-kernel]] <br />
Next, we build our packages:
<console>
###i## emerge -av app-portage/gentoolkit sys-kernel/pf-sources sys-kernel/dracut sys-boot/plymouth sys-boot/plymouth-openrc-plugin
</console>


=== 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|body=
###i## cd /usr/src/linux/
###i## cd /usr/src/linux/
###i## make clean
###i## make clean
Line 160: Line 180:
   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>
}}


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.
Next, we run <code>make localmodconfig</code>. 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|body=
###i## make localmodconfig
###i## make localmodconfig
Enable different security models (SECURITY) [Y/n/?] y
Enable different security models (SECURITY) [Y/n/?] y
Line 194: Line 214:
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)
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)
#
\#
# configuration written to .config
\# configuration written to .config
#
\#
warning: (GFS2_FS) selects DLM which has unmet direct dependencies (EXPERIMENTAL && INET && SYSFS && CONFIGFS_FS && (IPV6 || IPV6=n))
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: (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: (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)
</console>
}}


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


=== Building the Kernel ===
=== Building the Kernel ===
<console>
{{console|body=
###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>
}}


== Initramfs ==
== Initramfs ==
{{fancywarning| Make sure that you have built and installed your kernel sources / modules before building an initramfs.}}
{{warning|Make sure that you have built and installed your kernel sources / modules before building an initramfs.}}
To get your initramfs up and running, check out the [http://www.funtoo.org/Initramfs Initramfs] page. After following all the directions on the page to get your initramfs set up, continue following the ones here.
To get your initramfs up and running, check out the [http://www.funtoo.org/Initramfs Initramfs] page. After following all the directions on the page to get your initramfs set up, continue following the ones here.


Update the <tt>grub.cfg</tt> with boot update, then reboot and see how it works!
Update the {{c|grub.cfg}} with boot update, then reboot and see how it works!
<console>
{{console|body=
###i## boot-update -v
###i## boot-update -v
###i## reboot
###i## reboot
}}
== No ebuild methods ==
As chapter title says, there several ways to maintain your kernel sources without using portage system. These methods have their pros and cons and considered advanced, however, not difficult as it may look from start.
===Tracking linux kernel with git===
Kernel build from upstream git repository is very easy, however it require some additional efforts from users which we will try to describe. This method could be useful for users who avoiding installing and tracking kernel updates with portage ebuild system. Method requires basic git knowledge. In examples, we are going to describe getting linux kernel sources with a stable branch. First, checkout the stable kernel git repository:
<console>
###i## cd /usr/src
###i## git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
###i## cd linux-stable
</console>
{{fancyimportant|Initial clone of whole linux kernel sources is slightly more than 1GB in size. }}
Now, we have entire development  (stable) tree of linux kernel sources. The releases are set as corresponding git tags. Let's see what we have (a snippet of all available tags):
<console>
###i## git tag -l
v3.18.25
v3.4.99
v4.3.6
v4.4
v4.4-rc7
v4.4-rc8
v4.4.3
v4.5.3
###i##
</console>
Notice a *rc* tags, these are release candidates and not a stable releases recommended to use. Switching to a specific stable releases is as simple as:
<console>
###i## git checkout v4.5.3
</console>
Now, we set a  sources tree to track upstream 4.5.3 release. It is necessary to setup a symlink, so that <code>/usr/src/linux</code> points to existing kernel sources.
<console>
###i## pwd
/usr/src
###i## ls
linux-stable/
###i## ln -s /usr/src/linux-stable /usr/src/linux
</console>
Now, we are able to configure and compiling our linux kernel by exactly same steps described above in this wiki. Advantage of this method is that you controlling, changing, updating or downgrading you kernel by power of <code>git</code> without using any ebuilds.
If kernel version you set not good for various reasons, it is very easy to step back and choose older version as well as setting entirely different branch of development tree:
<console>
###i## git checkout v4.5.1
</console>
</console>
This will change your kernel sources tree 2 versions back within 4.5 releases. If you want entirely different branch:
<console>
###i## git checkout v3.4.99
</console>
Of course, when performing such "downgrades" a good idea would be running <code>make oldconfig</code>, so you aware of kernel configuration changes between different kernel versions.
=== A virtuals case ===
Now that we built our custom kernel without portage management, we can get into situation of necessity of building 3-rd party kernel modules, such as <code>virtualbox</code> modules.  Such ebuilds require  kernel sources to be present on your box. However, portage does not have information about our custom built kernel. Normally, ebuilds have additional dependency of <code>virtual/linux-sources</code>, which makes portage installing kernel prior to 3-rd party modules. Details can be found in http://www.funtoo.org/Virtual_Packages. Let's see what will happen in case we built kernel without ebuilds.
{{Note|Below output is an example (not the real output) for a clarification on how virtuals will work.}}
{{console|body=
###i## USE=headless emerge virtualbox -pv
These are the packages that would be merged, in order:
Calculating dependencies... done!
[ebuild  N    ] sys-devel/bin86-0.16.21::gentoo  151 KiB
[ebuild  N    ] dev-libs/libaio-0.3.110::gentoo  USE="-static-libs {-test}" ABI_X86="(64) -32 (-x32)" 0 KiB
[ebuild  N    ] sys-power/iasl-20150717::gentoo  USE="{-test}" 1 468 KiB
[ebuild  N    ] sys-block/thin-provisioning-tools-0.6.1::gentoo  USE="-static {-test}" 193 KiB
[ebuild  N    ] sys-fs/lvm2-2.02.116::gentoo  USE="lvm1 readline static thin udev -clvm -cman -device-mapper-only -lvm2create_initrd (-selinux) -static-libs (-systemd)" 0 KiB
[ebuild  N    ] dev-util/kbuild-0.1.9998_pre20131130::gentoo  1 668 KiB
[ebuild  N    ] dev-libs/libIDL-0.8.14::gentoo  418 KiB
[ebuild  N    ] sys-fs/mdadm-3.4::gentoo  USE="static" 498 KiB
[ebuild  N    ] sys-fs/cryptsetup-1.6.7::gentoo  USE="gcrypt nls python udev -kernel -nettle -openssl -pwquality -reencrypt -static -static-libs -urandom" PYTHON_SINGLE_TARGET="python3_4 -python2_7 -python3_3" PYTHON_TARGETS="python2_7 python3_4 -python3_3" 1 162 KiB
[ebuild  N    ] sys-kernel/genkernel-3.4.40.9-r2::gentoo  USE="cryptsetup -btrfs (-ibm) (-selinux)" 12 966 KiB
[ebuild  N    ] sys-kernel/debian-sources-4.5.2:4.5.2::gentoo  USE="binary" 88 077 KiB
[ebuild  N    ] virtual/linux-sources-2.6::gentoo  USE="-hardened" 0 KiB
[ebuild  N    ] app-emulation/virtualbox-modules-5.0.20::gentoo  USE="-pax_kernel" 582 KiB
[ebuild  N    ] app-emulation/virtualbox-5.0.20::gentoo  USE="alsa headless opengl pam python qt4 sdk udev -debug -doc -java -libressl -lvm -pulseaudio -vboxwebsrv -vnc" PYTHON_TARGETS="python2_7" 108 629 KiB
Total: 14 packages (14 new), Size of downloads: 215 805 KiB
* IMPORTANT: 3 news items need reading for repository 'gentoo'.
* Use eselect news read to view new items.
}}
Notice, that portage now attempting to install <code>debian-sources</code>. That's the case we are not satisfied with. Portage have an ability to by-pass this. In older versions of portage we had <code>--inject package</code> argument.  inject would create a fake <code>vardb</code> entry for the package specified so deps would be satisfied for anything needing that package. Nowadays, this replaced with <code>package.provided</code>. Details about it can be found in <code>man portage</code>. For this, you would do following:
{{console|body=
###i## install -d /etc/portage/profile
###i## echo "sys-kernel/vanilla-sources-4.1.24" >> /etc/portage/profile/package.provided
}}
Above trick makes portage think that your box has vanilla-sources-4.1.24 installed. You can merge <code>virtualbox</code> without complains. We find this approach not that handy. In Funtoo Linux, we added additional stub ebuild, called <code>dummy-sources</code>, which is  dependency in virtual/linux-sources. This stub ebuild installs no any files but satisfying virtual. Details are in https://bugs.funtoo.org/browse/FL-393. No need to play with <code>package.provided</code> and, of course, save time for reading man pages :)
How it works:
{{console|body=
###i## USE=headless emerge virtualbox -pv
These are the packages that would be merged, in order:
Calculating dependencies... done!
[ebuild  N    ] sys-devel/bin86-0.16.21::gentoo  151 KiB
[ebuild  N    ] sys-power/iasl-20150717::gentoo  USE="{-test}" 1 468 KiB
[ebuild  N    ] dev-util/kbuild-0.1.9998_pre20131130::gentoo  1 668 KiB
[ebuild  N    ] dev-libs/libIDL-0.8.14::gentoo  418 KiB
[ebuild  N    ] sys-kernel/dummy-sources-4.5.3::gentoo  0 KiB
[ebuild  N    ] virtual/linux-sources-2.6::gentoo  USE="-hardened" 0 KiB
[ebuild  N    ] app-emulation/virtualbox-modules-5.0.20::gentoo  USE="-pax_kernel" 582 KiB
[ebuild  N    ] app-emulation/virtualbox-5.0.20::gentoo  USE="alsa headless opengl pam python qt4 sdk udev -debug -doc -java -libressl -lvm -pulseaudio -vboxwebsrv -vnc" PYTHON_TARGETS="python2_7" 108 629 KiB
Total: 8 packages (8 new), Size of downloads: 112 913 KiB
* IMPORTANT: 3 news items need reading for repository 'gentoo'.
* Use eselect news read to view new items.
}}
Et voilà, we are avoiding unnecessary kernel installation by portage!


[[Category:HOWTO]]
[[Category:HOWTO]]
[[Category:Featured]]
[[Category:Featured]]
[[Category:Kernel]]
[[Category:Kernel]]

Latest revision as of 08:48, April 15, 2021

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 Funtoo 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 decision as to which kernel you want to install, emerge it:

root # emerge sys-kernel/[kernel-name]

Portage will now go about installing the sources to /usr/src/. This is default directory, where kernel sources installed.

You may use

root # eselect kernel list

to display a list of kernels currently installed. In case, you have more than one kernel installed, switching between them is easy with eselect. Below, we will choose the first kernel in the list. This step also creates /usr/src/linux symlink, which points to the actual directory with kernel sources inside /usr/src directory.

root # eselect kernel set 1

For example

oleg2 tmp # eselect kernel list
Available kernel symlink targets:
  [1]   linux-stable *
oleg2 tmp # ls -l /usr/src
total 4.0K
drwxr-xr-x 26 root root 4.0K Aug 16 09:13 linux-stable/
lrwxrwxrwx  1 root root   13 Sep  9 03:22 linux -> linux-stable//
oleg2 tmp #

Note that portage can also set this up for you automatically, if the "symlink" USE flag enabled for preferred kernel ebuild.

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/src/linux

We are now in the kernel sources directory, next sanitize the sources:

   Warning

this will delete your .config

root # make distclean

Copy configurations if you have previous configurations to copy:

root # cp /boot/config-3.13.7 .config
   Important

Notice, when updating to newer kernel, a copied config file is for older version of kernel! Run:

root # make oldconfig

This will provide an interactive menu, and a user asked to review newly added features, driver support and config changes. Do not dismiss this part. If no changes found between older and newer kernel configurations "oldconfig" script will write new .config in /usr/src/linux automatically. We can now run a script that allows us to modify the configuration. Run:

root # make menuconfig

or

root # make nconfig


here is where you insert kernel configs, or press / to search for kernel modules by name

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:

as modules:

  • Wireless/LAN drivers
  • Support for your graphics card
  • Support for your audio card
  • Support for USB devices


as built in:

  • scsi sata & or ata controllers
  • file system
   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

If you decided to use LZO compression in kernel settings, install lzop package first. It is needed for decompressing lzo kernel image and not included in default stage3 images.

root # emerge lzop

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 modules_install && make 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.
  • If your system is unencrypted, with file system, and hard drive controllers compiled in rather than as modules, it will not need an 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.

We 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 a little bit bloated) kernel. This is more than you can expect from any other ready to go distribution.

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:

  • sys-kernel/pf-sources
  • sys-kernel/ck-sources
  • sys-kernel/gentoo-sources
  • sys-kernel/git-sources
  • sys-kernel/debian-sources
  • sys-kernel/vanilla-sources
   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

Please see discussion page for listings of sys-kernel

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)
#
# configuration written to .config
#
warning: (GFS2_FS) selects DLM which has unmet direct dependencies (EXPERIMENTAL && INET && SYSFS && CONFIGFS_FS && (IPV6

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

   Warning

Make sure that you have built and installed your kernel sources / modules before building an initramfs.

To get your initramfs up and running, check out the Initramfs page. After following all the directions on the page to get your initramfs set up, continue following the ones here.

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

root # boot-update -v
root # reboot

No ebuild methods

As chapter title says, there several ways to maintain your kernel sources without using portage system. These methods have their pros and cons and considered advanced, however, not difficult as it may look from start.

Tracking linux kernel with git

Kernel build from upstream git repository is very easy, however it require some additional efforts from users which we will try to describe. This method could be useful for users who avoiding installing and tracking kernel updates with portage ebuild system. Method requires basic git knowledge. In examples, we are going to describe getting linux kernel sources with a stable branch. First, checkout the stable kernel git repository:

root # cd /usr/src
root # git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
root # cd linux-stable
   Important

Initial clone of whole linux kernel sources is slightly more than 1GB in size.

Now, we have entire development (stable) tree of linux kernel sources. The releases are set as corresponding git tags. Let's see what we have (a snippet of all available tags):

root # git tag -l
v3.18.25
v3.4.99
v4.3.6
v4.4
v4.4-rc7
v4.4-rc8
v4.4.3
v4.5.3
root # 

Notice a *rc* tags, these are release candidates and not a stable releases recommended to use. Switching to a specific stable releases is as simple as:

root # git checkout v4.5.3

Now, we set a sources tree to track upstream 4.5.3 release. It is necessary to setup a symlink, so that /usr/src/linux points to existing kernel sources.

root # pwd
/usr/src
root # ls 
linux-stable/
root # ln -s /usr/src/linux-stable /usr/src/linux

Now, we are able to configure and compiling our linux kernel by exactly same steps described above in this wiki. Advantage of this method is that you controlling, changing, updating or downgrading you kernel by power of git without using any ebuilds.

If kernel version you set not good for various reasons, it is very easy to step back and choose older version as well as setting entirely different branch of development tree:

root # git checkout v4.5.1

This will change your kernel sources tree 2 versions back within 4.5 releases. If you want entirely different branch:

root # git checkout v3.4.99

Of course, when performing such "downgrades" a good idea would be running make oldconfig, so you aware of kernel configuration changes between different kernel versions.

A virtuals case

Now that we built our custom kernel without portage management, we can get into situation of necessity of building 3-rd party kernel modules, such as virtualbox modules. Such ebuilds require kernel sources to be present on your box. However, portage does not have information about our custom built kernel. Normally, ebuilds have additional dependency of virtual/linux-sources, which makes portage installing kernel prior to 3-rd party modules. Details can be found in http://www.funtoo.org/Virtual_Packages. Let's see what will happen in case we built kernel without ebuilds.

   Note

Below output is an example (not the real output) for a clarification on how virtuals will work.

root # USE=headless emerge virtualbox -pv
These are the packages that would be merged, in order:

Calculating dependencies... done!
[ebuild  N     ] sys-devel/bin86-0.16.21::gentoo  151 KiB
[ebuild  N     ] dev-libs/libaio-0.3.110::gentoo  USE="-static-libs {-test}" ABI_X86="(64) -32 (-x32)" 0 KiB
[ebuild  N     ] sys-power/iasl-20150717::gentoo  USE="{-test}" 1 468 KiB
[ebuild  N     ] sys-block/thin-provisioning-tools-0.6.1::gentoo  USE="-static {-test}" 193 KiB
[ebuild  N     ] sys-fs/lvm2-2.02.116::gentoo  USE="lvm1 readline static thin udev -clvm -cman -device-mapper-only -lvm2create_initrd (-selinux) -static-libs (-systemd)" 0 KiB
[ebuild  N     ] dev-util/kbuild-0.1.9998_pre20131130::gentoo  1 668 KiB
[ebuild  N     ] dev-libs/libIDL-0.8.14::gentoo  418 KiB
[ebuild  N     ] sys-fs/mdadm-3.4::gentoo  USE="static" 498 KiB
[ebuild  N     ] sys-fs/cryptsetup-1.6.7::gentoo  USE="gcrypt nls python udev -kernel -nettle -openssl -pwquality -reencrypt -static -static-libs -urandom" PYTHON_SINGLE_TARGET="python3_4 -python2_7 -python3_3" PYTHON_TARGETS="python2_7 python3_4 -python3_3" 1 162 KiB
[ebuild  N     ] sys-kernel/genkernel-3.4.40.9-r2::gentoo  USE="cryptsetup -btrfs (-ibm) (-selinux)" 12 966 KiB
[ebuild  N     ] sys-kernel/debian-sources-4.5.2:4.5.2::gentoo  USE="binary" 88 077 KiB
[ebuild  N     ] virtual/linux-sources-2.6::gentoo  USE="-hardened" 0 KiB
[ebuild  N     ] app-emulation/virtualbox-modules-5.0.20::gentoo  USE="-pax_kernel" 582 KiB
[ebuild  N     ] app-emulation/virtualbox-5.0.20::gentoo  USE="alsa headless opengl pam python qt4 sdk udev -debug -doc -java -libressl -lvm -pulseaudio -vboxwebsrv -vnc" PYTHON_TARGETS="python2_7" 108 629 KiB

Total: 14 packages (14 new), Size of downloads: 215 805 KiB

 * IMPORTANT: 3 news items need reading for repository 'gentoo'.
 * Use eselect news read to view new items.

Notice, that portage now attempting to install debian-sources. That's the case we are not satisfied with. Portage have an ability to by-pass this. In older versions of portage we had --inject package argument. inject would create a fake vardb entry for the package specified so deps would be satisfied for anything needing that package. Nowadays, this replaced with package.provided. Details about it can be found in man portage. For this, you would do following:

root # install -d /etc/portage/profile
root # echo "sys-kernel/vanilla-sources-4.1.24" >> /etc/portage/profile/package.provided

Above trick makes portage think that your box has vanilla-sources-4.1.24 installed. You can merge virtualbox without complains. We find this approach not that handy. In Funtoo Linux, we added additional stub ebuild, called dummy-sources, which is dependency in virtual/linux-sources. This stub ebuild installs no any files but satisfying virtual. Details are in https://bugs.funtoo.org/browse/FL-393. No need to play with package.provided and, of course, save time for reading man pages :) How it works:

root # USE=headless emerge virtualbox -pv
These are the packages that would be merged, in order:

Calculating dependencies... done!
[ebuild  N     ] sys-devel/bin86-0.16.21::gentoo  151 KiB
[ebuild  N     ] sys-power/iasl-20150717::gentoo  USE="{-test}" 1 468 KiB
[ebuild  N     ] dev-util/kbuild-0.1.9998_pre20131130::gentoo  1 668 KiB
[ebuild  N     ] dev-libs/libIDL-0.8.14::gentoo  418 KiB
[ebuild  N     ] sys-kernel/dummy-sources-4.5.3::gentoo  0 KiB
[ebuild  N     ] virtual/linux-sources-2.6::gentoo  USE="-hardened" 0 KiB
[ebuild  N     ] app-emulation/virtualbox-modules-5.0.20::gentoo  USE="-pax_kernel" 582 KiB
[ebuild  N     ] app-emulation/virtualbox-5.0.20::gentoo  USE="alsa headless opengl pam python qt4 sdk udev -debug -doc -java -libressl -lvm -pulseaudio -vboxwebsrv -vnc" PYTHON_TARGETS="python2_7" 108 629 KiB

Total: 8 packages (8 new), Size of downloads: 112 913 KiB

 * IMPORTANT: 3 news items need reading for repository 'gentoo'.
 * Use eselect news read to view new items.

Et voilà, we are avoiding unnecessary kernel installation by portage!