Clean shutdown with systemd

written by Tomas M. 8 days ago

Warning, technical post, for advanced users and developers only :) (and for me, because I will surely forget this soon)

If you were used to access files in /mnt/live/* in Slax, then you will have to change your habits (and scripts) because that is now available in a different path: /run/initramfs/* ... Explanation why this change was necessary follows.

When Slax was based on Slackware, I had to manually patch the rc scripts for shutdown, to return control back to initramfs which could safely unmount all devices and reboot. But after changing to Debian base, there are no such rc scripts (due to systemd). Systemd has some hardcoded logic on what it tries to unmount. I had to digg into systemd source codes to find out what it actually does during reboot, and I was happy to find a solution.

Previous Slax puts the initramfs-root-filesystem in /mnt/live/ by calling pivot_root. But systemd has no idea this directory is special, and it tries to unmount it (including all submounts) on shutdown, making Debian report lots of red warnings. As it shows up, there is no possibility to exclude certain mountpoints from unmounting when the system is ending, however systemd has several exclude-paths hardcoded. It is / (root directory, obviously), /run/initramfs and /usr. So, instead of pivoting the root to /mnt/live, I had to modify linux-live-kit to pivot into /run/initramfs, in order for all systemd-based distros to ignore unmounting of initrd-based mounts, resulting in shutdown without errors. In order to unmount those properly, systemd executes /run/initramfs/shutdown.

One problem appeared immediately - Debian overmounts /run on boot with tmpfs, which made the /run/initramfs inaccessible (even mount --bind /run /somewhere couldn't help). So I had to make a little change in the initial startup - mount the 'run/' directory first with tmpfs, then create run/initramfs/, and then pivot root. Debian recognizes that /run is areadly mounted and doesn't overmount it again, leaving /run/initramfs accessible. Bingo.

Tomas M.

(c) 2017, Tomas M; rss