Linux boot insider

Source: Internet
Author: User

As the operating system is becoming more and more complex, the power-on and power-off processes become more and more intelligent. From a simple DOS system to a Windows NT system, people have already personally experienced these changes-not just starting and shutting down the core operating system, A considerable number of service projects must be started or closed at the same time. Similar to Windows NT, the number of service projects that need to be opened during Linux Startup is also large.

Here, we assume that you are familiar with the boot process of other operating systems and understand the self-check boot steps of the hardware, just from the boot loader of the Linux operating system (usually Lilo for personal computers) start with Linux boot guide.

Load the kernel

After lilo is started, if you choose Linux as the operating system for boot, the first thing to be loaded is the kernel. Remember that there is no operating system in the computer memory at this time, and PC (because of their natural design flaws) has no way to access all the memory on the machine. Therefore, the kernel must be fully loaded into the first MB of available Ram. For this purpose, the kernel is compressed. The header of this file contains the necessary code. First set the CPU to enter safe mode (to lift the memory limit), and then extract the remaining part of the kernel.

Run the kernel

After the kernel is decompressed in the memory, it can start to run. At this time, the kernel only knows its built-in functions, that is, the kernel part compiled as a module cannot be used. Basically, the kernel must have enough code to set its own virtual memory subsystem and root file system (usually ext2 File System ). Once the kernel starts running, the hardware detection determines which device drivers need to be initialized. From here on, the kernel can mount the root file system (this process is similar to Windows's process of identifying and accessing the C drive ). After the root file system is mounted to the kernel, a program called init will be started and run.

Note: here we intentionally omitted many details about Linux kernel startup, which are of interest only to kernel developers. If you're curious, visit the "kernel hackers Guide" at http://www.redhat.com: 8080 ".

INIT process

The INIT process is the first non-kernel process to be started, so its process id pid value is always 1. Init reads its configuration file/etc/inittab and determines the runlevel to be started ). Basically, the running level specifies the behavior of the entire system. Each level (represented by an integer ranging from 0 to 6) meets a specific purpose. If the initdefault level is defined, this value is directly selected. Otherwise, you must enter a value that represents the running level.

After entering a number indicating the running level, init executes a command script program according to the definition in the/etc/inittab file. The default running level depends on the selection of Logon programs in the installation phase: whether to use text-based or X-window-based logon programs.

RC command script program

We already know that when the running level changes, the/etc/inittab file will define which command script program to run. These Command Script programs start or stop various services at the specific running level. Because the number of services to be managed is large, you need to use the RC command script program. Among them, the most important one is/etc/rc. d/RC, which is responsible for calling the corresponding command script program for each running level in the correct order. As we can imagine, such a command script program can easily become difficult to control! A well-designed plan is required to prevent such incidents.

For each running level, there is a corresponding sub-directory in the/etc/rc. d sub-directory. The sub-directories at the running level are named as rcX. D. X indicates the number at the running level. For example, all the Command Script programs of running level 3 are saved in the/etc/rc. d/rc3.d subdirectory.

In the sub-directories of each running level, the/etc/rc is created. d/init. d sub-directory command script program symbolic links, however, these symbolic links do not use the command script program in/etc/rc. d/init. the original name in the D sub-directory. If the command script program is used to start a service, its symbolic link name starts with the letter S. If the command script program is used to close a service, the name of the symbolic link starts with K.

In many cases, the execution sequence of these script programs is very important. If you do not configure the network interface first, you cannot use DNS to resolve the host name! In order to arrange their execution sequence, a two-digit character is followed by the letter S or K, and a small value is followed by a large value. For example,/etc/rc. d/rc3.d/s50inet will be executed before/etc/rc. d/rc3.d/s55named (s50inet configures network settings and s55named starts the DNS server ).

Stored in/etc/rc. d/init. the Command Script programs in the D sub-directory and on the symbolic link are real practitioners who have completed the operations to start or stop various services. When/etc/rc. d/RC runs through each specific sub-directory of the running level, it calls each command script program for execution in order of numbers. It first runs the command script program with the letter K headers, and then runs the command script program with the letter S headers. The stop parameter is passed for the command script program that starts with letters K. Similarly, the start parameter is passed for the command script program that starts with letters S.

Write your own RC Command Script

In the days when the Linux system is maintained, the system administrator must modify the script for starting or shutting down the system. There are two ways to achieve the purpose of modification:

● If the modification only takes effect during boot, and the modification is not significant, you can simply edit the/etc/rc. d/rc. Local script. This command script program is executed at the last step of the boot process.

● If the modifications are meticulous or you want to shut down the process to make it stop running explicitly, you need to add a command script program in the/etc/rc. d/init. d subdirectory. The script program must accept the start and stop parameters and complete corresponding operations.

The first method is to edit the/etc/rc. d/rc. Local script. Of course, the two methods are relatively simple. If you want to add content to this command script program, you only need to open it using your favorite editor program, and then attach the command to be executed to the end of the file. This is indeed very convenient for the modification of one or two rows.

If you need to use a command script program, you must select the second method. Writing an RC Script is not as difficult as you think. Here is an example to see how it is implemented (by the way, you can use our example as a template to modify and add as needed ).

Suppose you want to call a special program every 60 minutes to bring up a message and remind you to leave the keyboard for a while. The command script contains the following parts:

● Description of the functions of the script program (so that it will not be forgotten one year later );

● Verify that the script program exists before trying to run it;

● Accept the start and stop parameters and perform the required actions.

After the parameter is specified, We can compile the script program of the command. This program is very simple. You can write it yourself. I will not give it here.

After writing a new command script program, add the necessary symbolic links to the relevant running level sub-directories to control the startup or stop of the script program. In my impression, I only want to make it start in runlevel 3 or runlevel 5 because I think only these two runlevel is the place where daily work is done. Finally, we hope the script program will be closed when it enters the running level 6 (restart.

Activate or disable a service item

Sometimes, you may find that a certain service is not required to be started during the boot process. This is especially true if you are considering replacing Windows NT files and print servers with Linux.

We already know that you can change the Symbolic Link name in a specific sub-directory of the running level to prevent the service from being started. For example, you can change the first letter of its name from S to K. Once you have mastered the command line and symbolic links, you will find that this is the fastest way to activate or disable services.

When learning this renaming method, you may feel that the graphic operation interface ksysv is easier to grasp. Although it was originally designed and used in the KDE environment, it also runs well in the default GNOME environment installed in Red Hat Linux 7.2. To start it, simply open an xterm window and enter the ksysv command. A window appears, listing all the parameters that can be modified and online help is also needed.

Warning: If you are learning the knowledge of this article in a real system, you need to use more common sense. When you try to modify the startup script program, remember that the modifications may make your system unable to work normally and cannot be restored using the restart method. Do not experiment with new settings on a running system. Back up all the files you want to modify. The most important thing is to prepare a boot disk at hand to prevent unexpected events.

 

Appendix: My Understanding of RC startup

1. When sbin/init is started, read the/etc/inittab and load the RC runlevel.

Run/etc/rc. d/rc. sysinit.

2. Run RC runlevel to load/etc/rc. d/rcX. D/S * K *

Here, both S * and K * are symbolic links under init. d.

3. Finally, link s99local to/etc/rc. d/rclocal and execute

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.