The problem
When working with large files on servers with limited hard drive capacity, adding an external drive to increase the disk space allows us to keep working within the same system.
The problem arises when we need to add the (large) files into an existing directory on the main drive. For example, how to use the external drive for a new user under the /home/ directory (usual setup for servers with VestaCP or HestiaCP)?
Setup steps
The solution for adding an external drive as a subdirectory on the main drive is to create a bind mount.
All of the commands below need root access, so we either run them as the root user or prefix each one with sudo.
First, we need to create the directory which will be the mount point on the main drive:
mkdir /home/new_username
Next, we create a directory on the mounted external drive (where the files will actually be stored), for example:
mkdir /mnt/volume_fra1_01/new_username
With both of these in place, we can set the external drive’s directory as the source of the files which will appear under the mount point in the main drive:
mount --bind /mnt/volume_fra1_01/new_username /home/new_username
The first path is the actual location of the files and the second path is the mount point (similar to creating a symbolic link).
From this point, all files placed in /home/new_username/ will be saved on the external drive without using up more space on the main drive as they are being added.
Because we created both directories as root, they are owned by root, and the new user can’t write to them. Once the user account exists, we make it the owner:
chown new_username:new_username /home/new_username
The mount point and the directory on the external drive now show the same directory, so changing the owner through one path changes it for both.
The next and necessary step is to add the mount to the file systems table (fstab) so the mount remains in place after a system restart. We open the fstab file for editing with Vim — vim /etc/fstab — and add the following as a new line:
/mnt/volume_fra1_01/new_username /home/new_username none bind,nofail 0 0
The external drive itself also needs its own line in fstab (some hosting providers add it when the drive is attached), and that line has to come before the bind mount line.
We save the file and exit Vim.
Before restarting, we check fstab for mistakes, because an error in it can stop the server from booting normally:
findmnt --verify
This reads fstab and reports any problems it finds. If there are none, we mount everything in fstab that isn’t mounted yet:
mount -a
If neither command reports an error, the bind mount will be restored after a restart.