How to make mount --bind permanent?

Solution 1:

What do you mean "clogging up /etc/fstab"? The best place to put this in is /etc/fstab; that's what it was made for!

All you have to do is add one line after the first mount:

# <device>                                 <dir>                 <type>  <options>                 <dump>  <pass>
UUID=288a84bf-876c-4c4b-a4ba-d6b4cc6fc0d2  /mnt/device            ext4    defaults,noatime,nofail   0       2
/mnt/device                                /srv/binded_device     none    bind                      0       0

Solution 2:

The easiest way is to mount --bind what you need like

mount --bind /home/sda1/Windows/Users/Me/Dropbox ~/Dropbox

Then open mtab

sudo nano /etc/mtab

Copy your line like

/home/sda1/Windows/Users/Me/Dropbox /home/me/Dropbox none rw,bind 0 0

and paste it in fstab so it would mount on reboot

sudo nano /etc/fstab

If you folder is on mounted disk make sure your binding line comes after disk mount

Solution 3:

Another solution (which is helpful when you're using LVM and the accepted answer will not work and some may consider more useful since it uses a bit more logic) would be doing something similar to this:

Append the following to your crontab
# crontab -l | tail -1 ; cat /usr/sbin/custom-compiler-mount

@reboot /usr/sbin/custom-compiler-mount

Essentially you would use crond to execute a script on reboot

#!/bin/bash
( until [[ $( (mount |& grep vg0-homevol 2>&1 9<&1 > /dev/null 1<&9) ) ]] ; 
do 
sleep 1 
done & wait;mount -o rbind /home/linuxgeek/experimental/s3/gcc/ /gcc & ) & >/dev/null