Difference between revisions of "Kernel/Arch Build System"

From ArchWiki
Jump to navigation Jump to search
(→‎Changing prepare(): Update according to fresh PKGBUILD)
m (→‎Updating: Should have used {{bc|1=)
 
(59 intermediate revisions by 6 users not shown)
Line 3: Line 3:
 
[[it:Kernels/Arch Build System]]
 
[[it:Kernels/Arch Build System]]
 
[[ja:カーネル/コンパイル/Arch Build System]]
 
[[ja:カーネル/コンパイル/Arch Build System]]
[[ru:Kernels/Arch Build System]]
+
[[ru:Kernel (Русский)/Arch Build System]]
 
[[zh-hans:Kernels/Arch Build System]]
 
[[zh-hans:Kernels/Arch Build System]]
 
See [[Kernels]] for the main article.
 
See [[Kernels]] for the main article.
Line 11: Line 11:
 
==Getting the Ingredients==
 
==Getting the Ingredients==
  
Since you'll be using [[makepkg]], follow the best practices outlined there first.  For example, you cannot run makepkg as root/sudo. Therefore, create a {{ic|build}} directory in your user home first.
+
Since you'll be using [[makepkg]], follow the best practices outlined there first.  For example, you cannot run makepkg as root/sudo. Therefore, create a {{ic|build}} directory in your user home first.
 
   $ cd ~/
 
   $ cd ~/
 
   $ mkdir build
 
   $ mkdir build
Line 18: Line 18:
 
[[Install]] the {{Pkg|asp}} package and the {{Grp|base-devel}} package group.
 
[[Install]] the {{Pkg|asp}} package and the {{Grp|base-devel}} package group.
  
You need a clean kernel to start your customization from. Fetch the latest kernel package files from ABS into your build directory by running:
+
You need a clean kernel to start your customization from. [[ABS#Retrieve PKGBUILD source using Git]] and few other files into your build directory by running:
  
 
  $ asp update linux
 
  $ asp update linux
  $ asp checkout linux
+
  $ asp export linux
  
 +
At this point, the directory tree looks like:
 +
{{bc|~/build/linux/-+
 +
              +--config
 +
              \__PKGBUILD}}
 
Then, get any other file you need (e.g. custom configuration files, patches, etc.) from the respective sources.
 
Then, get any other file you need (e.g. custom configuration files, patches, etc.) from the respective sources.
  
Line 30: Line 34:
  
 
   pkgbase=linux-custom
 
   pkgbase=linux-custom
 
Depending on the PKGBUILD you may have to also rename {{ic|linux.install}} to match the modified {{ic|pkgbase}}.
 
  
 
===Changing prepare()===
 
===Changing prepare()===
  
In prepare function, you can apply needed kernel patch or change kernel build configuration.  
+
In {{ic|prepare()}} function, you can [[Patching_packages#Applying_patches|apply needed kernel patches]]  or change kernel build configuration.  
  
 
If you need to change a few config options you can edit config file in the source.
 
If you need to change a few config options you can edit config file in the source.
Line 57: Line 59:
  
 
===Generate new checksums===
 
===Generate new checksums===
[[Install]] the {{Pkg|pacman-contrib}} package.
+
[[#Changing prepare()]] suggests a possible modification to {{ic|$_srcname/.config}}. Since this path is not where downloading the package files ended, its checksum was not checked by makepkg (which actually checked {{ic|$_srcname/../../config}}).
  
As we modified config, we need to generate new checksums by running:
+
If you replaced the downloaded {{ic|config}} with another config file before running makepkg, [[Install]] the {{Pkg|pacman-contrib}} package.
 +
Which will generate new checksums by running:
 
  $ updpkgsums
 
  $ updpkgsums
  
Line 78: Line 81:
 
==Installing==
 
==Installing==
  
After running ''makepkg'', you can have a look at the {{ic|linux.install}} file. You will see that some variables have changed.
+
Best practice is to [[install]] both packages together as they might be both needed (e.g. DKMS.)
 
+
  # pacman -U ''kernel-headers_package'' ''kernel_package''
Now, you only have to install the package as usual. Best practice is to install kernel headers first as they will be needed (e.g. to install the [[NVIDIA#Custom_kernel|nvidia]] driver) for the custom kernel later.
 
  # pacman -U ''kernel-headers_package''
 
# pacman -U ''kernel_package''
 
  
 
==Boot Loader==
 
==Boot Loader==
 
Now, the folders and files for your custom kernel have been created, e.g. {{ic|/boot/vmlinuz-linux-test}}. To test your kernel, update your [[bootloader]] configuration file and add new entries ('default' and 'fallback') for your custom kernel. If you renamed your kernel in the ''PKGBUILD pkgbase'' you may have to rename the initramfs.img in your ''$build/pkg/kernel/etc'' before installing with pacman. That way, you can have both the stock kernel and the custom one to choose from.
 
Now, the folders and files for your custom kernel have been created, e.g. {{ic|/boot/vmlinuz-linux-test}}. To test your kernel, update your [[bootloader]] configuration file and add new entries ('default' and 'fallback') for your custom kernel. If you renamed your kernel in the ''PKGBUILD pkgbase'' you may have to rename the initramfs.img in your ''$build/pkg/kernel/etc'' before installing with pacman. That way, you can have both the stock kernel and the custom one to choose from.
 +
 +
== Updating ==
 +
Assuming one has an arch kernel source that he wants to update, one method to do that is with https://git.archlinux.org/linux.git. Follows a concrete example. In what follows, the top kernel source directory is assumed at ~/build/linux/.
 +
 +
In general, arch sets an arch kernel source with two local git repositories. The one at archlinux-linux/ is a local bare [[git]] repository pointing to git://git.archlinux.org/linux.git. The other one is at src/archlinux-linux/, pulling from the first repository. Possible local patches, and building, is expected at src/archlinux-linux/.
 +
 +
For this example, the HEAD of the locally installed bare git repository source at archlinux-linux/ was initially pointing to {{ic|4010b622f1d2 Merge branch 'dax-fix-5.3-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/nvdimm/nvdimm}}, which is somewhere between v5.2.5-arch1 and v5.2.6-arch1.
 +
{{bc|
 +
$ cd ~/build/linux/archlinux-linux/
 +
$ git fetch --verbose
 +
}}
 +
One can see it fetched v5.2.7-arch1, which was the newest archlinux tag, because it prints what new tags were obtained. If no new tags were obtained then there is no newer archlinux source available.
 +
 +
Now the source can be updated where the actual build will take place.
 +
{{bc|
 +
$ cd ~/build/linux/src/archlinux-linux/
 +
$ git checkout master
 +
$ git pull
 +
$ git fetch --tags --verbose
 +
$ git branch --verbose 5.2.7-arch1 v5.2.7-arch1
 +
$ git checkout 5.2.7-arch1
 +
}}
 +
 +
You can verify you are on track with something like
 +
{{bc|1=
 +
$ git log --oneline 5.2.7-arch1 --max-count=7
 +
13193bfc03d4 Arch Linux kernel v5.2.7-arch1
 +
9475c6772d05 netfilter: nf_tabf676926c7f60les: fix module autoload for redir
 +
498d650048f6 iwlwifi: Add support for SAR South Korea limitation
 +
bb7293abdbc7 iwlwifi: mvm: disable TX-AMSDU on older NICs
 +
f676926c7f60 ZEN: Add CONFIG for unprivileged_userns_clone
 +
5e4e503f4f28 add sysctl to disallow unprivileged CLONE_NEWUSER by default
 +
5697a9d3d55f Linux 5.2.7
 +
}}
 +
This shows few specific archlinux patches between Linux 5.2.7 and Arch Linux kernel v5.2.7-arch1. The important lines here are Linux 5.2.7 and Arch Linux kernel v5.2.7-arch1. Obviously, there might be other patches at other versions, and the commit identifiers, such as f676926c7f60, as well as the kernel version, will be different for other versions.
 +
 +
The up to date PKGBUILD, as well archlinux kernel configuration file, can be pulled in by the {{ic|asp}} command:
 +
 +
{{bc|
 +
$ asp update linux
 +
$ cd ~/build
 +
$ asp -f export linux
 +
$ cd ~/build/linux/
 +
}}
 +
 +
{{note|Sometimes the {{ic|asp}} command does not update linux files even though there is a newer archlinux source tag. A possible reason is that archlinux linux files lag behind archlinux linux source.}}
 +
Then run manually most, if not all, the shell commands of PKGBUILD::prepare(). If you were [[#Modifying the PKGBUILD]] you might prefer to
 +
 +
$ mkdir ~/build/tmplinux/
 +
$ cd ~/build/tmplinux/
 +
 +
before you {{ic|export}} linux with {{ic|asp}}, so that you can merge archlinux modifications into {{ic|~/build/linux}}.
 +
 +
At this point, {{ic|makepkg --verifysource}} should succeed. And {{ic|makepkg --noextract}} should be able to build the packages as if the source was extracted by {{ic|makepkg --nobuild}}.
  
 
==See Also==
 
==See Also==
 
* https://kernel.org/doc/Documentation/kbuild/kconfig.txt and the parent directory
 
* https://kernel.org/doc/Documentation/kbuild/kconfig.txt and the parent directory

Latest revision as of 16:24, 26 November 2019

See Kernels for the main article.

The Arch Build System can be used to build a custom kernel based on the official linux package. This compilation method can automate the entire process, and is based on a very well tested package. You can edit the PKGBUILD to use a custom kernel configuration or add additional patches.

Getting the Ingredients

Since you'll be using makepkg, follow the best practices outlined there first. For example, you cannot run makepkg as root/sudo. Therefore, create a build directory in your user home first.

 $ cd ~/
 $ mkdir build
 $ cd build/

Install the asp package and the base-devel package group.

You need a clean kernel to start your customization from. ABS#Retrieve PKGBUILD source using Git and few other files into your build directory by running:

$ asp update linux
$ asp export linux

At this point, the directory tree looks like:

~/build/linux/-+
               +--config
               \__PKGBUILD

Then, get any other file you need (e.g. custom configuration files, patches, etc.) from the respective sources.

Modifying the PKGBUILD

Edit PKGBUILD and look for the pkgbase parameter. Change this to your custom package name, e.g.:

 pkgbase=linux-custom

Changing prepare()

In prepare() function, you can apply needed kernel patches or change kernel build configuration.

If you need to change a few config options you can edit config file in the source.

Or you can use GUI tool to tweak the options. Comment make olddefconfig in the prepare() function of the PKGBUILD, and add your favorite tool:

PKGBUILD
...
  msg2 "Setting config..."
  cp ../config .config
  #make olddefconfig

  make nconfig # new CLI menu for configuration
  #make menuconfig # CLI menu for configuration
  #make xconfig # X-based configuration
  #make oldconfig # using old config from previous kernel version
  # ... or manually edit .config
...
Warning: systemd has a number of kernel configuration requirements for general use, for specific usecases (e.g., UEFI) and for specific systemd functionality (e.g., bootchart). Failure to meet these requirements can result in your system being degraded or unusable. The list of required and recommended kernel CONFIGs can be found in /usr/share/doc/systemd/README. Check them before you compile.These requirements also change over time. Because Arch assumes you are using the official kernel, there will be no announcement of these changes. Before you install a new version of systemd, check the version release notes to make sure your current custom kernel meets any new systemd requirements.

Generate new checksums

#Changing prepare() suggests a possible modification to $_srcname/.config. Since this path is not where downloading the package files ended, its checksum was not checked by makepkg (which actually checked $_srcname/../../config).

If you replaced the downloaded config with another config file before running makepkg, Install the pacman-contrib package. Which will generate new checksums by running:

$ updpkgsums

Compiling

You can now proceed to compile your kernel by the usual command makepkg

If you have chosen an interactive program for configuring the kernel parameters (like menuconfig), you need to be there during the compilation.

 $ makepkg -s

The -s parameter will download any additional dependencies used by recent kernels such as xml and docs.

Note:

Installing

Best practice is to install both packages together as they might be both needed (e.g. DKMS.)

# pacman -U kernel-headers_package kernel_package

Boot Loader

Now, the folders and files for your custom kernel have been created, e.g. /boot/vmlinuz-linux-test. To test your kernel, update your bootloader configuration file and add new entries ('default' and 'fallback') for your custom kernel. If you renamed your kernel in the PKGBUILD pkgbase you may have to rename the initramfs.img in your $build/pkg/kernel/etc before installing with pacman. That way, you can have both the stock kernel and the custom one to choose from.

Updating

Assuming one has an arch kernel source that he wants to update, one method to do that is with https://git.archlinux.org/linux.git. Follows a concrete example. In what follows, the top kernel source directory is assumed at ~/build/linux/.

In general, arch sets an arch kernel source with two local git repositories. The one at archlinux-linux/ is a local bare git repository pointing to git://git.archlinux.org/linux.git. The other one is at src/archlinux-linux/, pulling from the first repository. Possible local patches, and building, is expected at src/archlinux-linux/.

For this example, the HEAD of the locally installed bare git repository source at archlinux-linux/ was initially pointing to 4010b622f1d2 Merge branch 'dax-fix-5.3-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/nvdimm/nvdimm, which is somewhere between v5.2.5-arch1 and v5.2.6-arch1.

$ cd ~/build/linux/archlinux-linux/
$ git fetch --verbose

One can see it fetched v5.2.7-arch1, which was the newest archlinux tag, because it prints what new tags were obtained. If no new tags were obtained then there is no newer archlinux source available.

Now the source can be updated where the actual build will take place.

$ cd ~/build/linux/src/archlinux-linux/
$ git checkout master
$ git pull
$ git fetch --tags --verbose
$ git branch --verbose 5.2.7-arch1 v5.2.7-arch1
$ git checkout 5.2.7-arch1

You can verify you are on track with something like

$ git log --oneline 5.2.7-arch1 --max-count=7
13193bfc03d4 Arch Linux kernel v5.2.7-arch1
9475c6772d05 netfilter: nf_tabf676926c7f60les: fix module autoload for redir
498d650048f6 iwlwifi: Add support for SAR South Korea limitation
bb7293abdbc7 iwlwifi: mvm: disable TX-AMSDU on older NICs
f676926c7f60 ZEN: Add CONFIG for unprivileged_userns_clone
5e4e503f4f28 add sysctl to disallow unprivileged CLONE_NEWUSER by default
5697a9d3d55f Linux 5.2.7

This shows few specific archlinux patches between Linux 5.2.7 and Arch Linux kernel v5.2.7-arch1. The important lines here are Linux 5.2.7 and Arch Linux kernel v5.2.7-arch1. Obviously, there might be other patches at other versions, and the commit identifiers, such as f676926c7f60, as well as the kernel version, will be different for other versions.

The up to date PKGBUILD, as well archlinux kernel configuration file, can be pulled in by the asp command:

$ asp update linux
$ cd ~/build
$ asp -f export linux
$ cd ~/build/linux/
Note: Sometimes the asp command does not update linux files even though there is a newer archlinux source tag. A possible reason is that archlinux linux files lag behind archlinux linux source.

Then run manually most, if not all, the shell commands of PKGBUILD::prepare(). If you were #Modifying the PKGBUILD you might prefer to

$ mkdir ~/build/tmplinux/
$ cd ~/build/tmplinux/

before you export linux with asp, so that you can merge archlinux modifications into ~/build/linux.

At this point, makepkg --verifysource should succeed. And makepkg --noextract should be able to build the packages as if the source was extracted by makepkg --nobuild.

See Also