Fedora
System compilation Linux
kernel
Table of contents
Recap
Check memory
When compiling the kernel, memory is required at least 4G
, or 2G
memory +
2G
swap memory ( Swap
/ Swapfile
).
Increase swap memory (Swap)
This article Swapfile
uses an example to illustrate the operation as follows:
-
① (This step can also be omitted) Enter a specific directory, take the
var
directory as an example, and create a swap file (Swapfile
) folder. The operation is as follows:cd /var sudo mkdir swap
-
② Use
dd
the command to create a swap file (Swapfile
).sudo dd if=/dev/zero of=/mountpoint/path/to/swapfile bs=1M count=2000
illustrate:
Use
dd
the command to create an empty file;if
Represents reading from a file;/dev/zero
It islinux
a unique0
generator;of
Indicates output to a file;/mountpoint
Represents the mount point of a certain partition;/path/to/
Represents a directory;swapfile
Indicates the name of the file to be created, which can be arbitrary;of
You can also fill it in directlyswapfile
, that is, output it to the file in the current folderswapfile
;If you follow this step, it means that a file
/var/swap
representing swap memory has been created in the directory ;swapfile
bs=1M
It meansblock size
1 megabyte, that is, the block size is 1 megabyte;The unit can also be
B
(Bytes
),K
(KB
),M
(MB
);If omitted, the default
512
unit is bytes; if the unit is omitted, the default unit is bytes;count=2000
Indicates8000
writingblock
;That is,
2GB
the file size is approximately. -
③ Initialize
swap
swap spaceGrant
Swapfile
the corresponding permissions and do the following:sudo chmod 600 /mountpoint/path/to/swapfile
Convert the file to
swap
a 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/fstab
the file.
Automatically mountSWAP
the 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
, specifyswap
the file path;The second item:
none
, indicating thatswap
the 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 optionnoauto
;Items 5 and 6:
0
and0
means 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
swapoff
and the operation of deleting the file is executed, the system will report an error and the deletion instruction cannot be executed.
swapoff
The command is equivalent to unmountingswap
the partition.
Install necessary dependencies
To use yum
or dnf
install 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 linux
the 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
git
acquisition:
git
Obtain 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 linux
the 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 /boot
the folder or /boot
the 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 makeconfig
the 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
KDE
the desktop or the latestQT
library , you can use the following command:sudo make xconfig
② If the system is based on
gtk
a library , such asGnome
orxfce
desktop, you can use the following command:sudo make gconfig
You can also perform
make allyesconfig
ormake allnoconfig
simply enable or disable all configurable configurations of the kernel.Note :
Under the virtual machine, there may be a prompt that
menuconfig
the 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
/clang
tools 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
pahole
software packages② Disable
CONFIG_DEBUG_INFO_BTF
the function, which canmake menuconfig
be 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 LLVM
the 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.
-jX
That is, concurrent execution;
X
CPU
That is, the number of cores setCPU
is determined by the appropriate number of cores. Remember not to exceedCPU
the 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/bzImage
whether 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 install
will 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 initramfs
file system
To use dracut
the generated initramfs
file system, do the following:
sudo dracut --force
Update grub
configuration
Use grub2
the command to update grub
the 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