Skip to content

Document autorun - #4972

Merged
albertony merged 2 commits into
rclone:masterfrom
albertony:doc_autorun
Apr 13, 2021
Merged

albertony merged 2 commits into
rclone:masterfrom
albertony:doc_autorun

Conversation

@albertony

@albertony albertony commented Jan 27, 2021 •

Copy link
Copy Markdown
Contributor

What is the purpose of this change?

Documenting how to set up autostart of rclone, e.g. for mount or serve commands.

Mostly focus on Windows.

TODO:

  • Currently appended to the install page. Don't know if it should be on a separate page.
  • Windows:
    • Uncertainties around user accounts and mounts, see discussions in this PR comments!
    • Anyone with experience with running rclone as service or scheduled task? What works good/bad?
  • Linux:
    • Did included a section for Linux, but could need some extension if anyone with experience wants to fill in...
  • macOS
    • Nothing about mac included for now...

Sources:

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

Related to this PR: #4953

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 :-)

@ncw

ncw commented Jan 28, 2021

Copy link
Copy Markdown
Member

I noted a stray Syncthing in the docs :-)

The Linux docs look a bit thin? Maybe we should ask @animosity22 to contribute a setting up rclone with systemd section?

Advice on how to avoid multiple rclone's running with cron is useful too.

@ivandeex

ivandeex commented Jan 28, 2021 •

Copy link
Copy Markdown
Member

I encountered that rclone mounts on Windows tend to have rather long command line which doesn't fit in the slim field of nssm dialog, but its environment tab is wide enough, so I prefer

RCLONE_OPT1=val1
RCLONE_OPT2=val2
...

to rclone --opt1=val1 --opt2=val2 ........... remote:

@albertony

Copy link
Copy Markdown
Contributor Author

Good tip, @ivandeex. I can include a note about this. I don't know if we should include even more details on how to configure it with nssm? You seem to have experience with rclone mount and nssm, are you running it as SYSTEM user then? When installing as "self-contained" service with powershell new-service, I were not able to make it create mounts that I could access without running the service as system user.

Same with task scheduler, except with the "Run only when user is logged on" mode where it runs in my interactive session. With the "Run whether user is logged on or not" it will always run elevated regardless of the "Run with highest privileges" option, and I can not access the mount from my user - not even from an elevated command prompt.

@albertony

Copy link
Copy Markdown
Contributor Author

I noted a stray Syncthing in the docs :-)

No, impossible! ... after a quick amend and force-push :)

It was in the linux description, which I did not spend much time on. The Synchting guide had what seemed to be good descriptions for Linux and macOS as well, but I was kind of hoping someone else were willing to verify/adapt them to rclone (or write from scratch).. Syncthing has only the "syncing forever in background" mode, while rclone typically will do periodic syncing on schedule, and then also have mounting and all the other possible modes, so the description may not fit exactly.

@ivandeex

ivandeex commented Jan 29, 2021 •

Copy link
Copy Markdown
Member

@albertony

are you running it as SYSTEM user then?

