Skip to content

Run rclone as a mount helper program - #5594

Merged
ivandeex merged 3 commits into
rclone:masterfrom
ivandeex:pr-mount-helper
Oct 2, 2021
Merged

ivandeex merged 3 commits into
rclone:masterfrom
ivandeex:pr-mount-helper

Conversation

@ivandeex

@ivandeex ivandeex commented Sep 10, 2021 •

Copy link
Copy Markdown
Member

What is the purpose of this change?

This PR will make rclonefs wrappers moot!

The PRedecessor #5593 solve(d) most problems that prevented rclone from running the mount in background i.e. working like a generic unix mount program (sshfs, mount.glusterfs and others can do it out of the box), namely:

  • have the main process prepare for mounting,
  • spawn a background process to handle the mount,
  • wait a little until the mount success or timeout (kill the background process if mount failed)
  • don't block, exit with status code

The last incompatibility left is the way mount arguments are passed.
The core /bin/mount program will (in the basic scenario) get the -t FSTYPE argument then run the /sbin/mount.FSTYPE helper program passing it mount options. Automountd and systemd generally follow the same logic...
But rclone expects GNU-styled flags like --key val while mount options are passed as -o key=val,... (or --opt=...)!
This patch fills the last gap.

Usage

Please symlink the rclone binary to /sbin/mount.rclone (and optionally /usr/bin/rclonefs) and just

  • run something like
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 !

P.S. There was a ticket #2805 requesting the new rclone mount --idle=X flag but now we can do it in a standard way making it mostly moot (except on windows)...

Implementation

This patch introduces new slice fs.Args which is by default a copy of os.Args but can be rewritten if rclone executable was invoked as mount.rclone or rclonefs.
UPDATE
This patch rewrites os.Args inplace in an early init() block if rclone executable was invoked as mount.rclone or rclonefs.

We use os.Args[0] rather than os.Executable to detect the name since the latter might resolve symlink while we want it unresolved.

Mount helpers usually lack the HOME or RCLONE_CONFIG environment variables, so config setup code in fs/config will require fs.Args to find a correct (probably converted) --config FILE running very early (most probably called from init blocks). Consequently the command line transformer also runs super early in the init block of the core fs package and avoids using other packages to prevent import loops. Utility functions IsDaemon and IsMountHelper were put directly in fs for this very reason.

Basically the conversion splits -o argument(s) by comma, replaces _ by - and prepends -- to transform -o options into Cobra flags. Options containing commas or spaces can be wrapped in single or double quotes (any quotes inside outer quotes should be doubled). The parser algorithm is based on the connection string parser with a few command-line specific modifications.

The option syntax additionally includes a few pseudo mount options treated specially:

  • env.NAME=VALUE will set an environment variable for this rclone instance and its children.
    This helps with Automountd and Systemd.mount which don't let set custom environment for mount helpers.
    Typically you will use env.HTTPS_PROXY=proxy.host:3128 or env.HOME=/root
  • command=cmount can be used to run any other command rather than default mount in the helper mode
  • args2env will pass mount options to the daemonized mount helper via RCLONE_X environment variables instead of CLI flags. This allows to hide secrets from ps or pgrep.
  • -o vv... will be transformed into --verbose=N, just for convenience
  • x-systemd.automount, _netdev, nosuid and alike are intended for automountd or systemd and ignored by rclone

Was the change discussed in an issue or in the forum before?

Based on #5593 (this PR includes preliminary patch of #5593 expected to annihilate at merge time)
Fixes #992

Checklist

  • I have read the contribution guidelines.
  • I have added tests for all changes in this PR if appropriate.
  • I have added documentation for the changes if appropriate.
  • All commit messages are in house style.
  • I'm done, this Pull Request is ready for review :-)

Comment thread cmd/cmd.go Resolved
Comment thread cmd/cmd.go Resolved
Comment thread fs/config/config.go Resolved

@ivandeex ivandeex left a comment •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ncw Please see my notes in the code...

@ivandeex

ivandeex commented Sep 11, 2021 •

Copy link
Copy Markdown
Member Author

@ncw
The latest iva build from https://github.com/ivandeex/rclone/releases already includes this patch so you can play right now. I've run the patch on a staging box for a month or two before submitting.
Note the -o args2env mount flag that hides mount options from pgrep -af rclone.

