Auto-mount from fstab entry with systemd options

I couldn't get the script that is in the wiki to work for me so I made my own. This script has the following features.

  • It works with systemd ability to generate *.mount files from fstab entries. Making it possible to mount on demand and unmount on idle.
  • It gives you feedback in journalctl, telling you what arguments were used and what rclone said if the mount fails.
  • Also possible to use this script for creating a system.d mount file directly.

Script installation

Simply place the text below in a new file in /usr/local/bin/ and give it the name rclonefspy and give it permissions to execute.

#!/usr/bin/env python3
from os import environ
from time import sleep
import sys
from subprocess import check_call, check_output, CalledProcessError,TimeoutExpired, Popen, PIPE

def write_to_journal(string):
    check_output(['systemd-cat'], input=string.encode('utf-8'))

write_to_journal('rclone-script started')

# Save passed arguments
remote_mount = sys.argv[1]
local_mount = sys.argv[2]
sys_args = sys.argv[4].split(',')

# Must add path to environment in order for rclone mount to work
env = environ.copy()
output_raw = check_output(['whereis', 'rclone'],)
path = output_raw.decode('utf-8').split(' ')[1].removesuffix('/rclone')
env['PATH'] = path

# If path extraction does not work enter desired path here
# env['PATH'] = '/usr/bin'

options = []
discarded_options = []

# Load options for rclone mount to consume
for arg in sys_args:
    if not arg.startswith('rc.'):
        discarded_options.append(arg)
        continue
    options.append(f'--{arg[3:]}')

write_to_journal(f'rclone-script options: {options}')
write_to_journal(f'rclone-script discarded options: {discarded_options}')

# Mount folder, if this doesn't work try with full path for rclone
output = Popen(
    ['rclone', 'mount', *options, remote_mount, local_mount ], env=env,
    stdout=PIPE, stderr=PIPE, close_fds=True, shell=False,
)
try:
    output.wait(1)
except TimeoutExpired as e:
    # Must wait for mount to be seen, in order for systemd not to complain
    write_to_journal('rclone-script rclone mount success')
    for tries in range(1, 20):
        try:
            check_call(['mountpoint', local_mount], env=env)
            write_to_journal(f'rclone-script mount found after {tries} tries')
            break
        except CalledProcessError:
            write_to_journal('rclone-script mount not found, sleeping')
            sleep(0.1)
    else:
        write_to_journal('rclone-script mount not found, not looking anymore')
        sys.exit(1)

    sys.exit(0)
else:
    write_to_journal(f'rclone-script mount failed. ErrMsg: {output.stderr.read().decode("utf-8")}')
    sys.exit(1)

fstab configuration

When creating an fstab entry, include the configuration options you would like rclone to use with the prefix rc. (the dot included).
So the option (if you mount normally) --allow-other would be entered as rc.allow-other in fstab.

An example entry for fstab is below:
gdrive: /mnt/gdrive fuse.rclonefspy noauto,x-systemd.automount,_netdev,x-systemd.mount-timeout=30,x-systemd.idle-timeout=10min,rc.fast-list,rc.drive-export-formats=.link.html,rc.config=/home/user/.config/rclone/rclone.conf,rc.allow-other 0 0

Any rclone option can be used. You most probably want to use rc.allow-other, or you would need root access to see your files, and rc.config at the minimum.

Troubleshooting

You can see feedback from the script with:
journalctl -b | grep rclone

Note

For some reason -- some times -- systemd will insist on trying the mount a second time. It will still mount ok, but an error message will be shown in the logs. It doesn't affect functionality for me but if you know what could be done to fix it, do tell.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.