Flexible and easy outsourcing of data with bind (btrfs subvolume like)
megavolt edited this page 2 years ago

There are in general these methods:

  1. Symbolic Links (symlinks) -> ln -s /actual-folder /linked-folder
  2. Mounted Binds(binds) -> mount --bind /actual-folder /virtual-folder

While Symlinks are well known on all Operating Systems, binds are more UNIX specific and a well kept open secret.

The approach is the same, but there are differences...

  1. Symlinks are files, which survive a reboot, while mounts does not, if not set in fstab.
  2. Symlink files contain just a link to the actual folder/file, while binds jail it and mirror it virtually.

I won't go into details about the bind option, because there are actually good docs about it:

  • man mount_namespaces
  • man mountpoint
  • man mount

In this case I just focus on one use case:

Create a private binding of a folder.

So lets say you a external Disk of some kind I don't want to spend time on shrinking and creating a new partition just for outsourcing the /home folder.

Normally you would create a partition at the installation process and set the mountpoint /home on it. But this time you have one partition for the / (root directory).

Expectations:

  • Both Partitions have Linux Filesystems, so no NTFS or FAT.
  • The external Drive is permanently available.

Process:

  1. You need mount the external Disk and make it permanent in /etc/fstab:
LABEL=data /mnt/data ext4 defaults,noatime,X-mount.mkdir=0755 0 2

:notebook: Normally you would have to create the folder, but the option X-mount.mkdir=0755 creates the mountpoint /mnt/data automatically.

  1. Now copy the home folder to the data disk and check if the size match:
cp --verbose --update --archive /home /mnt/data/home
du --human-readable --summarize /home /mnt/data/home
mv /home /home.old
  1. Now create a binding. It would look like this:
$ cat /etc/fstab  
# System mountpoints
UUID=XXXX-XXXX /boot/efi vfat umask=0077 0 2
UUID=XXXXXX-XXXXXX / ext4 defaults,noatime 0 1

# External Data Disk
LABEL=data /mnt/data ext4 defaults,noatime,x-mount.mkdir=0755 0 2

# Bindings of the external Disk
/mnt/data/home /home none defaults,bind,x-gvfs-hide,x-mount.mkdir=0755 0 0
  1. Now you just a reboot is needed.
  2. Check if it uses the new binding:
$ cat /proc/self/mountstats | grep "/home \|/ "
device /dev/sda2 mounted on / with fstype ext4
device /dev/sdb1 mounted on /home with fstype ext4

:notebook: The device name can be different. Important is that it is not on the same device as the root /

  1. Success!

Now you can remove the folder /home.old