Deepin system startup process and introduction of key components [1]
Runlevel
Which services are started when the system starts is determined by the run level. The systemd service will specify which target to run in. These targets usually correspond to the target of the run level. If NetworkManager is started at the multi-user.target level, the runlevels defined by systemd are as follows:
| Runlevel | Target | Description | | | 0 | poweroff.target | interrupt system (halt) | | 1 | rescue.taregt | single-user mode | | 2,3,4 | multi-user.target | multi-user, no GUI . User can log in via terminal or network | | 5 | graphical.target | Multi-user, graphical interface. Inherit level 3 service and start GUI service | | 6 | reboot.target | reboot |
The current runlevel of the system can be obtained or set by the following commands:
Get current runlevel
systemctl get-default
Modify runlevel
systemctl set-default , you can also add systemd.unit= to the kernel boot parameters in the grub interface for temporary modification.
In addition, the current running target can also be changed by systemctl isolate , similar to telinit 3 or telinit 5
Introduction to key system components
System components are divided into system-level components and user-level components according to the user at startup. System-level components have nothing to do with users and are generally started before the user logs in, while user-level components must be started after the user logs in.
grub
GRUB , GRand Unified Bootloader, is a multi-boot loader, inherited from the PUPA project. The project is working on developing a new boot loader to replace what is now called GRUB Legacy. The latter has been difficult to maintain, while GRUB has rewritten the code from scratch to achieve modularity and enhance portability [1]. GRUB today is also known as GRUB 2, while GRUB Legacy refers to versions 0.9x.
Install
When installing, you need to distinguish between MBR and UEFI to install separately, as follows:
MBR
In general, there is 31KB of free space between the 512-byte end of the MBR and the first partition if a DOS-compatible partition alignment is used. However, to provide enough space to embed GRUB’s core.img file, it is recommended to set this space to 1 to 2 MB. It is recommended to use partitioning software that supports 1 MB partition alignment, as this can also meet the needs of non-512-byte sector disk partitions.
Use the command to install: grub-install — target=i386-pc /dev/sdX , /sdX refers to the hard disk to be installed
UEFI
First you need to install grub and efibootmgr , then install by command: grub-install — target=x86_64-efi — efi-directory=/boot/efi — bootloader-id=BOOT
Generate bootstrap configuration
Generate the boot configuration with the command: grub-mkconfig -o /boot/grub/grub.cfg
When generating a boot configuration, you can specify some variables by modifying /etc/default/grub, such as setting kernel boot items, Grub theme, etc.
/etc/grub.d defines the functions that require additional processing to generate the boot configuration, which are implemented by shell scripts.
debugging
Add the following content to the boot configuration /boot/grub/grub.cfg to enable the debug mode. Note that the system will start very slowly after enabling it.
set pager=1 set debug=all
systemd
systemd is a collection of basic components of the Linux system that provides a system and service manager, runs as PID 1 and is responsible for starting other programs. Features include: support for parallel tasks; use socket and D-Bus bus activation services at the same time; start daemons on demand; use Linux cgroups to monitor processes; support snapshots and system recovery; maintain mount points and auto-mount Loading point; precise control between services based on dependencies. systemd supports SysV and LSB init scripts and can replace sysvinit.
In addition, functions also include logging processes, controlling basic system configuration, maintaining a list of logged-in users and system accounts, runtime directories and settings, running containers and virtual machines, and simply managing network configuration, network time synchronization, and log forwarding and name resolution etc.
The configuration files for various functions provided by systemd are in the /etc/systemd/ directory and can be modified as needed.
Service management
systemd provides the systemctl command to manage services. The specific usage can be viewed through man systemctl. The commonly used commands are as follows (take sshd as an example):
List loaded services
systemctl list-units
start the service
systemctl start sshd
Out of service
systemctl stop sshd
restart the service
systemctl restart sshd
View service status
systemctl status sshd
Start the startup service
systemctl enable sshd
Disable service startup
systemctl disable sshd
Block service
Will link the service to /dev/null , which is used to completely block the service.
systemctl mask sshd
Log management
The journalctl command is a log management tool provided by systemd, which is usually used for system logs. The common methods are as follows:
View startup log
journalctl -b 0 View the user log of this system startup
journalctl -b -1 View the user log of the last system startup
sudo journalctl -b 0 View the system log of this startup
sudo journalctl -b -1 to view the system log of the last boot
View logs after a specified time
journalctl — since=”15:00" View user logs after 15:00
sudo journalctl — since=”15:00" View system logs after 15:00
View the logs of the specified service
journalctl -u View all logs of the specified service
journalctl -b 0 -u View the log of the specified service after this system startup
Session management
systemd-logind provides session and user management functions for use by other processes through the dbus interface, and also provides the loginctl command for use on the command line.
Time management
systemd-timesyncd can manage time synchronization, and timedatectl can set the time. See also man timedatectl for usage.
Device management
udev is a device manager on linux that loads kernel modules in parallel, with potential performance advantages over previous solutions. But there is also an inherent disadvantage: the order in which modules are loaded each time is not guaranteed, and if the machine has multiple block devices, their device nodes may change randomly. For example, if there are two hard disks, /dev/sda may randomly become /dev/sdb .
udev configures devices through rule files. Rules are written as an administrator and saved in the /etc/udev/rules.d/ directory. The file name must end with .rules. The rules files provided by the various packages are located in /lib/udev/rules.d/ . If there are files with the same name in the /usr/lib and /etc directories, the file in /etc takes precedence.
The udevadm command is also a command that needs to be understood. It can query device information, monitor device time, control rule files, etc. The specific usage can still be viewed through man udevadm.
Power management
systemd provides the following power management functions: systemctl reboot [reboot] systemctl poweroff [shutdown] systemctl suspend [standby, hibernate to memory] systemctl hibernate [hibernate, hibernate to hard disk] systemctl hybrid-sleep [Hybrid sleep mode, hibernate to hard disk and standby at the same time]
Final summary
Today’s sharing ends here. I hope this article can help you improve your technical level. I will continue to explain the remaining content in the next article.
Remember to follow me.