No, I'm running it with my interactive Windows user.
My nssm configuration normally looks like this:

  • Application
    • Path: C:\path\to\rclone.exe
    • Startup directory: C:\temp
    • Arguments: mount --volname Volume remote: X: (that's it)
  • Details
    • Display name: Mount-Name
    • Startup: Automatic (delayed start)
  • Logon
    • This user: .\ivandeex <-----
    • Password: super secret
  • I/O
    • Output: C:\path\to\this-mount.log
    • Error: C:\path\to\this-mount.log (same file)
  • File rotation
    • Overwrite, Rotate, Rotate while service is running: Yes
    • Restrict rotation to file bigger than 1000000 bytes
  • Environment
RCLONE_CONFIG=C:\path\to\rclone.conf
RCLONE_VERBOSE=1
RCLONE_VFS_CACHE_MODE=full
RCLONE_CACHE_DIR=C:\path\to\cache
... vfs and backend options ...

Currently I configure manually on Windows. I don't use PowerShell commandlets for that.
One day I will automate it with an Ansible role (Linux role is already at https://github.com/ivansible)

If I need to restart a mount, I use Task Manager (the Services tab).

When installing as "self-contained" service with powershell new-service, I were not able to make it create mounts that I could access without running the service as system user.

It's because issue #3216 is still unresolved.
Someone will have to make rclone implement #3216 (comment), based on #3216 (comment).
I'll pick it sometime in 2021 H2 unless someone else fixes it.

For now you should run Windows mounts as a normal user.

@albertony

Copy link
Copy Markdown
Contributor Author

No, I'm running it with my interactive Windows user.

OK. It will run in session 0 as normal windows services I guess, so hidden, but does the rclone process run elevated with this setup?

My nssm configuration normally looks like this:

Thanks for the details!

It's because issue #3216 is still unresolved.

I'm not sure, but I think this is a somewhat different case. My case was that I could never find the mounts at all. Rclone would always run elevated, don't know if that is part of the reason.

Someone will have to make rclone implement #3216 (comment)

Either that, or #3340, are not these two different ways to solving the same thing? And with #3340 it is actually possible without changes in rclone - but it takes quite some advanced registry "hacking"?

And isn't that primarily to be administrated as part of the WinFsp.Launcher, with its network provider allowing you to use "net use" instead of "rclone mount" commands? From my experience, even when installing as stand-alone service with New-Service, it will use the WinFsp.Launcher service to perform the mount (the DefineDosDevice API call), because WinFsp has code that checks if running as service AND not LocalSystem THEN call WinFsp.Launcher via pipe api. Or does also being fully integrated with WinFsp.Launcher have additional handling of user/permissions? (the issue mentions "I think I should add an -o username=USERNAME", and that has been done now, so that and #3340 makes me a bit confused what the current state of it is).

@ivandeex

Copy link
Copy Markdown
Member

No, I'm running it with my interactive Windows user.

OK. It will run in session 0 as normal windows services I guess, so hidden, but does the rclone process run elevated with this setup?

No, it's not elevated.

My case was that I could never find the mounts at all. Rclone would always run elevated, don't know if that is part of the reason.

If I change account to SYSTEM or run rclone as elevated user, I hit the same issue.
So yes, it is the reason IMO

Someone will have to make rclone implement #3216 (comment)

Either that, or #3340, are not these two different ways to solving the same thing? And with #3340 it is actually possible without changes in rclone - but it takes quite some advanced registry "hacking"?

I see it this way (I may be wrong):
WinFSP (and its launcher) is a bridge between rclone and Windows "mount subsystem", which is full of legacy I'd prefer to not dig in if possible 😥
WinFSP provides a few solutions (different ways) to help with mounts in various scenarios:

@albertony

Copy link
Copy Markdown
Contributor Author

I just did a test with NSSM: It starts rclone elevated. And surprisingly (to me) WinFsp still detects it as running as a service, and uses the WinFsp.Launcher as I've described. After some reading it seems WinFsp is looking for process token WinServiceSid to detect if running as service, and rclone process has this token even when not being started directly as service but started as subprocess of nssm.exe. Starting from task scheduler it does not get this token (but instead WinBatchSid), and neither from command prompt (but instead WinInteractiveSid).

This means it uses WinFsp.Launcher, even when started from NSSM, which means the mounting (DefineDosDevice api call) is performed as SYSTEM user. So I can see the mounted drive as my user.

Starting identical rclone mount command from task scheduler with "Run whether user is logged on or not", it also is started elevated and process seems identical to when started as service - but it will not use WinFsp.Launcher, and therefore perform the mount as the configured task user - elevated - and I can not see the mounted drive. I think that is the explanation of the difference, which I found extremely strange and annoying when comparing the two methods.

No, it's not elevated.

Are you sure? If you stop the WinFsp.Launcher prosess, does your rclone mount from nssm still work?

If I change account to SYSTEM or run rclone as elevated user, I hit the same issue.

When you tested rclone as elevated user, was that interactive then? I can't see anything to control this from nssm..

@ivandeex

ivandeex commented Jan 29, 2021 •

Copy link
Copy Markdown
Member

Are you sure? If you stop the WinFsp.Launcher prosess, does your rclone mount from nssm still work?

It works with WinFsp.Launcher service running or stopped.
Tried a minute ago: stop WinFsp.Lanucher, restart rclone nssm service, check mount and logs,
then start WinFsp.Launcher, restart rclone, check mount

Windows 8.1 Pro x64
WinFSP 2020.1
HKLM\SOFTWARE\Wow6432Node\WinFsp exists but is empty

When you tested rclone as elevated user, was that interactive then? I can't see anything to control this from nssm..

SYSTEM user - on the nssm Log on tab switched to Local System account (I remember I didn't enable interactive desktop in nssm)
Elevated user - ran rclone from elevated powershell console (it was more than a year ago, I forgot details)

EDIT added more details

@albertony

Copy link
Copy Markdown
Contributor Author

It works with WinFsp.Launcher service running or stopped.

Strange, it is definitely opposite in my setup! Are you also on WinFsp version 2020.2?

HKLM\SOFTWARE\Wow6432Node\WinFsp exists but is empty

In my case too, or actually it contains subkeys for built-in memfs, but nothing rclone related. But if I am correct, then an entry here is (only) needed when you want the service to be controlled by WinFsp, and then also get the "net use" integration etc. But the pipe api from WinFsp library (rclone) to the WinFsp.Launcher service to request it to perform the mounting (DefineDosDevice) is independent of the WinFsp registry configuration. It only checks that: 1) Process has the service token on it. 2) Is not running as SYSTEM user. And in my case both are true. The rclone.exe gets the service token, also when started by nssm.

@ivandeex

Copy link
Copy Markdown
Member

It works with WinFsp.Launcher service running or stopped.

Strange, it is definitely opposite in my setup! Are you also on WinFsp version 2020.2?

Only 2020.1, upgraded last year.

HKLM\SOFTWARE\Wow6432Node\WinFsp exists but is empty

In my case too, or actually it contains subkeys for built-in memfs, but nothing rclone related. But if I am correct, then an entry here is (only) needed when you want the service to be controlled by WinFsp [...snip...]

Probably it was created by WinFSP installer. I never edited it, only customized nssm services.
Maybe you have different Windows? Mine is 8.1 Pro.

@ivandeex

Copy link
Copy Markdown
Member

It only checks that: 1) Process has the service token on it. 2) Is not running as SYSTEM user. And in my case both are true. The rclone.exe gets the service token, also when started by nssm.

