Difference between revisions of "Systemd (简体中文)"

From ArchWiki
Jump to: navigation, search
m (时区: rm space)
m (主机名)
Line 56: Line 56:
  
 
=== 主机名 ===
 
=== 主机名 ===
The hostname is configured in {{ic|/etc/hostname}}. The file should not contain the system's domain, if any. To set the hostname, do:
+
主机名通过文件{{ic|/etc/hostname}}进行配置。该文件不应包含系统的域名(如果有)。使用如下命令设置主机名:
 
  # hostnamectl set-hostname '''myhostname'''
 
  # hostnamectl set-hostname '''myhostname'''
  
See {{ic|man 5 hostname}} and {{ic|man 1 hostnamectl}} for details.
+
更多细节请查看{{ic|man 5 hostname}}{{ic|man 1 hostnamectl}}
  
 
示例文件:
 
示例文件:

Revision as of 15:11, 22 November 2012

Summary help replacing me
本文介绍 Systemd 的安装和配置。
相关页面
Systemd/Services
systemd/User
Systemd FAQ
Init to systemd cheatsheet
udev (简体中文)
翻译状态: 本文是英文页面 Systemd翻译,最后翻译时间:2012-11-13,点击这里可以查看翻译后英文页面的改动。

摘自项目主页:

systemd 是 Linux 下的一款系统和服务管理器,兼容 SysV 和 LSB 的启动脚本。systemd的特性有:支持并行化任务;同时采用 socket 式与 D-Bus 总线式激活服务;按需启动守护进程(daemon);利用 Linux 的 cgroups 监视进程;支持快照和系统恢复;维护挂载点;各服务间基于依赖关系进行精密控制。systemd 完全可以替代 Arch 默认的sysvinit启动系统。

提示: 这个帖子详细解释了 Arch 向 systemd 迁移的原因。

另见维基百科上的介绍

迁移前需考虑

  • 最好先参照 rc.conf (简体中文) 按照最新格式配置 initscripts。完成后,基本上就没什么需要配置的了。
  • 阅读该站,了解 systemd。
  • systemd 自己有一套日志(journal)系统,用于代替 syslog。两者也可以共存,参见后面的日志部分。
  • 虽然 systemd 可以替换 cronacpidxinetd 等的部分功能。至少目前还可以继续使用这些服务,无需立即切换。
  • 交互 initscripts 在 systemd 中无法工作。例如 netcfg-menu 无法在启动时显示。

安装

注意: 2012-10-13开始,安装程序已经默认安装systemdsystemd-sysvcompat

本部分帮助还在使用sysvinitinitscripts 的用户迁移到systemd.

  1. 官方软件仓库安装 systemd 并添加内核参数init=/usr/lib/systemd/systemd
  2. systemctl enable daemonname 启用 /etc/rc.conf 中的守护进程。从 /etc/rc.conf 到 systemd 服务的转换可以参考:守护进程列表Services
  3. 重启系统,执行命令$ cat /proc/1/comm,如果返回systemd,表示 systemd 已经正常启动。
  4. 删除 initscriptssysvinit,并安装systemd-sysvcompat.
  5. 最后,删除init=/usr/lib/systemd/systemd内核参数,现在已经不需要它了。

附加信息

  • 如果内核参数中有 quiet,可以先删除它以协助调试。
  • 使用 Systemd 的时候无需将用户加入optical, audio, scanner群组。加入这些组反而会有问题,例如audio组会导致程序阻塞软件混声。每个 PAM 登录都提供了一个登录会话,本地会话都会通过POSIX ACLs赋予声音设备访问权限和udisks移动设备挂载和卸载权限.
注意: Systemd-logind 替代了ConsoleKit,ConsoleKit 已经从源中删除,系统已 Systemd 启动才能完全正常工作,参阅 新闻通知

原生 systemd 配置文件

注意: 可能需要手动创建文件,将权限设置为 644,属于 root:root.

如果下列文件不存在,systemd 会从 /etc/rc.conf 读取相关配置。这只是一个临时解决方案,建议所有系统都使用 systemd 的配置文件,这也是目前 initscripts 推荐的配置方式。

小贴士: 某些文件可能需要手动创建。

主机名

