就像大家知道的那样,systemd是Fedora自Fedora14版以来的新的初始化系统,并且它也正在试图被一系列的其他Linux发行版所认可,就像OpenSUSE那样。对于系统管理员来说,systemd提供了一系列的新功能和改变,并大体上增强了管理的方法。在每一帖中我将尝试阐述systemd的一个新功能。它们中的许多短小而精悍,因此应该能提起一大批读者的兴趣。(呵呵)。但是,我们将引导你一点一点深入systemd为你提供的创新功能。
验证启动
从传统意义上说,当启动一个Linux系统时,你将看到许多信息飘过你的屏幕。在我们致力于加速启动并并行化启动进程的现在,这些信息出现的速度将会越来越快,并且越来越难以被阅读(因为你跟不上,而且我们还有图形化启动技术就像Plymouth)。尽管如此,启动屏幕的信息曾经是现在也是非常有用的,因为它显示了开机启动的服务是否成功的启动(通过那些绿色的[OK]和红色的[FAILED])。为了改进启动的快速化和并行化,同时在运行期间更容易的被获得,我们在systemd中添加了一项功能,使能跟踪并记住每一项服务的状态:是否它成功启动,是否它以非0错误码退出,是否它超时,是否它不正常的被终止(段错误或类似的)。这项功能同时记录启动与运行这两个时刻。很简单地,使用systemctl命令就可以查询所有服务的状态,包括使用systemd的新式语法的服务和SysV/LSB的老式服务。
[root@lambda] ~# systemctl UNIT LOAD ACTIVE SUB JOB DESCRIPTION dev-hugepages.automount loaded active running Huge Pages File System Automount Point dev-mqueue.automount loaded active running POSIX Message Queue File System Automount Point proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Formats File System Automount Point sys-kernel-debug.automount loaded active waiting Debug File System Automount Point sys-kernel-security.automount loaded active waiting Security File System Automount Point sys-devices-pc...0000:02:00.0-net-eth0.device loaded active plugged 82573L Gigabit Ethernet Controller[...]sys-devices-virtual-tty-tty9.device loaded active plugged /sys/devices/virtual/tty/tty9 -.mount loaded active mounted / boot.mount loaded active mounted /boot dev-hugepages.mount loaded active mounted Huge Pages File System dev-mqueue.mount loaded active mounted POSIX Message Queue File System home.mount loaded active mounted /home proc-sys-fs-binfmt_misc.mount loaded active mounted Arbitrary Executable File Formats File System abrtd.service loaded active running ABRT Automated Bug Reporting Tool accounts-daemon.service loaded active running Accounts Service acpid.service loaded active running ACPI Event Daemon atd.service loaded active running Execution Queue Daemon auditd.service loaded active running Security Auditing Service avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack bluetooth.service loaded active running Bluetooth Manager console-kit-daemon.service loaded active running Console Manager cpuspeed.service loaded active exited LSB: processor frequency scaling support crond.service loaded active running Command Scheduler cups.service loaded active running CUPS Printing Service dbus.service loaded active running D-Bus System Message Bus [email protected] loaded active running Getty on tty2 [email protected] loaded active running Getty on tty3 [email protected] loaded active running Getty on tty4 [email protected] loaded active running Getty on tty5 [email protected] loaded active running Getty on tty6 haldaemon.service loaded active running Hardware Manager [email protected] loaded active running sda shock protection daemon irqbalance.service loaded active running LSB: start and stop irqbalance daemon iscsi.service loaded active exited LSB: Starts and stops login and scanning of iSCSI devices. iscsid.service loaded active exited LSB: Starts and stops login iSCSI daemon. livesys-late.service loaded active exited LSB: Late init script for live image. livesys.service loaded active exited LSB: Init script for live image. lvm2-monitor.service loaded active exited LSB: Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or progress polling mdmonitor.service loaded active running LSB: Start and stop the MD software RAID monitor modem-manager.service loaded active running Modem Manager netfs.service loaded active exited LSB: Mount and unmount network filesystems. NetworkManager.service loaded active running Network Manager ntpd.service loaded maintenance maintenance Network Time Service polkitd.service loaded active running Policy Manager prefdm.service loaded active running Display Manager rc-local.service loaded active exited /etc/rc.local Compatibility rpcbind.service loaded active running RPC Portmapper Service rsyslog.service loaded active running System Logging Service rtkit-daemon.service loaded active running RealtimeKit Scheduling Policy Service sendmail.service loaded active running LSB: start and stop sendmail [email protected]:22-172.31.0.4:36368.service loaded active running SSH Per-Connection Server sysinit.service loaded active running System Initialization systemd-logger.service loaded active running systemd Logging Daemon udev-post.service loaded active exited LSB: Moves the generated persistent udev rules to /etc/udev/rules.d udisks.service loaded active running Disk Manager upowerd.service loaded active running Power Manager wpa_supplicant.service loaded active running Wi-Fi Security Service avahi-daemon.socket loaded active listening Avahi mDNS/DNS-SD Stack Activation Socket cups.socket loaded active listening CUPS Printing Service Sockets dbus.socket loaded active running dbus.socket rpcbind.socket loaded active listening RPC Portmapper Socket sshd.socket loaded active listening sshd.socket systemd-initctl.socket loaded active listening systemd /dev/initctl Compatibility Socket systemd-logger.socket loaded active running systemd Logging Socket systemd-shutdownd.socket loaded active listening systemd Delayed Shutdown Socket dev-disk-by\x1...x1db22a\x1d870f1adf2732.swap loaded active active /dev/disk/by-uuid/fd626ef7-34a4-4958-b22a-870f1adf2732 basic.target loaded active active Basic System bluetooth.target loaded active active Bluetooth dbus.target loaded active active D-Bus getty.target loaded active active Login Prompts graphical.target loaded active active Graphical Interface local-fs.target loaded active active Local File Systems multi-user.target loaded active active Multi-User network.target loaded active active Network remote-fs.target loaded active active Remote File Systems sockets.target loaded active active Sockets swap.target loaded active active Swap sysinit.target loaded active active System Initialization LOAD = Reflects whether the unit definition was properly loaded. ACTIVE = The high-level unit activation state, i.e. generalization of SUB. SUB = The low-level unit activation state, values depend on unit type. JOB = Pending job for the unit. 221 units listed. Pass --all to see inactive units, too. [root@lambda] ~#
(我已稍稍缩短输出,并删除与此贴无关的相关行)
看ACTIVE列,它输出了高端的服务状态(事实上,systemd维护的叫单元,它可以代表不止一种服务,但此事请听下回分解),分为active(运行),inactive(未运行),或其他状态。如果你仔细观察,你会发现列表中其中有一行被标记为maintenance并红色高亮。这提醒你该服务未能启动或者它报告了一个问题。在此例中为ntpd(网络时间服务)。现在让我们找出ntpd发生了什么,使用“systemctl status”命令。
[root@lambda] ~# systemctl status ntpd.service ntpd.service - Network Time Service Loaded: loaded (/etc/systemd/system/ntpd.service) Active: maintenance Main: 953 (code=exited, status=255) CGroup: name=systemd:/systemd-1/ntpd.service [root@lambda] ~#
它告诉我们ntpd在运行时被终止(PID=953),并准确告诉我们错误的原因:进程以错误码255而终止。
在后来的的systemd中,我们把它与ABRT相hook。然后,“systemctl status”如果输出一个服务崩溃了,它会直接显示ABRT的dump。
总结:使用“systemctl”和“systemctl status”命令作为一种更现代更完整的方式代替传统的SysV时代的启动信息。“systemctl status”不仅捕捉更详细的错误原因,同时显示运行时错误和启动错误。