Linux kernel learning 1 compiles and starts a minimized system

Mondo Technology Updated on 2024-02-01

The Linux kernel is the core component of the operating system, responsible for managing hardware resources, providing interface for system calls, and coordinating the interaction between user space and hardware.

The kernel sits at the lowest level of the operating system and interacts directly with the hardware to serve the upper layers.

The Linux kernel is open source** and complies with the GPL (General Public Licese) open source license.

Kernel**:

As of this writing, the latest version of the current kernel mainline is 68-rc1。

Hardware management, process management, memory management, file system, network support, system calls, interface interrupt and exception handling, security and permission control, virtualization, container support, linux kernel compilation, are mainly the following steps:

Prepare the compilation environment to obtain the kernel source**Configure the kernel compilation kernel This article wants to implement a minimized Linux system, in addition to the above compilation steps, you also need to do:

Build the user-space toolset to make the root file system configuration bootloader ubuntu 2204

apt install -y make make-guile gcc flex bison libelf-dev openssl libncurses-dev libssl-dev
sudo apt install git -y **source**git clone and switch to the latest stable branch git checkout v519
In the configuration screen, various kernel options can be set, such as:

Processor TypeDevice DriverFile SystemCallMemory ManagementNetwork SupportCore FunctionsThe Linux kernel can be configured in several ways:

make menuconfig

You need to install the pkg-config package management tool sudo apt install pkg-config -y

make defconfig

In this example, the default configuration is used.

There are several other command-line arguments:

make allyesconfig : create a config that can select yes if you can select yes; make allnoconfig : Create a configuration that allows you to select noHere the following command is used to configure::

Configure the kernel make menuconfig
Compiling the binary make of the generated kernel explicitly specifies that the compilation target is bzimagemake bzimage
bzimage is a format of the Linux kernel that is a compressed kernel image that contains all the ** and data needed for boot time.

The compiled result is located in the corresponding kernel architecture folder under arch

Here we choose busybox, which combines several standard UNIX tools into a single executable to provide a streamlined and full-featured user-space environment.

busybox contains a series of standard UNIX tools, such as: LS, CP, MV, MKDIR, CAT, etc.;

The very small size of busybox makes it easy to build a minimal Linux system, and it is also suitable for embedded systems.

wget -jxvf busybox-1.32.1.tar.bz2cd busybox-1.32.1. Set the default compilation option make defconfi**imconfig
Modified at the time of configuration. config static kernel configuration option, which is used to indicate whether or not to build a static link kernel binary. Set to y, which is used to build the static kernel binary, which will contain all the ** and drivers in the kernel.

make busybox install

bin directory: store the basic command tools of the system sbin: store some special command tools used by system administrators or system maintainersusr: store user-level applications and data, usually contain some additional tools and applications in busyboxlinuxrc: is an initialization script, used for system initialization and configuration tasks, such as mounting the file system, loading modules; In order to customize the system more flexibly, linuxrc will be removed later.

Creating a root file system is to create a directory structure that contains the core files of the operating system, command tools, configuration files, and other necessary files, which will become the root directory of the Linux system that is created. In Linux, the root file system is the topmost directory at system startup, denoted as .

The root file system to be done contains all the files and directories needed for the system to be up and running. The production process is generally:

Select a file system type, create a directory structure, add a file system configuration, configure system file devices, node settings, permissions and ownership, configure startup scripts, and compress file systems.

cd _installrm linuxrccd ..cd ..The root filesystem folder is named rootfsmkdir rootfscd rootfscp -r ./busybox-1.32.1/_install/bin/ .cp -r ../busybox-1.32.1/_install/sbin/ .cp -r ../busybox-1.32.1/_install/usr/ .mkdir dev proc sys
The init startup script is responsible for performing a series of initialization tasks at system startup, including setting the kernel message level, mounting dev, proc, sys file systems, and finally starting a shell. In this way, the system enters a basic operational environment after startup.

vim init

Contents :

#!bin sh sets the kernel log message level to 1 to reduce the kernel message output on the console dmesg -n 1 mounts the devtmpfs file system to the dev directory. devtmpfs is a temporary file system used to create device nodes, so that device nodes can be created and managed in dev. mount -t devtmpfs none dev mounts proc to the proc directory , the proc filesystem provides access to kernel and process information, usually used to interact with the kernel after mount mount -t proc none proc mounts the sysfs file system to the sys directory, sysfs provides access to information about system devices and kernel parameters mount -t sysfs none sys creates a new session, and put the process into it. cttyhack ensures that the process becomes the session leader of the control terminal; bin sh starts a new shell. setsid cttyhack /bin/sh
cd ..chmod 777 -r rootfs/
cd rootfsfind . cpio -r root:root -h newc -o | gzip > /rootfs.gz

The BIOS is usually designed to be used to load the operating system when the computer boots up. Common BIOS bootloaders include Grub (Grand Unified Bootloader), Lilo (Linux Loader), and more. SysLinux in this article is also a bootloader for BIOS booting. It is a lightweight, open-source bootloader commonly used to boot Linux systems, especially in embedded systems and LiveCD environments.

The general steps for BIOS booting with SysLinux include:

Install syslinux to the boot sector (MBR) or partition boot sector. Create and edit the configuration file for syslinux (syslinux.).cfg) to define the boot options. Install the operating system's bootloader, such as the Linux kernel, on your hard drive. Set the computer's BIOS boot order, making sure to boot from the hard drive. How:

wget syslinux-6.03.tar.gztar -xvf syslinux-6.03.tar
Create a new folder and copy all the files you need into the folder.

mkdir isobioscd isobioscp ../rootfs.gz .cp ../linux/arch/x86/boot/bzimage kernel.gzcp ../syslinux-6.03/bios/core/isolinux.bin .cp ../syslinux-6.03/bios/com32/elflink/ldlinux/ldlinux.c32 .
vim isolinux.cfg
Add content:

default kernel.gz initrd=rootfs.gz
Then run the command to generate an ios image:

sudo apt install -y xorriso -yxorriso -as mkisofs -o ../mybios.iso -b isolinux.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table ./
-as:Enable Xorriso's "Auto Scan" mode, which is used to scan available devices and **. mkisofs:Specifies the use of the MKISOFS module, which is used to create images of the ISO 9660 file system. -o ../mybios.iso:Specify the path and name of the output file, here save the ISO image as MyBIOSiso and place it in the upper directory (..)/)。-b isolinux.bin:Specify the path and name of the boot image file, which is set to isolinuxbin。-c boot.cat:Specify the path and name of the boot information file, which is set to bootcat。-no-emul-boot:Disable emulated boot. This option tells the ISO file system that the boot image should boot directly from the disk, not through an emulated floppy disk. -boot-load-size 4:Sets the load size of the bootloader, here the size of the bootloader loaded into memory to 4 sectors. -boot-info-table:Create a bootstrap table in the ISO 9660 file system. This table provides the information required by the bootloader. Specify the paths to the files and directories to include in the ISO mirror. HereRepresents all files and subdirectories in the current directory. The goal of this command is to create an image of the ISO 9660 file system that contains the boot information, the bootloader, and all files and subdirectories in the current directory. This type of ISO image is often used to make a bootable optical disc or USB image so that the operating system or tools in it can be loaded when the system boots.

Generated iso:

Commands such as ls pwd can be entered after startup.

Related Pages