Skip to content

mountlib: fix --daemon for automount and systemd mount - #5593

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

ivandeex merged 3 commits into
rclone:masterfrom
ivandeex:pr-automount

Conversation

@ivandeex

@ivandeex ivandeex commented Sep 10, 2021 •

Copy link
Copy Markdown
Member

What is the purpose of this change?

rclone mount works fine under Windows, Linux or BSD as a standalone process that can be cancelled by ^C when not needed anymore.
rclone mount --daemon somewhat 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 mount behavior on Windows. We simply don't need to as --daemon is 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 CheckMountEmpty method). Additionally it polls procfs to check whether mountpoint was mounted by rclone (in the CheckMountReady method).

I'm not a BSD or macOS guru so I left them as is. That's out of scope for this PR. The CheckMountEmpty method on BSD/macOS will still list directory. The CheckMountReady will 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 fork due to its problems with goroutines. Currently rclone uses the sevlyar/go-daemon package 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-file approach (this patch suppresses background output by redirecting to /dev/null, so you should use --log-file to 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 the go-daemon dependency and reimplements backgrounding directly.

Most importantly, go-daemon unconditionally performs setsid during 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 for fork-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's PGID (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). If PGID is 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 official rclonefs wrapper avoids the setsid problem -- it does not use the --daemon argument at all but sends rclone to background using the & shell operation. This patch removes setsid altogether.

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-daemon and checks for it early during initialization.

The Automount daemon requires that mount is fully setup by the time the parent process exits. Currently rclone --daemon parent exits immediately without waiting for setup to complete. This results in Automount failing with mount not ready. The rclonefs wrapper used to use sleep as a workaround. This patch adds a new flag named --daemon-wait which defines how long should the parent wait. On Linux we poll /proc to detect the ready state so this flag defines a maximum time to wait (see the WaitMountReady function). On macOS/BSD the detection is not yet implemented so this flag defines a constant time to wait equivalent to a simple sleep. I'd rather call this flag --daemon-timeout but 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 IsDaemon is put in the fs package rather than in lib/daemonize to prevent import loops because #5594 will call it very early, in the fs init block.

Systemd runs mount units without any environment (PATH, HOME or whatever) so this patch provides a fallback PATH to let go-fuse find the fusermount binary.

Additionally this patch moves a few utility functions unchanged from mount.go to utils.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

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

@ivandeex ivandeex added this to the v1.57 milestone Sep 10, 2021
@ivandeex ivandeex self-assigned this Sep 10, 2021
@ivandeex ivandeex mentioned this pull request Sep 10, 2021
5 tasks done
@ivandeex ivandeex mentioned this pull request Sep 10, 2021
@ivandeex

ivandeex commented Sep 11, 2021 •

Copy link
Copy Markdown
Member Author

Manual unit test:

  • create mount helper in /sbin/mount.rclone (and do chmod +x)
#!/bin/bash
remote=$1
mountpoint=$2
shift 2
args=""
while getopts :o: opts; do
    case $opts in
        o)
            for p in ${OPTARG//,/ }; do
                case "$p" in
                    rw|ro|suid|nosuid|dev|nodev|exec|noexec|noauto|x-systemd*)
                        continue ;;
                    *)
                        args="$args --$p" ;;
                esac
            done
            ;;
        *)
            echo "Invalid option: -$OPTARG" ;;
    esac
done
# note that no hacks are needed, just replace the bash process by rclone and move on
exec rclone mount --daemon $args "$remote" "$mountpoint" </dev/null >/dev/null 2>&1
  • create a sample rclone config to your likes
  • run mount like
sudo mount sftp1: /mnt -t rclone -o verbose=2,log-file=/tmp/rclone.log

@ivandeex ivandeex changed the title mountlib: fix --daemonize for automountd and systemd mount mountlib: fix --daemon for automount and systemd mount Sep 15, 2021
@ivandeex ivandeex modified the milestones: v1.57, v1.58 Sep 26, 2021
@ivandeex
ivandeex force-pushed the pr-automount branch 2 times, most recently from b4e1330 to 04da3b5 Compare September 30, 2021 07:09

@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 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.

Comment thread fs/log/log.go Resolved
@kforner

kforner commented Oct 1, 2021 •

Copy link
Copy Markdown

Hey, @ncw just pointed me at your fix.
I have this problem (among many) : https://forum.rclone.org/t/ls-on-autofs-triggered-rclone-mount-hangs-the-first-time/26781
and I'm delighted to see you're working on this.
I'm going to test it right now.
And funny thing your processing args loop looks exactly like mine in rclonefsdev ;)

@kforner

kforner commented Oct 1, 2021

Copy link
Copy Markdown

this fixes my issue !

auto.master.debug

