Fedora system compiles Linux kernel

FedoraSystem compilation Linuxkernel

Recap

Check memory

When compiling the kernel, memory is required at least 4G, or 2Gmemory + 2Gswap memory ( Swap/ Swapfile).

Increase swap memory (Swap)

This article Swapfileuses an example to illustrate the operation as follows:

  • ① (This step can also be omitted) Enter a specific directory, take the vardirectory as an example, and create a swap file ( Swapfile) folder. The operation is as follows:

    cd /var
    
    sudo mkdir swap
    
  • ② Use ddthe command to create a swap file ( Swapfile).

    sudo dd if=/dev/zero of=/mountpoint/path/to/swapfile bs=1M count=2000
    

    illustrate:

    Use ddthe command to create an empty file;

    ifRepresents reading from a file;

    /dev/zeroIt is linuxa unique 0generator;

    ofIndicates output to a file;

    /mountpointRepresents the mount point of a certain partition;

    /path/to/Represents a directory;

    swapfileIndicates the name of the file to be created, which can be arbitrary;

    ofYou can also fill it in directly swapfile, that is, output it to the file in the current folder swapfile;

    If you follow this step, it means that a file /var/swaprepresenting swap memory has been created in the directory ;swapfile

    bs=1MIt means block size1 megabyte, that is, the block size is 1 megabyte;

    The unit can also be B( Bytes), K( KB), M( MB);

    If omitted, the default 512unit is bytes; if the unit is omitted, the default unit is bytes;

    count=2000Indicates 8000writing block;

    That is, 2GBthe file size is approximately.

  • ③ Initialize swapswap space

    Grant Swapfilethe corresponding permissions and do the following:

    sudo chmod 600 /mountpoint/path/to/swapfile
    

    Convert the file to swapa file and execute it in the corresponding directory. The operation is as follows:

    sudo mkswap /mountpoint/path/to/swapfile
    

    If executed in this directory, you can also enter the file name directly. The operation is as follows:

    sudo mkswap swapfile
    

    To enable swap space, do the following:

    sudo swapon /mountpoint/path/to/swapfile 
    

    If you need to mount it automatically after booting, you can add it to /etc/fstabthe file.
    Automatically mount SWAPthe partition when booting, edit /etc/fstab, and add the following content in the last line:

    /path/to/swapfile    swap    swap    defaults    0    0
    

    You can also add the following content:

    /path/to/swapfile    none    swap    sw      0    0
    

    illustrate:

    The first item: /path/to/swapfile, specify swapthe file path;

    The second item: none, indicating that swapthe space has no mount points;

    The third item: swap, represents a swap space type;

    The fourth item: sw, indicating that the swap space is enabled, and there is also an uncommon option noauto;

    Items 5 and 6: 0and 0means no power-on inspection is required.

Delete swap memory (Swap)

If you want to delete it swapfile, you must first execute the following command:

sudo swapoff swapfile

Then perform the operation of deleting the file, the command is as follows:

sudo rm swapfile

Note :

If the operation is not performed swapoffand the operation of deleting the file is executed, the system will report an error and the deletion instruction cannot be executed.

swapoffThe command is equivalent to unmounting swapthe partition.

Install necessary dependencies

To use yumor dnfinstall the software required to compile the kernel, do the following:

sudo yum install git fakeroot ncurses-devel xz zstd dracut openssl-devel bc flex bison elfutils-devel audit-libs-devel slang-devel binutils-devel zlib-static libbabeltrace-devel java-1.8.0-openjdk-devel dwarves
sudo yum groupinstall "Development Tools"

or

sudo dnf install git fakeroot ncurses-devel xz zstd dracut openssl-devel bc flex bison elfutils-devel audit-libs-devel slang-devel binutils-devel zlib-static libbabeltrace-devel java-1.8.0-openjdk-devel dwarves## 添加用户组
sudo dnf groupinstall "Development Tools"

Add a user group and add a user to compile the kernel, as follows:

groupadd mockbuild

useradd -g mockbuild mockbuild

Get the kernel source code

  • ① Official website download:

The official website directly downloads the specified version of linuxthe kernel source code compression package that needs to be compiled. The official website URL is as follows:

Official website: The Linux Kernel Archives

Unzip the kernel compressed package you just downloaded, as follows:

tar -zxf linux-<version>.tar.xz
  • ②Command gitacquisition:

gitObtain the kernel source code by using the command linux, the operation is as follows:

git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

Configure the kernel

