Repository navigation
mountlib: fix --daemon for automount and systemd mount - #5593
Conversation
|
Manual unit test:
|
b4e1330 to
04da3b5
Compare
ncw
left a comment
There was a problem hiding this comment.
I put an inline note about docs for pid.
We probably need some docs about automount but they should go in #5594 probably which fixes it properly.
If you can add the small doc I'm happy for you to merge - it should be a great improvement.
|
Hey, @ncw just pointed me at your fix. |
|
this fixes my issue ! auto.master.debug auto.rclone.rebex running: accessing the mountpoint: rclone log: Note that we don't see the |
With #5594 you will forget rclonefsdev |
Interesting...
I was trying to hack your rclone.mount, using |
|
Possible but
|
|
Thanks. So what should I do ? Create a new ticket ? |
|
Looks more suitable for support ticket on the forum |
b740d50 to
611b5de
Compare
This option is useful to troubleshoot `rclone mount --daemon`
Current way of checking whether mountpoint has been already mounted (directory list) can result in race if rclone runs under Automount (classic or systemd). This patch adopts Linux ProcFS for the check. Note that mountpoint is considered empty if it's tagged as "mounted" by autofs. Also ProcFS is used to check whether rclone mount was successful (ie. tagged by a string containing "rclone"). On macOS/BSD where ProcFS is unavailable the old method is still used. This patch also moves a few utility functions unchanged to utils.go: CheckOverlap, CheckAllowings, SetVolumeName.
…ne#5593 This patch will: - add --daemon-wait flag to control the time to wait for background mount - remove dependency on sevlyar/go-daemon and implement backgrounding directly - avoid setsid during backgrounding as it can result in race under Automount - provide a fallback PATH to correctly run `fusermount` under systemd as it runs mount units without standard environment variables - correctly handle ^C pressed while background process is being setting up
This option is useful to troubleshoot `rclone mount --daemon`
Current way of checking whether mountpoint has been already mounted (directory list) can result in race if rclone runs under Automount (classic or systemd). This patch adopts Linux ProcFS for the check. Note that mountpoint is considered empty if it's tagged as "mounted" by autofs. Also ProcFS is used to check whether rclone mount was successful (ie. tagged by a string containing "rclone"). On macOS/BSD where ProcFS is unavailable the old method is still used. This patch also moves a few utility functions unchanged to utils.go: CheckOverlap, CheckAllowings, SetVolumeName.
|
Merged! |
This option is useful to troubleshoot `rclone mount --daemon`
Current way of checking whether mountpoint has been already mounted (directory list) can result in race if rclone runs under Automount (classic or systemd). This patch adopts Linux ProcFS for the check. Note that mountpoint is considered empty if it's tagged as "mounted" by autofs. Also ProcFS is used to check whether rclone mount was successful (ie. tagged by a string containing "rclone"). On macOS/BSD where ProcFS is unavailable the old method is still used. This patch also moves a few utility functions unchanged to utils.go: CheckOverlap, CheckAllowings, SetVolumeName.
…ne#5593 This patch will: - add --daemon-wait flag to control the time to wait for background mount - remove dependency on sevlyar/go-daemon and implement backgrounding directly - avoid setsid during backgrounding as it can result in race under Automount - provide a fallback PATH to correctly run `fusermount` under systemd as it runs mount units without standard environment variables - correctly handle ^C pressed while background process is being setting up
What is the purpose of this change?
rclone mountworks fine under Windows, Linux or BSD as a standalone process that can be cancelled by ^C when not needed anymore.rclone mount --daemonsomewhat works but is incompatible with automountd or systemd mount due to reasons explained below.Users used to mitigate incompatibilities by hacks in the rclonefs wrapper (see #3246 (comment) or wiki/rclone-mount-helper-script).
This pull request removes the need in hacks.
However the wrapper is still needed to translate
mount -o opt=val-style options into rclone flags (but see #5594).Disclaimer: this PR does not change
rclone mountbehavior on Windows. We simply don't need to as--daemonis no-op on this OS and automount is not present, it's a Unix only notion. And it's a Unix only PR.Implementation
New Mountpoint Checks
Current way of checking whether mount point is already covered by a mount is badly broken. It lists the directory to see if it contains files. First, an empty mount will be wrongly treated as no mount. Next, if this mount point is watched by Automountd or systemd.autofs then depending on the rclone PGID such access can trigger autofs kernel hooks. If rclone itself runs under autofs control, this can result in a race. The 2nd commit of this PR is dedicated to fixing this.
#992 (comment) noted possible fixes, in particular watching kernel mount uevents, a stat trick and
/proc/mounts(procfs). The uevent method can only find state changes but cannot tell if a directory was mounted before rclone started. The stat method suffers from the same autofs race. The procfs method uses one of major Linux kernel APIs (they are syscalls, procfs/sysfs, device ioctl, net sockets) and requires manual polling. It allows to query what software is responsible for a mount: autofs, kernel, rclone, fuse and so on. It's free from the race as it doesn't touch the mountpoint. Unfortunately it's only applicable on Linux.This PR adopts procfs method on Linux for checking whether mountpoint is already mounted by anyone or not (the
CheckMountEmptymethod). Additionally it polls procfs to check whether mountpoint was mounted by rclone (in theCheckMountReadymethod).I'm not a BSD or macOS guru so I left them as is. That's out of scope for this PR. The
CheckMountEmptymethod on BSD/macOS will still list directory. TheCheckMountReadywill just do nothing as I don't know how to identify mount source without procfs. Consequently I don't recomment to use rclone mount with Automountd on BSD unless you know what you are doing.Backgrounding
Golang does not natively support Unix
forkdue to its problems with goroutines. Currently rclone uses thesevlyar/go-daemonpackage which mitigates this by invoking the same executable file with the same arguments as a second rclone process. The new process will go through the same initialization steps until it reaches the daemonization point, thus mimics a fork. Nevertheless, the package solves a slightly different task. First, it provides options to redirect the background process output to a log which duplicates rclone's--log-fileapproach (this patch suppresses background output by redirecting to/dev/null, so you should use--log-fileto see output). Next, it allows to save identifier of background process in a pid file. This option was useful for classic SysV Unix daemons but not for rclone. This patch removes thego-daemondependency and reimplements backgrounding directly.Most importantly,
go-daemonunconditionally performssetsidduring backgrounding which appeared to be incompatible with Automount. The setsid call in the child process resets its process group id (PGID) to its PID thus detaching it from parent. While it was important forfork-based SysV Unix daemons, it makes autofs fail because Automount needs to tell the process serving the mount from processes accessing it, and they do it by the main process'sPGID(see https://blog.janestreet.com/how-does-automount-work-anyway, https://www.kernel.org/doc/Documentation/filesystems/automount-support.rst, https://www.kernel.org/doc/Documentation/filesystems/autofs4.txt). IfPGIDis changed, Automount will not treat the background rclone process as a mount provider. If the background process attempted to access the mountpoint directory (eg. when it's listed on macOS/BSD to ensure it's empty), the automount daemon (classic or systemd) will spawn another rclone mount resulting in a race. Note that the officialrclonefswrapper avoids thesetsidproblem -- it does not use the--daemonargument at all but sends rclone to background using the&shell operation. This patch removessetsidaltogether.The parent process and the background process must know their roles. This patch just sets a special environment variable when it spawns a background process in the same way as
sevlyar/go-daemonand checks for it early during initialization.The Automount daemon requires that mount is fully setup by the time the parent process exits. Currently
rclone --daemonparent exits immediately without waiting for setup to complete. This results in Automount failing withmount not ready. Therclonefswrapper used to usesleepas a workaround. This patch adds a new flag named--daemon-waitwhich defines how long should the parent wait. On Linux we poll/procto detect the ready state so this flag defines a maximum time to wait (see theWaitMountReadyfunction). On macOS/BSD the detection is not yet implemented so this flag defines a constant time to wait equivalent to a simplesleep. I'd rather call this flag--daemon-timeoutbut this name has been already taken.If mount fails in background or if user hits
^C, the parent will cleanup and kill the background process to prevent zombies.Miscellaneous
The function
IsDaemonis put in thefspackage rather than inlib/daemonizeto prevent import loops because #5594 will call it very early, in thefsinit block.Systemd runs mount units without any environment (PATH, HOME or whatever) so this patch provides a fallback
PATHto letgo-fusefind thefusermountbinary.Additionally this patch moves a few utility functions unchanged from
mount.gotoutils.go:CheckOverlap,CheckAllowings,SetVolumeName.Boilerplate
Was the change discussed in an issue or in the forum before?
https://forum.rclone.org/t/ls-on-autofs-triggered-rclone-mount-hangs-the-first-time/26781
Fixes #2968
Fixes #3246 (comment)
Checklist