主机名通过文件/etc/hostname进行配置。该文件不应包含系统的域名(如果有)。使用如下命令设置主机名:

# hostnamectl set-hostname myhostname

更多细节请查看man 5 hostnameman 1 hostnamectl

示例文件:

/etc/hostname
myhostname

本地化设置

The default system locale is configured in /etc/locale.conf. To set the default locale, do:

# localectl set-locale LANG="de_DE.utf8"
Note: Before you set the default locale, you first need to enable locales available to the system by uncommenting them in /etc/locale.gen and then executing locale-gen as root. The locale set via localectl must be one of the uncommented locales in /etc/locale.gen.

See man 1 localectl and man 5 locale.conf for details.

详情请参阅Locale

示例文件:

/etc/locale.conf
LANG=en_US.utf8

虚拟控制台

/etc/vconsole.conf 文件用来配置虚拟控制台,包括键盘映射和控制台字体:

/etc/vconsole.conf
KEYMAP=us
FONT=lat9w-16
FONT_MAP=8859-1_to_uni
Note: systemd-194 uses the kernel font and keymap by default. It is not necessary to have empty KEYMAP= and FONT= settings anymore.

Another way to set the keyboard mapping (keymap) is doing:

# localectl set-keymap de

This has the advantage that it will also set the same keymap for use in X11. See man 1 localectl and man 5 vconsole.conf for details.

详情参阅 控制台字体Keymap.

时区

The time zone is configured by creating an appropriate /etc/localtime relative symlink, pointing to a zoneinfo file under /usr/share/zoneinfo/. To do this automatically:

# timedatectl set-timezone Asia/Shanghai

See man 1 timedatectl, man 5 localtime, and man 7 archlinux for more details.

手动创建链接也行:

# ln -sf ../usr/share/zoneinfo/Asia/Shanghai /etc/localtime

硬件时钟

systemd 默认识别硬件时钟为协调世界时(UTC)。

注意: 推荐使用NTP服务来确保硬件时钟同步。

Hardware clock in localtime

If you want to change the hardware clock to use local time (STRONGLY DISCOURAGED) do:

# timedatectl set-local-rtc true

If you want to revert to the hardware clock being in UTC, do:

# timedatectl set-local-rtc false

如果设置成本地时间,处理夏令时有些麻烦,如果夏令时调整发生在关机时,下次启动时时间会出现问题(更多信息)。最新的内核直接从实时时钟芯片(RTC)读取时间,不使用 hwclock,内核把从 RTC 读取的时间当作 UTC 处理。所以如果硬件时间是地方时,系统启动一开始识别的时间是错误的,之后很快会进行矫正。这可能导致一些问题(尤其是时间倒退时)。

如果同时安装了 Windows 操作系统(默认使用地方时),那么一般 RTC 会被设置为地方时。Windows 其实也能处理 UTC,在注册表中设置下列DWORD键值为1即可:

HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation\RealTimeIsUniversal

详情参阅Time.

Kernel modules

Today, all necessary module loading is handled automatically by udev, so that, if you don't want/need to use any out-of-tree kernel modules, there is no need to put modules that should be loaded at boot in any config file. However, there are cases where you might want to load an extra module during the boot process, or blacklist another one for your computer to function properly.

开机加载

systemd 读取 /etc/modules-load.d/ 中的配置加载额外的内核模块。配置文件名称通常为 /etc/modules-load.d/<program>.conf。格式很简单,一行一个要读取的模块名,而空行以及第一个非空格字符为#;的行会被忽略,如:

/etc/modules-load.d/virtio-net.conf
# Load virtio-net.ko at boot
virtio-net

另见man 5 modules-load.d

禁用内核模块

禁用内核模块的方法和 Arch 默认的 initscripts 相同,因为该过程实际由 kmod 处理。参见:Kernel modules (简体中文)#黑名单

也可以使用自动挂载功能,使得网络文件系统仅在访问时挂载。添加 x-systemd.device-timeout=超时时间/etc/fstab 的 <options> 部分即可。

详情参见 man systemd.mount

Filesystem mounts

The default setup will automatically fsck and mount filesystems before starting services that need them to be mounted./etc/fstab 中设定的网络文件系统(如 NFSSamba)无需配置即可正常工作,systemd 将确保网络文件系统在网络链接就绪后挂载。/etc/fstab中设置的本地和远程文件系统应该能够正常工作。

