In specific scenarios, you may need to enable specific features or modules in the Alibaba Cloud Linux kernel or disable specific features in the default kernel to meet specific performance or security requirements. To do these, modify the Alibaba Cloud Linux kernel source code and use the Red Hat Package Manager (RPM) to recompile and re-create custom kernel RPM packages. This ensures that the kernel in use is compatible with Alibaba Cloud Linux releases and able to flexibly meet specific business requirements. This topic describes how to modify and compile RPM packages of the Alibaba Cloud Linux kernel in a container based on the Alibaba Cloud Linux Elastic Compute Service (ECS) environment.
Prerequisites
An ECS instance that runs Alibaba Cloud Linux is created. For more information, see Create an instance by using the wizard.
The instance uses an Alibaba Cloud Linux 2 or 3 image.
The instance type that the instance uses has 32 or more vCPUs.
NoteThe compilation of an
RPM
package requires an extended period of time to complete. We recommend that you use an instance type that has 32 or more vCPUs to improve compilation efficiency.
Step 1: Prepare the environment
Connect to the ECS instance for which you want to modify and compile RPM packages of the Alibaba Cloud Linux kernel.
For more information, see Use Workbench to connect to a Linux instance over SSH.
Run the following commands to download an Alibaba Cloud Linux Docker image and access the Docker container:
#Install Docker. sudo yum install -y docker #Pull a Docker image. sudo docker pull <image_url> #Start the Docker container. sudo docker run -itd --net host --name alinux-build <image_url> bash #Access the Docker container. sudo docker exec -it alinux-build bash
In the preceding commands, replace
<image_url>
with the URL of the Docker image that corresponds to Alibaba Cloud Linux 2 or 3. The following table describes the Docker image URLs.Image
Docker image URL
Alibaba Cloud Linux 2
alibaba-cloud-linux-2-registry.cn-hangzhou.cr.aliyuncs.com/alinux2/alinux2
Alibaba Cloud Linux 3
alibaba-cloud-linux-3-registry.cn-hangzhou.cr.aliyuncs.com/alinux3/alinux3
In this example, the Docker image that corresponds to Alibaba Cloud Linux 3 is downloaded. Sample commands:
sudo yum install -y docker sudo docker pull alibaba-cloud-linux-3-registry.cn-hangzhou.cr.aliyuncs.com/alinux3/alinux3 sudo docker run -itd --net host --name alinux-build alibaba-cloud-linux-3-registry.cn-hangzhou.cr.aliyuncs.com/alinux3/alinux3 bash sudo docker exec -it alinux-build bash
Step 2: Download the source code
Run the following commands to download the RPM package that you want to modify:
# Download the source code package.
yum install -y wget
wget <rpm_url>/<src.rpm_name>
# Install the source code package.
rpm -ivh <src.rpm_name>
In the preceding commands, replace
<rpm_url>
with the URL of theRPM
package that corresponds to Alibaba Cloud Linux 2 or 3. The following table describes the download URL of the RPM package that corresponds to Alibaba Cloud Linux 2 or 3.Image
RPM package URL
Alibaba Cloud Linux 2
Alibaba Cloud Linux 3
https://mirrors.aliyun.com/alinux/3/plus/source/SRPMS/kernels/
In the preceding commands, replace
<src.rpm_name>
with the name of thesrc.rpm
package that you want to modify.
In this example, the src.rpm
package that corresponds to Alibaba Cloud Linux 3, which is named kernel-5.10.134-13.1.al8.src.rpm
, is downloaded. Sample commands:
yum install -y wget
wget https://mirrors.aliyun.com/alinux/3/plus/source/SRPMS/kernels/kernel-5.10.134-13.1.al8.src.rpm
rpm -ivh kernel-5.10.134-13.1.al8.src.rpm
After you install the src.rpm package, the files of the src.rpm package are stored in the /root/rpmbuild
directory. You can run the ls /root/rpmbuild
command to query the files. Sample command output:
Step 3: Modify the source code
Run the following commands to install dependencies:
yum install -y rpm-build yum-utils yum-builddep -y <src.rpm_name>
In the preceding commands, replace
<src.rpm_name>
with the name of thesrc.rpm
package that you want to modify. In this example,<src.rpm_name>
is replaced bykernel-5.10.134-13.1.al8.src.rpm
.yum install -y rpm-build yum-utils yum-builddep -y kernel-5.10.134-13.1.al8.src.rpm
Run the following commands to decompress the source code package:
# Enter the source code directory. cd /root/rpmbuild/SOURCES # Decompress the source code package. tar xf <Name of the compressed source code package>
In the preceding command, replace
<Name of the compressed source code package>
based on your business requirements. In this example, <Name of the compressed source code package> is replaced bylinux-5.10.134-13.1.al8.tar.xz
.cd /root/rpmbuild/SOURCES tar xf linux-5.10.134-13.1.al8.tar.xz
Run the following command to enter the directory in which the decompressed source code package is located:
cd <Name of the decompressed source code package>
In the preceding command, replace
<Name of the decompressed source code package>
based on your business requirements. In this example, <Name of the decompressed source code package> is replaced withlinux-5.10.134-13.1.al8
.cd linux-5.10.134-13.1.al8
(Optional) Modify
config
configurations.Modify the source code or
config
files (configuration files) based on your business requirements. Select a method to modify the source code based on your business requirements. To modifyconfig
files, perform the following operations:Run the following command to list all
config
files and select the config files that you want to modify:ls /root/rpmbuild/SOURCES
The following table describes the
config
files in the/root/rpmbuild/SOURCES
directory.config file
Description
kernel-5.10.134-aarch64.config
The configuration file of the Arm architecture and the release version.
kernel-5.10.134-aarch64-debug.config
The configuration file of the Arm architecture and the debug version, which can be used only for testing.
kernel-5.10.134-x86_64.config
The configuration file of the x86 architecture and the release version.
kernel-5.10.134-x86_64-debug.config
The configuration file of the x86 architecture and the debug version, which is used only for testing.
In most cases, you can select the
kernel-5.10.134-aarch64.config
orkernel-5.10.134-x86_64.config
file based on whether the platform is anArm
orx86
architecture, or modify the configurations of both platforms.Run the following command to modify the
config
file:In this example, the
kernel-5.10.134-x86_64.config
file for thex86
architecture is used.Copy the
config
file to the source code directory.cd /root/rpmbuild/SOURCES cp kernel-5.10.134-x86_64.config linux-5.10.134-13.1.al8/.config
Enter the source code directory.
cd linux-5.10.134-13.1.al8
Refresh the default values in the
config
file.make olddefconfig
Use
menuconfig
to modify theconfig
file to ensure proper dependency configurations.make menuconfig
Access the
menuconfig
interface. You can press the/
key to search for the items that require modifications and modify the items based on your business requirements.Copy the modified
config
file to overwrite the original configurations.cp .config ../kernel-5.10.134-x86_64.config
Go back to the parent directory.
cd ..
NoteYou can modify the config file for the Arm architecture in the same manner.
Run the following commands to update the kernel version:
cd /root/rpmbuild/SPECS vi kernel.spec
Press the
I
key to enter Insert mode. Press theEsc
key, enter:wq
, and then press theEnter
key to save and close the config file.Suggestions for modification:
If the kernel has a major version in the *-x format, such as
5.10.134-12
, we recommend that you change the major version to a version in the*-x.y.z
format, such as5.10.134-12.0.1
. This way, you can distinguish the version from the official version.If the kernel has a minor version in the
*-x.y
format, such as5.10.134-13.1
, we recommend that you change the minor version to a version in the *-x.y.z format, such as5.10.134-13.1.1
.
In the following example, the
5.10.134-13.1
version number is updated to5.10.134-13.1.1
.In the config file, find
%define pkgrelease %{?KREL:%{KREL}}%{?!KREL:13.1}
and change it to%define pkgrelease %{?KREL:%{KREL}}%{?!KREL:13.1.1}
.You can also add the change description in the
changelog
section of thekernel.spec
file.
Step 4: Re-create the RPM package
Run the following commands to recompress the src.rpm package.
In this example, the
linux-5.10.134-13.1.al8
source code package is used.Enter the SOURCES directory.
cd /root/rpmbuild/SOURCES/
Change the file name of the source code directory.
In this example, version
5.10.134-13.1
is changed to version5.10.134-13.1.1
and the file name of the source code directory is modified tolinux-5.10.134-13.1.1.al8
. Modify the file name based on your business requirements.mv linux-5.10.134-13.1.al8 linux-5.10.134-13.1.1.al8
Recompress the source code package into a TAR package.
tar cJf linux-5.10.134-13.1.1.al8.tar.xz linux-5.10.134-13.1.1.al8
Remove the decompressed directory and original compressed package.
rm -rf linux-5.10.134-13.1.al8 rm -f linux-5.10.134-13.1.al8.tar.xz
Run the following commands to re-create the
src.rpm
package:cd /root rpmbuild -bs rpmbuild/SPECS/kernel.spec
The following sample command output indicates that the
src.rpm package
is stored in the/root/rpmbuild/SRPMS/kernel-5.10.134-13.1.1.al8.src.rpm
directory.(Optional) Run the following command to downgrade the
dwarves
package:If the kernel version contained in the
src.rpm
package is5.10.134-13.1.al8
or earlier, usedwarves-1.22-1.al8
for compilation. Otherwise, compilation errors may occur.yum downgrade dwarves-1.22-1.al8 -y
Run the following command to re-create the RPM package:
rpmbuild --rebuild /root/rpmbuild/SRPMS/<src.rpm_name>
In the preceding command, replace
<src.rpm_name>
with the new name of the src.rpm package. In this example, the new name of the src.rpm package iskernel-5.10.134-13.1.1.al8.src.rpm
.rpmbuild --rebuild /root/rpmbuild/SRPMS/kernel-5.10.134-13.1.1.al8.src.rpm
The compilation of an RPM package requires an extended period of time to complete. The following sample command output indicates that the RPM package is re-created.