From Fedora Project Wiki
Line 214: Line 214:
需要注意的是,如果你退出了tty8 的会话,你需要等到下次重新启动才能使用,除非你给Restart 的值是'always' ,这样你可以使用<code>systemctl</code> 手动开启(但是出于安全考虑,强烈建议你不要那么做)。
需要注意的是,如果你退出了tty8 的会话,你需要等到下次重新启动才能使用,除非你给Restart 的值是'always' ,这样你可以使用<code>systemctl</code> 手动开启(但是出于安全考虑,强烈建议你不要那么做)。


==如何自定义或增加一个自定义''unit'' 文件?==
==如何设置或增加一个自定义''unit'' 文件?==
''unit''文件在<code>/etc/systemd/system</code>下的优先级要高于<code>/lib/systemd/system</code>下的。按照个人的需求从后者移动到前者并进行自定义修改。
''unit''文件在<code>/etc/systemd/system</code>下的优先级要高于<code>/lib/systemd/system</code>下的。按照个人的需求从后者移动到前者并进行自定义修改。



Revision as of 15:55, 16 March 2012

systemd 是Linux下一个与SysV和LSB init scripts兼容的系统和服务管理器。systemd 使用socket和D-Bus来开启服务,提供基于守护进程的按需启动策略,保留了Linux cgroups的进程追踪功能,支持快照和系统状态恢复,维护挂载和自挂载点,实现了各服务间基于从属关系的一个更为精细的逻辑控制,拥有前卫的并行性能。它也可以降级作为sysvinit的一个替代品。更多信息,请参考视频:http://linuxconfau.blip.tv/file/4696791/http://www.youtube.com/watch?v=TyMLi8QF6sw

Note.png
对于系统管理员
系统管理员如果想了解如何使用本地命令systemctl 来替换SysVinit 的工作流程,可以访问此页。需要注意的是,在systemd 的世界里servicechkconfig 命令将继续像之前一样工作。


为什么是systemd?

http://0pointer.de/blog/projects/why.html

介绍

systemd 开启和监督整个系统是基于unit 的概念。unit 由一个与配置文件对应的名字和类型组成的(例如: avahi.service unit 有一个具有相同名字的配置文件,是守护进程Avahi 的一个封装单元)。unit 有以下几种类型:

  1. service :守护进程的启动、停在、重启和重载是此类unit 最为明显的类型。
  2. socket :此类unit 封装系统和互联网中的一个套接口。当下,systemd 支持流式、数据报和连续包的AF_INET、AF_INET6、AF_UNIX 接口。也支持传统的FIFOs 传输模式。每一个套接口unit 都有一个相应的服务unit 。相应的服务在第一个连接进入socket 或FIFO 时就会启动(例如:nscd.socket 在有新连接后便启动nscd.service )。
  3. device :此类unit 封装一个存在于Linux 设备树中的设备。每一个使用udev 规则标记的设备都将会在systemd 中作为一个设备unit 出现。udev 的属性设置可以作为配置设备unit 依赖关系的配置源。
  4. mount :此类unit 封装系统结构层次中的一个挂载点。
  5. automount :此类unit 封装系统结构层次中的一个自挂载点。每一个自挂载unit 对应一个已挂载的挂载unit (需要在自挂载目录可以存取的情况下尽早挂载)。
  6. target :此类unit 为其他unit 进行逻辑分组。它们本身实际上并不做什么,只是引用其他unit 而已。这样便可以对unit 做一个统一的控制。(例如:multi-user.target 相当于在传统使用SysV 的系统中运行级别5;bluetooth.target 只有在蓝牙适配器可用的情况下才调用与蓝牙相关的服务,如:bluetooth 守护进程、obex 守护进程等)
  7. snapshot :与target unit 相似,快照本身不做什么,唯一的目的就是引用其他unit

文档

systemd 拥有丰富的文档。参考 http://0pointer.de/blog/projects/systemd-docs.html

特性

工具

内核启动命令行

Fedora 中systemd 的开发状态

在Fedora 14的特性中,systemd 是作为一个技术预览。在Fedora 15中替代Upstart 作为默认管理器。

System V init 与systemd 的对接

