I have bind mounts configured in my /etc/fstab. The source (i.e. where the files physically exist) is on a removable USB drive.

Right now it works but I have to mount/unmount everything individually when I plug/unplug the drive. I would like to be able to have the bind mounts follow the state of of the USB drive automatically.

Is there a way?

Edit here is relevant section of fstab:

/dev/mapper/external-crypt	/mount/external		auto	nosuid,nodev,nofail,x-systemd.device-timeout=5	0 0
/mount/external/realfolder	/home/user/folder	none	bind,nofail	0 0

So when I run sudo mount "/mount/external" I want sudo mount /home/user/folder to be implied; likewise for umount.

  • tal@lemmy.today
    link
    fedilink
    English
    arrow-up
    2
    ·
    2 months ago

    Followup: OP, based on your post update, you do indeed mean bind mounts. Okay.

    As I said above, what I would probably do myself for most cases is to just use a symlink, not a bind mount. That’s easy, doesn’t require futzing around with the system, and in most cases probably does what you want, whereas a bind mount may not (e.g. if you have a filesystem indexer or backup system, they’re not going to back the stuff up or index it twice).

    That’d be something like:

    $ ln -s /mount/external/realfolder /home/user/folder
    

    That does mean that /home/user/folder will still be visible when you don’t have the media inserted, though it’ll just be a dangling symlink.

    If you definitely do want to do it with bind mounts…hmm. It looks like there are some additional hurdles.

    https://unix.stackexchange.com/questions/799166/linux-auto-mounting-usb-with-udev

    According to a comment there, it looks like systemd-udev shouldn’t run a mount command directly, as it runs commands in a private namespace; it should use systemd-mount --no-block, which will ask systemd to set this up. You could probably write a bash script that runs a series of commands, including the systemd-mount command. It may be a bit involved for a linux4noobs solution, and I can’t give an off-the-cuff answer from personal experience, but I’d guess that it may very well be technically possible, and that’s probably what I’d be looking into if I were determined to specifically to use bind mounts.