See man 5 systemd.mount for details.

Automount

  • If you have a large /home partition, it might be better to allow services that do not depend on /home to start while /home is being fsck'ed. This can be achieved by adding the following options to the /etc/fstab entry of your /home partition:
noauto,x-systemd.automount

This will fsck and mount /home when it is first accessed, and the kernel will buffer all file access to /home until it is ready.

  • The same applies to remote filesystem mounts. If you want them to be mounted only upon access, you will need to use the noauto,x-systemd.automount parameters. In addition, you can use the x-systemd.device-timeout=# option to specify a timeout in case the network resource is not available.
  • If you have encrypted filesystems with keyfiles, you can also add the noauto parameter to the corresponding entries in /etc/crypttab. Systemd will then not open the encrypted device on boot, but instead wait until it is actually accessed and then automatically open it with the specified keyfile before mounting it. This might save a few seconds on boot if you are using an encrypted RAID device for example, because systemd doesn't have to wait for the device to become available. For example:
/etc/crypttab
data /dev/md0 /root/key noauto

LVM

If you have LVM volumes not activated via the initramfs, enable lvm.service (provided by the lvm2 package):

# systemctl enable lvm

Similarly, if you have LVM on encrypted devices mounted later during boot (e.g. from /etc/crypttab), enable lvm-on-crypt.service (also provided by the lvm2 package):

# systemctl enable lvm-on-crypt

ACPI 电源管理

systemd 能够处理某些电源相关的 ACPI 事件,通过 /etc/systemd/logind.conf 的下列选项配置:

  • HandlePowerKey:按下电源键后的动作
  • HandleSleepKey:按下挂起键后的动作
  • HandleHibernateKey: 按下休眠键后的动作
  • HandleLidSwitch:合上笔记本盖后待机

动作可以是 ignore, poweroff, reboot, halt, suspend, hibernatekexec.

系统默认设置为:

HandlePowerKey=poweroff
HandleSuspendKey=suspend
HandleHibernateKey=hibernate
HandleLidSwitch=suspend

如果不用图形界面,或者使用 i3awesome 这样简单的桌面管理器时,systemd 可以替代 acpid 处理 ACPI 事件。

在当前版本的 systemd 中,这些 Handle 选项将会被应用到整个系统当中,除非它们被别的程序——例如某个桌面环境中的电源管理器——给“阻止”(inhibited)。如果其它的程序没有阻止这些 Handle ,你可能会先被 systemd 挂起你的系统,然后当系统被唤醒之后,电源管理器又会再次将系统挂起。

注意: 目前只有GNOMEKDE 支持 "inhibited" 命令。在其它的桌面管理器同样实现该功能之前,如果你想使用Xfce, acpid 或者其它程序来管理 ACPI 事件,你需要把 Handle 选项设置为 ignore 。包含该功能的新版本还在开发当中。
Note: Systemd can also use other suspend backends (such as Uswsusp or TuxOnIce), in addition to the default kernel backend, in order to put the computer to sleep or hibernate.

休眠接口

运行 systemctl suspendsystemctl hibernate 命令时,Systemd 不使用 pm-utils 进行系统休眠。所以 pm-utils 钩子和用户自定义钩子都不会运行。Systemd 提供了一个类似的机制,在事件发生时,运行 /usr/lib/systemd/system-sleep/ 中的所有可执行文件。运行时会传入两个参数:

  • prepost,标示机器是关闭还是唤醒
  • suspendhibernate,标示挂起还是睡眠

pm-utils 不同,systemd 会并行执行脚本,不是按顺序执行。

脚本输出通过 systemd-suspend.servicesystemd-hibernate.service 记录,可以通过 journal 查看:

# journalctl -b -u systemd-suspend

也可以使用 sleep.target, suspend.targethibernate.target 设置要执行的钩子脚本,详情参阅 man systemd.specialman systemd-sleep

脚本示例:

