Tags Archives: bind

Using mount – – bind

A bind mount provides an alternate view or mapping of a directory tree.

 

Usually mounting a device presents a view of that storage device in the form of a directory tree.

 

 

A bind mount on the other hand takes an existing directory tree and replicates it under a different mount point.

 

 

The directories and files in the bind mount are the same as the original directory tree from that point.

 

 

Any modification on one side will be reflected on the other side, since the two views depict the same data tree.

 

 

 

They can be especially useful if you want to allow only partial access to a section of a directory tree on a device.

 

Bind mounts are simple to implement.

 

Instead of mounting a device on a particular path, you are mounting a specific path of the file system from that device onto another path.

 

 

 

In Linux, bind mounts are available as a kernel feature. However, they can also be implemented using other methods, such as fusermount or bindfs (not covered here). The examples below cover solely the Linux kernel version (mount – – bind).

 

 

 

Example:

 

(note the  correct syntax is:

 

mount – – bind  (ie with two – – dashes  – as this webpage may not be displaying this correctly it is NOT *one* dash but two) 

 

mount –bind /media/kevin/PRIMARY_MEDIA /srv/nfs4/PRIMARY_MEDIA

 

this mounts /media/kevin/PRIMARY_MEDIA on /srv/nfs4/PRIMARY_MEDIA

 

it is the same as

 

mount -t ext4 /media/kevin/PRIMARY_MEDIA on /srv/nfs4/PRIMARY_MEDIA

 

 

we can also mount a directory on another directory. We do this by using the mount command with the –bind parameter. think of the bind mount as an alias.

 

Like with the mount command, using mount –bind you can mount a specific path from that device’s file system to a specific new path:

 

 

root@asus:/# mount –bind /media/kevin/PRIMARY_MEDIA/MEDIA/IT/ /mnt
root@asus:/#
root@asus:/#

 

 

 

the command findmnt –real displays the actual mounts:

 

root@asus:/# findmnt –real
TARGET SOURCE FSTYPE OPTIONS
/ /dev/nvme0n1p4 ext4 rw,relatime,errors=remount-ro
├─/run/user/1000/doc portal fuse.porta rw,nosuid,nodev,relatime,user_id=1000,group_id=10
├─/boot/efi /dev/nvme0n1p1 vfat rw,relatime,fmask=0077,dmask=0077,codepage=437,io
├─/media/kevin/PRIMARY_BACKUP /dev/sdc2 ext4 rw,relatime,errors=remount-ro
├─/srv/nfs4/PRIMARY_MEDIA /dev/sda1 ext4 rw,relatime
├─/media/kevin/SECONDARY_MEDIA /dev/sdb1 ext4 rw,nosuid,nodev,relatime
├─/media/kevin/DATAVOLUMELUKS /dev/mapper/DATAVOLUMELUKS
│ ext4 rw,relatime
├─/mnt /dev/sdc1[/MEDIA/IT] ext4 rw,relatime,errors=remount-ro
├─/media/kevin/GEMINI geminivpn:/ nfs4 rw,relatime,vers=4.0,rsize=131072,wsize=131072,na
│ └─/media/kevin/GEMINI/DATA geminivpn:/DATA nfs4 rw,relatime,vers=4.0,rsize=131072,wsize=131072,na
└─/media/kevin/PRIMARY_MEDIA /dev/sdc1 ext4 rw,relatime,errors=remount-ro
root@asus:/#

 

 

this means that the file system under /mnt contains the contents of /media/kevin/PRIMARY_MEDIA/MEDIA/IT (in other words using the device name: /dev/sdc1[/MEDIA/IT]

 

 

and not the entire /media/kevin/PRIMARY_MEDIA

 

(the entire device is in this case also mounted under └─/media/kevin/PRIMARY_MEDIA /dev/sdc1 and also as mount –bind at ─/srv/nfs4/PRIMARY_MEDIA /dev/sda1

 

note the different device names sda1 and sdc1 !

 

root@asus:/#
root@asus:/# mount | grep PRI
/dev/sda1 on /srv/nfs4/PRIMARY_MEDIA type ext4 (rw,relatime)
/dev/sdc2 on /media/kevin/PRIMARY_BACKUP type ext4 (rw,relatime,errors=remount-ro)
/dev/sdc1 on /media/kevin/PRIMARY_MEDIA type ext4 (rw,relatime,errors=remount-ro)
root@asus:/#

 

in this case the sda1 does not really exist, the actual device is sdc1

 

root@asus:/# fdisk -l /dev/sda
fdisk: cannot open /dev/sda: No such file or directory
root@asus:/#

 

root@asus:/# fdisk -l /dev/sdc
Disk /dev/sdc: 4,55 TiB, 5000947302400 bytes, 9767475200 sectors
Disk model: My Passport 2627
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: F8D14D49-BA2C-C244-A469-26B4B26E63D0

 

Device Start End Sectors Size Type
/dev/sdc1 2048 4194306047 4194304000 2T Linux filesystem
/dev/sdc2 4194306048 6291458047 2097152000 1000G Linux filesystem
root@asus:/#

 

 

You can place bind mount entries in the /etc/fstab.

 

Simply use the bind command in the options, together with any other options you want to include.

 

The “device” is the existing tree. The filesystem column can be empty ie “none” or “bind” (it will be ignored, but using a filesystem name would cause confusion). For example:

 

/somefolder/somewhere /readonly/somewhere none bind,ro

 

 

Continue Reading