Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ivandeex/5bb2d26c9e3d01de62285c58cf08f23f to your computer and use it in GitHub Desktop.
Save ivandeex/5bb2d26c9e3d01de62285c58cf08f23f to your computer and use it in GitHub Desktop.
rclone systemd automount
[Unit]
Description=rclone automount mrcry
Before=remote-fs.target
After=network-online.target
[Automount]
Where=/mnt/mysftp
TimeoutIdleSec=30
[Install]
WantedBy=multi-user.target
[Unit]
Description=rclone mount sbsftp
After=network-online.target
[Mount]
Where=/mnt/mysftp
What=mysftp:
Type=rclone
Options=rw,_netdev,gid=911,allow_other,bglog
#!/bin/bash
#set -x
remote="$1"
mountpoint="${2%%/}"
shift 2
bglog=no
method=mount
rclone="/usr/bin/rclone"
args=""
export PATH=/bin:/usr/bin
export RCLONE_CONFIG="/etc/rclone/rclone.conf"
export RCLONE_VERBOSE=0
export RCLONE_CACHE_DIR="/var/cache/rclone"
unset RCLONE_VFS_CACHE_MODE
unset RCLONE_DIR_CACHE_TIME
unset RCLONE_UID
unset RCLONE_GID
unset RCLONE_ALLOW_ROOT
unset RCLONE_ALLOW_OTHER
# Process -o parameters
while getopts :o: opts; do
if [ "$opts" != "o" ]; then
echo "invalid option: -${OPTARG}"
continue
fi
params=${OPTARG//,/ }
for param in $params; do
case "$param" in
# generic mount options
rw|ro|dev|nodev|suid|nosuid|exec|noexec|auto|noauto|user)
continue ;;
# systemd options
_netdev|nofail|x-systemd.*)
continue ;;
# wrapper options
proxy=*)
export http_proxy=${param#*=}
export https_proxy=${param#*=} ;;
config=*)
export RCLONE_CONFIG=${param#*=} ;;
verbose=*)
export RCLONE_VERBOSE=${param#*=} ;;
method=*)
method=${param#*=} ;;
bglog)
bglog=yes ;;
# vfs options
cache-dir=*)
export RCLONE_CACHE_DIR=${param#*=} ;;
vfs-cache-mode=*)
export RCLONE_VFS_CACHE_MODE=${param#*=} ;;
dir-cache-time=*)
export RCLONE_DIR_CACHE_TIME=${param#*=} ;;
# fuse options
uid=*)
export RCLONE_UID=${param#*=} ;;
gid=*)
export RCLONE_GID=${param#*=} ;;
allow_root)
export RCLONE_ALLOW_ROOT=true ;;
allow_other)
export RCLONE_ALLOW_OTHER=true ;;
# other rclone options
*) args="$args --$param" ;;
esac
done
done
if [[ $bglog = yes ]]; then
stamp=$(date '+%y%m%d-%H%M%S')
pid=$$
where=$(basename "$mountpoint")
logfile=/tmp/rclone-${stamp}-${pid}-${where}.log
touch "$logfile"
chmod 666 "$logfile"
# activate verbose background logging
export RCLONE_VERBOSE=3
export RCLONE_LOG_FORMAT=date,time,microseconds
export RCLONE_LOG_FILE=$logfile
# deactivate systemd log flavor in rclone
unset INVOCATION_ID
fi
# NOTE: --daemon hangs under systemd automount, using `&`
# shellcheck disable=SC2086
"$rclone" "$method" "$remote" "$mountpoint" ${args} </dev/null >&/dev/null &
while [[ $(grep -c " ${mountpoint} fuse.rclone " /proc/mounts) = 0 ]]; do
sleep 0.5
done
#!/bin/bash
#set -x
remote="$1"
mountpoint="${2%%/}"
shift 2
bglog=no
method=mount
rclone="/usr/bin/rclone"
args=""
export PATH=/bin:/usr/bin
export RCLONE_CONFIG="/etc/rclone/rclone.conf"
export RCLONE_VERBOSE=0
export RCLONE_CACHE_DIR="/var/cache/rclone"
unset RCLONE_VFS_CACHE_MODE
unset RCLONE_DIR_CACHE_TIME
unset RCLONE_UID
unset RCLONE_GID
unset RCLONE_ALLOW_ROOT
unset RCLONE_ALLOW_OTHER
# Process -o parameters
while getopts :o: opts; do
if [ "$opts" != "o" ]; then
echo "invalid option: -${OPTARG}"
continue
fi
params=${OPTARG//,/ }
for param in $params; do
case "$param" in
# generic mount options
rw|ro|dev|nodev|suid|nosuid|exec|noexec|auto|noauto|user)
continue ;;
# systemd options
_netdev|nofail|x-systemd.*)
continue ;;
# wrapper options
proxy=*)
export http_proxy=${param#*=}
export https_proxy=${param#*=} ;;
config=*)
export RCLONE_CONFIG=${param#*=} ;;
verbose=*)
export RCLONE_VERBOSE=${param#*=} ;;
method=*)
method=${param#*=} ;;
bglog)
bglog=yes ;;
# vfs options
cache-dir=*)
export RCLONE_CACHE_DIR=${param#*=} ;;
vfs-cache-mode=*)
export RCLONE_VFS_CACHE_MODE=${param#*=} ;;
dir-cache-time=*)
export RCLONE_DIR_CACHE_TIME=${param#*=} ;;
# fuse options
uid=*)
export RCLONE_UID=${param#*=} ;;
gid=*)
export RCLONE_GID=${param#*=} ;;
allow_root)
export RCLONE_ALLOW_ROOT=true ;;
allow_other)
export RCLONE_ALLOW_OTHER=true ;;
# other rclone options
*) args="$args --$param" ;;
esac
done
done
if [[ $bglog = yes ]]; then
stamp=$(date '+%y%m%d-%H%M%S')
pid=$$
where=$(basename "$mountpoint")
logfile=/tmp/rclone-${stamp}-${pid}-${where}.log
touch "$logfile"
chmod 666 "$logfile"
# activate verbose background logging
export RCLONE_VERBOSE=3
export RCLONE_LOG_FORMAT=date,time,microseconds
export RCLONE_LOG_FILE=$logfile
# deactivate systemd log flavor in rclone
unset INVOCATION_ID
fi
# NOTE: --daemon hangs under systemd automount, using `&`
# shellcheck disable=SC2086
"$rclone" "$method" "$remote" "$mountpoint" ${args} </dev/null >&/dev/null &
while [[ $(grep -c " ${mountpoint} fuse.rclone " /proc/mounts) = 0 ]]; do
sleep 0.5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment