I'm working my way through understanding more about unix/linux and how the various tools/packages/options interact with one another (for example, the role of systemd, or how fstab works). I'm also trying to read/absorb/understand as much as I can about some of the "typical" or "best practice" ways of doing things, which leads me to the title of the question: removable media.
I'm testing on a headless Debian distro (rPi OS, lite) and have udev and systemd both at my disposal. The ultimate goal here is to mount and access removable media both 1) if it's present at boot and 2) if it gets inserted/removed after boot. I'm reading through various threads, stack posts, etc. and trying to absorb the various ways to do this, pitfalls/errors/bad practices to watch out for, and what a "best" way to do this would be for a robust implementation.
So far I've gotten a couple things working, separately:
- mounting using fstab entry at startup
- mounting using udev rule on insert/re-insert
The fstab entry I've successfully added mounts the block device based on its identifier of something like /dev/mmcblk[x]p[x]. Using some options like nofail and others in the fstab documentation, it will successfully find and mount the drive at boot, without holding up the system, as an "automount" or removable type.
Separately then, I've also been able to create a udev rule that leverages systemd-mount to recognize when a new block device is inserted through the "add" action. It then calls on systemd-mount to create an "automount" point.
Testing both of these (separately) I've found that the fstab entry will successfully mount at boot but not detect a "re-insert" where the device is removed and then re-inserted. Additionally, the udev rule does a great job of capturing when a new device is inserted, but does not mount it when the system boots.
The above then leads me to the root of my question; when handling removable media mounting/unmounting, what's a good "typical" or "best practice" way of handling things? Does both an fstab and udev rule need to be created to handle both cases?
Secondly, I'm not 100% sure yet I'm not missing something in a configuration/option somewhere for both of this. For example:
- I've seen that fstab can also use systemd.[x] parameters like
systemd.mountto create the "trap" for the removable media. From my understanding this will create the mount point and essentially have things ready for the media (to be inserted) but will not actually mount the device until it's accessed (or a request to access the mount point is made). However, that still seems to be only related to devices mounted at boot time and not dynamic handling of devices. This seems odd to me and makes me think I'm not understanding the capability of fstab or its interactions with the system as a whole. - Similarly, with the udev rule, it seems odd to me that the "add" function doesn't trigger on startup if the device is already plugged in / present. I'd expect at some point the device has to be "added" but I'm assuming it's related to a timing/availability aspect of how things are processed when the kernel is booting up and when udev is available to process rules; which is why it never sees it as an "add" truly.
Thanks for reading; couple of last things: I'm aware of the udisk2 and autofs modules which also seem to handle things like this. I'm working my way into those but would like to understand the principles those (likely) work on first, to better understand/use those tools and be successful in implementing them.
Also for completeness, these are what I currently have for the auto-mount tactics discussed above (side-note I do realize the mount permissions between them are different; this is just what I've been experimenting with thus far).
fstab entry: /mnt/mmcblk1p1 /mnt/uSD vfat auto,nofail,rw,noexec,user 0 0
udev rule: ACTION=="add",SUBSYSTEM=="block",ENV{ID_FS_USAGE}=="filesystem",RUN{program}+="/usr/bin/systemd-mount --no-block -A -G --options=rw,umask=000 %N /mnt/uSD"