前回は、systemd が最初に読み込む default.target を取っ掛かりとして、target の種類や使い方を調べてみた。また、SysVinit との違いを確認しながら、systemd の作法を少しずつ確認した。
今回は、unit の種類と systemctl の使い方を中心に、systemd への理解を深めるように作業してみた。
systemctl の出力は横に長いものが多いので、sample は横スクロール対応とした。
unit の一覧
存在している unit の一覧は、systemctl list-unit-files で確認できる。
$ systemctl list-unit-files UNIT FILE STATE proc-sys-fs-binfmt_misc.automount static -.mount generated dev-hugepages.mount static dev-mqueue.mount static ... 略 ...
存在していても読み込まれない unit もある。実際に読み込まれている unit の一覧は、systemctl list-units で確認できる。
$ systemctl list-units UNIT LOAD ACTIVE SUB DESCRIPTION proc-sys-fs-binfmt_misc.automount loaded active running Arbitrary Executable File Formats File System Automount Point sys-devices-pci0000:00-0000:00:1b.0-sound-card0.device loaded active plugged 5 Series/3400 Series Chipset High Definition Audio sys-devices-pci0000:00-0000:00:1c.4-0000:03:00.0-net-enp3s0.device loaded active plugged RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (P8P67 and other motherboards) sys-devices-pci0000:00-0000:00:1d.0-usb4-4\x2d1-4\x2d1.2-4\x2d1.2:1.2-sound-card1.device loaded active plugged Webcam Pro 9000 ... 略 ...
どちらも、type を指定すれば、その type のみを表示する。
設定ファイルの存在する unit と動的生成 unit
enakai00.hatenablog.com
.device や .scope の様に、system 起動時に動的に生成される unit もある。udev と連携して hardware を利用する為の機能を提供する unit を生成している。
unit の種類
https://jp.linux.com/news/linuxcom-exclusive/421712-lco2014092602jp.linux.com
チートシート (覚書)
下記に、よく使うコマンドを示します。
# systemctl start [name.service]
# systemctl stop [name.service]
# systemctl restart [name.service]
# systemctl reload [name.service]
$ systemctl status [name.service]
# systemctl is-active [name.service]
$ systemctl list-units --type service --allsystemd は、12 のユニット タイプを持ちます。.service は、システムサービスを意味し、上記のコマンドで指定するときは、.service 拡張子を外してください。他に指定しない場合、systemd は .service を仮定します。他のタイプは、次のとおりです。
Target: group of units
Automount: filesystem auto-mountpoint
Device: kernel device names, which you can see in sysfs and udev
Mount: filesystem mountpoint
Path: file or directory
Scope: external processes not started by systemd
Slice: a management unit of processes
Snapshot: systemd saved state
Socket: IPC (inter-process communication) socket
Swap: swap file
Timer: systemd timer
- .service は system service。各種 daemon 等。昔の /etc/rc.d の init file と似たもの。
- .target は unit のひとまとめ。
- .automount、.mount は (仮想含む) file system の mount。
- .device は sysfs や udev に現れる device 名称。なので unit file は無い。
- .path は file と directory の変更を監視。
- .scope、.slice、.snapshot が分かりにくい。後で調べよう。
- .socket は port で待つ。昔の xinetd 代替と思う。
- .swap は swap を有効にする。
- .timer は時間指定で実行する。cron 代替と思う。
unit の依存関係を表示
$ systemctl list-dependencies default.target default.target * |-lightdm.service * |-rtkit-daemon.service * |-systemd-update-utmp-runlevel.service * `-multi-user.target * |-binfmt-support.service * |-console-setup.service ... 略 ...
systemd control group (cgroup)
systemd では process に割り当てる資源も制限することが出来る。slice や scope、service の単位で制限できる。プロセスのまとまりを表現するのがこの3つとの理解で良いのかも。
systemd-cgls で control group の繋がりを表示することが出来る。
$ systemd-cgls Control group /: -.slice |-user.slice ... 略 ... |-init.scope ├─init.scope │ └─1 /sbin/init └─system.slice ├─lightdm.service │ ├─469 /usr/sbin/lightdm │ └─485 /usr/lib/xorg/Xorg :0 -seat seat0 -auth /var/run/lightdm/root/:0 -nolisten tcp vt7 -novtswitch ├─systemd-timesyncd.service │ └─342 /lib/systemd/systemd-timesyncd ├─dbus.service │ └─453 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation ├─udisks2.service │ └─845 /usr/lib/udisks2/udisksd --no-debug ├─ssh.service │ └─479 /usr/sbin/sshd -D ├─system-getty.slice │ └─getty@tty1.service │ └─464 /sbin/agetty --noclear tty1 linux ├─systemd-logind.service │ └─461 /lib/systemd/systemd-logind ├─rtkit-daemon.service │ └─457 /usr/lib/rtkit/rtkit-daemon ├─polkit.service │ └─777 /usr/lib/policykit-1/polkitd --no-debug ├─cron.service │ └─458 /usr/sbin/cron -f ├─systemd-udevd.service │ └─266 /lib/systemd/systemd-udevd ├─rsyslog.service │ └─452 /usr/sbin/rsyslogd -n ├─systemd-journald.service │ └─236 /lib/systemd/systemd-journald └─systemd-networkd.service └─273 /lib/systemd/systemd-networkd
結果
- systemctl list-unit-files で unit の一覧を表示できる。
- systemctl list units で有効な unit の一覧を表示できる。
- unit は 12 種類が存在していて、昔の xinetd や cron 代替もある。
- systemctl list-dependencies で unit の依存関係を表示できる。
- systemd-cgls で control group の繋がりを表示出来る。
まだまだ分からない事が多いので、徐々に調べてみる。
その他 : 横スクロール
スーパーPre記法で横スクロールを出す方法については、次のサイトを参考にさせて頂いた。
a-kuma3.hatenablog.com
sprint-life.hatenablog.com
CSS とか不勉強なのでそのまま使わせてもらった。Design に追記してもうまく行かなかった。