Premise: pc host, MBR architecture
-
Step 1: post (power on system test) power on self test.
There is a rom chip (CMOS) on the main board of the pc. after power up, the cpu looks for the raw, reads the instructions inside, and checks whether there is memory, hard disk, display device, etc. on the machine.
There is a bios (basic input output system) program in CMOS
-
Step 2: boot sequence
Search the boot devices in order. The first device with bootloader is the device used for this boot.
bootloader:
-
Functions:
- Locate the grub program on the disk and load it to run.
- grub provides a menu for users to choose. It says that you can choose to run the kernel list
- Load the kernel program selected by the user from the disk into the specific space of the memory, then decompress and expand it. After that, the kernel starts to run, and then the bootloader exits, and the kernel takes over everything.
Note: since bootloader only has 446 bytes, it can't read LVM, the logical partition of soft RAID can only read the physical partition, so the kernel program can only be stored on the physical partition.
-
Species:
LILO: linux loader. It has a fatal weakness. If the kernel is stored on a cylinder after 1024 of the disk, it cannot load the kernel. Android phones use Lilo.
-
GRUB: Grand Uniform Bootloader
CentOS 5 and 6 use the version grub 0.x (alias: grub legacy)
The version used by cengtos7 is grub 1.x (alias: grub2)
Where is the bootloader program?
If it is MBR architecture, it is placed in the first 446bytes in track No. 0 and sector No. 0.
Because there are only 446 bytes, the space is too small, and the program that can be written is limited, so linux uses GRUB mechanism.
GRUB mechanism: do not let bootloader directly load the kernel, but let bootloader load another program / boot/grub on the disk. Because / boot/grub is on disk, the 446 byte constraint is broken.
The first stage: bootloader loading / boot/grub program
Stage 1.5: filesystem driver?
-
The second stage:
centos5,6: load the configuration file under / boot/grub
-
centos7: load / etc/grub.d /, / etc/default/grub, / boot/grub2/grub.cfg
# ll /etc/grub.d/ -rwxr-xr-x. 1 root root 8702 Nov 9 2018 00_header -rwxr-xr-x. 1 root root 1043 Jul 5 2018 00_tuned -rwxr-xr-x. 1 root root 232 Nov 9 2018 01_users -rwxr-xr-x. 1 root root 10781 Nov 9 2018 10_linux -rwxr-xr-x. 1 root root 10275 Nov 9 2018 20_linux_xen -rwxr-xr-x. 1 root root 2559 Nov 9 2018 20_ppc_terminfo -rwxr-xr-x. 1 root root 11169 Nov 9 2018 30_os-prober -rwxr-xr-x. 1 root root 214 Nov 9 2018 40_custom -rwxr-xr-x. 1 root root 216 Nov 9 2018 41_custom -rw-r--r--. 1 root root 483 Nov 9 2018 README # ll /etc/default/grub -rw-r--r--. 1 root root 279 Nov 29 16:53 /etc/default/grub # ll /boot/grub2/grub.cfg -rw-r--r--. 1 root root 4287 Nov 29 16:53 /boot/grub2/grub.cfg
-
-
Step 3: kernel initialization
Detect all hardware devices
-
In order to load the root file system on the disk, first load the file system on the ramdisk and find the disk driver inside.
Note: it is also possible not to use ramdisk. When you compile the kernel on your own machine, the compiler detects the local disk type, so when you compile, you can compile the disk driver into the kernel, so the kernel doesn't need to look for ramdisk.
Use disk drive to load the root file system in read-only mode. Read only purpose: to prevent bug s in the kernel and delete the contents of the root file system. No problem, then change to read and write.
-
Run the first application in user space: / sbin/init
Init program before centos5: SysV init
Configuration file used: / etc/inittab
centos6's init program: Upstart
Configuration file used: / etc/init/*.conf
init program of centos7: Systemd (redhat refers to Mac OS)
Configuration files used: files under the directory of / usr/lib/systemd/system, and files under the directory of / etc/systemd/system
- Execute all units of / usr/lib/systemd/system/initrd.target and mount the partition in / etc/fstab
- Switch from ramdisk root file system to disk root
- Execute / etc/systemd/system/default.target
- Execute / usr/lib/systemd/system/sysinit.target and / usr/lib/systemd/system/basic.target
- systemd starts the local service and server service in / usr/lib/systemd/system/multi-user.target
- System D executes / etc/rc.d/rc.local in / usr/lib/systemd/system/multi-user.target
- systemd executes getty.target and login service in / usr/lib/systemd/system/multi-user.target
- If the execution result of the command systemctl get default is graphic, execute / usr / lib / SYSTEMd / system / graphic.target
Step 4: sbin/init will start the / sbin/mingetty program to display the text interface that can be logged in.
New features of Systemd:
- Start independent services in parallel
- Activate services on demand, not all of them are started up, waste resources there, and start when users really use them
- Can provide a snapshot of user space processes
- Service control logic can be defined based on dependencies
The core concept of Systemd: unit
unit is an abstraction that is identified, identified, and configured by its associated configuration file.
The configuration file mainly contains system services, monitored socket s, saved snapshots, and other init related information.
See which unit file s:
# systemctl list-unit-files -a UNIT FILE STATE proc-sys-fs-binfmt_misc.automount static dev-hugepages.mount static brandbot.path disabled cups.path enabled ...
Place directory of unit configuration file:
-
/usr/lib/systemd/system
The main startup script settings of each service are similar to / ect/init.d/*.conf in centos5 and 6
-
/run/systemd/system/
The service script generated during system execution takes precedence over the above directory
-
/etc/systemd/system
The execution script created by the administrator is similar to the function of / etc / rc.d/rc.d/s script in CentOS 5 and 6, and runs prior to the above directory.
Common types of unit:
-
Service: the file extension is service, which is used to define the
Command compatible with cenos5, 6: service NAME start|stop|status|restart
-
Target: the file extension is target, which simulates all scripts of K * and S * in the directories of centos5 and 6 (rc0.d, rc1.d, rc2.d, rc3.d, rc4.d, rc5.d, rc6.d)
There is no concept of running level in CentOS 7. CentOS 5 and 6 are controlled by running level.
-
Device: the file extension is device, which is used to define the device recognized by the kernel
The device files of CentOS 5 and 6 are recognized by udev, and the device files of CentOS 7 are recognized by udev and systemd.
-
Mount: the file extension is mount, which defines the mount point of the file system
If you directly enter the mount command in centos7, you will find many mount points of cgroup. The resource allocation is controlled by cgroup. docker uses cgroup to allocate resources and uses namespace to isolate the network and other spaces.
Socket: the file extension is socket, which is used to identify the socket file used for inter process communication.
Snapshot: the file extension is snapshot, which is used to manage the system snapshot
Swap: the file extension is swap, which is used to manage the swap device
Automount: the file extension is automount, and the file system auto mount point is set. For example, insert the U SB flash disk and automatically identify
Path: the file extension is path, which is used to define the file or directory in the file system.
Key features:
-
The activation mechanism based on socket: the separation of socket and program.
Just start the socket to listen to the port, but the program does not start. When the supervisor hears the request coming, the program is activated.
A bus based activation mechanism that activates a service if there is access to it on the bus
Based on the activation mechanism of device, when the device is connected, the unit of mount, device and automount will be activated automatically. If the unit finds that the device is coming, the device file will be created and mounted. If the mount point does not exist, the mount point will be created automatically.
Activation mechanism based on path. Use case: when a service terminates unexpectedly, if it creates a file or a project. This path mechanism is to monitor whether this file or directory is generated in the system. If so, activate a service (tell the company's people that your service is hung, etc.).
System snapshot: save the current status of each unit in the persistent device storage. Used to recover unit to the specified time point. Let unit swim between the past and the future.
Backward compatibility with sysv init scripts: scripts placed under / etc/init.d/ can also be activated through systemd.
Incompatible:
- systemctl commands are fixed and cannot be defined at will like service.
- For services that are not started by systemld, systemctl commands cannot communicate with them, so they cannot be managed.
Management system service: unit of service type is responsible for the management of service
systemctl - Control the systemd system and service manager
systemctl [OPTIONS...] COMMAND [NAME...]
COMMANDS Unit Commands list-units [PATTERN...] list-sockets [PATTERN...] list-timers [PATTERN...] start PATTERN... stop PATTERN... restart PATTERN... try-restart PATTERN... reload-or-restart PATTERN... reload-or-try-restart PATTERN... kill PATTERN... is-active PATTERN... is-failed PATTERN... status [PATTERN...|PID...]] show [PATTERN...|JOB...] cat PATTERN... reset-failed [PATTERN...] list-dependencies [NAME]
Various operations on services: service units
-
Start stop restart status
centos5,6: service NAME start|stop|restart|status
centos7: the. service after systemctl start|stop|restart|status NAME[.service] can be omitted
# systemctl status crond ● crond.service - Command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2020-02-26 09:28:20 CST; 5h 49min ago
Loaded: already loaded. CentOS 5 and 6 are loaded with the chkconfig command.
enabled: start automatically
disabled: power on will not start automatically
InActive/Active: not started or started.
active(running): one or more runs of continuous processing
active(exited): one time configuration completed successfully
active(waiting): waiting for an event while running
static: power on does not start, but can be activated by another started service.
Example: telnet server.
After installing it, we found that it has 2 unit file s. telnet@.service is static,
Try to start telnet@.service. If it can't be started, it means that static can't be started directly.
How to start it? Just start telnet.socket. After telnet.socket starts, it becomes active (listening).
Using lsof-i: port number, you can see that telnet service is managed (supervised) by systemd.
lsof: list open files
# rpm -ql telnet-server /usr/lib/systemd/system/telnet.socket /usr/lib/systemd/system/telnet@.service # systemctl list-unit-files -a | grep telnet telnet@.service static telnet.socket disabled # systemctl start telnet@.service Failed to start telnet@.service: Unit name telnet@.service is missing the instance name. See system logs and 'systemctl status telnet@.service' for details # systemctl is-active telnet.socket unknown # systemctl start telnet.socket # systemctl status telnet.socket ● telnet.socket - Telnet Server Activation Socket Loaded: loaded (/usr/lib/systemd/system/telnet.socket; disabled; vendor preset: disabled) Active: active (listening) since Fri 2020-02-28 10:21:26 CST; 8min ago Docs: man:telnetd(8) Listen: [::]:23 (Stream) Accepted: 0; Connected: 0 # lsof -i :23 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME systemd 1 root 24u IPv6 64689 0t0 TCP *:telnet (LISTEN)
-
Try to restart: try restart pattern...: if the service is not running, it does not do so.
centos7:
# systemctl try-restart httpd
CentOS 5, 6: this function is not supported in general service scripts, but it is also supported
# service NAME condrestart
-
Reload configuration file or try to restart: reload or try restart pattern
# systemctl reload-or-try-restart httpd
-
Reload configuration file or restart: reload or restart pattern
# systemctl reload-or-restart httpd
-
To see if the service is active:
# systemctl is-active httpd active # systemctl stop httpd # systemctl is-active httpd unknown
-
List all active service s:
# systemctl list-units -t service UNIT LOAD ACTIVE SUB DESCRIPTION abrt-ccpp.service loaded active exited Install ABRT coredump hook abrt-oops.service loaded active running ABRT kernel log watcher
-
List all identified service s:
centos5,6: chkconfig --list
# systemctl list-units -t service -a
-
Common options: - t and - a
-t: Specify the type of unit file
-a: View all
-
It is forbidden to manually start or automatically start a service:
centos5,6: chkconfig mySer.sh on
-
Cancel forbidding manual startup and automatic startup of a service
The purpose is to prevent misoperation. The service that should not be started is started
The principle is to create a symbolic link, so you can also manually create a symbolic link to achieve the same function.
# systemctl mask httpd.service Created symlink from /etc/systemd/system/httpd.service to /dev/null. # ll /etc/systemd/system/httpd.service lrwxrwxrwx. 1 root root 9 Feb 27 11:40 /etc/systemd/system/httpd.service -> /dev/null # systemctl start httpd.service Failed to start httpd.service: Unit is masked. # systemctl status httpd.service ● httpd.service Loaded: masked (/dev/null; bad) Active: inactive (dead) # systemctl unmask httpd.service Removed symlink /etc/systemd/system/httpd.service. # ll /etc/systemd/system/httpd.service ls: cannot access /etc/systemd/system/httpd.service: No such file or directory
To set automatic start-up after power on:
-
Check whether a service is started automatically: systemctl is enabled httpd.service
centos5,6: chkconfig --list NAME
The principle is to create a symbolic link, so you can also manually create a symbolic link to achieve the same function.
# ll /etc/systemd/system/multi-user.target.wants/ | grep httpd # systemctl is-enabled httpd.service disabled # systemctl enable httpd.service Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service. # ll /etc/systemd/system/multi-user.target.wants/ | grep httpd lrwxrwxrwx. 1 root root 37 Feb 27 11:54 httpd.service -> /usr/lib/systemd/system/httpd.service # systemctl is-enabled httpd.service enabled
The service under the directory / etc/systemd/system/multi-user.target.wants / is started automatically after power on.
-
To see which services a service depends on:
# systemctl list-dependencies httpd httpd.service ● ├─-.mount ● ├─system.slice ● └─basic.target ● ├─microcode.service ● ├─rhel-dmesg.service
-
Kill the service, followed by the name of the process, not pid. Similarly, kill command.
# systemctl kill httpd
Various operations on the operation level: target units
unit profile:
# ls /usr/lib/systemd/system/*.target lrwxrwxrwx. 1 root root 15 Nov 29 16:46 /usr/lib/systemd/system/runlevel0.target -> poweroff.target lrwxrwxrwx. 1 root root 13 Nov 29 16:46 /usr/lib/systemd/system/runlevel1.target -> rescue.target lrwxrwxrwx. 1 root root 17 Nov 29 16:46 /usr/lib/systemd/system/runlevel2.target -> multi-user.target lrwxrwxrwx. 1 root root 17 Nov 29 16:46 /usr/lib/systemd/system/runlevel3.target -> multi-user.target lrwxrwxrwx. 1 root root 17 Nov 29 16:46 /usr/lib/systemd/system/runlevel4.target -> multi-user.target lrwxrwxrwx. 1 root root 16 Nov 29 16:46 /usr/lib/systemd/system/runlevel5.target -> graphical.target lrwxrwxrwx. 1 root root 13 Nov 29 16:46 /usr/lib/systemd/system/runlevel6.target -> reboot.target # systemctl list-unit-files -t target -a UNIT FILE STATE anaconda.target static basic.target static bluetooth.target static cryptsetup-pre.target static cryptsetup.target static ctrl-alt-del.target disabled default.target enabled ...
The operation levels in CentOS 5 and 6 are symbolic links, pointing to other unit s
Operation level in CentOS 5 and 6 | unit file in centos7 |
---|---|
0 | runlevel0.target ->poweroff.target |
1 | runlevel1.target ->rescue.target |
2 | runlevel2.target ->multi-user.target |
3 | runlevel3.target ->multi-user.target |
4 | runlevel4.target ->multi-user.target |
5 | runlevel5.target ->graphical.target |
6 | runlevel6.target ->reboot.target |
The operation levels in CentOS 5 and 6 are different for each management. Targets in CentOS 7 can use other target s
# systemctl list-dependencies graphical.target graphical.target ● ├─accounts-daemon.service ● ├─gdm.service ● ├─initial-setup-reconfiguration.service ● ├─network.service ● ├─rtkit-daemon.service ● ├─systemd-readahead-collect.service ● ├─systemd-readahead-replay.service ● ├─systemd-update-utmp-runlevel.service ● ├─udisks2.service ● └─multi-user.target
It is found that graphical.target relies on multi-user.target, that is, graphical.target reuses multi-user.target.
CentOS 5 and 6 are troublesome to reuse. They need to manually copy scripts to other running level directories.
-
Switch operation level:
centos5,6: init N
centos7: systemctl isolate NAME.target
The who -r and runlevel and init N commands are still available in centos7.
# who -r run-level 5 2020-02-28 09:07 # runlevel N 5 # systemctl isolate multi-user.target # who -r run-level 3 2020-02-28 11:18 last=5 # runlevel 5 3 # systemctl isolate graphical.target # runlevel 3 5 # who -r run-level 5 2020-02-28 11:18 last=3
Prerequisite for switching: the value of allowaisolate in the NAME.target file must be yes.
[Unit] Description=Multi-User System Documentation=man:systemd.special(7) Requires=basic.target Conflicts=rescue.service rescue.target After=basic.target rescue.service rescue.target AllowIsolate=yes
After modifying the unit file, you must use the systemctl daemon reload command to take effect.
Modified into AllowIsolate=no # emacs /usr/lib/systemd/system/multi-user.target # tail -1 /usr/lib/systemd/system/multi-user.target AllowIsolate=no //Since the systemctl daemon reload command is not running, running systemctl isolate multi-user.target has no effect, but you will be prompted to run systemctl daemon reload command # systemctl isolate multi-user.target Warning: multi-user.target changed on disk. Run 'systemctl daemon-reload' to reload units. //Make changes effective # systemctl daemon-reload //The change has taken effect, so the switch is not successful. The reason is allowaisolate = no # systemctl isolate multi-user.target Failed to start multi-user.target: Operation refused, unit may not be isolated. See system logs and 'systemctl status multi-user.target' for details.
-
See what target to use when turning on
# systemctl get-default graphical.target
centos5,6 is to view / etc/inittab. There are many lines in centos5, and centos6 has only one line id:3:initdefault:
-
Set what target to use when power on
Principle: create symbolic links to target to use
/etc/systemd/system/default.target symbolic link, which points to, use which. So every time you delete it, you create a new one.
# systemctl set-default multi-user.target Removed symlink /etc/systemd/system/default.target. Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target. # systemctl set-default graphical.target Removed symlink /etc/systemd/system/default.target. Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.
-
Switch to rescue mode: systemctl rescue
When CentOS 7 is started, it can enter rescue mode. Load device drivers.
CentOS 5 and 6 must use CD to enter rescue mode.
-
Switch to the lower rescue mode: systemctl emergency
No device drivers are loaded. This mode is not allowed to be used for repair if the device cannot be turned on due to bad driver.
-
Other common commands:
Principle init, poweroff, halt and reboot in CentOS 6 are symbolic links of systemctl
# ll /usr/sbin/reboot lrwxrwxrwx. 1 root root 16 Nov 29 16:46 /usr/sbin/reboot -> ../bin/systemctl # ll /usr/sbin/poweroff lrwxrwxrwx. 1 root root 16 Nov 29 16:46 /usr/sbin/poweroff -> ../bin/systemctl
- Shutdown: systemctl halt, systemctl poweroff
- Restart: systemctl reboot
- Suspend: systemctl suspend
- Snapshot: systemctl hibernate
- Snapshot and suspend: systemctl hybrid sleep
Temporarily modify the kernel startup options through kernel parameters
When starting up, press e on the selected kernel to enter grub2 configuration, and then at the end of line 16 of Linux, add the kernel startup selection:
- systemd.unit=rescue.target (enter rescue mode directly)
- systemd.unit=emergency.target (enter rescue mode directly)
- systemd.unit=multi-user.target (directly into multi-user text mode)
Write unit file
/The contents of usr/lib/systemd/system/httpd.service are as follows:
# cat /usr/lib/systemd/system/httpd.service [Unit] Description=The Apache HTTP Server After=network.target remote-fs.target nss-lookup.target Documentation=man:httpd(8) Documentation=man:apachectl(8) [Service] Type=notify EnvironmentFile=/etc/sysconfig/httpd ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND ExecReload=/usr/sbin/httpd $OPTIONS -k graceful ExecStop=/bin/kill -WINCH ${MAINPID} # We want systemd to give httpd some time to finish gracefully, but still want # it to kill httpd after TimeoutStopSec if something went wrong during the # graceful stop. Normally, Systemd sends SIGTERM signal right after the # ExecStop, which would kill httpd. We are sending useless SIGCONT here to give # httpd time to finish. KillSignal=SIGCONT PrivateTmp=true [Install] WantedBy=multi-user.target
-
unit file usually consists of three parts
- [Unit]: general options.
- Description: provide description information (information displayed in systemctl status)
- After: after this service starts, this service starts
- [Name]: a special option related to a specific type. If it is. Service, this is service
- [Install]: define some options used by systemctl enable and systemctl disable commands when services are enabled or disabled.
- [Unit]: general options.
Lines that begin with are comments
-
Correlation Boolean
1. yes, on and true are all enabled
2, 0, off, no, false are all off
The default unit of time is seconds, milliseconds: ms, minutes: m, etc. must be displayed plus the unit
Refer to Ruan Yifeng's log for details: http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html