Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Customization method of Linux Peripheral File system

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Preface

Generally speaking, the Linux system we are talking about refers to various operating system distributions based on Linux Kernel and GNU Project. In order to master the use of the Linux operating system, understand the operation process of the Linux operating system, understand the relationship between the kernel and the peripheral support system, and deepen the understanding of the open source operating system, I decided to build a wheel-customize a Linux file system.

There are two ways to do this:

Implement init** directly by yourself\ * (M1) *

Load bios hardware information-> read MBR-> execute Grub-> load kernel- > load driver-> init-> execute bash

Use the system / sbin/init**\ * (M2) *

Load bios hardware information-> read MBR-> execute Grub-> load kernel- > load driver-> init-> / sbin/init-> get run-level information-> / etc/rc.d/rc.sysinit-> services-> / etc/rc.d/rc.local-> mingetty-> login

Let's choose * M1 * first.

Train of thought

Copy the necessary parts from the original system to the new memory using initrd.img mechanism to test and match the original file kernel and module startup in RAM Disk

Step1: get the shell version of initrd.img

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

Create script init

Among them: / bin directory is the common command, init is the script written by yourself, / lib64 directory is the dynamic library that the application depends on.

Init content

Now we need to use the command line to create the bin and sbin directories and add basic commands such as bash, ls, rm, cp, mv, echo, cat, less, and so on. Since these commands rely on some dynamically linked shared libraries in directories such as / lib64, you need to copy the dependent libraries to the corresponding directory of the small system, and use the ldd command to query the application and its dependent dynamic libraries. When complete, execute:

Find. | | cpio-H newc-o | gzip > / boot/initrd.img |

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

After so much effort to generate initrd.img, how to test the newly created initrd.img, you need to add an entry to the grub startup configuration file for testing.

Title CentOS 6 Miniroot (hd0,0) kernel / vmlinuz-2.6.32-642.el6.x86_64initrd / initrd.img

In this way, the startup option will appear after the restart.

Step2: complete the ability to mount the original system

In order to mount the original system, you must load the driver modules necessary for the original system to run in initrd.img, such as the driver of the ext4 file system, the driver of the scsi device, etc., / sbin/modinfo and / sbin/insmod, and put the driver into / module

Step3: complete the ability to own and manage devices (udev)

It is more reliable than our own implementation to automatically load the required driver module by using the service program udevd, which manages and monitors the host equipment. The rule file of udevd is in the / lib/udev/ directory, the configuration file is in the / etc/udev/ directory, and the name service exchange of the / etc/nsswitch.conf configuration is also required. The dependent libraries are the files that start with libnss in the / lib directory. Copy the above files to our directory, and then use the command / sbin/start\ _ udev to start the udevd service. (udevd needs to call some other system commands, such as / sbin/modprobe, which can be traced with strace.)

Directory file of the mini system

Where: / dev directory is the directory where the system stores available devices, and / log is the log record file generated using the strace command.

Step4: complete the login login capability

Because the mechanism of login is complicated, which involves many aspects such as process management mechanism, process group, console and so on, we use * M2commands to copy the / sbin/init command to the mini system directory, and the init script is changed to

#! / bin/bashexec / sbin/init

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

The process of / sbin/init is roughly divided into three parts: the first block is udevd load driver module, file system check and root switch, and the relevant configuration is in / etc/rc.sysinit; the second block is to start various services, which is configured in the / etc/rc.d/ directory; and the third block is the login part, which needs to call commands such as / sbin/mingetty and / bin/login. Copy the above-mentioned commands and files to the corresponding directory of the small system, and modify the configuration.

Since initrd.img runs directly in memory as a temporary root file system after the mini system starts, and our small system does not need to make a root switch, note the body of the remount\ _ needed () function in / etc/rc.sysinit, so that the root switch will not be done.

Since the system uses a new Upstart startup method (/ sbin/init program has been provided by the upstart software package), copy the configuration files related to Upstart startup to the mini system directory:

/ etc/inittab configuration default run level

/ etc/init/rcS.conf loads the rc.sysinit script to complete the system initialization task

/ etc/init/rc.conf compatible script, responsible for call processing at each run level

/ etc/init/rcS-sulogin.conf starts / sbin/sushell environment in single-user mode

Ctrl+Alt+Del hotkey operation under / etc/init/control-alt-delete.conf control terminal

/ etc/init/start-ttys.conf configure the number of open tty terminals and device files

/ etc/sysconfig/init controls the number of open tty terminals and the terminal color scheme

/ etc/init/tty.conf controls the opening of the tty terminal

Change the running priority of bootmini/etc/inittab to 2, then / sbin/init will execute files in the bootmini/etc/rc.d/rc2.d/ directory that start with S and change the names of some service files that do not need to be opened to start with K.

The operation that the user needs to perform automatically after the system is booted can be added to the bootmini/etc/rc.d/rc.local file.

Login program is based on authentication system PAM, configuration files are in / etc/pam.d/ directory, related library files are / lib64/security/ and its dependent library files; login also involves user group management / bin/chgrp, / bin/chown, / bin/chmod, etc., saving user name files / etc/passwd, / etc/group, user password file is / etc/shadow. Some of the other files involved can be analyzed through strace.

A complete version of the mini system that can be run on a real machine

Some directory files:

/ etc

/ bin

/ sbin

/ usr/bin

/ usr/sbin

At this point, the file system is ready to run. In the next article, we will create a new wheel-- cut the Linux kernel. The real machine effect will also be seen in the second part.

Summary

The above is the whole content of this article, I hope that the content of this article has a certain reference and learning value for your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report