说是有一个 RedHat 的客户提出需求:为了防止意外重启 Linux,要求 root 用户明确地执行某条命令后才允许对 Linux 进程重启操作。我们都知道 systemd linux 中的 reboot 是基于 reboot.target 来实现的,因此最直观的做法应该就是 mask 掉这个 target 了。
lujun9972:~/ $ sudo systemctl mask reboot.target
[sudo] lujun9972 的密码:
Created symlink /etc/systemd/system/reboot.target → /dev/null.
lujun9972:~/ $ sudo systemctl reboot
Failed to reboot system via logind: Access denied
Failed to start reboot.target: Unit reboot.target is masked.
lujun9972:~/ $ reboot now
Failed to write reboot parameter file: 权限不够
lujun9972:~/ $ sudo reboot now
Failed to talk to init daemon.
然后 RedHat 工程师玩出了另一种花样(下面内容摘自于 access.redhat 商业网/solutions/1580343):
[Unit]
Description=Reboot Guard
[Service]
ExecStart=/bin/true
[Install]
RequiredBy=shutdown.target
这一步是核心操作了,由于 /bin/true 执行后立即停止,因此这个被 shutdown.target 依赖的单元被显示停止,那么 shutdown.target 单元本身也就跟着立即停止了。
[Unit]
Description=Start Reboot Guard
[Service]
ExecStart=/bin/systemctl enable reboot-guard
[Install]
WantedBy=multi-user.target
这一步保证Linux启动后自动enable reboot-guard 服务
[root]# systemctl reboot
Failed to issue method call: Transaction contains conflicting jobs 'stop' and 'start' for reboot.target. Probably contradicting requirement dependencies configured.
[root]# shutdown -h now
Failed to issue method call: Transaction contains conflicting jobs 'stop' and 'start' for poweroff.target. Probably contradicting requirement dependencies configured.
Broadcast message from root@example.com on pts/1 (Mon 2015-08-17 13:37:10 EDT):
The system is going down for power-off NOW!
[root]# systemctl poweroff
Failed to issue method call: Transaction contains conflicting jobs 'stop' and 'start' for poweroff.target. Probably contradicting requirement dependencies configured.
[root]# systemctl disable reboot-guard.service
rm '/etc/systemd/system/shutdown.target.requires/reboot-guard.service'
[root]# systemctl poweroff
Broadcast message from root@example.com on pts/1 (Mon 2015-08-17 13:37:50 EDT):
The system is going down for system halt NOW!
[ OK ] Started Show Plymouth Power Off Screen.
[ OK ] Started Restore /run/initramfs.
[ OK ] Stopped LSB: Bring up/down networking.
Stopping Network Manager...
[ OK ] Stopped Network Manager.
...
[ OK ] Reached target Unmount All Filesystems.
[ OK ] Stopped target Local File Systems (Pre).
Stopping Remount Root and Kernel File Systems...
[ OK ] Stopped Remount Root and Kernel File Systems.
[ OK ] Reached target Shutdown.
Cannot finalize remaining file systems and devices, giving up.
dracut Warning: Killing all remaining processes
Powering off.
[ 4653.288570] Power down.
