How to poweroff when there's no systemd/init (e.g. using init=/bin/bash)?
Unmount the filesystems that you had mounted. The root filesystem is a special case; for this you can use mount / -o remount,ro
. On Linux, umount /
also happens to work, because it is effectively converted to the former command.
That said, you don't need to worry about unmounting too much, unless
- You have mounted an old filesystem like FAT - as used by the EFI system partition - or ext2, which does not implement journalling or equivalent. With a modern filesystem,
sync
is supposed to be enough, and the filesystem will repair itself very quickly on the next boot. - You might have left a running process that writes to the filesystem, and you had intended to shut it down cleanly. In that case it's useful to attempt to umount the filesystems, because umount would fail and show a busy error to remind you about the remaining writer.
The above is the important part. After that, you can also conveniently power off the hardware using poweroff -f
. Or reboot with reboot -f
.
There is a systemd
-specific equivalent of poweroff -f
: systemctl poweroff -f -f
. However poweroff -f
does the same thing, and systemd
supports this command even if it has been built without SysV compatibility.
Technically, I remember my USB hard drive was documented as requiring Windows "safe remove" or equivalent. But this requirement is not powerfail safe, and Linux does not do this during a normal shutdown anyway. It's better interpreted as meaning that you shouldn't jog the hard drive while it is spinning - including by trying to unplug it. A full power off should stop the drive spinning. You can probably hear, feel, or see if it does not stop :-).
I will simply execute below two commands:
echo s > /proc/sysrq-trigger <= For sync
echo o > /proc/sysrq-trigger <= For shutdown the system
Assuming magic key is enabled in kernel
Ok, so it just occurred to me that I had the option to exec init
. From there, I would probably be able to later poweroff
. I wonder if there are better alternatives, though.