System Startup Process: init

Source: Internet
Author: User
Tags dmesg
System Startup Process: init-general Linux technology-Linux programming and kernel information. The following is a detailed description. In the previous article, we learned how to display and manage running processes. Today, I want to describe how these processes actually start.

Many clever things happen when you start your computer into FreeBSD. I cannot discuss all the details here, but FreeBSD handbook provides an excellent explanation of the pilot process. When you start your computer, you may notice that the kernel has detected the hardware and displayed relevant results on the terminal. When the detection ends, the kernel starts two processes: process 0 (swapper) and process 1 (init ).

The program responsible for process control Initialization is init; other processes cannot be started without it. During boot, init has two important tasks: first, it loads the startup script under rc control, and then it initializes the terminal so that users can log on. Let's describe these functions separately, starting with rc:

Whatis rc
Rc (8)-command scripts for auto-reboot and daemon startup
These scripts are actually located in/etc/rc; normally, the configuration file located in/etc corresponds to the fifth part of the Manual, so you can modify the configuration file correctly according to the manual. However, if you call:

Man 5 rc
You will get the following information:

No entry for rc in section 5 of the manual
This seems odd for the Part 8 in the manual mentioned above, because it contains system maintenance and operation commands, which are usually background processes. Let's take a further look at this file:

More/etc/rc
# System startup script run by init on autoboot
# Or after single-user.
# Output and error are redirected to console by init,
# And the console is the controlling terminal.
# Note that almost all of the user-Defined able behavior
# Is no longer in # this file, but rather in/etc/defaults/rc. conf.
# Please check that file first before contemplating any changes
# Here. If you do need to change this file for some reason, we
# Woshould like to know about it.
Good, very clear; it seems that we can't mess up the file ourselves. There are some important things that are necessary to guide our system correctly. Let's jump back and look at some important parts to find out what actually happened at startup. Note that when the rc Script is processed during the boot process, init records all output and error messages to the terminal.

One of the first tasks that rc does is to set the PATH variable so that it can find the executable program on your FreeBSD system:

PATH =/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin
Then it looks for the/etc/defaults/rc. conf and/etc/rc. conf files:

# If there is a global system configuration file, suck it in.
If [-f/etc/defaults/rc. conf]; then
./Etc/defaults/rc. conf
Elif [-f/etc/rc. conf]; then
./Etc/rc. conf
Fi
Then it performs a file system consistency check. If you shut down the FreeBSD system abnormally, you will see it complaining during the boot process.

Echo Automatic boot in progress...
Fsck-p
Assuming that fsck has no problem, it will immediately load your file system:

# Mount everything snapshot t nfs filesystems.
Mount-a-t nonfs
Before any other process starts, your CMOS clock must be adjusted to an understandable form of kernel clock:

Adjkerntz-I
Then sort out the var directory and write the boot information to dmesg. boot:

