12 rclone mount helper script
Animosity edited this page 2021-11-23 10:09:38 -05:00

rclone as mount helper (from v1.57 on)

As of v1.57 rclone does not need mount wrappers anymore (see mount documentation).

Please symlink rclone binary (usually /usr/bin/rclone) to /sbin/mount.rclone (and optionally /usr/bin/rclonefs). After that you can run rclone mount in a classic way:

mount sftp1:subdir /mnt/data -t rclone -o vfs_cache_mode=writes,sftp_key_file=/path/to/pem

or create systemd mount units (provide explicit config=...,cache-dir=... as mounts run without HOME !)

# /etc/systemd/system/mnt-data.mount
[Unit]
After=network-online.target
[Mount]
Type=rclone
What=sftp1:subdir
Where=/mnt/data
Options=rw,allow_other,args2env,vfs-cache-mode=writes,config=/etc/rclone.conf,cache-dir=/var/rclone

optionally augmented by systemd automount unit

# /etc/systemd/system/mnt-data.automount
[Unit]
After=network-online.target
Before=remote-fs.target
[Automount]
Where=/mnt/data
TimeoutIdleSec=600
[Install]
WantedBy=multi-user.target

or add in /etc/fstab a line like

sftp1:subdir /mnt/data rclone rw,noauto,nofail,_netdev,x-systemd.automount,args2env,vfs_cache_mode=writes,config=/etc/rclone.conf,cache_dir=/var/cache/rclone 0 0
  • or use classic Automountd

For details refer to https://github.com/rclone/rclone/pull/5594

Known issues

Option --rc does not work together with --daemon or in mount.rclone mode. We are aware of this limitation and work on fixing it. It is tracked by tickets:

rclonefs helper script (old releases before v1.57)

To enable mounting a rclone volume using system mount tools, the following helper script must be named rclonefs and placed in $PATH (e.g. in /usr/local/bin):

#!/bin/bash
remote=$1
mountpoint=$2
shift 2

# Process -o parameters
while getopts :o: opts; do
    case $opts in
        o)
            params=${OPTARG//,/ }
            for param in $params; do
                if [ "$param" == "rw"   ]; then continue; fi
                if [ "$param" == "ro"   ]; then continue; fi
                if [ "$param" == "dev"  ]; then continue; fi
                if [ "$param" == "suid" ]; then continue; fi
                if [ "$param" == "exec" ]; then continue; fi
                if [ "$param" == "auto" ]; then continue; fi
                if [ "$param" == "nodev" ]; then continue; fi
                if [ "$param" == "nosuid" ]; then continue; fi
                if [ "$param" == "noexec" ]; then continue; fi
                if [ "$param" == "noauto" ]; then continue; fi
                if [[ $param == x-systemd.* ]]; then continue; fi
                trans="$trans --$param"
            done
            ;;
        \?)
            echo "Invalid option: -$OPTARG"
            ;;
    esac
done

# exec rclone
trans="$trans $remote $mountpoint"
# NOTE: do not try "mount --daemon" here, it does not play well with systemd automount, use '&'!
# NOTE: mount is suid and ignores pre-set PATHs -> specify explicitely
PATH=$PATH rclone mount $trans </dev/null >/dev/null 2>/dev/null &

# wait until mounting is complete
until grep -q " ${mountpoint// /\\\\040} fuse.rclone " /proc/mounts; do
    sleep 0.5
done

fstab

In /etc/fstab you can add something like:

remote:/path/to/remote/folder	/mnt/rclone	fuse.rclonefs	config=/home/user/.rclone.conf,allow-other,default-permissions,read-only,max-read-ahead=16M	0	0

Obviously, replace /home/user/.rclone.conf with the path to your config and replace remote:/path/to/remote/folder with the name of your remote the path you want to mount.

autofs

You can use the above mount wrapper with autofs, but note you must supply the --allow-non-empty option otherwise autofs will lock up entering the mount (see #3246).

An example config entry for autofs might look like this:

remote	-fstype=fuse.rclonefs,config=/home/$USER/.config/rclone/rclone.conf,allow-other,allow-non-empty	:remote:

systemd

Alternatively if you're a systemd convert and want more control over when rclone mounts itself you can use a mount unit file. You will need to name this file after the Where= directive, eg: mnt-rclone.mount

[Unit]
Description=rclone mount for remote:/path/to/remote/folder
Requires=systemd-networkd.service
Wants=network-online.target
After=network-online.target

[Mount]
What=remote:/path/to/remote/folder
Where=/mnt/rclone
Type=fuse.rclonefs
Options=auto,config=/home/user/.rclone.conf,allow-other,default-permissions,read-only,max-read-ahead=16M
TimeoutSec=30

[Install]
WantedBy=multi-user.target

Another way for mounting through systemd is proposed here.