Enter linuxthe kernel source code folder and do the following:

cd linux-<version>

Before officially compiling the kernel, you must first configure the modules that need to be included. If you do not know the kernel configuration, you can also copy the current kernel configuration file. The operation is as follows:

sudo cp -v /boot/config-$(uname -r) /path/to/.config

If the current kernel configuration file is not in /bootthe folder or /bootthe kernel configuration file is not found in the folder, you can use the following command:

sudo zcat /proc/config.gz > /path/to/.config

illustrate:

Among them is /path/to/the folder where the downloaded kernel source code is located;

If you have entered the folder where the downloaded kernel source code is located, just enter .config, that is:

sudo cp /boot/config-$(uname -r) .config
sudo zcat /proc/config.gz > .config

Use makeconfigthe configuration kernel parameters as follows:

sudo make menuconfig

illustrate:

The above commands are text-based menu configurations with strong applicability and can also be used to compile the kernel remotely;

Of course, if the system has a desktop, you can choose the following graphical interface menu to configure the kernel;

① If the system is based on KDEthe desktop or the latest QTlibrary , you can use the following command:

sudo make xconfig

② If the system is based on gtka library , such as Gnomeor xfcedesktop, you can use the following command:

sudo make gconfig

You can also perform make allyesconfigor make allnoconfigsimply enable or disable all configurable configurations of the kernel.

Note :

  • Under the virtual machine, there may be a prompt that menuconfigthe execution fails because the screen is too small to display.

    Therefore, ensure that the display area under the virtual machine is sufficient.

  • When configuring kernel parameters, you can choose to enable or disable some modules.

    If you don't know how to choose, just exit by default.

If you need to recompile due to errors or other reasons when compiling the kernel source code, you need to clean up the previous compilation results. The operation is as follows:

sudo make mrproper

Note :

  • If you use LLVM/ clangtools to build the kernel, you may get the following error:
  BTF: .tmp_vmlinux.btf: pahole (pahole) is not available
   Failed to generate BTF for vmlinux
   Try to disable CONFIG_DEBUG_INFO_BTF
   make: *** [Makefile:1170: vmlinux] Error 1
  • The solution is as follows:

    • ① Install the included paholesoftware packages

    • ② Disable CONFIG_DEBUG_INFO_BTFthe function, which can make menuconfigbe found in the menu. Press the space bar to cancel:

Main menu

        -> Kernel hacking

                ->Compile-time checks and compiler options

                        -> Compile the kernel with debug info(DEBUG_INFO [=y])

After exiting menuconfig, enter the following command on the command line to start compilation:

sudo make -jX

If you use LLVMthe tool chain to compile, you need to enter the following command:

sudo make LLVM=1 -jX

If you use the clang tool chain to compile, you need to enter the following command:

sudo make CC=clang LD=ld.bfd -jX

Note: You can do other things at this time. This process is relatively slow. Ordinary machines usually run at the hour level.

-jXThat is, concurrent execution;

XCPUThat is, the number of cores set CPUis determined by the appropriate number of cores. Remember not to exceed CPUthe number of cores;

This command increases speed and can take a long time to execute.

Normally, if no errors are reported during compilation, the new kernel can be installed.

But you also need to check arch/x86/boot/bzImagewhether the file has been generated. The operation is as follows:

ls arch/x86/boot/bzImage -lh

If it is not generated, you need to execute the following command to generate the kernel compressed image file:

sudo make bzImage

Note :

If this file is not generated, an error make installwill be reported.

To install the kernel module, proceed as follows:

sudo make modules_install

To install the kernel, proceed as follows:

sudo make install

Generate initramfsfile system

To use dracutthe generated initramfsfile system, do the following:

sudo dracut --force

Update grubconfiguration

Use grub2the command to update grubthe configuration as follows:

sudo grub2-mkconfig -o /boot/grub2/grub.cfg
sudo grubby --set-default /boot/vmlinuz-5.6.9

Check whether the modification is successful:

sudo grubby --default-kernel
sudo grubby --default-index
sudo grubby --info=ALL

Restart the system to take effect

Execute the following command to restart the system to take effect the compiled kernel:

sudo reboot

Or use the following command to restart the system:

sudo init 6

When restarting the system, if you see the startup interface, the newly compiled kernel can be seen in the kernel startup item.

After startup, use the following command to view the current kernel version:

uname -a

Not original, please do not reprint

Guess you like

Origin blog.csdn.net/GaaraZ/article/details/127219550