I have a (I think) very particular situation on my embedded Linux which I cannot work out properly...
Due to particular (custom) checks to be performed on filesystems at startup before mounting them, I use an initial ramdisk to check and mount all the requested filesystem, and then I transfer them to the root filesystem when init is completed (so no /etc/fstab
is used for mounting device, but rather they are mounted through a script residing inside the initial ramdisk).
To be more specific, the /boot
partition (mmcblk1p1
) is mounted first (in READ-ONLY mode); inside that partition I have a file (rootfs.squashfs
) corresponding to a squashfs image of the target root filesystem... so, after some other operations (not relevant for the purpose of this post) the initial ramdisk script ends up mounting the squashfs image on /rootfs
and at the end of the init script the system root is switched to /rootfs
(via switch-root
command);
The problem arises at system reboot (or shutdown or poweroff): when the system tries to umount all mounted filesystems, then the umount of /boot
fails (because the device is busy, as it is obvious, since the root filesystem is mounted from a squashfs file residing in /boot).
Is there a way for avoiding to umount /boot
at reboot/shutdown/poweroff, thus avoid the umount fail? Since it is mounted read-only it should not be risky at all, isn't it? Or am I missing something?
P.S.: I do not know if it may be helpful and/or relevant: my OS init is managed via systemd
.
You don't actually need to change the order. Since the filesystem is read-only, it's safe to just tell the kernel that it can finish the unmount later (at such time as no references remain) rather than requiring it to succeed immediately.
In the
boot.mount
unit configuration, set the flagLazyUnmount=true
.https://www.freedesktop.org/software/systemd/man/latest/systemd.mount.html#LazyUnmount=
(If you never created
.mount
unit files by hand, they're created by a systemd generator that scans fstab; you can still override mount units with drop-in files even if you don't create them yourself).Systemd supports an 'exitrd' feature (also known as 'shutdown initramfs'), where during late shutdown it will pivot back into a RAM filesystem and will execute your provided script, which can then unmount the "real" root filesystem in any way it wants.
The exitrd will be used when a
/run/initramfs/shutdown
executable is found. After systemd has stopped all units and exec'd into its final shutdown handler, instead of printing "Unable to finalize filesystems" it will switch_root to/run/initramfs
, place the real root filesystem at/oldroot
, and exec the/shutdown
script.