This article will describe rebuilding kernel with Arch Build System aka ABS.
Table of Contents
1 Install ABS and download kernel
Install ABS with pacman.
$ sudo pacman -S --noconfirm abs
Download core/linux build scripts.
$ sudo abs core/linux
Copy build scripts to work directory.
$ cp -a /var/abs/core/linux . $ cd linux
2 Install packages for building kernel
Packages for building kernel are defined at makedepends array in PKGBUILD.
Please run below command on bash because PKGBUILD is bash script.
$ . PKGBUILD
$ sudo pacman -S --asdeps --noconfirm ${makedepends[@]}
You can also install packages for building kernel automatically with makepkg -s option.
3 GPG key
You need to import below GPG keys for validation kernel source code.
$ gpg --keyserver keys.gnupg.net --recv-keys 79BE3E4300411886 $ gpg --keyserver keys.gnupg.net --recv-keys 38DBBDC86092693E
You can skip GPG key validation with makepkg –skippgpcheck.
4 Change kernel name
Changing pkgbase in PKGBUILD can change kernel file name.
$ head PKGBUILD # $Id: PKGBUILD 267890 2016-05-12 16:05:36Z tpowa $ # Maintainer: Tobias Powalowski <[email protected]> # Maintainer: Thomas Baechler <[email protected]> pkgbase=linux # Build stock -ARCH kernel #pkgbase=linux-custom # Build kernel with a different name _srcname=linux-4.5 pkgver=4.5.4 pkgrel=1 arch=('i686' 'x86_64')
This article will use linux-custom which is prepared in PKGBUILD.
$ sed -i -e 's/^pkgbase=linux/#pkgbase=linux/g' \ -e 's/^#pkgbase=linux-custom/pkgbase=linux-custom/g' PKGBUILD
5 Edit kernel code
Extract tarball, apply patch and copy config file. Running makepkg -o will call prepare function in PKGCONFIG.
$ makepkg -o
Changing code in src/linux-4.5 directory will be applied to package.
5.1 Create patch file
Copy linux-4.5 directory and create patch file with diff command.
$ cd src
$ cp -a linux-4.5{,.org}
$ # Change code in linux-4.5
$ diff -uprN linux-4.5{.org,} > ../hello.patch
$ cd ..
This article created below "Hello, World" patch which names hello.patch.
$ diff -uprN linux-4.5.org/init/main.c linux-4.5/init/main.c
--- linux-4.5.org/init/main.c 2016-03-14 04:28:54.000000000 +0000
+++ linux-4.5/init/main.c 2016-06-04 07:26:53.643333334 +0000
@@ -933,6 +933,8 @@ static int __ref kernel_init(void *unuse
{
int ret;
+ printk("Hello, World");
+
kernel_init_freeable();
/* need to finish all async __init code before freeing the memory */
async_synchronize_full();
Get hash value of patch with sha256sum.
$ sha256sum hello.patch a389f1bacf13a378993b96dce517f5b8465dbb59f9a11ba2bd12f12e2c99aab8 hello.patch
Append patch file name to source array, append hash value to sha256sums array and running patch command in prepare function. A hash value of source's n element is at sha256sum's n element.
$ diff -uprN PKGBUILD{.org,}
--- PKGBUILD.org 2016-06-04 10:31:34.003333329 +0000
+++ PKGBUILD 2016-06-04 10:34:47.213333334 +0000
@@ -20,7 +20,8 @@ source=("https://www.kernel.org/pub/linu
'config' 'config.x86_64'
# standard config files for mkinitcpio ramdisk
'linux.preset'
- 'change-default-console-loglevel.patch')
+ 'change-default-console-loglevel.patch'
+ 'hello.patch')
sha256sums=('a40defb401e01b37d6b8c8ad5c1bbab665be6ac6310cdeed59950c96b31a519c'
'SKIP'
@@ -29,7 +30,8 @@ sha256sums=('a40defb401e01b37d6b8c8ad5c1
'2355efbab340d16c1b60a7805b987a78e57266809ba6c986ceef68ef7ce71db0'
'cee1781f96e55a909757c4533cdacb57c3ffe6f6f01f709e8a5a837dc4a68bba'
'f0d90e756f14533ee67afda280500511a62465b4f76adcc5effa95a40045179c'
- '1256b241cd477b265a3c2d64bdc19ffe3c9bbcee82ea3994c590c2c76e767d99')
+ '1256b241cd477b265a3c2d64bdc19ffe3c9bbcee82ea3994c590c2c76e767d99'
+ 'a389f1bacf13a378993b96dce517f5b8465dbb59f9a11ba2bd12f12e2c99aab8')
validpgpkeys=(
'ABAF11C65A2970B130ABE3C479BE3E4300411886' # Linus Torvalds
'647F28654894E3BD457199BE38DBBDC86092693E' # Greg Kroah-Hartman
@@ -51,6 +53,8 @@ prepare() {
# (relevant patch sent upstream: https://lkml.org/lkml/2011/7/26/227)
patch -p1 -i "${srcdir}/change-default-console-loglevel.patch"
+ patch -p1 -i "${srcdir}/hello.patch"
+
if [ "${CARCH}" = "x86_64" ]; then
cat "${srcdir}/config.x86_64" > ./.config
else
6 Edit kernel configuration
Copy config.x86_64 to .config and change .config with make menuconfig.
$ cd src/linux-4.5 $ cp ../../config.x86_64 .config $ make menuconfig # Change kernel config $ cp .config ../../config.x86_64 $ cd ../..
Change CONFIG_MESSAGE_LOGLEVEL_DEFAULT from 4 to 7. CONFIG_BMP085 is changed by normalization of make menuconfig.
$ diff -uprN config.x86_64{.org,}
--- config.x86_64.org 2016-06-04 19:29:26.463333332 +0000
+++ config.x86_64 2016-06-04 19:29:52.260000000 +0000
@@ -1,6 +1,6 @@
#
# Automatically generated file; DO NOT EDIT.
-# Linux/x86 4.5.2-1 Kernel Configuration
+# Linux/x86 4.5.4-1 Kernel Configuration
#
CONFIG_64BIT=y
CONFIG_X86_64=y
@@ -1929,7 +1929,7 @@ CONFIG_HMC6352=m
CONFIG_DS1682=m
# CONFIG_TI_DAC7512 is not set
CONFIG_VMWARE_BALLOON=m
-CONFIG_BMP085=y
+CONFIG_BMP085=m
CONFIG_BMP085_I2C=m
# CONFIG_BMP085_SPI is not set
CONFIG_USB_SWITCH_FSA9480=m
@@ -7180,7 +7180,7 @@ CONFIG_TRACE_IRQFLAGS_SUPPORT=y
# printk and dmesg options
#
CONFIG_PRINTK_TIME=y
-CONFIG_MESSAGE_LOGLEVEL_DEFAULT=4
+CONFIG_MESSAGE_LOGLEVEL_DEFAULT=7
# CONFIG_BOOT_PRINTK_DELAY is not set
CONFIG_DYNAMIC_DEBUG=y
Get hash value of new config.x86_64.
$ sha256sum config.x86_64 eb057cc5c43c6ff7b572e5229e0916bd741f074c5286cf9b282c335ae5c95ab1 config.x86_64
Update hash value in PKGCONFIG.
$ diff -uprN PKGBUILD{.org,}
--- PKGBUILD.org 2016-06-04 19:30:29.386666644 +0000
+++ PKGBUILD 2016-06-04 19:34:10.796666667 +0000
@@ -28,7 +28,7 @@ sha256sums=('a40defb401e01b37d6b8c8ad5c1
'6a9cfe691ac77346c48b7f83375a1880ebb379594de1000acad45da45d711e42'
'SKIP'
'2355efbab340d16c1b60a7805b987a78e57266809ba6c986ceef68ef7ce71db0'
- 'cee1781f96e55a909757c4533cdacb57c3ffe6f6f01f709e8a5a837dc4a68bba'
+ 'eb057cc5c43c6ff7b572e5229e0916bd741f074c5286cf9b282c335ae5c95ab1'
'f0d90e756f14533ee67afda280500511a62465b4f76adcc5effa95a40045179c'
'1256b241cd477b265a3c2d64bdc19ffe3c9bbcee82ea3994c590c2c76e767d99'
'a389f1bacf13a378993b96dce517f5b8465dbb59f9a11ba2bd12f12e2c99aab8')
7 Build kernel
Build kernel and create package. Running makepkg -e will call build function in PKGCONFIG.
$ makepkg -e
8 Install kernel
Install new kernel which names linux-custom and update grub.cfg.
$ sudo pacman -U --noconfirm linux-custom-4.5.4-1-x86_64.pkg.tar.xz $ sudo grub-mkconfig -o /boot/grub/grub.cfg
Files of linux-custom is installed to /boot.
$ ls /boot/ grub initramfs-linux-fallback.img vmlinuz-linux initramfs-linux-custom-fallback.img initramfs-linux.img vmlinuz-linux-custom initramfs-linux-custom.img lost+found
9 Execution result
GRUB menu is not changed.
A kernel parameter of "Arch Linux" menu is changed from linux to linux-custom.
A entry of linux-custom is appended in "Advanced options for Arch Linux" menu.
After boot, uname -r shows new kernel version.
$ uname -r 4.5.4-1-custom
dmesg shows "Hello, World".
$ dmesg [ 0.027234] Freeing SMP alternatives memory: 24K (ffffffff81a21000 - ffffffff81a27000) [ 0.029717] ftrace: allocating 23133 entries in 91 pages [ 0.050603] Hello, World [ 0.050738] x2apic enabled [ 0.050927] Switched APIC routing to physical x2apic.
CONFIG_MESSAGE_LOGLEVEL_DEFAULT is changed from 4 to 7.
$ zcat /proc/config.gz | grep CONFIG_MESSAGE_LOGLEVEL_DEFAULT CONFIG_MESSAGE_LOGLEVEL_DEFAULT=7