[Dry goods] Teach you to compile a Linux kernel in ten minutes and run it in a virtual machine!

foreword

This article will briefly introduce how to compile a 5.19 kernel on the Linux system, and then run it in the QEMU virtual machine.

Download the Linux kernel source code

First, we need to download the code for Linux:

https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.19.10.tar.xz

Then, unzip it to a place, such as creating a new folder called linux-5.19-build on the desktop, and then unzip the code into it.

At this point, our directory structure should look like this:

Install required packages

Enter the following command at the console to install the required packages.

sudo apt-get install git fakeroot build-essential ncurses-dev xz-utils libssl-dev bc flex libelf-dev bison qemu qemu-system qemu-kvm

If your computer is not running the Ubuntu/Debian series of Linux distributions, please use the corresponding package manager to install the above software.

Configure Linux compilation options

Linux has many compilation options, we can choose the default one.

First, we open the terminal by right-clicking in the folder of the linux source code. Please make sure that the "current working directory" displayed on the terminal is "linux-5.19-build/linux-5.19.10".

Next, we create the default compilation configuration for Linux, and enter in the console:

make defconfig

When the words "configuration written to .config" are displayed, it means that the configuration is created successfully. We can go to the next step.

Note: If you want to trim or cross-compile the linux kernel, please use the "make menuconfig" option to customize your compilation configuration. Of course, for novices, the default configuration is fine.

Start compiling Linux

Finally, we can start compiling the Linux kernel, we just need to enter the following command in the console.

make -j $(nproc) bzImage

Depending on the performance of the computer, the compilation time ranges from 1 minute to 20 minutes or even longer.

ps: I compiled it on an E5-2640v4 dual-socket server (40 logical processors in total), and it took 1 minute and 36 seconds. It took 2 minutes and 25 seconds on a laptop with an AMD R7-4800H (total 16 logical processors).

When "Kernel: arch/x86/boot/bzImage is ready" is displayed, it means that the compilation is complete.

How does it work? The job is not done yet!

The Linux kernel is just a kernel. If we want to run it, we need a disk file system to place some required files and programs. So we also need a tool: busybox

Configure BusyBox

According to Wikipedia's definition:

BusyBox

BusyBox is a software that integrates more than 300 most commonly used Linux commands and tools. BusyBox includes some simple tools, such as ls, cat, and echo, etc., as well as some larger and more complex tools, such as grep, find, mount, and telnet.

So how do we download it?

We can do this, enter the following command in the linux-5.19-build folder to download the busybox code and unzip it.

wget https://busybox.net/downloads/busybox-1.35.0.tar.bz2
tar xvf busybox-1.35.0.tar.bz2

After decompression, the directory structure is as shown in the figure:

 

Then we open the console under the busybox folder, enter the following command, and configure its compilation options:

make menuconfig

Then such an interface will pop up:

 

We can operate it with the up, down, left, and right keys. Select Settings, and press Enter to enter.

Keep pressing the arrow keys to find Build Options down

Then select "Build static binary" and press the y key on the keyboard to select it. The * sign in the front box will light up after selection. as the picture shows:

 

Then, we press the right key of the keyboard, move the bottom cursor to Exit, press Enter, and return to the main interface.

 

Then the main interface is also exit.

An interface will pop up asking if we want to save, we can choose Yes.

 

Create a disk image

Next, we go back to the "desktop/linux-5.19-build" folder and enter the following command in the console to create a disk image:

dd if=/dev/zero of=rootfs.img bs=1M count=20
mkfs.ext4 rootfs.img

Then, we then run the following command to compile BusyBox and load it into the disk image.

mkdir fs
sudo mount -t ext4 -o loop rootfs.img ./fs
cd busybox-1.35.0
sudo make install -j $(nproc) CONFIG_PREFIX=../fs
# 为文件系统创建一些必须的文件夹
cd ../fs
sudo mkdir proc dev etc home mnt
sudo cp -r ../busybox-1.35.0/examples/bootfloppy/etc/* etc/
cd ..
# 更改权限,以免无法运行
sudo chmod -R 777 fs/ 
# 卸载磁盘镜像
sudo umount fs

Start the Linux kernel

At this point, our preparations have all been completed! We can start the Linux kernel in the QEMU virtual machine.

Enter the command in the console under the "desktop/linux-5.19-build" folder:

qemu-system-x86_64 -kernel ./linux-5.19.10/arch/x86_64/boot/bzImage  -hda rootfs.img  -append "root=/dev/sda"

Thus, the kernel is successfully started. As shown in the figure, we can see that there are some folders under the current directory.

 

We can also use some common Linux commands to operate.

Please indicate the source of the reprint: teach you to compile a Linux kernel in ten minutes and run it in a virtual machine | | Longjin's Blog

Welcome to pay attention to my public account "Denglong", let us know more things together~

Guess you like

Origin blog.csdn.net/qq_34026204/article/details/127936017