Tags Archives: mount

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

NFS mounts do not work: Error message: mount: bad option

NFS mounts do not work and you receive the error message: 

 

mount: bad option; for several filesystems (e.g. nfs, cifs) you might need a /sbin/mount.<type> helper program.

 

 

 

Example of this Problem

 

root@len:/home/kevin# mount -v -t nfs -o proto=tcp,vers=4,nolock geminivpn:/home/kevin/DATA /media/kevin/DATA
mount: /media/kevin/DATA: bad option; for several filesystems (e.g. nfs, cifs) you might need a /sbin/mount.<type> helper program.
Filesystem Size Used Avail Use% Mounted on
udev 3.8G 0 3.8G 0% /dev
tmpfs 783M 1.6M 781M 1% /run
/dev/sda12 156G 69G 79G 47% /
tmpfs 3.9G 120M 3.8G 4% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda10 512M 4.0K 512M 1% /boot/efi
tmpfs 783M 60K 783M 1% /run/user/1000
/dev/sda11 4.0G 1.8G 2.2G 45% /media/kevin/LENOVO
/dev/mapper/DATAVOLUMELUKS 12G 7.4G 3.8G 67% /media/kevin/DATAVOLUMELUKS
root@asusvpn:/home/kevin/LOCAL 395G 313G 63G 84% /mnt

 

 

Cause of the Problem and Solution

 

This problem occurs because the system requires the systemd service nfs-common to be installed and running as a service.

 

The solution is to install nfs-common and start the service

 

root@len:/home/kevin# apt install nfs-common
Reading package lists… Done
Building dependency tree
Reading state information… Done

 

Preparing to unpack …/5-nfs-common_1%3a1.3.4-2.5ubuntu3.4_amd64.deb …
Unpacking nfs-common (1:1.3.4-2.5ubuntu3.4) …
Setting up libtirpc-common (1.2.5-1) …
Setting up keyutils (1.6-6ubuntu1) …
Setting up libnfsidmap2:amd64 (0.25-5.1ubuntu1) …
Setting up libtirpc3:amd64 (1.2.5-1) …
Setting up rpcbind (1.2.5-8) …
Created symlink /etc/systemd/system/multi-user.target.wants/rpcbind.service → /lib/systemd/system/rpcbind.service.
Created symlink /etc/systemd/system/sockets.target.wants/rpcbind.socket → /lib/systemd/system/rpcbind.socket.
Setting up nfs-common (1:1.3.4-2.5ubuntu3.4) …
.. .. .. ..
.. .. .. ..
Creating config file /etc/idmapd.conf with new version
Adding system user `statd’ (UID 125) …
Adding new user `statd’ (UID 125) with group `nogroup’ …
Not creating home directory `/var/lib/nfs’.
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-client.target → /lib/systemd/system/nfs-client.target.
Created symlink /etc/systemd/system/remote-fs.target.wants/nfs-client.target → /lib/systemd/system/nfs-client.target.
nfs-utils.service is a disabled or a static unit, not starting it.
Processing triggers for systemd (245.4-4ubuntu3.16) …
Processing triggers for man-db (2.9.1-1) …
Processing triggers for libc-bin (2.31-0ubuntu9.7) …
root@len:/home/kevin# mountnfsgemini

 

 

The nfs mounts then work ok

 

Continue Reading