Linux core (3)

xiaoxiao2021-03-06  95

Linux core (3)

Www.ibmtc.pku.edu.cn/crs/kernel/kernel.htm

(This article allows this article to be used in academic purposes.)

Vivid chapter

Guide and shutdown

The content of this section is mainly focused on the Linux kernel, and it is also briefly described system tools and system calls related to each core components. The main story of this section is not yet in the previous chapter. Content related to system management.

This chapter explains the boot and shutdown process of the Linux system. The guidance process of different system platforms is slightly different, and this chapter tells the leading process of the Linux system as the Intel platform. Since Linux uses buffer cache technology, it is a multi-user system, so it must be followed when it is turned off, otherwise it will cause data loss and file system destruction.

And the system's boot, shutdown process related concepts and single user mode, run level, etc. The correct understanding of these concepts is the necessary conditions for configuration and maintenance systems.

16.1 Trouble Process of Linux

In general, the boot process of the operating system is divided into two steps. First, after the computer hardware is turned on, it is generally called a "boot loader" from the fixed position of the floppy disk or the hard disk. Then, the boot loader is responsible for loading and running the operating system. The boot loader is very small, generally only hundreds of bytes, and the operating system is large and complicated. With the above two-stage boot process, the curing software in the computer can be kept small enough, and it is also easy to implement guidance to different operating systems.

Different computer platform guidance processes are mainly in the first phase of the guiding process. For the Linux system on the PC, the computer (ie BIOS) is responsible for reading the boot loader from the first sector (ie, the boot sector) of the floppy disk or hard disk, and then loads from the disk or other position by the boot loader. Enter the operating system.

For a typical PC BIOS, it can be configured to boot from a floppy disk or from a hard disk. When booting from a floppy disk, the BIOS reads and runs the code in the boot sector. The code in the boot sector reads hundreds of blocks before the floppy dish (depending on the actual kernel size), and then place these code in a pre-defined memory location. When booting Linux using a floppy disk, there is no file system, the kernel is in a continuous sector, so that the boot process can be simplified. However, if you can boot Linux from the floppy disk containing the file system using LILO (Linux Loader).

When booting from the hard disk, since the hard disk is partitioned, the boot process is more complicated than the floppy disk. The BIOS first reads and runs the code in the main boot record in the hard disk. These code first check the partition tables in the main boot record, find the active partition (ie, the flag is the bilitary partition), then read and run the active partition boot The code in the sector. The active partition The role of the sector and the function of the floppy boot sector: read the kernel image from the partition and start the kernel. Unlike the floppy guide, the kernel image is saved in the hard disk partition file system, not in the subsequent consecutive sector, so the code in the hard disk boot sector also needs to position the kernel image in the file system The location, then load the kernel and start the kernel. Usually, the most common method is to use LILO to complete this phase of boot. LILO can be configured to load different kernel images, and can even start different operating systems, or you can specify the kernel command line parameters via LILO. For details on LILO, see the relevant HOWTO documentation.

Guide from the floppy disk and boot from the hard disk, but the common boot mode is guided from the hard disk. However, in a multi-boot system (multiple operating systems), from the hard disk boot may bring some trouble, therefore, the usual practice is to first boot from the hard disk from the floppy disk, and so on, then install LILO from the hard disk. After Linux kernel is loaded, the Linux kernel is initialized for hardware and device drivers, and then running init. INIT is the first user-level process launched by Linux kernel. The identification number of its license is 1, which plays an important role in the system boot and shutdown. In the subsections behind this chapter, the init process will be described in detail. The initialization work in the Linux kernel can be generally described as follows:

The Linux kernel is generally compressed, so it must first decompress itself. Some of the code in front of the kernel image completes decompression.

If the system is installed in the system, Linux can recognize the SVGA card, Linux prompts the user to select the appropriate text display mode. However, if text mode is set in advance during the compilation process of the kernel, the display mode is not prompted. This display mode can also be set via LILO or RDEV.

The kernel is next to detect other hardware devices, such as hard drives, floppy disks, and network cards, etc., and configure the corresponding device driver. At this time, the core will output some hardware information, similar to the output:

LILO boot:

