Rclone mount on startup with systemd

I’m trying to mount my googledrive on my ubuntu server. Mounting in the console does work, but I want it to mount automaticly after a reboot. So I tried creating a rclonemount.service in /etc/systemd/system/ :

[Unit]
Description=rclonemount
Wants=network-online.target
After=network-online.target

[Service]
TimeoutStartSec=10
ExecStart=/usr/sbin/rclone mount gdriveencrypted: /media/gdrivemount/ --config /root/.rclone.conf --allow-other --no-modtime --dir-cache-time 60m

[Install]
WantedBy=multi-user.target

That doesn’t quite work, I get the following

qp:~# systemctl status rclonemount
● rclonemount.service - rclonemount
   Loaded: loaded (/etc/systemd/system/rclonemount.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Fri 2016-12-02 11:13:19 CET; 27s ago
  Process: 1222 ExecStart=/usr/sbin/rclone mount gdriveencrypted: /media/gdrivemount/ --config /root/.rc
 Main PID: 1222 (code=exited, status=1/FAILURE)

Dec 02 11:13:17 qp systemd[1]: Started rclonemount.
Dec 02 11:13:18 qp rclone[1222]: 2016/12/02 11:13:18 Couldn't find home directory or read HOME environment variable.
Dec 02 11:13:18 qp rclone[1222]: 2016/12/02 11:13:18 Defaulting to storing config in current directory.
Dec 02 11:13:18 qp rclone[1222]: 2016/12/02 11:13:18 Use -config flag to workaround.
Dec 02 11:13:18 qp rclone[1222]: 2016/12/02 11:13:18 Error was: user: Current not implemented on linux/amd64
Dec 02 11:13:19 qp rclone[1222]: 2016/12/02 11:13:19 Failed to create file system for "gdriveencrypted:": failed to make remote 
Dec 02 11:13:19 qp systemd[1]: rclonemount.service: Main process exited, code=exited, status=1/FAILURE
Dec 02 11:13:19 qp systemd[1]: rclonemount.service: Unit entered failed state.
Dec 02 11:13:19 qp systemd[1]: rclonemount.service: Failed with result 'exit-code'.

I’m not that linux-savvy, so any help is appreciated.

You need to give it a path to a config file with --config /path/to/config.file

Not sure what user that you’ll be starting it as, maybe you should make an rclone user to do this or do it under your own user. You do that by putting User=XXX in the [Service] section I think.

Isn’t there the necessary –config parameter?

i added a user, now the error log is down to

● rclonemount.service - rclonemount
   Loaded: loaded (/etc/systemd/system/rclonemount.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Fri 2016-12-02 15:49:02 CET; 2h 11min ago
 Main PID: 1235 (code=exited, status=1/FAILURE)

Dec 02 15:49:01 qp systemd[1]: Started rclonemount.
Dec 02 15:49:02 qp rclone[1235]: 2016/12/02 15:49:02 Failed to create file system for "gdriveencrypted:": failed to make remote "remotegdrive:encrypted" to wrap: couldn't read info about Drive: Get https://www.googleapis.com/drive/v2/about?alt=json: Post https://accounts.google.com/o/oauth2/token: dial tcp: lookup accounts.google.com on [::1]:53: read udp [::1]:55031->[::1]:53: read: connection refused
Dec 02 15:49:02 qp systemd[1]: rclonemount.service: Main process exited, code=exited, status=1/FAILURE
Dec 02 15:49:02 qp systemd[1]: rclonemount.service: Unit entered failed state.
Dec 02 15:49:02 qp systemd[1]: rclonemount.service: Failed with result 'exit-code'.

If I execute the same command rclone mount gdriveencrypted: /media/gdrivemount/ --config /root/.rclone.conf --allow-other --no-modtime --dir-cache-time 60m in a terminal, the mount works, so the google token should work.

Yes… Interesting that it complained about it. Must be something to do with the environment of systemd.

That looks like it is having trouble speaking to the name resolver - the udp reads on port 53. Not sure why that should be. It is trying to connect to [::1]:53 which is localhost port 53 in IPv6 speak. Are you running a nameserver on localhost?

not that I know of?

I also tried a fstab mount like described in this post https://github.com/ncw/rclone/issues/727#issuecomment-247061268. I get no errors but also no mount.

@mistermin were you able to fix this and automount via systemd ?

No, I didn’t get the automount with systemd working.

I have a pretty dumb workaround: I started a tmux session and mounted the drive there. It doesnt autostart after a boot but at least it stays mounted even when i close the ssh connection to the server.

If anyone has a better, working alternative, please comment :wink:

ok, I got it working

I was able to start it manually so my suspicion was that is was a timing issue. In your config there is a parameter TimeoutStartSec=10, but that isn’t doing what you expect it would do.
Configures the time to wait for start-up. If a daemon service does not signal start-up completion within the configured time, the service will be considered failed and will be shut down again

So it is not waiting 10 seconds, but it is considered failed when not started within 10 seconds

With systemd-analyze plot > plot.svg (image file can be opened in browser) I analyzed the systemd startup order and noticed that my rclonemount.service was very short after the network. so I pushed it back by making it depended of multi-user.target.
That seemed to do the trick

Still a bit dirty though I think, maybe the rclone mount command could get a wait command-line parameter before the actual mount is created

My systemd file rclonemount.service

[Unit]
Description=rclonemount
After=multi-user.target

[Service]
Type=simple
User=misteriks
ExecStart=/home/misteriks/Projects/Proj1/bin/rclone mount eacd: /home/misteriks/acd --config /home/misteriks/.rclone.conf --allow-other --max-read-ahead 200M --dir-cache-time 5m --acd-templink-threshold 0 --bwlimit 0 --checkers 32 --low-level-retries 1 --quiet --stats 0 --timeout 30s
ExecStop=fusermount -u /home/misteriks/acd
Restart=on-abort

[Install]

I’d love a bit for the docs about this, either as a PR or an email!

I believe the most important bits are in my post. Anything specific you are looking for?

Some text I can drop in the mount docs would be perfect!

@misteriks
Make sure you have service enabled : https://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/
systemctl enable systemd-networkd-wait-online.service

Then After=network-online.target will work

1 Like

Thank you so much for this. This is what worked for me using LibreELEC (Krypton).

[Unit]
Description=rclonemount
Requires=network-online.service
After=network-online.service

[Service]
Type=simple
ExecStartPre=/bin/mkdir -p /storage/remote
ExecStart=/storage/.config/rclone/rclone mount acd: /storage/remote --config /storage/.config/rclone/rclone.conf --checkers 16 --dir-cache-time 30m --quiet --read-only
ExecStop=/usr/bin/fusermount -u /storage/remote
Restart=on-abort

[Install]
WantedBy=kodi.target

Save it as: /storage/.config/system.d/rclonemount.service

3 Likes

How is it working on libreelec?

Sorry to bring back this subject but if we create a new user what permission it need to mount the remote on /mnt/***** ?

I can start the service by using systemctl start but it didn’t start at reboot :

[Unit]
Description=rclonemount
After=network-online.target

[Service]
Type=simple
User=root
ExecStart=/usr/sbin/rclone mount --config /home/user/.config/rclone/rclone.conf --allow-non-empty --allow-other --read-only --buffer-size 1G --timeout 5s --contimeout 5s --acd-templink-threshold 0 -v …
ExecStop=/bin/fusermount -u /mnt/encrypt
Restart=on-abort

[Install]
WantedBy=multi-user.target

This is the easiest way to daemonize a process from an SSH terminal and/or from a shell script:

( rclone mount --<options> <src> <dst> &)&

It will immediately echo back that the process has completed, but the backgrounded sub-shell will continue even when you log out. You can verify by logging out and then back in:

ps -e | grep rclone

The parent process with be pid 1, just as if you had started it from systemd.

If you want to start on boot, add this to crontab:

@reboot ( /path/to/rclone mount --<options> <src> <dst> &)&

2 Likes

I just use the @reboot in cron as doing the network.online never worked for me. I didn’t care to spend much time to troubleshoot it.

> felix@plex:~/scripts$ crontab -l | grep media_mount
> @reboot /home/felix/scripts/media_mount
> felix@plex:~/scripts$ cat media_mount
> # Mount GD Fuse
> #/usr/bin/google-drive-ocamlfuse /GD -o allow_other,auto_cache -headless -debug >> /home/felix/.gdfuse/default/mount.log 2>&1 &
> /usr/bin/google-drive-ocamlfuse /GD -o allow_other,auto_cache -verbose >> /home/felix/.gdfuse/default/mount.log 2>&1 &
> sleep 2

> # Mount the 3 Directories via rclone for the encrypt
> /usr/bin/rclone mount \
> --allow-other \
> --default-permissions \
> --uid 1000 \
> --gid 1000 \
> --umask 002 \
> --acd-templink-threshold 0 \
> --buffer-size 100M \
> --syslog \
> --stats 1m \
> -v \
> media: /media &

> # Wait a sec
> sleep 2
> /usr/bin/unionfs-fuse -o cow,allow_other,direct_io,auto_cache,sync_read /local/movies=RW:/media/Movies=RO /Movies
> /usr/bin/unionfs-fuse -o cow,allow_other,direct_io,auto_cache,sync_read /local/tv=RW:/media/TV=RO /TV

> # Prime the directories
> sleep 2
> cd /Movies
> ls -alR | wc -l
> cd /TV
> ls -alR | wc -l

I just a run a checker ever 5 minutes to validate nothing is bad. Little clunky but it does what it needs to do:

*/5 * * * * /home/felix/scripts/check_GD
felix@plex:~/scripts$ cat check_GD
#!/bin/bash
if grep -qs '/local' /proc/mounts; then
	sleep 1
	else
	echo "It's not mounted." | mail -s "/local not mounted" me@gmail.com
	exit 1
fi

if grep -qs '/GD' /proc/mounts; then
    	#echo "It's mounted."
	FILE=/GD/.exists_and_writeable

	if [[ -r $FILE && -w $FILE ]]
	then
		#All is good
		#echo "File exists and is writeable"
		exit
	else
		echo "/GD is not writeable" | mail -s "/GD not writeable" me@gmail.com
        	/home/felix/scripts/media_unmount
        	sleep 10
        	/home/felix/scripts/media_mount
	fi
else
    	echo "It's not mounted." | mail -s "/GD not mounted" me@gmail.com
	/home/felix/scripts/media_unmount
	sleep 10
	/home/felix/scripts/media_mount
fi

I also would recommend setting the –syslog parameter. On my raspberry pi fuse claims that in the /etc.fuse.conf the user_allow_other option needs to be enabled if you use –allow-other parameter. And maybe you get some more output there.

Also use the -vv parameter for debug logging.

If you use the -vv flag, rclone will produce Error, Notice, Info and Debug messages.

I have some problems too mounting the drive with rclone on startup. When I start the service manually sudo systemctl start mount-rclone.service the drive mounts successfully. But at startup I got errors in the systemlog. Guess it’s because the wlan is not up at this time. Need to check in the next days.

Any news on this topic?

I am trying to do the same and since you said it needs to wait for the wifi, I did it like @misteriks in this topic and wait for multi-user.target. However, this is my error message in the logs:

2017/09/09 09:31:08 Failed to create file system for “onedrive:/”: failed to get root: Get https:// api.onedrive .com/v1.0/drive/root:/: Post https:// login.live. com/oauth20_token.srf: dial tcp: lookup login. live .com on [::1]:53: read udp [::1]:43541->[::1]:53: read: connection refused

(I had to separate the links in the log line above, new users are only allowed two links per post :stuck_out_tongue:)

And my systemd file looks like this now:

[Unit]
Description=rclone
After=multi-user.target

[Service]
Type=simple
User=pcmciakai
ExecStartPre=/bin/mkdir -p /home/pcmciakai/OneDrive
ExecStart=/usr/bin/rclone mount onedrive: /home/pcmciakai/OneDrive --config /home/pcmciakai/.config/rclone/rclone.conf --low-level-retries 1 --quiet --stats 0 --timeout 30s
ExecStop=/usr/bin/fusermount -u /home/pcmciakai/OneDrive
ExecStopPost=/bin/rmdir /home/pcmciakai/OneDrive
Restart=on-abort

[Install]
WantedBy=multi-user.target