/usr/lib/systemd/system-sleep/example.sh
#!/bin/sh	
case $1/$2 in
  pre/*)
    echo going to $2 ...
    ;;
  post/*)
    echo waking up from $2 ...
    ;;
esac

See man 7 systemd.special and man 8 systemd-sleep for more details

临时文件

/usr/lib/tmpfiles.d//etc/tmpfiles.d/ 中的文件描述了 systemd-tmpfiles 如何创建、清理、删除临时文件和目录,这些文件和目录通常存放在 /run/tmp 中。配置文件名称为 /etc/tmpfiles.d/<program>.conf。此处的配置能覆盖 /usr/lib/tmpfiles.d/ 目录中的同名配置。

临时文件通常和服务文件同时提供,以生成守护进程需要的文件和目录。例如 Samba 服务需要目录 /var/run/samba 存在并设置正确的权限位,就象这样:

/usr/lib/tmpfiles.d/samba.conf
D /var/run/samba 0755 root root

此外,临时文件还可以用来在开机时向特定文件写入某些内容。比如,要禁止系统从USB设备唤醒,利用旧的 /etc/rc.local 可以用 echo USBE > /proc/acpi/wakeup,而现在可以这么做:

/etc/tmpfiles.d/disable-usb-wake.conf
w /proc/acpi/wakeup - - - - USBE

推荐使用新方法,因为 systemd 并不原生支持 /etc/rc.local

详情参见 man 4 tmpfiles.d

单元(unit)

一个单元配置文件可以描述如下内容之一:系统服务、socket、系统设备、挂载点、交换分区/文件、启动目标(target)、文件系统路径、由 systemd 管理的计时器。文件格式受 .desktop 文件启发,而最初起源是 Windows 下的 .ini 文件。详情参见 man systemd.unit

详情参阅 man 5 systemd.unit.

从 initscripts 迁移到 systemd

Initscripts 模拟

initscripts 软件包提供了 systemd 和 Arch 传统配置的整合。When initscripts are installed in parallel with systemd, with the system running on systemd, systemd will do the following:

  1. Parse the DAEMONS array of /etc/rc.conf and start all listed daemons at boot
  2. Execute /etc/rc.local during boot
  3. Execute /etc/rc.local.shutdown during shutdown

Initscripts emulation is simply meant as a transitional measure to ease users' move to systemd, and will eventually go away. Native systemd does not rely on rc.conf centralised configuration, so it is recommended to use native systemd configuration files, which will take precedence over /etc/rc.conf.

Note: The recommended way to replace /etc/rc.local is to write the custom service files for any things you want to run on the system startup. See the corresponding section.
Note: If you disabled Template:Keypress to reboot in /etc/inittab, you will have to reconfigure this setting for systemd by running systemctl mask ctrl-alt-del.target as root.

If you disabled Template:Keypress to reboot in /etc/inittab, you will have to reconfigure this setting for systemd by running systemctl mask ctrl-alt-del.target as root.

Moving away from the DAEMONS array

For a pure systemd setup, you should remove the /etc/rc.conf file entirely and enable services only via systemctl. For each <service_name> in the DAEMONS array in /etc/rc.conf, run:

# systemctl enable <service_name>
Tip: For a list of commonly used daemons with their initscripts and systemd equivalents, see this table.

If <service_name>.service does not exist:

  • Most probably, systemd uses a different name. For example, cronie.service replaces the crond init daemon; alsa-store.service and alsa-restore.service replace the alsa init daemon. Another important instance is the network daemon, which is replaced with another set of service files (see Configuring Network for more details.)
  • Otherwise, a service file may not be available for systemd. In that case, you'll need to keep rc.conf to start the service during boot up.
Tip: You may look inside a package that contains daemon start scripts for service names. For instance:
$ pacman -Ql cronie
[...]
cronie /etc/rc.d/crond                            #Daemon initscript listed in the DAEMONS array (unused in a "pure" systemd configuration)
[...]
cronie /usr/lib/systemd/system/cronie.service     #Corresponding systemd daemon service
[...]
  • Finally, some services do not need to be explicitly enabled by the user. For instance, dbus.service will automatically be enabled when dbus-core is installed. alsa-store.service and alsa-restore.service are also enabled automatically by systemd. Check the list of available services and their state using the systemctl command like this: systemctl status <service_name>.

systemd 简单使用

The main command used to introspect and control systemd is systemctl. Some of its uses are examining the system state and managing the system and services. See man 1 systemctl for more details.

  • systemctl:控制 systemd 状态,包括服务的启动和停止。
  • systemd-cgls:以树形递归显示指定 Linux 控制组(cgroup)的内容
  • systemadm:控制 systemd 状态的图形前端(由 AUR 软件包 systemd-ui-gitAUR 提供)。
小贴士: systemctl 参数中添加 -H <用户名>@<主机名> 可以实现对其他机器的远程控制。该过程使用 SSH 链接。

分析系统状态

输出激活的单元:

$ systemctl

以下命令等效:

$ systemctl list-units

输出运行失败的单元:

$ systemctl --failed

所有可用的单元文件存放在 /usr/lib/systemd/system//etc/systemd/system/ 目录(后者优先级更高)。

使用单元

一个单元可以是系统服务(.service)、挂载点(.mount)、sockets(.sockets)。

使用 systemctl 控制单元时,通常需要使用单元文件的全名,包括扩展名(例如 sshd.service)。但是有些单元可以在systemctl中使用简写方式。

  • 如果无后缀,sistemctl 默认后缀为.service. 例如netcfgnetcfg.service 是等价的。
  • 挂载点会自动转化为相应的.mount单元。例如/home等价于home.mount
  • 设备会自动转化为相应的.device单元,所以/dev/sda2等价于dev-sda2.device.

立即激活单元:

# systemctl start <unit>

立即停止单元:

# systemctl stop <unit>

重启单元:

# systemctl restart <unit>

命令单元重新读取配置:

# systemctl reload <unit>

输出单元运行状态:

$ systemctl status <unit>

检查单元是否配置为自动启用:

$ systemctl is-enabled <unit>

开机自动激活单元:

# systemctl enable <unit>
注意: 如果服务没有Install段落,一般意味着应该通过其它服务自动调用它们。如果真的需要手动安装,可以直接连接服务,将 foo 替换为真实的服务名:
# ln -s /usr/lib/systemd/system/foo.service /etc/systemd/system/graphical.target.wants/

取消开机自动激活单元:

# systemctl disable <unit>

显示单元的手册页(必须由单元文件提供):

$ systemctl help <unit>

电源管理

If you are in a local systemd-logind user session and no other session is active, the following commands will work without root privileges. If not (for example, because another user is logged into a tty), systemd will automatically ask you for the root password.

重启:

$ systemctl reboot

退出系统并停止电源(shut down and power-off):

$ systemctl poweroff

退出系统并中断(shut down and halt):

$ systemctl halt

待机:

$ systemctl suspend

休眠:

$ systemctl hibernate

启动桌面环境

使用登录管理器

通过启动登录管理器(或称显示管理器),即可进行图形界面登录。目前,Arch 提供了 GDMKDMSLiMXDMLXDM 的 systemd 服务文件。以 KDM 为例,配置开机启动:

# systemctl enable kdm.service

执行上述命令后,登录管理器应当能正常工作了。如果不是的话,很可能是因为你修改了default.target。默认情况应当如下:

# ls -l /etc/systemd/system/default.target
/etc/systemd/system/default.target -> /usr/lib/systemd/system/graphical.target

删除被修改的 default.target,systemd 会自动使用默认配置(即 graphical.target):

# rm /etc/systemd/system/default.target

Using systemd-logind

Note: As of 2012-10-30, ConsoleKit has been replaced by systemd-logind as the default mechanism to login to the DE.

In order to check the status of your user session, you can use loginctl. All PolicyKit actions like suspending the system or mounting external drives will work out of the box.

$ loginctl show-session $XDG_SESSION_ID

Writing custom .service files

Handling dependencies

With systemd, dependencies can be resolved by designing the unit files correctly. The most typical case is that the unit A requires the unit B to be running before A is started. In that case add Requires=B and After=B to the [Unit] section of A. If the dependency is optional, add Wants=B and After=B instead. Note that Wants= and Requires= do not imply After=, meaning that if After= is not specified, the two units will be started in parallel.

Dependencies are typically placed on services and not on targets. For example, network.target is pulled in by whatever service configures your network interfaces, therefore ordering your custom unit after it is sufficient since network.target is started anyway.

Type

There are several different start-up types to consider when writing a custom service file. This is set with the Type= parameter in the [Service] section. See man systemd.service for a more detailed explanation.

  • Type=simple: systemd considers the service to be started up immediately. The process must not fork. Do not use this type if other services need to be ordered on this service, unless it is socket activated.
  • Type=forking: systemd considers the service started up once the process forks and the parent has exited. For classic daemons use this type unless you know that it is not necessary. You should specify PIDFile= as well so systemd can keep track of the main process.
  • Type=oneshot: This is useful for scripts that do a single job and then exit. You may want to set RemainAfterExit= as well so that systemd still considers the service as active after the process has exited.
  • Type=notify: Identical to Type=simple, but with the stipulation that the daemon will send a signal to systemd when it is ready. The reference implementation for this notification is provided by libsystemd-daemon.so.
  • Type=dbus: The service is considered ready when the specified BusName appears on DBus's system bus.

Replacing provided unit files

The unit files in /etc/systemd/system/ take precedence over the ones in /usr/lib/systemd/system/. To make your own version of a unit (which will not be destroyed by an upgrade), copy the old unit file from /usr/lib/ to /etc/ and make your changes there. Alternatively you can use .include to parse an existing service file and then override or add new options. For example, if you simply want to add an additional dependency to a service file, you may use:

/etc/systemd/system/<service-name>.service
.include /usr/lib/systemd/system/<service-name>.service

[Unit]
Requires=<new dependency>
After=<new dependency>

Then run the following for your changes to take effect:

# systemctl reenable <unit>
# systemctl restart <unit>
Tip: You can use systemd-delta to see which unit files have been overridden and what exactly has been changed.

Syntax highlighting for units within Vim

Syntax highlighting for systemd unit files within Vim can be enabled by installing vim-systemdAUR from the AUR.

目标(target)

启动级别(runlevel)是一个旧的概念。现在,systemd 引入了一个和启动级别功能相似又不同的概念——目标(target)。不像数字表示的启动级别,每个目标都有名字和独特的功能,并且能同时启用多个。一些目标继承其他目标的服务,并启动新服务。systemd 提供了一些模仿 sysvinit 启动级别的目标,仍可以使用旧的 telinit 启动级别 命令切换。

获取当前目标

不要使用 runlevel 命令了:

$ systemctl list-units --type=target

创建新目标

在 Fedora 中,启动级别 0、1、3、5、6 都被赋予特定用途,并且都对应一个 systemd 的目标。然而,没有什么很好的移植用户定义的启动级别(2、4)的方法。要实现类似功能,可以以原有的启动级别为基础,创建一个新的目标 /etc/systemd/system/<新目标>(可以参考 /usr/lib/systemd/system/graphical.target),创建 /etc/systemd/system/<新目标>.wants 目录,向其中加入额外服务的链接(指向 /usr/lib/systemd/system/ 中的单元文件)。

目标表

SysV 启动级别 Systemd 目标 注释
0 runlevel0.target, poweroff.target 中断系统(halt)
1, s, single runlevel1.target, rescue.target 单用户模式
2, 4 runlevel2.target, runlevel4.target, multi-user.target 用户自定义启动级别,通常识别为级别3。
3 runlevel3.target, multi-user.target 多用户,无图形界面。用户可以通过终端或网络登录。
5 runlevel5.target, graphical.target 多用户,图形界面。继承级别3的服务,并启动图形界面服务。
6 runlevel6.target, reboot.target 重启
emergency emergency.target 急救模式(Emergency shell)

切换启动级别/目标

systemd 中,启动级别通过“目标单元”访问。通过如下命令切换:

# systemctl isolate graphical.target

该命令对下次启动无影响。等价于telinit 3telinit 5

修改默认启动级别/目标

开机启动进的目标是 default.target,默认链接到 graphical.target (大致相当于原来的启动级别5)。可以通过 kernel parameters更改默认启动级别:

  • systemd.unit=multi-user.target (大致相当于级别3)
  • systemd.unit=rescue.target (大致相当于级别1)

另一个方法是修改 default.target。可以通过 systemctl 修改它:

# systemctl enable multi-user.target

命令执行情况由 systemctl 显示:链接 /etc/systemd/system/default.target 被创建,指向新的默认启动级别。 该方法当且仅当目标配置文件中有以下内容时有效:

[Install]
Alias=default.target

目前,multi-user.targetgraphical.target 都包含这段内容。

日志

自版本 38 起,systemd 提供了自己日志系统(logging system),称为 journal. 使用 systemd 日志,无需额外安装日志服务(syslog)。读取日志的命令:

# journalctl

By default (when Storage= is set to auto in /etc/systemd/journald.conf), the journal writes to /var/log/journal/. If the directory /var/log/journal/ does not exist (e.g. if you or some program delete it), systemd will not create it automatically, but instead write its logs to /run/systemd/journal. This means that logs will be lost on reboot.

过滤输出

journalctl可以根据特殊字段过滤输出,例如:

Show all messages from this boot:

# journalctl -b

显示新信息:

# journalctl -f

显示给定可执行文件的所有消息:

# journalctl /usr/lib/systemd/systemd

显示指定过程的所有消息:

# journalctl _PID=1

显示指定单元的所有消息:

# journalctl -u netcfg

详情参阅man journalctlsystemd.journal-fields

日志大小限制

如果按上面的操作保留日志的话,默认日志最大限制为所在文件系统容量的 10%,即:如果 /var/log/journal 储存在 50GiB 的根分区中,那么日志最多存储 5GiB 数据。可以修改 /etc/systemd/journald.conf 中的 SystemMaxUse 来指定该最大限制。如限制日志最大 50MiB:

SystemMaxUse=50M

详情参见 man journald.conf.

配合 syslog 使用

systemd 提供了 socket /run/systemd/journal/syslog,以兼容传统日志服务。所有系统信息都会被传入。要使传统日志服务工作,需要让服务链接该 socket,而非 /dev/loghttp://lwn.net/Articles/474968/ 官方说明)。以 Arch 源中的 syslog-ng 已经包含了需要的配置。

设置开机启动 syslog-ng:

 # systemctl enable syslog-ng

优化

启动过程分析

systemd 提供了一个分析启动过程的工具 —— systemd-analyze。可以用它看看哪些单元拖慢了开机过程,并据此进行优化。安装 python2-dbuspython2-cairo 软件包后该工具才能工作。

查看开机过程在内核/用户空间消耗的时间:

$ systemd-analyze
Tip: 如果在 /etc/mkinitcpio.confHOOKS 数组添加 timestamp,并重新生成启动内存镜像(initramfs),systemd-analyze 还可以显示处理该启动镜像花费的时间。

按照耗费时间顺序,输出启动每个单元耗费的时间:

$ systemd-analyze blame

生成类似于 bootchart 的开机过程图表:

$ systemd-analyze plot > plot.svg

使用 bootchart

由于没有办法在内核参数设置两个 init,所以不能使用源里的 bootchart。不过,AUR 软件包 bootchart2AUR 提供了一个的 systemd 服务,安装后启用即可:

# systemctl enable bootchart.service

详情参阅 bootchart 文档

预读

systemd 自己实现了一个 readahead,可以用来提高开机效率。不过,效果会因内核版本和硬件情况而不同(极端的还会变慢)。开启 readahead:

# systemctl enable systemd-readahead-collect.service systemd-readahead-replay.service

要知道,readahead 的超级牛力只有在重启几次后才会显现。

Early start for services

One central feature of systemd is D-Bus and socket activation. This causes services to be started when they are first accessed and is generally a good thing. However, if you know that a service (like UPower) will always be started during boot, then the overall boot time might be reduced by starting it as early as possible. This can be achieved (if the service file is set up for it, which in most cases it is) by issuing:

# systemctl enable upower

This will cause systemd to start UPower as soon as possible, without causing races with the socket or D-Bus activation.

精简输出信息

修改启动加载器内核参数中的 verbosequiet 即可。对于某些用户,特别是 SSD 用户,TTY 的龟速实际上成为了性能瓶颈,精简输出信息实际上有利于提高性能。

疑难解答

关机/重启十分缓慢

如果关机特别慢(甚至跟死机了一样),很可能是某个拒不退出的服务在作怪。systemd 会等待一段时间,然后再杀死它。 请阅读 Fedora wiki 上的文章,确认你是否是该问题受害者。

Disable warning bell

Add command xset -b to the .xinitrc file. Discussion on this forum topic.

相关资源