I didn't dig this deep.

@albertony

albertony commented Jan 30, 2021 •

Copy link
Copy Markdown
Contributor Author

Only 2020.1

No changes between 2020.1 and 2020.2 relevant for this case, as far as I can see. I'm considering installing 2020.1 to verify, though..

Maybe you have different Windows? Mine is 8.1 Pro.

Yes, I have Win 10 (1909). If WinFsp version is not the cause, then this should mean that in Win8.1 rclone does not get the service token, as shown by the following screenshot mix from SysInternals Process Explorer:

rclone

Edit: And perhaps also the difference of elevated service process or not is due to Windows versions.

@ivandeex

ivandeex commented Feb 5, 2021

Copy link
Copy Markdown
Member

@albertony
I would like to check the "token", "elevation" and "session" thingies on my side. I'm not good at Windows. What would you like me to look at (besides process explorer)?

@albertony

albertony commented Feb 6, 2021 •

Copy link
Copy Markdown
Contributor Author

@albertony
I would like to check the "token", "elevation" and "session" thingies on my side. I'm not good at Windows. What would you like me to look at (besides process explorer)?

Great! I was hoping to write a guide that describes exactly how to get it to work, but if there is so much differences between versions etc it is going to be hard - must either write much shorter/vaguer, or separate guides for different versions etc... More work than I was planning to invest..

These process/security internals are hard to grasp, so I am by no means an expert myself. Process Explorer is a great tool to get some insight into Windows! I don't know if you meant you had checked it or not: But if you are willing to check: Find nssm, and the rclone beneath it. On both of them: Double click to show properties. Go to Security tab. Find an entry for group name starting with "Mandatory Label" and flag "Integrity". The last part of the name should be Low, Medium or High, where High is an indication of it running with full administrative privileges ("elevated"). Then see if there is an entry for group "NT AUTHORITY\SERVICE" and flag "Mandatory". This is the "token" given to processes running as services. Applications you start yourself will get the interactive token (NT AUTHORITY\INTERACTIVE), and task scheduler uses batch token (NT AUTHORITY\BATCH). I am wondering if rclone is elevated or not, and if it has the service token or not. It would also be interesting to know the same for the nssm process.

From what I understand WinFsp checks the presence of "NT AUTHORITY\SERVICE" in the process, in our case rclone, and when this token is present it assumes it is running as service, and does not separate between it being running as a stand-alone service (like when I just installed it with New-Service command in PowerShell) or as part of the WinFsp.Launcher system.

@ivandeex

ivandeex commented Feb 9, 2021

Copy link
Copy Markdown
Member

I don't know if you meant you had checked it or not

Checked just now.

Process explorer

procexp-rclone

nssm process

nssm-props

rclone process

rclone-props

Windows version

win8-ver

Notes:

  • "Обязательная метка" (rus) == "Mandatory label" (eng)
  • "Высокий обязательный уровень" (cryptic) == "High mandatory level" (plain)
  • "СЛУЖБА" == "SERVICE"

@ivandeex

ivandeex commented Feb 9, 2021

Copy link
Copy Markdown
Member

Ah...
WinFSP version:

winfsp-ver

rclone version
rclone v1.54-iva01
- os/arch: windows/amd64
- go version: go1.13.4

(which is in fact vanilla rclone 1.54.0)

and this stuff works for me.

