[Introduction] I tried building a Linux kernel
table of contents
Hello, this
is Infrastructure Wasshoi Man from the System Solutions Department.
my last blog , I tried increasing the font size as an experiment, but I went too far , so I decided to reflect on this and write in normal size this time.
Do you know what a kernel is?
In a computer, the OS acts as a bridge between hardware and software.
This OS is made up of multiple pieces of software rather than a single piece of software, and the core part of that is the kernel.
My prerequisite knowledge of the kernel was up to this point, and I felt like I had only vaguely heard of it.
After that, while I was busy with my daily work, I was secretly thinking about the vaguely unidentified "Carnel," but
I could see the shadow of the Colonel in every aspect of my work.
``Adjust kernel swappiness to adjust swap usage''
``Tune connections with net.ipv4.tcp_max_syn_backlog'' `
`If you fail to write fstab, a kernel panic will occur''
As a part-time infrastructure engineer, I felt that I wanted to get closer to this person that I sometimes passed by in the hallway, and before I knew it, I had the same feeling that many of you must have felt.
Isn't it kind of cool to know the kernel? ?
What on earth is there in the depths of the OS that I admire so much?
In order to unravel the mystery, our research team headed deep into the OS.
What is build?
By the way, the great thing is that the kernel is open source.
You can see the code. It's the best.
However, it is written in C language (*), so it cannot be used as is.
You need to convert it to binary and use it.
This building or compiling.
When building, for example, you can add features that weren't originally included in the kernel, remove unnecessary features to reduce memory consumption, or modify the source code itself to make it the strongest kernel you can think of. It is possible to.
It's good to be the strongest. The Linux distributions in the world are probably created in the same way. Probably (of course, there are also distributions that were born after various things happened
(*) Recently, there seems to be a lot of support for the programming language "Rust", which is famous for its concept of "ownership", to be adopted as a second language for kernel development, so there may be more packages other than C in the future.
Now, what is a kernel?
I'll try building it anyway in order to better understand Title collection. It's just a matter of practice.
Get ready to build
There are several ways to build, but this time
I would like to build on CentOS7 No matter where you are in the world, installing the source and doing this and that is a pain in the ass, including the management aspect, but it also has the advantage of being able to install the version you like and being less likely to have problems depending on a particular distribution when working. , we have chosen this method this time.
Additionally, the work described below is performed in a virtual environment using Vagrant's Bento Box
First, let's download the kernel source code.The
kernel is available at the link below (or a mirror site of this site).
*This article uses longterm: 5.15.58.
The Linux Kernel Archives
https://www.kernel.org/
# Download tar file [root@localhost]# cd /tmp [root@localhost tmp]# curl -O https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.15.58. tar.xz
# Unzip [root@localhost tmp]# tar xvf linux-5.15.58.tar.xz
The unzipped directory is filled with many related files.
Please refer to the URL below for the structure.
https://linuxjf.osdn.jp/JFdocs/The-Linux-Kernel-15.html
Next, let's put what we need into the environment.
There are several things you will need, but the most common ones are as follows:
- Compiler "GCC"
- Build tool "make"
- "Header files" required for libraries used when building tools around the kernel
- Other libraries
We will install it using these commands.
# Install a set of tools for source build [root@localhost ~]# yum groupinstall "Development tools"
Running the command will install a number of tools.
Important tools such as "GCC" are included in a group package called "Development tools" and are installed all together using the "groupinstall" option.
However, this is not enough, so we will install some additional items.
are written in Documentation/process/changes.rst
in the directory you unzipped earlier (hereinafter referred to as the source directory)
# Install required packages [root@localhost ~]# yum install clang squashfs-tools pcmciautils quota ppp nfs-utils procps-ng kernel-devel udev mcelog python-sphinx openssl-devel dwarves elfutils-libelf-devel ncurses-devel
``ncurses-devel'' is used to control the console screen, and is included because it is required when executing the ``make menuconfig'' command that will be performed later.
Let's change the build settings
Now that the tool is ready, I would like to tinker with the kernel settings, and there are multiple ways to change the settings.
You can change it by directly editing the ``.config'' file, but it's difficult, so this time we'll use a setting method called ``menuconfig'' that allows you to change it by selecting it from the menu.
Now, let's move to the source directory and issue the necessary commands.
[root@localhost linux-5.15.58]# make menuconfig ~~~ *** Compiler is too old. *** Your GCC version: 4.8.5 *** Minimum GCC version: 5.1.0
I got angry. Sorry.
The version of GCC is old.
I see, updating the current GCC isbothersomeBecause it is difficult,software collections We will use the newer GCC using the mechanism provided by Red Hat.
# Install the package [root@localhost linux-5.15.58]# yum install scl-utils centos-release-scl # Install the toolset that includes gcc [root@localhost linux-5.15.58]# yum install devtoolset-8 # Enable gcc8 to be used in the current shell [root@localhost linux-5.15.58]# source scl_source enable devtoolset-8 # Check version [root@localhost linux-5.15.58]# gcc -v
Now it looks like we can finally use menuconfig, so let's run it.
[root@localhost linux-5.15.58]# make menuconfig
Something
like a settings window appeared.
It is also written at the top of the screen, but here is a summary of the operation methods and screen explanations.
- Select to select Exit to return Toggle between Select and Exit using the Tab key
- Activate the function with the Y key Disable the function with the N key Modularize the function with the M key (a state that is called only when necessary) Transition through these states in order with the Space key
- “<*>” is displayed next to an enabled function. “<>” is displayed next to a disabled function. “<M>” is displayed next to a modularized function.
Now that I've reached the settings screen, I want to set it up, but
apparently there are about 20,000 It's too huge.
Open source is amazing.
There are too many, so I will omit the details. (I hope I can learn about it and write about it on my blog. Or rather, I'll write about it.)
There is documentation The training has only just begun.
Now that you've finished making excuses, select "Exit" to exit the "make menuconfig" screen.
You will be asked if you want to save, so if you save and exit, a file called ".config" will have been created in the first level of the source directory.
If you look inside, you will see that this is the kernel build configuration file.
As I wrote above, you can edit this file directly without using "menuconfig" or other tools.
Let's build
Once you've gotten this far, the rest is easy.
Let's move to the source directory and build the kernel.
Depending on the specs, it may take quite a while, so it's best to run it in parallel.
# Check the number of processors [root@localhost linux-5.15.58]# nproc
In my virtual environment the result was "2"
# Build the kernel in parallel using the make command, taking the number of processors confirmed with the nproc command as an optional argument [root@localhost linux-5.15.58]# make -j2
When you type the command, some processing begins.
It's quite long, so grab a snack and wait.
After a few hours of waiting, it's done.
On the way there, my PC was as hot as the hood of a summer car.
Is it a CPU benchmark?
If you check the source directory, you can see that various related files such as "vmlinux" and "modules.builtin" have been created.
The build is finally complete here.
Let's install the built kernel
I'll take the opportunity to try installing it.
Before that, I did some research and found out that there seems to be a practice of placing the linux source directory under "/usr/src/". (Apparently it's also a question on LPIC. Huh? I have Level 1, right?)
So he quickly decides to move the directory.
The location of this location differs depending on the distribution, so it might be interesting to do some research.
# Move directory [root@localhost]# mv /tmp/linux-5.15.58 /usr/src/ # Create symbolic link [root@localhost]# ln -s /usr/src/linux-5.15.58 /usr/src /linux
Compliance completed!
Now let's finally install the kernel.
# Install module files [root@localhost linux]# make modules_install # Check the current kernel version first [root@localhost linux]# uname -r 3.10.0-1160.71.1.el7.x86_64 # Kernel & initialization disk Install image [root@localhost linux]# make install
Now, let's try restarting once.
[root@localhost linux]# reboot
So, when I start up VirtualBox again,
/sbin/mount.vboxsf: mounting failed with the error: No such device
Yeah...
you won't stand up...
I don't know why you're angry, but I know that something is making you angry.
That kind of thing happens in real life too.
Apparently, due to changes in the kernel version, there is a difference between the version managed by VirtualBox and the shared folder is failing to mount.
I have a virtual environment. ``The verification I want to perform is not progressing due to problems depending on the virtual environment.''
It seems like if you put in a plugin, it will do some good things.
There are many pioneers, really.
# Plugin installation vagrant plugin install vagrant-vbguest # Run vagrant vbguest # Reload when everything is finished vagrant reload
Try ssh to check the version.
[root@localhost ~]# uname -r 3.10.0-1160.71.1.el7.x86_64
Hmm?
It hasn't changed
Let's check the kernel versions that can be selected when booting the OS.
[root@localhost ~]# sudo awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg 0 : CentOS Linux (5.15.58) 7 (Core) 1 : CentOS Linux 7 Rescue f9788dcfa7cdc542bad786c12fab4a3c (3.10.0-1160.71.1.el7.x86_64) 2 : CentOS Linux (3.10.0-1160.71.1.el7.x86_64) 7 (Core) 3 : CentOS Linux (3.10.0-1160.66 . 1.el7.x86_64) 7 (Core) 4 : CentOS Linux (0-rescue-62851673ae88124499bf281ce5a57918) 7 (Core)
Yes, there is
"0: CentOS Linux (5.15.58) 7 (Core)" ← This is it, it seems that the one that was built
did not switch automatically. Is this also a VirtualBox specification? ? (suspiciousness)
For now, use the "grub2-set-default" command to set booting to list "0" as the default.
[root@localhost ~]# grub2-set-default 0
Now, reboot again and try ssh.
[root@localhost ~]# uname -r 5.15.58
This
is the version I built this time!
Good
thoughts
Linux is profound.
There were so many things I didn't know, and I learned a lot, including troubleshooting.
If I were to create my own Linux distribution,
I would probably have to install the packages myself in addition to the kernel.
It looks difficult, but it looks fun!
I feel like my own inadequacy came out a lot in the writing, but
I was able to learn a little bit about Linux, so I'm satisfied.
thank you very much!