Notes on building a Linux Kernel on an Ubuntu System
- [Linux From Scratch}(https://www.youtube.com/watch?v=8WsDcW5SQ9Y) - follows along with the online Linux From Scratch instructions
Refer to my KernelGlosssaryConcept document.
Refer to my separate KernelDirectoriesAndFiles document
Download and install the toolchain needed to build the kernel{
sudo apt-get install git build-essential kernel-package fakeroot libncurses5-dev libssl-dev ccacheIn general, it is preferable to download the kernel to a directory under your home directory, using your standard permissions.
git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
Gets the latest mainline kernel.
Download the desired version from https://www.kernel.org/ as a tarball
The kernel code is in a tarball compressed with xz compression. To extract use the following command:
tar xf <kernel>.tar.xzCopy your existing kernel config file to the root of your source tree to be used as a template for the new build:
cp /boot/config-`uname -r` .configModify this configuration using make (note oldconfig is the actual name to use, don't replace this), by responding to the prompts (safe to take the defaults):
make oldconfigor automatically accept all defaults:
yes '' | make oldconfigTo make any additional changes (optional) to the config run:
make menuconfigNote: menuconfig is just one option, it is a curses full screen text version. make config is a plain text version, make xconfig is a graphical Qt base version, and make gconfig is a graphical based GTK+ version.
The build is a lengthy process, so dedicate as many processors to your machine as feasible (on a Hyper-V image you can have up to the total number of logical processors on your host (cores + hyper threads). This total includes all virtual machines currently running, so if you have 16 logical host processors, you can have one virtual machine with 16 virtual processors, or two virtual machines with 8 virtual processors each, etc.
To see the number of processors (including logical/virtual) the systems is running on:
nprocFor example, his would show 8, if this is running on a virtual machine with 8 virtual processors.
To ensure their are no other prior build files or dependencies
make clean
make mrproperNote: make clean is per Ubuntu docs, and make mrproper* is per the kernel's README
This will take a very long time (as long as an hour+)
make -j `getconf _NPROCESSORS_ONLN` deb-pkg LOCALVERSION=-customThe deg-pkg tells what type of packages to use, and LOCALVERSIN=custom says to use the custom label as part of the package names (you can change this to whatever you want).
Install the *.deb packages, here is an example, but it needs to be changed for the specific versions involved
cd ..
sudo dpkg -i linux-image-4.14.0-tlk01a_4.14.0-tlk01a-1_amd64.deb
sudo dpkg -i linux-headers-4.14.0-tlk01a_4.14.0-tlk01a-1_amd64.deb
sudo dpkg -i linux-libc-dev_4.14.0-tlk01a-1_amd64.debNote the cd .., this is because the *.db packages were placed on directory up in the hierarchy from where you ran the build.
Optionally, install the debug kernel
sudo dpkg -i linux-image-4.14.0-tlk01a-dbg_4.14.0-tlk01a-1_amd64.debNote how this will put the new kernel in the /boot directory and update the GRUB menu:
Selecting previously unselected package linux-image-4.14.0-tlk01a.
(Reading database ... 123840 files and directories currently installed.)
Preparing to unpack linux-image-4.14.0-tlk01a_4.14.0-tlk01a-1_amd64.deb ...
Unpacking linux-image-4.14.0-tlk01a (4.14.0-tlk01a-1) ...
Setting up linux-image-4.14.0-tlk01a (4.14.0-tlk01a-1) ...
update-initramfs: Generating /boot/initrd.img-4.14.0-tlk01a
WARNING: Setting CRYPTSETUP in /etc/initramfs-tools/initramfs.conf is deprecated and will stop
working in the future. Use /etc/cryptsetup-initramfs/conf-hook instead.
Searching for GRUB installation directory ... found: /boot/grub
Searching for default file ... found: /boot/grub/default
Testing for an existing GRUB menu.lst file ... found: /boot/grub/menu.lst
Searching for splash image ... none found, skipping ...
Found kernel: /boot/vmlinuz-4.13.0-16-generic
Found kernel: /boot/vmlinuz-4.14.0-tlk01a
Found kernel: /boot/vmlinuz-4.13.0-16-generic
Replacing config file /run/grub/menu.lst with new version
Updating /boot/grub/menu.lst ... done
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.14.0-tlk01a
Found initrd image: /boot/initrd.img-4.14.0-tlk01a
Found linux image: /boot/vmlinuz-4.13.0-16-generic
Found initrd image: /boot/initrd.img-4.13.0-16-generic
doneReboot into the new kernel after that packages above are installed. In my case the GRUB menu only displayed for about 1 second (it previously was about 5) before booting into the new kernel. I rebooted a second time to verify that the previous kernel was still selectable and bootable.
Run the following to verify you are in the new kernel:
uname -aThis is additional information for understanding the build process. It is not necessary for doing the build described in the initial sections, but can provide useful information for customizing future builds.
The GRUB related files are found in the /boot/ directory, along with kernel, initrd, and other files. The /boot/grub/grub.cfg contains the grub menu, along with the commands need to boot various operating system. Do not change this, instead make changes in /etc/default/grub and the /etc/grub.d/* files. That way any changes you make won't be overwritten the next time a new kernel is added. The /etc/default/grub - is where you should make changes to GRUB, such as menu timeout, while custom menu changes and additions should be made to the files in the /etc/grub.d/ directory. After making changes in these files run sudo update-grub to apply the changes to the /boot/grub/grub.cfg file.
The initrd is the boot loader initialized RAM disk. It is a gzipped cpio archive that has a minimal root file system. It is is the initial root file system the boot loader loads into memory just before loading the kernel. The kernel then uses this initial root file system to load drivers and then mount the actual root file system.
Because the initrd is a gzipped cpio archive, it can be unzipped and extracted.
- Copy the initrd file from the /boot folder (it will have a name such as, initrd.img-4.14.0-default to a temporary directory, giving the file a *.gz gzip extension
- Run gunzip initrd.img-4.14.0-default to unzip to the cpio archive file
- Run sudo cpio -i < initrd.img-4.14.0-default to extract the files and directories from the cpio archive.
You can now see what file are initially used by the kernel as it is booting up. One of the most interesting files to look at is the init script in the root. This shows the process of bringing up the OS, establishing /proc, /dev, /sys, /tmp directories, calling other scripts, and moving things from the virtual file system to the real file system
To get a starting configuration, you can use an existing defconfig as the template for your make config. To see what is available for your system enter:
make help | grep defconfigThis will give you a list that applies to the architectures available to you (assuming you haven't set up cross platform compiling).
This is similar to what I did in my first build here, except I got my initial configuration from copying it from the /boot/config using the Ubuntu config for my OS version.
| Date/Time | Kernel Installed | Built on System | Build Time | Processors | Image | Issues |
|---|---|---|---|---|---|---|
| 2017-11-17 23:58 | vmlinuz-4.14.0 | Ubuntu vmlinuz-4.13.0-16 | 55 minutes | 8 | Hyper-V | None |