Systemd/zh-cn

systemd 是一个Linux的现代的sysvinit来替换传统的init. Gentoo支持init替换为systemd.

systemd 是一个现代的 SysV-style init 和 rc (run command) 作为linux系统传统的init替代项.

内核
systemd利用Linux内核的许多现代特点. 要想支持systemd，内核必须高于ebuild 2.6.39 在最新的版本, 有一个快捷的方法来为systemd启用强制选择和可选的内核的选项:

手动配置内核选项 (这是在不使用时，唯一的选择 ), 下面是内核配置选项的要求或建议:

UEFI系统应该启用下面的:

如果你的系统使用BFQ scheduler, 这里推荐使用 BFQ upstream 来启用 "BFQ hierarchical scheduling support" under "Enable the block layer -> IO Schedulers".

要获取一个最新的列表,请参阅部分上游的“需求”说明：README

目录
目录被systemd 使用，其他的应用程序作为一个非连续性存储的运行时数据像文件, 网络编程接口和状态文件.

systemd包将自动创建 本身的路径文件. 然而， 请注意,这一变化也将触发自动安装的OpenRC， 并可能会触发其使用的不同软件包.

Upstream only supports the file being a symlink to. Not creating this symlink will also cause problems with mount and df. In the past some utilities wrote information (like mount options) into and thus it was supposed to be a regular file. Nowadays all software is supposed to avoid this problem. Still, before switching the file to become a symbolic link, please check to be sure that the system is not affected by any reported regressions.

要创建链接文件，执行：

在启动时确保/usr路径存在
For a split configuration, use an initramfs to mount  before starting systemd. See the Initramfs Gentoo Guide for instructions.

使用LVM2和initramfs
When sys-fs/lvm2 is used and the system is booted using an initramfs, the initramfs will have to be created using by running:

is either  or one of the other genkernel targets which imply the creation of an initramfs. For more information, look at the output of genkernel --help:

When LVM is used, the lvmetad daemon needs to be started as well. Otherwise systemd will be unable to mount LVM volumes. lvmetad can be enabled in :

安装
contains udev. Once installed, can be removed as systemd will be the provider for.

Enable the  USE flag globally (in ). The  USE flag should also be disabled to prevent conflicts with the systemd-logind service. It is also possible to switch to a systemd subprofile to use saner USE flags defaults in which case it is not necessary to change :

最后更新系统与Flags:

When dependency problems occur (such as blocking ),  might be registered in the world file. Try to resolve this by deselecting it:

使用systemd引导启动
为了运行systemd,切换init可用内核 (或者 initramfs)使用.

The following subsections document how to switch the init in one of the boot managers or the kernel.

Grub Legacy (0.x)
The  argument should be added to the kernel command-line. An example excerpt from would look like so:

Should the system boot using OpenRC, try using  instead of.

Grub 2
当 被使用时， 增加 init 选项 GRUB_CMDLINE_LINUX :

手动配置GRUB2文件时(仅限专家), 增加 参数  或  命令.

当使用 genkernel-next's 引导时, 使用 来替代.

内核配置
init设置也可以在内核中配置硬编码. 请看 "Processor type and features -> Built-in kernel command line". 注意：这个方法同时适用于grub</tt> 和 grub2</tt>.

设置root密码
请不要忘记设置系统的根密码. 如果出现错误,systemd将提示输入根密码进入维护模式.

安装后的设置
systemd支持几个系统配置文件来设置系统最基本的细节要求.

主机名称
要设置主机名称，创建/编辑 然后并简单地输入所需的主机名.

当使用systemd启动时，一个叫 的工具 ，可以编辑和来达到目的. 要更改主机名，执行：

参考 来获得更多选项.

区域设置
通常,区域设置将从OpenRC迁移安装systemd时被合理设置. 在你需要时,可以设置语言环境 按照Gentoo手册的说明:

如果你通过systemd启动，那么工具localectl</tt> 可以被使用来设置区域设置和控制台或X11键盘映射. 要更改系统语言环境请运行下列命令：

要更改虚拟控制台键盘映射表：

最后,要设置X11布局:

如果需要model, 变量和选项也可以被指定：

