This article is published by the Cloud + community

Akik: I am a good boy

In general, when we say Linux systems, we mean the various operating system distributions based on the Linux Kernel and GNU Project. In order to master the use of Linux operating system, understand the operating process of Linux operating system, understand the relationship between kernel and peripheral support system, and deepen the understanding of open source operating system, I decided to build a wheel — my own custom Linux file system.

There are two ways to do this:

  • Direct self implementationInit **\* (M1) ***

Load BIOS hardware information -> Read MBR -> run Grub -> load kernel -> Load driver -> init -> run bash

  • Using the system/sbin/init**\* (M2) ***

Load BIOS hardware information -> read MBR -> run Grub -> load kernel -> load driver -> init -> /sbin/init -> obtain run-level information -> /etc/rc.d/rc.sysinit -> Services – > /etc/rc.d/rc.local – > mingetty – > login

Let’s start with ***M1***.

Train of thought

  1. Copy the necessary parts to the new memory using the original system
  2. usinginitrd.imgMechanism inRAM DiskIn the test
  3. Boot with the original file kernel and module

Step1:Get the shell version of initrd.img

First, we can write a script init so that the kernel gets a Bash directly after booting with the file system.

The /bin directory contains common commands, init is a script written by yourself, and /lib64 is a dynamic library that applications rely on.

Now we need to use the command line to create the bin and sbin directories and add the bash, ls, rm, cp, mv, echo, cat, less and other basic commands to them. These commands need to rely on some dynamically linked shared libraries in the /lib64 directory. Therefore, you need to copy the dependent libraries to the corresponding directory of the small system and run the LDD command to query the application program and its dependent dynamic libraries. When done, execute:

find . | cpio -H newc -o | gzip > /boot/initrd.img
Copy the code

Package the root file system as initrd.img and place it in the /boot directory. The system automatically executes init in initrd.img during startup.

How to test the newly created initrd.img after all the effort to generate it requires adding an entry to the GRUB boot configuration file for testing.

title CentOS 6 Mini
root (hd0,0)
kernel /vmlinuz2.632.- 642.el6.x86_64
initrd /initrd.img
Copy the code

This will bring up the boot option after the restart.

Step2: completeAbility to mount the original system

To mount the original system, you must load the driver modules required for running the original system in initrd.img, such as the ext4 file system driver and SCSI device driver. /sbin/modinfo and /sbin/insmod are placed in/ module

Step3:Complete owning managed Device capability (UDEV)

Using the service program UDEVD to manage and monitor the host devices to automatically load the required driver modules, which is more reliable than our own implementation. The udevd rule file is in the /lib/udev/ directory, the configuration file is in the /etc/udev/ directory, and the configuration file is in the /etc/udev/ directory. The name service switch configured in /etc/nsswitch.conf depends on the libnss file in the /lib directory. Then run the /sbin/start\_udev command to start the udevd service. (UDevd needs to call some other system commands, such as /sbin/modprobe, which can be traced by strace).

The /dev directory is where the system stores available devices. /log is a log file generated by running the strace command.

Step4:The login login capability is enabled

Since the mechanism of login is relatively complex, involving process management mechanism, process group, console and many other aspects, we use ***M2*** to copy the /sbin/init command to the small system directory and change the init script to

#! /bin/bash
exec /sbin/init
Copy the code

After giving control to /sbin/init, the system must wait until it makes a series of calls to the login screen before the user can regain control.

/etc/rc.sysinit is configured in /etc/rc.sysinit. The second block is to start all the services in /etc/rc.d/. The third block is the login part, which calls commands such as /sbin/mingetty and /bin/login. Copy the preceding commands and files to the corresponding directory of the small system, and modify the configuration.

Initrd. img runs in memory as a temporary root file system after startup, and we do not need a root switch. Therefore, comment out the body of the remount\_needed() function in /etc/rc.sysinit.

Because the system uses the new Upstart startup mode (the /sbin/init program is provided by the Upstart software package), copy the configuration file related to the Upstart startup to the small system directory:

Conf Load the rc.sysinit script to complete system initialization. /etc/init. rc.conf Compatible script. Responsible for all run-level call processing /etc/init. rcs-sulogin. conf to start the /etc/init. sushell environment /etc/init. control-alt- for single user modedeleteConf To control the number of enabled TTY terminals and the device file /etc/sysconfig/init to control the number of enabled TTY terminals and the color scheme of tty terminals /etc/init. tty. Conf Controls the startup of tty terminalsCopy the code

If the bootmini/etc/inittab running priority is changed to 2, /sbin/init will execute files starting with S in the bootmini/etc/rc.d/rc2.d/ directory, and change the file names starting with K for some services that do not need to be started.

In bootmini/etc/rc. D/rc can join the user needs to a local file system startup automatically after the operation.

The login program is based on the authentication system PAM. The configuration file is in the /etc/pam.d/ directory, and the related library files include /lib64/security/ and its dependent library files. Login also involves user group management, such as /bin/chgrp, /bin/chown, and /bin/chmod. The user name files /etc/passwd and /etc/group are saved. The user password file is /etc/shadow. Some of the other files involved are available through Strace to aid in analysis.

A complete version of a small system that runs on a real machine

Partial directory files:

At this point, the file system is ready to run. In the next article, we’ll reinvent the wheel — tailoring the Linux kernel. The real effects will also be seen in the next chapter.

This article has been published by Tencent Cloud + community in various channels

For more fresh technology dry goods, you can follow usTencent Cloud technology community – Cloud Plus community official number and Zhihu organization number