/- 	/etc/auto.rclone.rebex --timeout=30

auto.rclone.rebex

/tmp/rebex2   -fstype=rclone,allow-non-empty,allow-other,log-file=/tmp/rclonefs.log,verbose   "::ftp,host=test.rebex.net,user=demo,pass=7l3xYbNT_gPzlsphlwKo11VRU4JUTAuE:"

running:

sudo automount -fFvd  auto.master.debug

accessing the mountpoint:

%mount  | grep rebex2
/etc/auto.rclone.rebex on /tmp/rebex2 type autofs (rw,relatime,fd=5,pgrp=20174,timeout=30,minproto=5,maxproto=5,direct,pipe_ino=196900)

 %ls -l /tmp/rebex2/pub/example/
total 404
drwxr-xr-x 1 root root     0 Feb 10  2021 ./
drwxr-xr-x 1 root root     0 Oct 19  2020 ../
-rw-r--r-- 1 root root 19156 Feb 16  2007 imap-console-client.png
-rw-r--r-- 1 root root 36672 Mar 19  2007 KeyGenerator.png

%mount  | grep rebex2
/etc/auto.rclone.rebex on /tmp/rebex2 type autofs (rw,relatime,fd=5,pgrp=20174,timeout=30,minproto=5,maxproto=5,direct,pipe_ino=196900)
:ftp{Q8nY_}: on /tmp/rebex2 type fuse.rclone (rw,nosuid,nodev,relatime,user_id=0,group_id=0,allow_other)

### after the timeout
%mount  | grep rebex2
/etc/auto.rclone.rebex on /tmp/rebex2 type autofs (rw,relatime,fd=5,pgrp=20174,timeout=30,minproto=5,maxproto=5,direct,pipe_ino=196900)

rclone log:

2021/10/01 17:45:13 NOTICE: Config file "/.rclone.conf" not found - using defaults
2021/10/01 17:45:13 NOTICE: Config file "/root/.config/rclone/rclone.conf" not found - using defaults
2021/10/01 17:45:13 INFO  : ftp://test.rebex.net:21: poll-interval is not supported by this remote

Note that we don't see the INFO : Exiting... in the log, that may be the only glitch so far.

@ivandeex

ivandeex commented Oct 1, 2021 •

Copy link
Copy Markdown
Member Author

And funny thing your processing args loop looks exactly like mine in rclonefsdev

With #5594 you will forget rclonefsdev

@kforner

kforner commented Oct 1, 2021 •

Copy link
Copy Markdown

And funny thing your processing args loop looks exactly like mine in rclonefsdev

With #5594 you will forget rclonefsdev

Interesting...
But I'm also trying to run the autofs rclone mount with my current user (not root) with expected benefit of:

  • having the right user/group ids by default
  • using the user config automatically
  • having more control on the rclone executable
  • security...

I was trying to hack your rclone.mount, using su or sudo for that but with no success so far.
Basically I'd like to reproduce with autofs what you have when you run manually the rclone mount with my user.
Any idea ? Is is theoretically possible ?

@ivandeex

ivandeex commented Oct 1, 2021

Copy link
Copy Markdown
Member Author

Possible but

  • niche (suggested way is to run mount as root and specify some of -o uid=x,gid=y,allow_other)
  • out of scope for this ticket

@kforner

kforner commented Oct 1, 2021

Copy link
Copy Markdown

Thanks. So what should I do ? Create a new ticket ?

@ivandeex

ivandeex commented Oct 1, 2021

Copy link
Copy Markdown
Member Author

Looks more suitable for support ticket on the forum

@ivandeex ivandeex modified the milestones: v1.58, v1.57 Oct 2, 2021
@ivandeex
ivandeex force-pushed the pr-automount branch 3 times, most recently from b740d50 to 611b5de Compare October 2, 2021 20:18
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
@ivandeex
ivandeex merged commit 8b8a943 into rclone:master Oct 2, 2021
ivandeex added a commit that referenced this pull request Oct 2, 2021
This option is useful to troubleshoot `rclone mount --daemon`
ivandeex added a commit that referenced this pull request Oct 2, 2021
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.
@ivandeex
ivandeex deleted the pr-automount branch October 2, 2021 20:46
@ivandeex

ivandeex commented Oct 2, 2021

Copy link
Copy Markdown
Member Author

Merged!

lafkpages pushed a commit to lafkpages/rclone that referenced this pull request Jul 20, 2026
This option is useful to troubleshoot `rclone mount --daemon`
lafkpages pushed a commit to lafkpages/rclone that referenced this pull request Jul 20, 2026
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.
lafkpages pushed a commit to lafkpages/rclone that referenced this pull request Jul 20, 2026
…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
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.

Mounting using AutoFS doesn't work daemonize after mount

3 participants