时间和日期
可以使用timedatectl</tt>来设置时间和日期. 这也允许用户设置同步，而无需依赖 或者其他的systemd自己的工具.

学习如何使用timedatectl</tt>只需运行：

自动加载模块
自动加载模块配置在不同的文件中，或者更确切地说，目录中的文件. 配置文件存储在. 在启动与模块列表的每个文件将被加载，文件格式是由换行符分隔模块的列表，并且可以使用任何名称，只要它与结束. 加载模块可以由程序、服务或其他方式,取决于符合个人喜好. 举个例子 virtualbox.conf</tt> 如下表所示：

systemd-networkd系统守护进程管理网络配置
systemd-networkd在有线网络接口的简单的配置上是很有用的. 它在默认情况下是禁用的.

要配置systemd-networkd，在路径下创建一个文件：请参考systemd.network(5) 一个简单的DHCP配置如下:

注意systemd-networkd 默认不会自动更新，要systemd管理DNS设置,替换来用一个符号连接并启动到systemd-resolved.

网络管理
通常NetworkManager的用于配置网络设置. 为了这个目的，简单地用一个基于X11的桌面时，运行以下命令：

如果情况并非如此,网络需要从终端来配置，nmcli值得试试， 或者遵循配置指导 nmtui</tt>：

nmtui 是一个ncurses的前端，在控制台模式下运行，同时将引导用户的过程中.

处理日志文件
systemd 有自己的方式处理日志文件,而不需要依赖任何外部日志系统 (像 或 ). 日志信息可以通过 journalctl</tt>来读取. 但是无论如何，它仍然可以被配置为使用一个更优秀的外部工具来处理它们. 请打开终端输入 man journald.conf 来学习如何配置journald来满足你的个人的需求.

/tmp现在是在tmpfs
除非明确指定挂载其他的文件系统到 在 中配置， systemd将会挂载 作为tmpfs. 这意味着它在每次启动时被清空，其大小将被限制在系统的RAM（内存）大小的50％. 要知道这是为什么，以及想要的行为和如何对其进行修改，请参考：API File Systems.

配置复杂的引导过程
当迁移到systemd用户通常注意到差异引导过程的时间


 * 启动选项 不仅影响了内核输出，也影响了systemd本身.  再就是，当你为你的系统设置systemd时，可以删除该选项来查看哪些是容易发生的错误. 之后，将它添加到一个静默的（和更快的）启动.
 * 甚至通过  启动选项， systemd 仍然可以通过也传递配置以显示其状态.
 * 当不使用 这个选项时，一些日志信息可能会刷满控制台. 这是由内核配置造成的(请参考 man 5 proc 并查找). 要调整它通过 的引导参数内核（以及根据喜好更新值，例如设置成像1的较低值）.

服务
在某一时刻，系统需要重新启动来让systemd运行（在system模式）. 请务必仔细阅读本文，以确保在系统重新启动之前尽可能完整的配置systemd. 注意journalctl</tt> 在systemd不运行时工作. 但 systemctl</tt> 在systemd不运行时不会做任何工作. 登录到正在运行的系统systemd后完成服务配置（启用和启动服务）.

OpenRC 服务
Although systemd originally intended to support running old init.d scripts, that support is not suited well for a dependency-based RC like OpenRC and thus is completely disabled on Gentoo. OpenRC provides additional measures to ensure that init.d scripts can't be run when OpenRC was not used to boot the system (otherwise the results would be unpredictable).

可用服务列表
All available service units can be listed using the  argument of systemctl</tt>:

The following file suffixes are of interest:

Alternatively the systemctl</tt> tool can be used to list all services (including implicit ones):

And finally check for services that failed to start:

Enabling, disabling, starting, and stopping services
The usual way of enabling a service is using the following command:

Services can be disabled likewise:

These commands enable services using their default name in default target (both specified in "Install" section of the service file). However, sometimes services either don't provide that information or users prefer to have another name/target.

Note that these commands only enable or disable the system to be started on a next boot; to start the service right now, use:

Services can be stopped likewise:

Installing custom unit files
Custom unit files can be placed in, where they will be recognized after running systemctl daemon-reload</tt>:

