╭─root@blog /home/ubuntu ╰─# dmesg [ 0.000000] Linux version 5.0.0-1031-gcp (buildd@lcy01-amd64-020) (gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)) #32-Ubuntu SMP Tue Feb 11 03:55:48 UTC 2020 (Ubuntu 5.0.0-1031.32-gcp 5.0.21) [ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-5.0.0-1031-gcp root=PARTUUID=9b22aacc-c8b9-497a-9583-a20c1be968c4 ro scsi_mod.use_blk_mq=Y console=ttyS0 [ 0.000000] KERNEL supported cpus: [ 0.000000] Intel GenuineIntel [ 0.000000] AMD AuthenticAMD [ 0.000000] Hygon HygonGenuine [ 0.000000] Centaur CentaurHauls …………………………………………………………………………………………………… [ 2.486896] Write protecting the kernel read-only data: 22528k [ 2.489395] Freeing unused kernel image memory: 2016K [ 2.490937] Freeing unused kernel image memory: 1708K [ 2.500867] x86/mm: Checked W+X mappings: passed, no W+X pages found. [ 2.503126] x86/mm: Checking user space page tables [ 2.513767] x86/mm: Checked W+X mappings: passed, no W+X pages found. [ 2.516258] Run /sbin/init as init process [ 3.992535] systemd[1]: systemd 237 running in system mode. (+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN2 +IDN -PCRE2 default-hierarchy=hybrid) [ 4.002297] systemd[1]: Detected virtualization kvm. [ 4.004593] systemd[1]: Detected architecture x86-64. [ 4.031531] systemd[1]: Set hostname to <blog>. [ 5.343604] systemd[1]: Reached target Swap. [ 5.355156] systemd[1]: Started Dispatch Password Requests to Console Directory Watch. [ 5.367360] systemd[1]: Created slice User and Session Slice. [ 5.379321] systemd[1]: Set up automount Arbitrary Executable File Formats File System Automount Point. [ 5.395189] systemd[1]: Started Forward Password Requests to Wall Directory Watch. [ 5.411078] systemd[1]: Reached target Local Encrypted Volumes. [ 5.644759] EXT4-fs (sda1): re-mounted. Opts: (null) [ 11.663490] bpfilter: Loaded bpfilter_umh pid 456
init 初始化阶段
一旦内核自解压完成,启动并初始化后,内核启动第一个用户空间应用程序,即 systemd 进程(其是老式 System V 系统的 init 程序的替代品),并转移控制权到 systemd。这是调用的第一个使用标准 C 库编译的程序,在此进程之前,还没有执行任何标准的 C 应用程序。至此整个系统引导过程的结束,kernel和 systemd 处于运行状态,接下来就由 systemd 来启动各项程序。
[ 2.516258] Run /sbin/init as init process [ 3.992535] systemd[1]: systemd 237 running in system mode. (+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN2 +IDN -PCRE2 default-hierarchy=hybrid)
主角 systemd
为什么文章题名为 Linux 的小伙伴 systemd 呐,其实广义上来讲 Linux 是众多 Linux 系统发行版的集合,但严格来讲 Linux 仅仅是一个 OS 的 kernel 而已,仅仅有一个内核是无法组成一个系统的,所以 Linux kernel 还需要他的几个兄弟比如 GNU 、 systemd 、X Window 、GNOME 、 KDE 、Xfce 等等其他用户层面的程序来构建出一套完整的操作系统出来。
还有如果你要是当着自由软件基金会主席 RMS 理查德·斯托曼 说 Linux 的话,他会给你发火哦,你要说 GNU/Linux 才行😂。
将 Debian 哲学与方法论,GNU 工具集、Linux 内核,以及其他重要的自由软件结合在一起所构成的独特的软件发行版称为 Debian GNU/Linux。当你将 Debian 的安装镜像刻录到 U 盘之后显示的名称就是 Debian GNU/Linux 。关于 GNU 和 Linux 的故事可以去看一哈 操作系统革命 纪录片,以及 RMS 的自传《若为自由故:自由软件之父理查德·斯托曼传》以及 Linus 的自传《只是为了好玩儿》。
systemd 简介
讲完了 Linux 系统启动的流程,接下来就到了本文的主角 systemd 上场啦。剽窃一段 archlinux.org 上的文档来介绍一下 systemd 。(archlinux 的文档是做的最好的哦!
systemd 是一个 Linux 系统基础组件的集合,提供了一个系统和服务管理器,运行为 PID 1 并负责启动其它程序。功能包括:
systemd is a suite of basic building blocks for a Linux system. It provides a system and service manager that runs as PID 1 and starts the rest of the system.
systemd provides aggressive parallelization capabilities, uses socket and D-Bus activation for starting services, offers on-demand starting of daemons, keeps track of processes using Linux control groups, maintains mount and automount points, and implements an elaborate transactional dependency-based service control logic. systemd supports SysV and LSB init scripts and works as a replacement for sysvinit.
Other parts include a logging daemon, utilities to control basic system configuration like the hostname, date, locale, maintain a list of logged-in users and running containers and virtual machines, system accounts, runtime directories and settings, and daemons to manage simple network configuration, network time synchronization, log forwarding, and name resolution.
╭─root@sg-02 ~ ╰─# systemctl cat nginx 1 ↵ # /lib/systemd/system/nginx.service # Stop dance for nginx # ======================= # # ExecStop sends SIGSTOP (graceful stop) to the nginx process. # If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control # and sends SIGTERM (fast shutdown) to the main process. # After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends # SIGKILL to all the remaining processes in the process group (KillMode=mixed). # # nginx signals reference doc: # http://nginx.org/en/docs/control.html # [Unit] Description=A high performance web server and a reverse proxy server Documentation=man:nginx(8) After=network.target
╭─root@ ~ ╰─# cat /usr/lib/systemd/system/graphical.target # This file is part of systemd. # # systemd is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version.
╭─root@docker-230 ~ ╰─# cat /usr/lib/systemd/system/multi-user.target # This file is part of systemd. # # systemd is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version.
╭─root@docker-230 ~ ╰─# cat /usr/lib/systemd/system/sysinit.target # This file is part of systemd. # # systemd is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version.
╭─root@blog /home/ubuntu ╰─# systemctl status webps 130 ↵ ● webps.service - WebP Server Loaded: loaded (/opt/webps/webps.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2020-03-18 13:58:00 UTC; 22h ago Docs: https://github.com/n0vad3v/webp_server_go Main PID: 5732 (webp-server) Tasks: 52 (limit: 684) Memory: 185.2M CPU: 1min 30.092s CGroup: /system.slice/webps.service └─5732 /opt/webps/webp-server --config /opt/webps/config.json
Mar 19 05:23:20 blog webp-server[5732]: Save to exhaushttps://p.k8s.li/20200109192923891.png.1584538399.webp ok
列出所有单元:
systemctl list-unit-files
1 2 3 4 5 6
╭─root@docker-230 ~ ╰─# systemctl list-unit-files UNIT FILE STATE proc-sys-fs-binfmt_misc.automount static dev-hugepages.mount static dev-mqueue.mount static
列出激活的单元
systemctl list-units 等同于 systemctl
1 2 3 4 5 6
╭─root@docker-230 ~ ╰─# systemctl list-units 127 ↵ UNIT LOAD ACTIVE SUB DESCRIPTION proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Formats File System sys-devices-pci0000:00-0000:00:15.0-0000:03:00.0-host2-target2:0:0-2:0:0:0-block-sda-sda1.device loaded active plu sys-devices-pci0000:00-0000:00:15.0-0000:03:00.0-host2-target2:0:0-2:0:0:0-block-sda-sda2.device loaded active plu
╭─root@blog /home/ubuntu ╰─# systemctl status nginx -H sg2 ● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2020-02-17 08:41:11 UTC; 1 months 0 days ago Docs: man:nginx(8) Main PID: 3305 Tasks: 2 (limit: 1152) CGroup: /system.slice/nginx.service ├─ 3305 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; └─17700 nginx: worker process