Linux, 工作, 生活, 家人

Linux

Build CentOS 8 Kernel from Source on ARM64 Platform

Due to CentOS website document cannot provide a simple instruction to build CentOS 8 stream with some easy steps. So, I make a note to record these procedures about how to build it.

Detail description and instruction, please refer to CentOS document, but if you have read the document and still don’t know how to do that, it can follow my instruction to build the kernel from CentOS 8 kernel source.

Install Necessary Packages

On the document, it missed some packages if you want to build CentOS kernel.

# follow original guide to install necessary packages
yum groupinstall "Development Tools"
yum install ncurses-devel
yum install hmaccalc zlib-devel binutils-devel elfutils-libelf-devel
# Install EPEL packages
sudo yum install epel-release
# Enable powertools packages
sudo dnf config-manager --set-enabled powertools

Fetch CentOS Linux Kernel Source

This steps will fetch current version CentOS kernel source code.
It will download current kernel version of kernel.src.rpm.

cd ~
mkdir -p ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros

KVER=`uname -r | sed 's/\.aarch64//'`
rpm -i https://vault.centos.org/8-stream/BaseOS/Source/SPackages/kernel-$KVER.src.rpm

Install Kernel Necessary Package

After get the kernel source code, it still needs some extra software packages to build the CentOS Linux kernel. If it doesn’t enable powertool on previous step, this step will fail.

cd ~/rpmbuild/SPECS

for pkg in `rpmbuild -bp --target=$(uname -m) kernel.spec 2>&1 | grep 'is needed by' |awk '{print $1}' | tr '\n' ' '`;do sudo yum install $pkg -y;done

Build CentOS Linux kernel

# Executes  the  "%prep" stage from the spec file, will not compile it
rpmbuild -bp --target=$(uname -m) kernel.spec
# compile kernel source code
rpmbuild -bb --target=`uname -m` kernel.spec 2> build-err.log | tee build-out.log

if everything is ok, the file will output on ~/rpmbuild/RPMS/aarch64/

Install rpm command

# Install necessary kernel files (examples)
yum localinstall  kernel-4.18.0-408.el8.aarch64.rpm kernel-core-4.18.0-408.el8.aarch64.rpm kernel-headers-4.18.0-408.el8.aarch64.rpm kernel-modules-4.18.0-408.el8.aarch64.rpm kernel-modules-extra-4.18.0-408.el8.aarch64.rpm
# Install all relative rpms 
rpm -ivh kernel-*.rpm 
yum localinstall kernel-*.rpm
# second time install all relative rpms 
rpm -ivh --force kernel-*.rpm

Apply patch for CentOS kernel

Another important but less instruction talk about how to apply it.

  1. Edit the kernel.spec, add new line above linux-kernel-test.patch
## Patches needed for building this package

Patch000001: new_patch_4.18.patch

# empty final patch to facilitate testing of kernel patches
Patch999999: linux-kernel-test.patch

2. find “ApplyOptionalPatch linux-kernel-test.patch” and add new one before it.

ApplyOptionalPatch new_patch_4.18.patch
ApplyOptionalPatch linux-kernel-test.patch

3. Put file “new_patch_4.18.patch” to ~/rpmbuild/SOURCES/

4. Rebuild kernel

ref.
CentOS wiki : I Need to Build a Custom Kernel
CentOS wiki : I Need the Kernel Source
How to compile kernel module for CentOS8

發佈留言