is reserved for service files installed by the package manager.

Customizing unit files
When only minor changes to a unit are needed, there's no need to create a full copy of the original unit file in. Overriding settings in a package management provided unit can be achieved by drop-in files in a directory named after the original unit (e.g. ) in.

A reload of systemd is needed to inform it of the changes:

Then the service needs to be restarted to apply the changes:

Verify that the changed property was applied to the service:

Enabling a service under a custom name
When the name provided by "Alias" in the unit's "[Install]" section does not meet the expectations and providing a permanent new value for this through a customization is not desired, a symlink can be created manually in. The name of the directory can either specify a target or another service which will depend on the new one.

For example, to install as  in the :

To disable the service, just remove the symlink:

Native services
Some of Gentoo packages already install systemd unit files. For these services, it is enough to enable them. A quick summary of packages installing unit files can be seen on systemd eclass users list.

The following table lists systemd services matching OpenRC ones:

Timer services
Since version 197 systemd supports timers, making cron unnecessary on a systemd system. Since version 212 persistent services are supported, replacing even anacron. Persistent timers are run at the next opportunity if the system was powered down when the timer was scheduled.

The following is an example on how to make a simple timer that runs in the context of a user. It will even run if the user is not logged in. Every timed service needs a timer and a service file that is activated by the timer as follows:

Firstly, tell systemd to rescan the service files:

It is possible to trigger the backup manually by running the following command:

Start and stop the timer manually as follows:

Finally, to activate the timer at every system start, run:

To check the last results of running the service:

Emailing failures
If a timed service runs and fails an e-mail can be send out to inform the user or administrator. This is possible with the "OnFailure" stanza which specifies what should happen if a service fails. A failure is detected by a non-zero return code of the invoked script.

For that change the script as follows:

This requires to have the service installed, which can be found in kylemanna's systemd-utils repository.

Replacing cron
The above timer and service files can also be added to to make them available system-wide. The install section should then say  to enable the service at system start.

However, cron also runs the scripts in and other locations. Several packages place scripts there that they expect to be run daily. This behavior can be emulated with systemd by installing. Then activate the new cron replacement with the following commands:

Troubleshooting

 * Upstream debugging guide
 * Upstream debugging guide
 * Upstream debugging guide

Graphical sessions opened in random places
By default systemd only launches a getty</tt> process when it's going to be used. This causes some display managers (like GDM) to use the remaining TTYs for opening graphical sessions on demand, which can result in having consoles and graphical sessions placed randomly depending on the order they were used.

To stick with a more "classical" behavior (i.e, consoles placed from to  and graphical sessions using the remaining TTYs) force it to always launch getty</tt> on those:

lvm
When switching from OpenRC to systemd and lvm is needed to properly mount the system volumes, activate the lvm service:

While it might not be needed for activation of the root volume (if lvm is integrated into the initramfs) it might not work for other lvm volumes, unless the service is activated.

systemd-bootchart
As systemd-bootchart attempts to start, reconfigure it to invoke systemd instead:

The result of the bootchart is a report in SVG format located in.

syslog-ng conflicts with systemd
systemd creates as datagram socket  so syslog-ng needs to be told to read from a unix-dgram instead of a unix-stream as otherwise syslog-ng would be using a "wrong" stream:

sys-fs/cryptsetup configuration
systemd does not seem to respect (see ) so it needs to be configured through the  file:

You may need to create a new service file from your file. To do this, enable the  USE flag for. It will install. Executing it will create a service file in, which you can now copy to , adjust manually and add to the desired runlevel.

Check for units that failed to start
Check for units that failed to start with:

Enable Debug Mode
To get more informations set the following in :

Or enable the debug-shell, that opens a terminal at tty9. This helps to debug services during the boot process.

e4rat usage
Please remember to edit setting 'init' to, otherwise it will keep booting OpenRC.

GRSecurity hardening
With grsecurity enabled, systemd-networkd might log the following error:

The error raises due to systemd-networkd working under a non-root user with grsecurity refusing access to the complete structure for such users. To disable this option, disable the kernel option.

External resources

 * FAQ
 * Tips and tricks