I don't yet call for review as I still need to put user notes from this PR's top post in the mount docs.

When reviewing, please skip the #5593 commit as it will annihilate when we merge #5593.

@ivandeex ivandeex left a comment •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

@ncw

ncw commented Sep 11, 2021

Copy link
Copy Markdown
Member

I had a quick skim of the code and it seems to do the right things :-)

I don't know if you saw my prototype for this? 78ffe37

Not sure there is anything you can learn from that, but I thought I'd put it out there.

It directly modifies os.Args rather than using an fs.Args which is less code to change.

@ivandeex

Copy link
Copy Markdown
Member Author

It directly modifies os.Args rather than using an fs.Args which is less code to change.

I wanted to keep a backup of original arguments just for debugging purposes.
If you think it's an overkill I can drop fs.Args and modify os.Args inplace.

@ivandeex

Copy link
Copy Markdown
Member Author

I don't know if you saw my prototype for this? 78ffe37

Not yet. Noted...

@ncw

ncw commented Sep 12, 2021

Copy link
Copy Markdown
Member

It directly modifies os.Args rather than using an fs.Args which is less code to change.

I wanted to keep a backup of original arguments just for debugging purposes.
If you think it's an overkill I can drop fs.Args and modify os.Args inplace.

I'd say let's modify os.Args directly - we can always put a debug in with the old and the new values or something.

@ivandeex ivandeex modified the milestones: v1.57, v1.58 Sep 26, 2021
@ivandeex
ivandeex force-pushed the pr-mount-helper branch 4 times, most recently from 9eef890 to 949c854 Compare September 30, 2021 10:38
@ivandeex

Copy link
Copy Markdown
Member Author

let's modify os.Args directly - we can always put a debug in with the old and the new values or something.

done

@ncw ncw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this code looks great - thank you.

However it could do with some docs - you wrote some nice ones in the PR - maybe cut and paste into cmd/mountlib/help.go? (Though I think that file is getting too long and we should probably break it out into its own docs page say docs/content/mount.md at some point...)

@ivandeex ivandeex left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two small questions...

Comment thread fs/config.go Outdated

@ivandeex ivandeex Oct 1, 2021 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ncw
Thank you very much for review.

Do you mind if I put mountlib documentation in a separate commit?

Also I'd like to note that the early --verbose=x parser in fs/config.go isn't directly connected with mount helper cli translator and may be split into yet another commit.
Do you agree? Or better keep it squashed?

Thank you

@ivandeex
ivandeex force-pushed the pr-mount-helper branch 2 times, most recently from 0f10ef8 to 506a4a5 Compare October 2, 2021 09:52
@ivandeex ivandeex removed this from the v1.58 milestone Oct 2, 2021
@ivandeex ivandeex added this to the v1.57 milestone Oct 2, 2021
@ivandeex
ivandeex merged commit a065fb2 into rclone:master Oct 2, 2021
ivandeex added a commit that referenced this pull request Oct 2, 2021
@ivandeex
ivandeex deleted the pr-mount-helper branch October 2, 2021 22:44
@ivandeex

ivandeex commented Oct 2, 2021

Copy link
Copy Markdown
Member Author

@ncw @albertony

Please be aware that I documented rclone mount helper in a dedicated commit bd82c19 (a065fb2 on master) partially including the make commanddocs output for your reference.

Also updated the related wiki article https://github.com/rclone/rclone/wiki/rclone-mount-helper-script

@ncw

ncw commented Oct 3, 2021

Copy link
Copy Markdown
Member

Thank looks great, thank you.

@ivandeex

ivandeex commented Oct 3, 2021

Copy link
Copy Markdown
Member Author

Cool.
I'm moving on with submitting FTP stuff accumulated this summer.

@Hugal31 Hugal31 mentioned this pull request May 2, 2022
4 of 5 tasks
lafkpages pushed a commit to lafkpages/rclone that referenced this pull request Jul 20, 2026
lafkpages pushed a commit to lafkpages/rclone that referenced this pull request Jul 20, 2026
lafkpages pushed a commit to lafkpages/rclone that referenced this pull request Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: mount.rclone helper

2 participants