大量(但不是全部)核心服务(/lib/systemd/system 中有的,可以通过http://fedoraproject.org/wiki/User:Johannbg/QA/Systemd/compatability 检查它们的状态)已经转换到了systemd 中。预计在Fedora 16时完成所有转换工作。详情请参考 http://fedoraproject.org/wiki/Features/SysVtoSystemd 。systemd 完全兼容原始系统的初始化脚本。

systemd 的服务管理程序

systemctl 是最主要的工具。它结合servicechkconfig 于一体。你可以使用它永久性或只在当前阶段开启/禁用服务。

列出正在运行的服务或其他:

 systemctl 

更多详细信息请参考手册页。systemd-cgls 以树形列出正在运行的进程。它可以递归显示给定控制组内容。详情参阅systemd-sgls 手册页。

如何启动/关闭、开启/禁用服务?

运行一个服务:

  systemctl start foo.service 

关闭一个服务:

  systemctl stop foo.service 

重启一个服务:

 systemctl restart foo.service 

显示一个服务(无论运行与否)的状态:

 systemctl status foo.service 

在开机时启用一个服务:

 systemctl enable foo.service 

在开机时禁用一个服务:

 systemctl disable foo.service 

检查一个是否已开机启用:

 systemctl is-enabled foo.service; echo $? 

0 表示已开机启用,1 表示没有开机启用。

更多详情请参考systemctl 手册。

如何改变运行级别?

systemd 使用比sysvinit 的运行级更为自由的target 概念作为替代。

第3运行级用myti-user.target 替代。第5运行级用graphical.target 替代。runlevel3.target 和runlevel5.target 分别是指向muti-user.target 和graphical.target 的符号链接。

你可以使用下面的命令切换到“运行级3”:

 systemctl isolate multi-user.target (or) systemctl isolate runlevel3.target 

你也可以使用下面的命令切换到“运行级5”:

 systemctl isolate graphical.target (or) systemctl isolate runlevel5.target 

如何改变默认运行级别?

systemd 使用链接来指向默认的运行级别。在创建新的链接前,你可以通过下面命令删除存在的链接:

 rm /etc/systemd/system/default.target 

默认切换到运行级3:

 ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target 

默认切换到运行级5:

 ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target 

systemd 不使用/etc/inittab 文件。

如何查看当下运行级别?

runlevel 命令在systemd 下仍然可以工作。你可以继续使用它,尽管systemd 使用'target' 概念(多个的'target' 可以同时激活)替换了之前系统的runlevel 。等价的systemd 命令是

 systemctl list-units --type=target 

如何关机?

你可以使用

poweroff

更多的可能有: halt -pinit 0shutdown -P now

需要注意到在之前的Fedora 发布版中haltpoweroff 的效果一样。但是systemd 对区别这两项,因而,没有参数的halt 做的就是像它说的那样(停止)——仅仅停止系统而不关机。

service 命令兼容systemd 吗?

是的。service 经过修改可以在处理systemd 服务时调用systemctl 实现。因而下面的命令做的事情相同

 service NetworkManager stop 

(or)

 systemctl stop NetworkManager.service 

chkconfig 命令兼容systemd 吗?

是的,如果是开启/关闭服务,兼容性保证两种方式都可以运行。不过chkconfig 经过修改使得在处理systemd 服务时调用systemctl 工具。同样,在处理传统sysv 初始化文件时systemd 自动调用chkconfig

因此,下面的命令做的事情是一样的

 chkconfig NetworkManager off 

(or)

 systemctl disable NetworkManager.service 

chkconfig --list 不会列出systemd 服务,只列出Sys V 服务。chkconfig 的输出结果里添加了对此的说明信息。

system-config-services 与systemd 兼容吗?

Feodra 15的system-config-services 版本也可以处理systemd 的服务文件。如果你遇到问题,直接报告一个bug 。

如何改变默认gettys 数目?

添加一个新的getty :

只需要在getty.target.wants/ 目录下新建一个链接到getty 的示例即可:

ln -sf /lib/systemd/system/getty@.service /etc/systemd/system/getty.target.wants/getty@tty9.service
systemctl daemon-reload
systemctl start getty@tty9.service

删除一个getty :

直接删掉getty.target.wants/ 目录下你不想要的哪个getty 链接即可:

rm /etc/systemd/system/getty.target.wants/getty@tty5.service /etc/systemd/system/getty.target.wants/getty@tty6.service
systemctl daemon-reload
systemctl stop getty@tty5.service getty@tty6.service

systemd 不使用/etc/inittab 文件。

虚拟终端如何设置自动登录?

首先创建一个新的类似与getty@.service 的服务:

# cp /lib/systemd/system/getty@.service \
     /etc/systemd/system/autologin@.service
# ln -s /etc/systemd/system/autologin@.service \
        /etc/systemd/system/getty.target.wants/getty@tty8.service

然后编辑ExecStart、Restart 和Alias 的值,如:

...
ExecStart=-/sbin/mingetty --autologin USERNAME %I
Restart=no
...
Alias=getty.target.wants/getty@tty8.service

最后重新加载守护进程,运行服务:

systemctl daemon-reload
systemctl start getty@tty8.service

需要注意的是,如果你退出了tty8 的会话,你需要等到下次重新启动才能使用,除非你给Restart 的值是'always' ,这样你可以使用systemctl 手动开启(但是出于安全考虑,强烈建议你不要那么做)。

如何设置或增加一个自定义unit 文件?

unit文件在/etc/systemd/system下的优先级要高于/lib/systemd/system下的。按照个人的需求从后者移动到前者并进行自定义修改。

If a line starts with .include followed by a file name, the specified file will be parsed at this point. Make sure that the file that is included has the appropiate section headers before any directives.

You should use .include statement instead of copying the whole unit file from /lib/systemd/system to /etc/systemd/system if possible. This will enable to update the unchanged directives correctly during future package updates.

Be careful when using .include together with directives that can be defined multiple times (like EnvironmentFile=), since we can only add new directives, but we can't remove already defined ones. We have to copy the whole file from /lib/systemd/system to /etc/systemd/system in this case.

Let's say we use a lighttpd server and we want to lower its niceness value. All we need to do is to add Nice=-5 to the lighttpd.service file. We can do this by either copying the whole file from /lib/systemd/system/lighttpd.service to /etc/systemd/system/lighttpd.service or creating the following file in /etc/systemd/system/lighttpd.service:

如何调试系统事件?

参考 How_to_debug_Systemd_problems

预读功能

systemd 有内置的预读功能(默认升级时未启用),它可以提高开机速度,但具体提升幅度视个人硬件而定。 要使用它,使用命令:

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

关于/usr 分区的警告

详情请参考: http://freedesktop.org/wiki/Software/systemd/separate-usr-is-brokenhttp://cgit.freedesktop.org/systemd/tree/README

man 手册

systemd 有多方面的文档,其中也包括一些man手册页。其网页版地址是: http://0pointer.de/public/systemd-man/

参考