@albertony

albertony commented Feb 10, 2021 •

Copy link
Copy Markdown
Contributor Author

Thank you for that info, I really appreciate it! Glad you included the english translations at the end! ;)

So, I conclude that your nssm and rclone processes both:

  • Are elevated (High Integrity Level)
  • Has service token (NT AUTHORITY\SERVICE)
  • Run as your personal user (not the built-in SYSTEM / Local System account)

That matches what I see in my test!

But in my test it works only when WinFsp.Launcher service is running (because of this this). When I stop WinFsp.Launcher the mount no longer works: The nssm-based service I created for rclone mount is running, but mount drive does not appear, rclone log file contains "Mount failed", and I see in process explorer that rclone.exe is being restarted by nssm.

I have WinFsp 2020.2. I think I tried to see through the changes between .1 and .2 without finding anything that stood out as relevant. I may have to uninstall and install 2020.1 to try it out, I can't think of anything else that could explain the difference..

@albertony

Copy link
Copy Markdown
Contributor Author

I may have to uninstall and install 2020.1 to try it out, I can't think of anything else that could explain the difference..

Just did. No difference between WinFsp 2020.1 and 2020.2.

@ncw

ncw commented Mar 23, 2021

Copy link
Copy Markdown
Member

Is this OK to merge now? Merge window for 1.55 approaching fast ;-)

@albertony

Copy link
Copy Markdown
Contributor Author

Is this OK to merge now? Merge window for 1.55 approaching fast ;-)

No! 😞

I'm not sure it is correct as is, and the observed differences in behavior without finding a good reason makes me rethink this entire doc, e.g. if it must be rewritten with much less detail, I'm not sure...

@albertony

albertony commented Apr 11, 2021 •

Copy link
Copy Markdown
Contributor Author

Is this OK to merge now? Merge window for 1.55 approaching fast ;-)

No! 😞

Changed my mind!

But:

I decided to change direction a bit, and deleted the detailed step-by-step guide for scheduled task, and some other details, and most importantly any sections affected by the uncertainty around user account, elevation, service token discussed above!

My thinking is that I will not be able to complete the detail level as it was, and to clear out all uncertainties, within reasonable amount of time! And then there would be a lot of, hopefully, useful documentation that would also just rot in that big draft...

So, if we can make this PR into something that we want to merge on short term, then a new PR can be created to with intention of getting the uncertainties cleared out and fill in more details as needed...

@ncw, @ivandeex : What do you think? I have pushed a version now that should be ready for review. I will read through it a couple of times more myself, so might push some small updates still. Also, it is currently spread over several commits (to be able to easily see the removed sections etc) that I will squash later. Edit: Oh, and I added one commit which has not to do with autorun, but is an update to the mount doc related to user accounts. The topic is quite related to the autorun part, though, and links to it, so I just included it here instead of as a new PR.

@albertony
albertony marked this pull request as ready for review April 11, 2021 20:28
@albertony
albertony requested a review from ncw April 11, 2021 20:28
@albertony
albertony force-pushed the doc_autorun branch 2 times, most recently from 5e53f64 to e187c1e Compare April 11, 2021 21:18
@ncw

ncw commented Apr 12, 2021

Copy link
Copy Markdown
Member

So, if we can make this PR into something that we want to merge on short term, then a new PR can be created to with intention of getting the uncertainties cleared out and fill in more details as needed...

Yes that sounds like a good plan. I'd rather have something now and fill in gaps later.

Will take a look now

@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 those docs look very useful.

The Linux ones need filling out but I'm happy to merge this now.

Can you squash the commits before merging?

@albertony

Copy link
Copy Markdown
Contributor Author

Great!

Can you squash the commits before merging?

Done!

@ncw

ncw commented Apr 12, 2021

Copy link
Copy Markdown
Member

Merge when ready :-)

@albertony
albertony merged commit 6366d3d into rclone:master Apr 13, 2021
@albertony
albertony deleted the doc_autorun branch April 13, 2021 20:33
@ivandeex

Copy link
Copy Markdown
Member

@albertony
There is a trick with github.
If you squash to one commit, it adds PR number to the commit message.
This allows to easily find full discussion thread from the commit.
But if you don't squash, github retains original messages and the link gets lost (unless you manually amend in advance).

@albertony

Copy link
Copy Markdown
Contributor Author

Ah, ok, sorry about that - and thanks for the tip.

Actually, I kind of knew about it... except I thought the commit messages should rather be "clean", for changelogs etc, so I have even removed the PR number when doing squash merges 🤦

If I click on the commit, there is a link to the PR in small font just below the commit message, but that is not enough I take it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants