Cool bind mount trick with systemd

The effect is to have folders on a partition bind mounted at various locations, but the full partition not mounted anywhere.

E.g. if /dev/sdb1 contains /f1 and /f2 and fstab looks like this:

# Mount to /mnt because it already exists and won't leave an extra empty folder after unmounted.
# But if you want it to remain, create a root folder, maybe hidden like ".bindmounts" and mount to that.
UUID=[uuid for /dev/sdb1] /mnt ext4 defaults 0 0

# Create root /myf1 and /myf1 first
/mnt/f1 /myf1 none bind,defaults 0 0
/mnt/f2 /myf2 none bind,defaults 0 0

So after that, if you reboot, ls /mnt would show f1 and f2. How to hide/remove that? Make a systemd service. E.g. I’ll call it unmount-mnt.service

[Unit]
Description=Unmount mnt
After=mnt.mount myf1.mount myf2.mnt

[Service]
Type=oneshot
ExecStart=umount /mnt

[Install]
WantedBy=local-fs.target

Put it in /lib/systemd/system then run

systemctl enable unmount-mnt.service

Reboot, and /mnt should be empty, and any content you have on sdb1 in folders f1 and f2 should now be visible under /myf1 and /myf2.

This works because systemd converts /etc/fstab to *.mount units. And this service waits until everything is mounted and then unmounts /mnt.

It’s my first shot at a systemd service, so I’m not sure if I did everything “right.”

QUESTION FOR MORE EXPERIENCED PEOPLE:

Will this cause any problems that you know of? I tested it in a VM and it works, but I didn’t test exhaustively to see if something down the road could drop the bind mounts, or something like that.

IMHO keep it simple (KISS acronym) is the way… within fstab method and systemd.automount you can direct mount folders everywhere you need outside /mnt and share mountpoints by link it with ln [OPTION]... -s DIRECTORY TARGET...it a symbolink link.

According to fstab wiki, I set my mountpoint automagic with this fstab entry for my external ssd full of music files, for example:

LABEL=MUKKA525 /home/babiz/Musica vfat defaults,noauto,x-systemd.automount 0 0

I use LABEL mountpoint cause is very comfortable for me lol, after reboot you get drive mounted in folder ( everywhere you want according to your R/W permissions ) but a empty folder must be present on target mountpoint.
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 489G 0 disk
└─sda1 8:1 0 489G 0 part /home/babiz/Musica
nvme0n1 259:0 0 476,9G 0 disk
├─nvme0n1p1 259:1 0 1000M 0 part /efi
├─nvme0n1p2 259:2 0 380,1G 0 part /
├─nvme0n1p3 259:3 0 16M 0 part
├─nvme0n1p4 259:4 0 78,9G 0 part
└─nvme0n1p5 259:5 0 16,9G 0 part [SWAP]

Next you can make many symlink ahead mountpoint and target directory you want from terminal commands:

$ mkdir ~/mounts is a example folder inside my home
$ ln -s ~/Musica/Musika/Abba/ mounts point my automount folder everywhere I want

$ ls -l mounts/
totale 0
lrwxrwxrwx 1 babiz babiz 31 11 feb 00.32 Abba → /home/babiz/Musica/Musika/Abba/
lrwxrwxrwx 1 babiz babiz 32 11 feb 00.15 AC-DC → /home/babiz/Musica/Musika/AC-DC/

For example, if you need to hide master mountpoint for all users, you can mount it under /root directory and adjust access for users by change symlink permission.

In short this is the way I learned many years ago when systemd isn’t here and system-V rulez lol.
Indeed fstab adjusted with systemd automount feature is super cool thing mate.
Acutally Arch Wiki for fstab is super cool filled with useful tips. :ghost: @discobot quote

:left_speech_bubble: Do not be embarrassed by your mistakes. Nothing can teach us better than our understanding of them. This is one of the best ways of self-education. — Thomas Carlyle

I know about symlinks, but there are cases where they don’t work exactly the same as a bind mount, e.g. in a chroot.

Yes, but the main reason I want to completely remove the master mount is, sometimes I’ll do something like “sudo find / -type f | [other processing commands]” and then have to filter out the master mount, every time I do it. Just gets tedious, and was looking for a cleaner way.

And partly, what I described was an experiment… “Can I do it.” If it’s useful, I’ll use it, if not I’ll do something else.

1 Like

Oh well now I understand, you aim to get more advanced stuffs, nice man.