Loading Linux.

Memory: sized by int13 088h

Console: 16 Point Font, 400 Scans

Console: Color VGA 80x25, 1 Virtual Console (MAX 63)

PCIBIOS_INIT: BIOS32 Service Directory Structure AT 0x000F7510

PCIBIOS_INIT: BIOS32 Service Directory Entry AT 0xFD7D2

PCIBIOS_INIT: PCI Bios Revision 2.10 Entry At 0xFD9E6

PROBING PCI HARDWARE.

Calibrating delay loop .. OK - 231.83 BOGOMIPS

Memory: 30760K / 32704K Available (748K Kernel Code, 384K Reserved, 812K Data)

Swansea University Computer Society Net3.035 for Linux 2.0

Net3: UNIX Domain sockets 0.13 for Linux Net3.035.

Swansea University Computer Society TCP / IP for Net3.034

IP Protocols: IGMP, ICMP, UDP, TCP

Linux IP MultiCast Router 0.07.

VFS: Diskquotas Version DQUOT_5.6.0 Initialize

Checking 386/387 Coupling ... OK, FPU Using Exception 16 Error Reporting.

Checking 'HLT' Instruction ... OK.

Linux Version 2.0.36 (root@porky.red hat.com) (GCC Version 2.7.2.3) # 1 Tue Oct 13 22:17:11 EDT 1998

Starting Kswapd V 1.4.2.2

Serial Driver Version 4.13 with no serial options enabledtty00 AT 0x03f8 (IRQ = 4) IS A 16550A

Real Time Clock Driver v1.09

Ramdisk Driver Initialized: 16 ramdisks of 4096k size

IDE: I82371 PIIX (Triton) on PCI Bus 0 function 57

IDE0: BM-DMA AT 0xFCD0-0XFCD7

IDE1: BM-DMA AT 0xFCD8-0XFCDF

HDA: Hitachi_DK237A-32, 3102MB W / 512KB Cache, CHS = 788/128/63, UDMA

HDC: CD-224E, ATAPI CDROM DRIVE

IDE0 AT 0x1F0-0X1F7, 0X3F6 on IRQ 14

IDE1 AT 0x170-0X177, 0X376 on IRQ 15

FLOPPY Drive (s): fd0 is 1.44M

FDC 0 Is A POST-1991 82077

MD driver 0.36.3 max_md_dev = 4, MAX_REAL = 8

SCSI: 0 Hosts.

SCSI: Detected Total.

Partition CHECK:

HDA: HDA1 HDA2 HDA3 HDA4

Next, the kernel is hovering the root file system. The location of the root file system can be specified when the compile core can also be specified by LILO or RDEV. The type of file system can be automatically detected. If the bundle fails due to some reasons, the kernel starts failed and eventually terminates the system. The root file system is typically hung as a read-only file system.

Thereafter, the kernel launches the init process (located in / sbin / init). The startup work of the init process is described in detail in the subsequent section. In general, it will launch some important background daemon.

Then, init switches to multi-user mode and starts a getty process for each virtual console and serial line, and the Getty process manages the user from the virtual console and the serial terminal. According to different configurations, INIT can also start other processes.

At this point, the system's boot process ends.

16.2 shutdown

For Linux systems, it must always be shut down in the correct way. Because Linux uses the buffer cache, the data written to the disk is not written to the physical disk immediately, and therefore, the direct power off will result in the loss of the data or the damage of the file system.

For multi-tasking systems, there may be many background processes in operation, only with the correct shutdown operation to ensure that all daemon can save your own data.

In the Linux system, shutdown commands can be used. According to different applications, ShutDown can have different methods of use, but there are usually two methods:

If only one user using a Linux system, the usual process is as follows: Exit all of the running processes; log out of all virtual terminals; log in as root in a virtual terminal; ensure that the root file system, then run Shutdown-H NOW, at this time, the system immediately enters the shutdown process.

If there are many users in the system, the shutdown -h time message should be used. This command can notify all users will shut down within the time specified by TIME, remind the user to exit all programs and save the data. Time is a minute time, Message is a warning message displayed on each terminal and xterm. This warning message can be repeated before the shutdown is turned off. When the actual shutdown process begins, all file systems (outside the root file system) are unmissuous, all user processes are forcibly killed, and the daemon is turned off. After that, the init print information indicates that the power is turned off, and it is time to really turn off the power.

