FANCOMI Ad-Tech Blog

株式会社ファンコミュニケーションズ nend・新規事業のエンジニア・技術ブログ

Systemdの運用に向けて

はじめまして。インフラ担当のk_koderaと申します。

仕事だけでなく個人でもお世話になっているCentOSですが、6月10日にRed Hat Enterprise Linux 7、そして7月7日にはCentOS 7がリリースされました。リリース前から噂で聞いていたSystemdの採用。詳しいことはよくわかりませんが、コマンド操作に変更が入ると聞き気になっていました。そのSystemdの採用が現実となったので、来るべきSystemdの運用に向けて少し覗いてみたいと思います。

まず今までのシステムの起動処理の仕組みとSystemdを超簡単にまとめてみました。

システム名 仕組み
SysVinit ラン・レベルに応じてサービスを決められた順番に起動する
Upstart /etc/event.dディレクトリ配下に格納されている定義ファイルでイベントに応じてサービスを並列に起動する
Systemd さまざまな処理をUnitとして定義し、Unit同士の依存関係を維持して最適な順序で並列に処理する

SystemdにはUnitという概念が導入されているので、こちらも簡単に確認しておきます。

Unit(拡張子 内容
service サービスの起動・停止
device デバイス情報(udevがデバイスを認識)
mount ファイルシステムのマウント(/etc/fstabを元に自動生成)
swap swap領域の有効化(/etc/fstabを元に自動生成)
socket ソケットによるサービス起動(接続があったときに指定のデーモンを起動)
target 複数のUnitをグループ化する設定(Unit間の依存や起動順を設定できる)

これだけだと「ふーん」って感じかもしれませんが、まずは今までのコマンド操作がどのように変わったか使って慣れることが肝心ですよね。この中で使う機会が多いのはやはりサービスですかね。今までだと呪文のように/etc/init.d/~とかservice~と打っていましたが、Systemdではsystemctlコマンドを使用します。(以下、CentOS 7で操作)

[shell]# 起動

systemctl start sshd.service

停止

systemctl stop sshd.service

再起動

systemctl restart sshd.service

状態:プロセスやログも表示されます

systemctl status sshd.service

sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled) Active: active (running) since 火 2014-08-26 23:11:56 JST; 41s ago Process: 5055 ExecStartPre=/usr/sbin/sshd-keygen (code=exited, status=0/SUCCESS) Main PID: 5058 (sshd) CGroup: /system.slice/sshd.service └─5058 /usr/sbin/sshd -D

Aug 26 23:11:56 localhost.localdomain systemd[1]: Started OpenSSH server daemon. Aug 26 23:11:56 localhost.localdomain sshd[5058]: Server listening on 0.0.0.0 port 22. Aug 26 23:11:56 localhost.localdomain sshd[5058]: Server listening on :: port 22. ...(以下省略)...[/shell]

Unitの拡張子を指定しない場合はserviceとして扱われるので、.serviceは省略できます。

[shell]# systemctl start sshd.service

.serviceを省略してもOK

systemctl start sshd[/shell]

サービスの自動起動の確認(chkconfig --list)はSystemdだと次のようになります。(--typeオプションでserviceのみ表示)

[shell]# systemctl list-unit-files --type=service UNIT FILE STATE auditd.service enabled autovt@.service disabled avahi-daemon.service enabled blk-availability.service disabled brandbot.service static console-getty.service disabled console-shell.service disabled ...(以下省略)...[/shell]

※enabled:有効、disabled:無効、static:自動起動の設定なし

サービスの自動起動の設定

[shell]# 有効

systemctl enable firewalld.service

無効

systemctl disable firewalld.service[/shell]

ちなみにsystemctlコマンドを引数なしで実行すると有効になっているUnitの一覧が表示されます。

[shell]# systemctl

UNIT LOAD ACTIVE SUB DESCRIPTION ...(省略)... auditd.service loaded active running Security Auditing Service avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack crond.service loaded active running Command Scheduler dbus.service loaded active running D-Bus System Message Bus getty@tty1.service loaded active running Getty on tty1 iprdump.service loaded active running LSB: Start the ipr dump daemon iprinit.service loaded active running LSB: Start the ipr init daemon iprupdate.service loaded active running LSB: Start the iprupdate utility kdump.service loaded active exited Crash recovery kernel arming kmod-static-nodes.service loaded active exited Create list of required static device nodes for the current lvm2-lvmetad.service loaded active running LVM2 metadata daemon lvm2-monitor.service loaded active exited Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or lvm2-pvscan@8:2.service loaded active exited LVM2 PV scan on device 8:2 network.service loaded active exited LSB: Bring up/down networking NetworkManager.service loaded active running Network Manager polkit.service loaded active running Authorization Manager postfix.service loaded active running Postfix Mail Transport Agent rhel-dmesg.service loaded active exited Dump dmesg to /var/log/dmesg rhel-import-state.service loaded active exited Import network configuration from initramfs rhel-readonly.service loaded active exited Configure read-only root support rsyslog.service loaded active running System Logging Service sshd.service loaded active running OpenSSH server daemon ...(省略)...[/shell]

systemctlコマンドはlessで表示されますので、catのように表示させるときは--no-pagerオプションを使います。

[shell]# systemctl --no-pager ...(省略)... slices.target loaded active active Slices sockets.target loaded active active Sockets swap.target loaded active active Swap sysinit.target loaded active active System Initialization timers.target loaded active active Timers systemd-tmpfiles-clean.timer loaded active waiting Daily Cleanup of Temporary Directories

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.

93 loaded units listed. Pass --all to see loaded but inactive units, too. To show all installed unit files use 'systemctl list-unit-files'.[/shell]

Unitの定義ファイルは2か所に配置されています。

ディレクトリ 内容
/usr/lib/systemd/system システムデフォルトの配置先
/etc/systemd/system Unit定義ファイルの追加・修正を行うディレクトリ

Systemdは/etc/~に配置されているUnitを優先しますので、変更する場合は/usr/~に置かれているファイルを/etc/~にコピーして修正します。

[shell]# ls /usr/lib/systemd/system -.slice local-fs.target.wants sshd.socket NetworkManager-dispatcher.service lvm2-lvmetad.service sshd@.service NetworkManager-wait-online.service lvm2-lvmetad.socket suspend.target NetworkManager.service lvm2-monitor.service swap.target auditd.service lvm2-pvscan@.service sys-fs-fuse-connections.mount autovt@.service machine.slice sys-kernel-config.mount avahi-daemon.service messagebus.service sys-kernel-debug.mount ...(以下省略)...

ls /etc/systemd/system

basic.target.wants dbus-org.freedesktop.NetworkManager.service default.target.wants sockets.target.wants dbus-org.fedoraproject.FirewallD1.service dbus-org.freedesktop.nm-dispatcher.service getty.target.wants sysinit.target.wants dbus-org.freedesktop.Avahi.service default.target[/shell]

本当にさわりだけですが、少しでもSystemdに興味を持つきっかけになっていただけたら幸いです。

参考