1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

systemdまで調べてみたよ

Posted at

勉強前イメージ

SysVinitの後継の後継
systemdのことを知りたくてSysVinit→Upstartまで調べてたどり着いた

調査

systemdとは

systemdの概略

OS起動の仕組みで、Upstartと同様に各プロセスを並列起動する
Upstartより高速になっており、システム管理の共通化なども行っている
主なOSでいくと、CentOS7はsystemdを採用している

systemdの特徴

  • 高速なシステム起動

Upstartよりも高い並列度で処理するため、起動がより早くなっている

  • システム管理の共通化

今まで使用していたシェルスクリプトが廃止され、ユニットファイルになった

SysVinit

CentOS5_/etc/init.d/sshd
[root@localhost ~]# head /etc/init.d/sshd 
# !/bin/bash
#
# Init file for OpenSSH server daemon
#
# chkconfig: 2345 55 25
# description: OpenSSH server daemon
#
# processname: sshd
# config: /etc/ssh/ssh_host_key
# config: /etc/ssh/ssh_host_key.pub

systemd

CentOS7_/usr/lib/systemd/system/sshd.service
[root@localhost ~]# head /usr/lib/systemd/system/sshd.service 
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
  • プロセスの起動が柔軟

Unit単位での管理。
タイマー(cronの代替)や、socketへの通信検出によるプロセス起動などが出来る

  • pidではなくcgroupがプロセスを管理する

SysVinitはpidでサービスを管理していましたが、systemdでは cgroup で管理しています。

  • SysVinitからの互換性がある

Upstartは互換性がなく、移行するのが大変でしたが、
SysVinitは互換性があり、楽にsystemdに移行することが出来ます。

systemdの起動順序

    1. systemdプロセス開始

Upstartまでは initプロセスでしたが、 systemdからはsystemdプロセスが起動してサービスの管理を行います。

CentOS5_SysVinit_initプロセス
[root@localhost ~]# pstree
init─┬─acpid
     ├─anacron
     ├─atd
     ├─auditd─┬─audispd───{audispd}
     │        └─{auditd}
     ├─automount───4*[{automount}]
     ├─crond
     ├─dbus-daemon───{dbus-daemon}
     ├─...
     ├─...
CentOS7_systemd_systemdプロセス
[root@localhost ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
        ├─VBoxService───8*[{VBoxService}]
        ├─agetty
        ├─auditd───{auditd}
        ├─chronyd
        ├─crond
        ├─...
        ├─...
    1. default.targetを処理

systemd は default.target に記述されているものを各ユニットとしてを立ちあげます。
以下のように実際には /lib/systemd/system/multi-user.target へのシンボリックリンクになっています。

[root@localhost system]# ll /etc/systemd/system/default.target
lrwxrwxrwx. 1 root root 37  8月 28 16:48 /etc/systemd/system/default.target -> /lib/systemd/system/multi-user.target
    1. リンク先のtargetを確認

上記のようにdefault.targetは実際にはこのサーバでは /lib/systemd/system/multi-user.target へのシンボリックリンクになっているので、
このファイルを解析する

[root@localhost system]# cat /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.

[Unit]
Description=Multi-User System
Documentation=man:systemd.special(7)
Requires=basic.target
Conflicts=rescue.service rescue.target
After=basic.target rescue.service rescue.target
AllowIsolate=yes
    1. 解析結果により、必要なサービスを起動する(依存関係順に従いながら並列に)

サービス管理について

SysVinitではプロセスで管理していたが、systemdではUnit単位での管理になっている
systemctl コマンドを使用し、以下の様に使う

コマンド
systemctl コマンド ユニット

以下、主なコマンドやユニットの拡張子を記載

コマンド 詳細
list-units 全てのユニットとその状態を表示
start 指定のユニットを起動する
restart 指定のユニットを再起動する
stop 指定のユニットを停止する
reload 指定のユニットを再読み込みする
status 指定のユニットの実行状況を確認する
ユニットの拡張子 詳細
.service サービスの起動,停止などの設定に関するもの
.mount ファイルシステムのマウント,アンマウントの設定に関するもの
.target 複数のUnitをとりまとめるUnit
  • (例)httpd.serviceユニットの実行状況の確認
systemctl status httpd.service
  • (例)sshd.serviceを再起動
systemctl restart sshd.service

勉強後イメージ

長かったsystemd。
最初systemdってsystemctlのイメージしかなかったけど、
それは上辺だけだった....いろいろ進化されてるんだな
頑張ってserviceコマンド卒業する...いつもどっちがどっちかわからなくなっちゃう
まだまだ理解できてないけど、頑張る

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?