There are also situations where the normal shutdown occurs. If the kernel is in an emergency state and terminate the CPU, then the power will have to be turned off, and then restart the computer. However, if other reasons are not properly shut down, the kernel and daemon are still in operation. At this time, you should wait for a sufficient time (ten minutes) to turn the Update daemon refreshed after the buffer cache, then turn off the power.

16.3 Reboot

There are two ways to reboot from the Linux system, one is to turn off first, then turn on the power, reboot; another is the -r option using the shutdown command.

In most Linux systems, the common Ctrl Alt DEL hottects in DOS can run the shutdown -r now command to reboot the system. However, the action of the button can be configured, and the system can be configured to press the CTRL Alt DEL button to run the shutdown -r now command, or the user who runs the reboot command when the above-described key combination is defined.

16.4 Emergency guidance floppy disk

In the case of erroneous installation, the Linux system may not be guided from the hard disk, and other routes must be guided using other ways. Typically in the PC, the system can be booted from the floppy disk and solve the problem. To this end, an emergency guidance floppy disk must be included.

Most Linux business issues can establish an emergency boot floppy disk during the installation process. However, these floppy discs often contain only the system kernel, and the program used to solve the problem may not be in the floppy disk. Therefore, sometimes you need to create a custom boot floppy, but also need to update your emergency boot floppy disk.

16.5 init

INIT is the first user-level process started by the kernel, which plays an important role in the Linux system. Although INIT is critical to the system, most of the cases do not need too much attention to its configuration and running. Typically, you only need to configure init when you need to create a serial terminal, set the dial-in and dial-out modem or change the default run level.

After the kernel ends its own boot process (after loading memory, after initializing the hardware, and device driver), start the user-level process init. In general, the kernel is first looking for / sbin / init. If the kernel cannot find init, try to find / bin / sh, where the SH cannot be found, the system's boot process failed.

After init startup, first complete the following tasks:

Check the file system;

Clear / TMP;

Start various services;

Launch a getty for each terminal and the virtual console. GETTY is responsible for the user's login, and Chapter 17 will detail the login and logout process.

After the above tasks are completed, the guidance process of the system will end.

Whenever the user logs out of a terminal or the virtual console, the init process restarts a getty for the terminal or the virtual console so that other users can log in. In addition, the init process is also responsible for managing the "orphan" process in the system. If a process is created after a child process, the child process becomes a orphan process before the child process terminates. The init process is responsible for "adoption" process, that is, the orphan process will immediately become the child process of the init process. This treatment is important for technical reasons, and on the one hand, it is easy to handle process tables and processes. More in init's variants, most Linux issues are written in sysvinit (by Miquel Van SMoorenburg), named based on System V. UNIX's BSD version has different init. The main difference is whether there is a run level: System V has a run level, and the BSD does not run the level. But this difference is not an essential difference. INIT described in this chapter is based on sysvinit.

16.6 Start getty: / etc / inittab file

When INIT is started, it reads the / etc / initab configuration file. During the normal operation of the system, if the Signhup signal is sent to the init process (Kill-Hup 1 as root user), then the init process will re-read the / etc / inittab file so that without rebooting the system. Let the init's configuration file take effect.

Each line in the / etc / inittab file consists of four fields, and each field is separated by a semicolon:

ID: Runlevels: Action: Process

The meaning of the above fields is described in Table 16-1. In addition, the / etc / inittab file can include blank lines, the line starting with "#" as a comment line.

Table 16-1 / ETC / INITTAB file meaning

id

This field is an identifier of each row as in the initTab file. For each line of defining getty, the identifier specifies the terminal (ie, the device file name / DEV / TTY) in the device file name). For other lines, in addition to the length limit, there is no special requirements, but the primary key of the database table should have a unique value.

Runlevels

This field specifies the run level, each running level is represented by a single number, which can represent a plurality of run levels, but cannot contain any separator. The run level will be described in next section.

