2020-05-20 10:39:20 +00:00
|
|
|
// Package mount implements a FUSE mounting system for rclone remotes.
|
2016-07-17 22:03:23 +00:00
|
|
|
|
2021-09-09 12:25:25 +00:00
|
|
|
//go:build linux || freebsd
|
2021-02-03 17:46:08 +00:00
|
|
|
// +build linux freebsd
|
2016-07-17 22:03:23 +00:00
|
|
|
|
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
2018-07-18 15:21:35 +00:00
|
|
|
"fmt"
|
2020-07-24 14:41:31 +00:00
|
|
|
"runtime"
|
2016-09-09 07:39:19 +00:00
|
|
|
|
2016-07-17 22:03:23 +00:00
|
|
|
"bazil.org/fuse"
|
2017-05-02 21:35:07 +00:00
|
|
|
fusefs "bazil.org/fuse/fs"
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/cmd/mountlib"
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/vfs"
|
2016-07-17 22:03:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-07-23 12:08:38 +00:00
|
|
|
mountlib.NewMountCommand("mount", false, mount)
|
2020-04-25 05:03:07 +00:00
|
|
|
mountlib.AddRc("mount", mount)
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
|
|
|
|
2017-05-02 21:35:07 +00:00
|
|
|
// mountOptions configures the options from the command line flags
|
2020-07-23 16:17:01 +00:00
|
|
|
func mountOptions(VFS *vfs.VFS, device string, opt *mountlib.Options) (options []fuse.MountOption) {
|
2017-05-02 21:35:07 +00:00
|
|
|
options = []fuse.MountOption{
|
2020-07-23 16:17:01 +00:00
|
|
|
fuse.MaxReadahead(uint32(opt.MaxReadAhead)),
|
2017-05-02 21:35:07 +00:00
|
|
|
fuse.Subtype("rclone"),
|
2018-05-03 08:45:24 +00:00
|
|
|
fuse.FSName(device),
|
2020-07-23 16:17:01 +00:00
|
|
|
fuse.VolumeName(opt.VolumeName),
|
2017-05-02 21:35:07 +00:00
|
|
|
|
|
|
|
// Options from benchmarking in the fuse module
|
|
|
|
//fuse.MaxReadahead(64 * 1024 * 1024),
|
|
|
|
//fuse.WritebackCache(),
|
|
|
|
}
|
2020-07-23 16:17:01 +00:00
|
|
|
if opt.AsyncRead {
|
2020-02-04 10:21:07 +00:00
|
|
|
options = append(options, fuse.AsyncRead())
|
|
|
|
}
|
2020-07-23 16:17:01 +00:00
|
|
|
if opt.AllowNonEmpty {
|
2017-05-02 21:35:07 +00:00
|
|
|
options = append(options, fuse.AllowNonEmptyMount())
|
|
|
|
}
|
2020-07-23 16:17:01 +00:00
|
|
|
if opt.AllowOther {
|
2017-05-02 21:35:07 +00:00
|
|
|
options = append(options, fuse.AllowOther())
|
|
|
|
}
|
2020-07-23 16:17:01 +00:00
|
|
|
if opt.AllowRoot {
|
2020-02-25 14:21:03 +00:00
|
|
|
// options = append(options, fuse.AllowRoot())
|
|
|
|
fs.Errorf(nil, "Ignoring --allow-root. Support has been removed upstream - see https://github.com/bazil/fuse/issues/144 for more info")
|
2017-05-02 21:35:07 +00:00
|
|
|
}
|
2020-07-23 16:17:01 +00:00
|
|
|
if opt.DefaultPermissions {
|
2017-05-02 21:35:07 +00:00
|
|
|
options = append(options, fuse.DefaultPermissions())
|
|
|
|
}
|
2020-07-22 16:58:49 +00:00
|
|
|
if VFS.Opt.ReadOnly {
|
2017-05-02 21:35:07 +00:00
|
|
|
options = append(options, fuse.ReadOnly())
|
|
|
|
}
|
2020-07-23 16:17:01 +00:00
|
|
|
if opt.WritebackCache {
|
2017-05-02 21:35:07 +00:00
|
|
|
options = append(options, fuse.WritebackCache())
|
|
|
|
}
|
2020-07-23 16:17:01 +00:00
|
|
|
if opt.DaemonTimeout != 0 {
|
|
|
|
options = append(options, fuse.DaemonTimeout(fmt.Sprint(int(opt.DaemonTimeout.Seconds()))))
|
2018-07-18 15:21:35 +00:00
|
|
|
}
|
2020-07-23 16:17:01 +00:00
|
|
|
if len(opt.ExtraOptions) > 0 {
|
2017-06-19 12:44:49 +00:00
|
|
|
fs.Errorf(nil, "-o/--option not supported with this FUSE backend")
|
|
|
|
}
|
2020-07-23 16:17:01 +00:00
|
|
|
if len(opt.ExtraFlags) > 0 {
|
2017-06-19 12:44:49 +00:00
|
|
|
fs.Errorf(nil, "--fuse-flag not supported with this FUSE backend")
|
|
|
|
}
|
2017-05-02 21:35:07 +00:00
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
|
|
|
// mount the file system
|
|
|
|
//
|
|
|
|
// The mount point will be ready when this returns.
|
|
|
|
//
|
|
|
|
// returns an error, and an error channel for the serve process to
|
|
|
|
// report an error when fusermount is called.
|
2020-07-23 16:17:01 +00:00
|
|
|
func mount(VFS *vfs.VFS, mountpoint string, opt *mountlib.Options) (<-chan error, func() error, error) {
|
2020-07-24 14:41:31 +00:00
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
fs.Logf(nil, "macOS users: please try \"rclone cmount\" as it will be the default in v1.54")
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:17:01 +00:00
|
|
|
if opt.DebugFUSE {
|
2020-07-23 12:08:38 +00:00
|
|
|
fuse.Debug = func(msg interface{}) {
|
|
|
|
fs.Debugf("fuse", "%v", msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 16:58:49 +00:00
|
|
|
f := VFS.Fs()
|
2017-05-02 21:35:07 +00:00
|
|
|
fs.Debugf(f, "Mounting on %q", mountpoint)
|
2020-07-23 16:17:01 +00:00
|
|
|
c, err := fuse.Mount(mountpoint, mountOptions(VFS, f.Name()+":"+f.Root(), opt)...)
|
2017-05-02 21:35:07 +00:00
|
|
|
if err != nil {
|
2020-07-22 16:58:49 +00:00
|
|
|
return nil, nil, err
|
2017-05-02 21:35:07 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 16:17:01 +00:00
|
|
|
filesys := NewFS(VFS, opt)
|
2021-03-16 13:23:36 +00:00
|
|
|
filesys.server = fusefs.New(c, nil)
|
2017-05-02 21:35:07 +00:00
|
|
|
|
|
|
|
// Serve the mount point in the background returning error to errChan
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
go func() {
|
2021-03-16 13:23:36 +00:00
|
|
|
err := filesys.server.Serve(filesys)
|
2017-05-02 21:35:07 +00:00
|
|
|
closeErr := c.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = closeErr
|
|
|
|
}
|
|
|
|
errChan <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
// check if the mount process has an error to report
|
|
|
|
<-c.Ready
|
|
|
|
if err := c.MountError; err != nil {
|
2020-07-22 16:58:49 +00:00
|
|
|
return nil, nil, err
|
2017-05-02 21:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unmount := func() error {
|
2017-11-07 18:03:23 +00:00
|
|
|
// Shutdown the VFS
|
|
|
|
filesys.VFS.Shutdown()
|
2017-05-02 21:35:07 +00:00
|
|
|
return fuse.Unmount(mountpoint)
|
|
|
|
}
|
|
|
|
|
2020-07-22 16:58:49 +00:00
|
|
|
return errChan, unmount, nil
|
2017-05-02 21:35:07 +00:00
|
|
|
}
|