All Products
Search
Document Center

Alibaba Cloud Linux:Modify and compile the Alibaba Cloud Linux kernel RPM package

Last Updated:Oct 28, 2024

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 on the Custom Launch tab.

  • The instance uses an Alibaba Cloud Linux 2 or 3 image.

  • The instance type that the instance uses has 32 or more vCPUs.

    Note

    It takes a long period of time to compile an RPM package. We recommend that you use an instance type that has 32 or more vCPUs to improve compilation efficiency.

Step 1: Prepare the environment

  1. Connect to the instance for which you want to modify and compile RPM packages of the Alibaba Cloud Linux kernel.

    For more information, see Connect to a Linux instance by using a password or key.

  2. 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 the RPM package that corresponds to Alibaba Cloud Linux 2 or 3 and <src.rpm_name> with the name of the RPM package that contains the source code (src.rpm package). 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

https://mirrors.aliyun.com/alinux/2/plus/source/SRPMS/

Alibaba Cloud Linux 3

https://mirrors.aliyun.com/alinux/3/plus/source/SRPMS/kernels/

In this example, the kernel-5.10.134-13.1.al8.src.rpm package that corresponds to Alibaba Cloud Linux 3 is downloaded. Sample commands:

yum install -y wget
wget https://mirrors.aliyun.com/alinux/3/plus/source/SRPMS/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:

image

Step 3: Modify the source code

  1. Run the following commands to install dependencies:

    yum install -y rpm-build yum-utils
    yum-builddep -y <src.rpm_name>

    In the preceding command, replace <src.rpm_name> with the name of the src.rpm package that you want to modify. In this example, <src.rpm_name> is replaced with kernel-5.10.134-13.1.al8.src.rpm. Sample commands:

    yum install -y rpm-build yum-utils
    yum-builddep -y kernel-5.10.134-13.1.al8.src.rpm
  2. 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 needs. In this example, <Name of the compressed source code package> is replaced with linux-5.10.134-13.1.al8.tar.xz. Sample command:

    cd /root/rpmbuild/SOURCES
    tar xf linux-5.10.134-13.1.al8.tar.xz
  3. 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 needs. In this example, <Name of the decompressed source code package> is replaced with linux-5.10.134-13.1.al8. Sample command:

    cd linux-5.10.134-13.1.al8
  4. Optional. Modify the source code or configuration files based on your needs. You can modify the source code based on your needs. This topic only describes how to modify configuration files. To modify configuration files, perform the following operations:

    1. Run the following command to list all configuration files and select the configuration files that you want to modify:

      ls /root/rpmbuild/SOURCES

      image

      The following table describes the configuration files in the /root/rpmbuild/SOURCES directory.

      Configuration file

      Description

      kernel-5.10.134-aarch64.config

      Arm architecture and release version of the config configuration.

      kernel-5.10.134-aarch64-debug.config

      The config configuration of the Arm architecture and 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 kernel-5.10.134-aarch64.config or kernel-5.10.134-x86_64.config file based on whether the platform is an Arm or x86 architecture, or modify the configurations of both platforms at the same time.

    2. Run the following commands to modify a configuration file.

      In this example, the kernel-5.10.134-x86_64.config file for the x86 architecture is used. Sample commands:

      1. Copy the configuration 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
      2. Enter the source code directory.

        cd linux-5.10.134-13.1.al8
      3. Refresh the default value in the configuration file.

        make olddefconfig
      4. Use menuconfig to modify the configuration file to ensure proper dependency configurations.

        make menuconfig

        Enter the menuconfig window. You can press the / key to search for the items that require modifications and modify the items based on your needs.

        image

      5. Copy the modified configuration file to overwrite the original configurations.

        cp .config ../kernel-5.10.134-x86_64.config
      6. Go back to the parent directory.

        cd ..
      Note

      You can modify the config file for the Arm architecture in the same way.

  5. Run the following commands to update the kernel version:

    cd /root/rpmbuild/SPECS
    vi kernel.spec

    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 as 5.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 as 5.10.134-13.1, we recommend that you change the minor version to a version in the *-x.y.z format, such as 5.10.134-13.1.1.

    In the following example, the 5.10.134-13.1 version number is updated to 5.10.134-13.1.1. In the configuration file, find %define pkgrelease %{?KREL:%{KREL}}%{?!KREL:13.1} and change it to %define pkgrelease %{?KREL:%{KREL}}%{?!KREL:13.1.1}. 1

    You can also add the change description in the kernel.spec changelog. 2

Step 4: Re-create the RPM package

  1. 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.

    1. Enter the SOURCES directory.

      cd /root/rpmbuild/SOURCES/
    2. Modify 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 to linux-5.10.134-13.1.1.al8. Modify the file name based on your needs.

      mv linux-5.10.134-13.1.al8 linux-5.10.134-13.1.1.al8
    3. 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
    4. Remove the decompressed directory and original compressed package.

      rm -rf linux-5.10.134-13.1.al8
      rm linux-5.10.134-13.1.al8.tar.xz
  2. 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 /root/rpmbuild/SRPMS/kernel-5.10.134-13.1.1.al8.src.rpm.

    image

  3. (Conditionally required) Run the following command to downgrade the dwarves package:

    If your kernel version (queried by using the uname -r command) is later than version 5.10.014, you must use an early version of the Dwarves package for compilation. Otherwise, compilation errors may occur.

    yum downgrade dwarves
  4. 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 modified name of the src.rpm package. In this example, the modified name of the src.rpm package is kernel-5.10.134-13.1.1.al8.src.rpm. Sample command:

    rpmbuild --rebuild /root/rpmbuild/SRPMS/kernel-5.10.134-13.1.1.al8.src.rpm

    It takes a long period of time to compile an RPM package. The following sample command output indicates that the RPM package is re-created. 1