Action

This field specifies the action of the row, for example, after RESPAWN indicates that the command specified by the specified command exits, and the ONCE indicates that the command of the next field is only run once.

PROCESS

This field specifies the command to run.

If you want to start Getty on the first virtual console and run on all usual multi-user runs (2 ~ 5), you should define as follows:

1: 2345: Respawn: / sbin / getty 9600 TTY1

The first field is 1, indicating that the line is used to define / dev / tty1; the second field indicates that the line is applied to multiple run levels 2, 3, 4, and 5; the third field indicates that when Getty is exited Need to run again (so that you can log in again, log in again); the last field describes the commands of running getty on the first virtual terminal.

If you want to add a terminal or dial in a modem line for a system, you need to add a corresponding row in / etc / inittab, each row, can define a terminal or a dial-in line. See Man Page: INIT (8), INITTAB (5) and Getty (8). 16.7 Run Level

Run Level Refers to init and a running status of the entire system, which defines the service provided by the system. The running level is indicated by a number, and the running level and meaning representing each number are given by Table 16-2.

Table 16-2 Run level

0

Terminate the system.

1

Single user mode (used for special management tasks).

2 ~ 5

Usually operations (user-defined).

6

Reboot.

Running level (2 ~ 5) defined by the user can be determined by the system administrator. Some administrators use the run level to define the running subsystem, for example, whether to run x, whether it is operable to operate the network. But usually does not need to modify the default run level. Different Linux distributions may differ from the definition of running levels 2 to 5. For example, Red Hat Linux 5.x Defines Run Level 5 to enter X Window, to replace the virtual terminal of the character with an XDM and log in in a graphical way.

As shown below, it is a line defined in the / etc / inittab:

12: 2: Wait: /etc/init.d/rc 2

The second field of the above line defines whether the row is applied to the run level 2. WAIT in the third field indicates that the commands that are defined later, INIT run once, and after entering the run level, init must wait for the command to end.

The command in the fourth field actually specifies the hard work of establishing a run level. It is necessary to start the service that has not been activated, and the service that should not be run in the new run level.

When INIT is started, it is necessary to find a line in / etc / inittab to specify the default run level:

ID: 3: INITDEFAULT:

Depending on the default run level, init will run all rows that contain the default run level after normal startup.

Use the Telinit or init command to switch to other run levels, for example:

Telinit 1

Will enter the run level 1, i.e., single user mode.

By specifying command line parameters such as Single or EMERGENCY for the kernel, request init can enter non-default run levels at startup to select a single user mode. Single user mode will be described in detail in 16.10.

Special settings for 16.8 / etc / inittab file

/ etc / inittab has some special settings that allow the init response to certain special circumstances. These special settings are specified by the special keywords of the third field. Table 16-3 shows some special keywords.

Table 16-3 Special settings in ETC / INITAB Keywords

Powerwait

Allows the system by the Init to shut down in the event of a power supply. This keyword assumes that the uninterruptible power supply (UPS) is used, and the software notification INIT power of the UPS is closed.

CtrLaltdel

When the user presses the Ctrl Alt DEL button on the console keyboard, the init retransmission system is allowed. However, the system administrator can configure the system to ignore the button combination.

Sysinit

The command to run after the system is guided. This command usually clears the / TMP directory.

For more information about / etc / inittab file special settings for the keyword, see Man Penin INITTAB (5).

16.9 single user mode

Single user mode, the run level 1 is the most important run level, at this level, only the system administrator can use the computer, and only very little system service is running. For some system management tasks, single user mode is indispensable, for example, if you want to run FSCK on the / usr partition, you need to unload the file system. Unless all system services can be killed, it will not be able to unload the file system. Using Telinit to request a single user mode in the running state. When booting, the single user mode can be entered by providing a Single or EMERGENCY command line parameter for the kernel.

When booting, if the automatic FSCK fails, the boot script running in init will automatically enter the single user mode. In this case, by entering the single user mode, the use of bad file systems can be avoided.

For security considerations, the correct configuration system will ask the root user password before entering the single user mode.

转载请注明原文地址:https://www.9cbs.com/read-95342.html

New Post(0)