From ebfeec9fb45e89f6134243bf7ce7625ae2b52a89 Mon Sep 17 00:00:00 2001 From: ishuah Date: Fri, 2 Mar 2018 16:30:04 +0300 Subject: [PATCH] mount: run rclone mount in the background - fixes #723 --- cmd/mountlib/daemon.go | 15 +++++++++++++ cmd/mountlib/daemon_unix.go | 32 +++++++++++++++++++++++++++ cmd/mountlib/mount.go | 11 ++++++++- docs/content/commands/rclone_mount.md | 14 +++++++++--- 4 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 cmd/mountlib/daemon.go create mode 100644 cmd/mountlib/daemon_unix.go diff --git a/cmd/mountlib/daemon.go b/cmd/mountlib/daemon.go new file mode 100644 index 000000000..a8124fef9 --- /dev/null +++ b/cmd/mountlib/daemon.go @@ -0,0 +1,15 @@ +// Daemonization interface for non-Unix variants only + +// +build windows darwin,!cgo + +package mountlib + +import ( + "log" + "runtime" +) + +func startBackgroundMode() bool { + log.Fatalf("background mode not supported on %s platform", runtime.GOOS) + return false +} diff --git a/cmd/mountlib/daemon_unix.go b/cmd/mountlib/daemon_unix.go new file mode 100644 index 000000000..318f5a3d0 --- /dev/null +++ b/cmd/mountlib/daemon_unix.go @@ -0,0 +1,32 @@ +// Daemonization interface for Unix variants only + +// +build !windows +// +build !darwin cgo + +package mountlib + +import ( + "log" + + "github.com/sevlyar/go-daemon" +) + +func startBackgroundMode() bool { + cntxt := &daemon.Context{} + d, err := cntxt.Reborn() + if err != nil { + log.Fatalln(err) + } + + if d != nil { + return true + } + + defer func() { + if err := cntxt.Release(); err != nil { + log.Printf("error encountered while killing daemon: %v", err) + } + }() + + return false +} diff --git a/cmd/mountlib/mount.go b/cmd/mountlib/mount.go index b5863097d..980106ce7 100644 --- a/cmd/mountlib/mount.go +++ b/cmd/mountlib/mount.go @@ -23,6 +23,7 @@ var ( AllowOther = false DefaultPermissions = false WritebackCache = false + Daemon = false MaxReadAhead fs.SizeSuffix = 128 * 1024 ExtraOptions []string ExtraFlags []string @@ -174,6 +175,14 @@ will see all files and folders immediately in this mode. } } + // Start background task if --background is specified + if Daemon { + daemonized := startBackgroundMode() + if daemonized { + return + } + } + err := Mount(fdst, args[1]) if err != nil { log.Fatalf("Fatal error: %v", err) @@ -196,7 +205,7 @@ will see all files and folders immediately in this mode. flags.FVarP(flagSet, &MaxReadAhead, "max-read-ahead", "", "The number of bytes that can be prefetched for sequential reads.") flags.StringArrayVarP(flagSet, &ExtraOptions, "option", "o", []string{}, "Option for libfuse/WinFsp. Repeat if required.") flags.StringArrayVarP(flagSet, &ExtraFlags, "fuse-flag", "", []string{}, "Flags or arguments to be passed direct to libfuse/WinFsp. Repeat if required.") - //flags.BoolVarP(flagSet, &foreground, "foreground", "", foreground, "Do not detach.") + flags.BoolVarP(flagSet, &Daemon, "daemon", "", Daemon, "Run mount as a daemon (background mode).") // Add in the generic flags vfsflags.AddFlags(flagSet) diff --git a/docs/content/commands/rclone_mount.md b/docs/content/commands/rclone_mount.md index a5c221553..8fa9ea3b2 100644 --- a/docs/content/commands/rclone_mount.md +++ b/docs/content/commands/rclone_mount.md @@ -20,6 +20,11 @@ This is **EXPERIMENTAL** - use with care. First set up your remote using `rclone config`. Check it works with `rclone ls` etc. +You can either run mount in foreground mode or background(daemon) mode. Mount runs in +foreground mode by default, use the `--daemon` flag to specify background mode mode. +Background mode is only supported on Linux and OSX, you can only run mount in +foreground mode on Windows. + Start the mount like this rclone mount remote:path/to/files /path/to/local/mount @@ -28,12 +33,15 @@ Or on Windows like this where X: is an unused drive letter rclone mount remote:path/to/files X: -When the program ends, either via Ctrl+C or receiving a SIGINT or SIGTERM signal, -the mount is automatically stopped. +When running in background mode the user will have to stop the mount manually (specified below). + +When the program ends while in foreground mode, either via Ctrl+C or receiving +a SIGINT or SIGTERM signal, the mount is automatically stopped. The umount operation can fail, for example when the mountpoint is busy. -When that happens, it is the user's responsibility to stop the mount manually with +When that happens, it is the user's responsibility to stop the mount manually. +Stopping the mount manually: # Linux fusermount -u /path/to/local/mount # OS X