Clean_var (){
If [! -F/var/run/clean_var]; then
Rm-rf/var/run /*
Find/var/spool/lock! -Type d-delete
Rm-rf/var/spool/uucp/. Temp /*
# Keep a copy of the boot messages around
Dmesg>/var/run/dmesg. boot
Then rc will read the following files:

/Etc/rc. sysctl
/Etc/rc. serial
/Etc/rc. pccard
/Etc/rc. network
/Etc/rc. network6
Then reset the terminal permission:

# Whack the pty perms back into shape.
Chflags 0/dev/tty [pqrsPQRS] *
Chmod 666/dev/tty [pqrsPQRS] *
Chown root: wheel/dev/tty [pqrsPQRS] *
And clear the "garbage" generated by yourself and the/tmp directory:

# Clean up left-over files
# Clearing/tmp at boot-time seems to have a long tradition. It doesn' t
# Help in any way for long-living systems, and it might accidentally
# Clobber files you wowould rather like to have preserved after a crash
# (If not using mfs/tmp anyway ).
# See also the example of another cleanup policy in/etc/periodic/daily.
# Remove X lock files, since they will prevent you from restarting X11
# After a system crash.
Now rc is ready to start some background processes, including syslogd and named:

# Start system logging and name service. Named needs to start before syslogd
# If you don't have a/etc/resolv. conf.
Then there are inetd, cron, lpd, sendmail, sshd, and usbd:

# Now start up miscellaneous daemons that don't belong anywhere else
Then rc will update motd (daily information) and execute "uname-m". This command will display the schema type on the screen.

(The end of the/etc/rc file)
When it reaches the end of/etc/rc, the rc work is completed. Here, let's review: init calls the rc Script, which reads some global and local configuration files to correctly load the file system and create an environment that can be started by the system background process. Your Operating System is now started and running, but so far, there is no environment in which users can actually interact with the operating system. This is the second important feature of init.

The configuration file/etc/ttys will be read to determine the initialization terminal. Unlike/etc/rc, this file can be frequently edited by super users to ensure that init is used to initialize the desired terminal.

To understand this file, we must understand that there are three types of terminals on your FreeBSD system. Virtual terminals starting with "ttyv" and followed by a number are all terminals that users can obtain physically stored on the FreeBSD system. By default, the first of these virtual terminals, or "ttyv0", indicates the console. A serial line or dial-up terminal starts with "ttyd" and is followed by a number. They are terminals that users can obtain when Remotely accessing your FreeBSD system using a modem. The last terminal type is a Pseudo Terminal or network terminal. They start with "ttyp" and follow a number or letter to access your FreeBSD system through a network connection.

If we use the following command to view this file:

More/etc/ttys
We can see that this file is divided into three parts, each corresponding to these three types of terminals. At the same time, each part is divided into four columns, which are summarized as the following chart:

Column name
Description

Name
Terminal Device Name

Getty
The program that starts running on the terminal, usually getty. Other projects include xdm, which is used to start the X Window System or none, indicating that no program exists.

Type
For virtual terminals, the corresponding type is cons25. Other common values include pseudo-terminal network, modem entry dialup, and unknown, which are used by the user to try to connect to a terminal of an undefined type.

Status
It must be on or off. If it is on, init runs the program specified in the getty column. If the word "secure" appears, it means that the tty can be logged on as root. To avoid this situation, you can use the word "insecure ".


Let's explain the file from the virtual terminal section. We can see that it starts with setting the console:

# If console is marked "insecure", then init will ask
# For the root password when going to single-user mode.
Console none unknown off secure
If a problem occurs when running the fsck command during the boot process, init will enable your FreeBSD system to enter the single-user mode so that the root user can fix the problem. If you use insecure instead of secure to set the console, init will ask for a password before you can continue.

Ttyv0 "/usr/libexec/getty Pc" cons25 on secure
# Virtual terminals
Ttyv1 "/usr/libexec/getty Pc" cons25 on secure
Ttyv2 "/usr/libexec/getty Pc" cons25 on secure
Ttyv3 "/usr/libexec/getty Pc" cons25 on secure
Ttyv4 "/usr/libexec/getty Pc" cons25 on secure
Ttyv5 "/usr/libexec/getty Pc" cons25 on secure
Ttyv6 "/usr/libexec/getty Pc" cons25 on secure
Ttyv7 "/usr/libexec/getty Pc" cons25 on secure
Ttyv8 "/usr/X11R6/bin/xdm-nodaemon" xterm off secure
You can see that there are eight virtual terminals in addition to the console on my FreeBSD system. I can access each terminal by pressing ALT and a control key. For example, ALT F1 can access the console, ALT F2 can access ttyv1, ALT F3 can access ttyv2, and so on. If I start the X session, it can be accessed using ALT F8. If I change the word off on ttyv8 to on, I can get an X terminal instead of the console at boot. Then, you can continue to access other terminals by using ALT and the function key. All of my virtual terminals are labeled as "secure", indicating that they can be logged on as root. The number of virtual terminals you can have is determined by your FreeBSD version. If you want to create more virtual terminals, read this faq.

Now let's move to the dialing terminal:

# Serial terminals
# The 'dialup' keyword identifies dialin lines to login, fingerd etc.
Ttyd0 "/usr/libexec/getty std.9600" dialup off secure
Ttyd1 "/usr/libexec/getty std.9600" dialup off secure
Ttyd2 "/usr/libexec/getty std.9600" dialup off secure
Ttyd3 "/usr/libexec/getty std.9600" dialup off secure
You can see that I have four available dial-up terminals, but they are all closed. If I want users to access my FreeBSD system through a modem, I must open at least one of them and decide whether to allow these users to log on as root. If not, change the word "secure" to "insecure ". Do you see that the getty column contains the number 9600, which indicates that the data transmission rate is 9600bps. Because many modem servers now have a higher rate, I can change it to 57600. Finally, read the dialing service section in FreeBSD handbook.

The last part of the/etc/ttys file is a network or Pseudo Terminal. You will see that there are many of them, accurate to 255, range from:

# Pseudo terminals
Ttyp0 none network

To

TtySv none network
They are not allowed by default.

If you want/etc/ttys changes to take effect to enable init to use these changes, you can send an HUP signal to init as a superuser, for example:

Kill-1 1
-1 indicates signal 1 (HUP), and 1 indicates process 1 (init ).

So now, which/etc/ttys file mentioned in the getty program continues to run? The description of getty in man 8 is as follows:

Description
Init (8) Call the getty program to open and initialize the tty line, read a login name, and then call login (1 ).
Therefore, init reads/etc/ttys and starts a getty process on each terminal you set in the configuration file. The job of getty is to monitor the terminal to see if someone is trying to log on. If yes, getty will start the logon program to verify the user's login name and password. If the verification is successful, the logon program starts the user's logon command interpreter and places the user in their home directory. When a user has a command interpreter, they can interact with the operating system. Now it allows the command interpreter to explain user input and make sure necessary processes are started.

When a user logs out, call init again to start other getty processes to continue the monitoring terminal and wait for other login attempts.

Let's take a look at the output of the ps command to summarize the entire process just now. FreeBSD 4.1 has installed the ps program by default. I will use the-ax switch to include the system process:

Ps-ax
PID TT STAT TIME COMMAND
0 ?? DLs 0: 00. 01 (swapper)
1 ?? ILs 0: 00. 16/sbin/init --
2 ?? DL 0: 00. 02 (pagedaemon)
3 ?? DL 0: 00. 00 (vmdaemon)
4 ?? DL 0: 00. 02 (bufdaemon)
5 ?? DL 0: 01. 02 (syncer)
1056 ?? Is 0: 00. 00 adjkerntz-I
1187 ?? Ss 0: 00. 08 syslogd-s
1206 ?? Is 0: 00. 05 inetd-wW
1208 ?? Is 0: 00. 11 cron
1622 ?? Ss 0: 00. 02 sendmail: accepting connections on port 25 (sendmail)
1621 v0 Ss. 12-csh (csh)
1701 v0 R +. 00 ps-ax
1699 v1 Is +. 01/usr/libexec/getty Pc ttyv1
1619 v2 Is +. 01/usr/libexec/getty Pc ttyv2
1618 v3 Is +. 01/usr/libexec/getty Pc ttyv3
1617 v4 Is +. 01/usr/libexec/getty Pc ttyv4
1616 v5 Is +. 01/usr/libexec/getty Pc ttyv5
1615 v6 Is +. 01/usr/libexec/getty Pc ttyv6
1614 v7 Is +. 01/usr/libexec/getty Pc ttyv7
Now you know many processes: swapper's PID is 0 and init is 1. Adjkerntz, syslogd, inetd, cron, and sendmail are all started by rc. Of course, I have to run this ps command on a command interpreter; in the above case, it runs from the c shell interpreter on ttyv0. The getty process waits for logon from the virtual Terminal 1 to 7. On virtual terminal 8, the getty process is not run because the terminal is marked as "off" in/etc/ttys ".
Related Article

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.