2020-04-25 07:03:07 +02:00
|
|
|
package mountlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-11-04 11:12:57 +01:00
|
|
|
"errors"
|
2020-04-25 07:03:07 +02:00
|
|
|
"log"
|
2020-05-14 17:05:23 +02:00
|
|
|
"sort"
|
2020-06-04 19:13:10 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
2020-04-25 07:03:07 +02:00
|
|
|
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/rc"
|
2024-07-03 12:34:29 +02:00
|
|
|
"github.com/rclone/rclone/vfs/vfscommon"
|
2020-04-25 07:03:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-06-05 20:32:56 +02:00
|
|
|
// mutex to protect all the variables in this block
|
|
|
|
mountMu sync.Mutex
|
2020-04-25 07:03:07 +02:00
|
|
|
// Mount functions available
|
2020-06-04 19:13:10 +02:00
|
|
|
mountFns = map[string]MountFn{}
|
|
|
|
// Map of mounted path => MountInfo
|
2021-01-03 01:05:52 +01:00
|
|
|
liveMounts = map[string]*MountPoint{}
|
|
|
|
// Supported mount types
|
|
|
|
supportedMountTypes = []string{"mount", "cmount", "mount2"}
|
2020-04-25 07:03:07 +02:00
|
|
|
)
|
|
|
|
|
2021-01-03 01:05:52 +01:00
|
|
|
// ResolveMountMethod returns mount function by name
|
|
|
|
func ResolveMountMethod(mountType string) (string, MountFn) {
|
|
|
|
if mountType != "" {
|
|
|
|
return mountType, mountFns[mountType]
|
|
|
|
}
|
|
|
|
for _, mountType := range supportedMountTypes {
|
|
|
|
if mountFns[mountType] != nil {
|
|
|
|
return mountType, mountFns[mountType]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2020-05-14 17:05:23 +02:00
|
|
|
// AddRc adds mount and unmount functionality to rc
|
|
|
|
func AddRc(mountUtilName string, mountFunction MountFn) {
|
2020-06-05 20:32:56 +02:00
|
|
|
mountMu.Lock()
|
|
|
|
defer mountMu.Unlock()
|
2020-05-14 17:05:23 +02:00
|
|
|
// rcMount allows the mount command to be run from rc
|
|
|
|
mountFns[mountUtilName] = mountFunction
|
|
|
|
}
|
|
|
|
|
2020-04-25 07:03:07 +02:00
|
|
|
func init() {
|
|
|
|
rc.Add(rc.Call{
|
|
|
|
Path: "mount/mount",
|
|
|
|
AuthRequired: true,
|
|
|
|
Fn: mountRc,
|
|
|
|
Title: "Create a new mount point",
|
|
|
|
Help: `rclone allows Linux, FreeBSD, macOS and Windows to mount any of
|
|
|
|
Rclone's cloud storage systems as a file system with FUSE.
|
|
|
|
|
2020-05-14 17:05:23 +02:00
|
|
|
If no mountType is provided, the priority is given as follows: 1. mount 2.cmount 3.mount2
|
2020-04-25 07:03:07 +02:00
|
|
|
|
2021-08-16 11:30:01 +02:00
|
|
|
This takes the following parameters:
|
2020-04-25 07:03:07 +02:00
|
|
|
|
|
|
|
- fs - a remote path to be mounted (required)
|
|
|
|
- mountPoint: valid path on the local machine (required)
|
2021-08-16 11:30:01 +02:00
|
|
|
- mountType: one of the values (mount, cmount, mount2) specifies the mount implementation to use
|
2020-07-23 18:20:55 +02:00
|
|
|
- mountOpt: a JSON object with Mount options in.
|
2020-07-22 19:31:54 +02:00
|
|
|
- vfsOpt: a JSON object with VFS options in.
|
2020-04-25 07:03:07 +02:00
|
|
|
|
2021-08-16 11:30:01 +02:00
|
|
|
Example:
|
2020-04-25 07:03:07 +02:00
|
|
|
|
|
|
|
rclone rc mount/mount fs=mydrive: mountPoint=/home/<user>/mountPoint
|
2020-05-14 17:05:23 +02:00
|
|
|
rclone rc mount/mount fs=mydrive: mountPoint=/home/<user>/mountPoint mountType=mount
|
2020-07-23 18:20:55 +02:00
|
|
|
rclone rc mount/mount fs=TestDrive: mountPoint=/mnt/tmp vfsOpt='{"CacheMode": 2}' mountOpt='{"AllowOther": true}'
|
2020-07-22 19:31:54 +02:00
|
|
|
|
|
|
|
The vfsOpt are as described in options/get and can be seen in the the
|
2021-08-16 11:30:01 +02:00
|
|
|
"vfs" section when running and the mountOpt can be seen in the "mount" section:
|
2020-07-22 19:31:54 +02:00
|
|
|
|
|
|
|
rclone rc options/get
|
2020-04-25 07:03:07 +02:00
|
|
|
`,
|
|
|
|
})
|
2020-05-14 17:05:23 +02:00
|
|
|
}
|
|
|
|
|
2020-06-04 19:13:10 +02:00
|
|
|
// mountRc allows the mount command to be run from rc
|
2020-11-05 16:18:51 +01:00
|
|
|
func mountRc(ctx context.Context, in rc.Params) (out rc.Params, err error) {
|
2020-05-14 17:05:23 +02:00
|
|
|
mountPoint, err := in.GetString("mountPoint")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-07-03 12:34:29 +02:00
|
|
|
vfsOpt := vfscommon.Opt
|
2020-07-22 19:31:54 +02:00
|
|
|
err = in.GetStructMissingOK("vfsOpt", &vfsOpt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-23 18:34:54 +02:00
|
|
|
mountOpt := Opt
|
|
|
|
err = in.GetStructMissingOK("mountOpt", &mountOpt)
|
2020-07-23 18:20:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-12 13:07:48 +02:00
|
|
|
if mountOpt.Daemon {
|
2023-03-25 23:27:31 +01:00
|
|
|
return nil, errors.New("daemon option not supported over the API")
|
2022-10-12 13:07:48 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 17:05:23 +02:00
|
|
|
mountType, err := in.GetString("mountType")
|
|
|
|
|
2020-06-05 20:32:56 +02:00
|
|
|
mountMu.Lock()
|
|
|
|
defer mountMu.Unlock()
|
|
|
|
|
2021-01-03 01:05:52 +01:00
|
|
|
if err != nil {
|
|
|
|
mountType = ""
|
|
|
|
}
|
|
|
|
mountType, mountFn := ResolveMountMethod(mountType)
|
|
|
|
if mountFn == nil {
|
2023-03-25 23:27:31 +01:00
|
|
|
return nil, errors.New("mount option specified is not registered, or is invalid")
|
2020-05-14 17:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get Fs.fs to be mounted from fs parameter in the params
|
2020-11-05 16:18:51 +01:00
|
|
|
fdst, err := rc.GetFs(ctx, in)
|
2020-05-14 17:05:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-25 07:03:07 +02:00
|
|
|
|
2022-04-21 21:28:09 +02:00
|
|
|
mnt := NewMountPoint(mountFn, mountPoint, fdst, &mountOpt, &vfsOpt)
|
|
|
|
_, err = mnt.Mount()
|
2021-01-03 01:05:52 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("mount FAILED: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-10-03 12:24:58 +02:00
|
|
|
go func() {
|
|
|
|
if err = mnt.Wait(); err != nil {
|
|
|
|
log.Printf("unmount FAILED: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mountMu.Lock()
|
|
|
|
defer mountMu.Unlock()
|
|
|
|
delete(liveMounts, mountPoint)
|
|
|
|
}()
|
2021-01-03 01:05:52 +01:00
|
|
|
// Add mount to list if mount point was successfully created
|
2022-04-21 21:28:09 +02:00
|
|
|
liveMounts[mountPoint] = mnt
|
2021-01-03 01:05:52 +01:00
|
|
|
|
|
|
|
fs.Debugf(nil, "Mount for %s created at %s using %s", fdst.String(), mountPoint, mountType)
|
|
|
|
return nil, nil
|
2020-05-14 17:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2020-04-25 07:03:07 +02:00
|
|
|
rc.Add(rc.Call{
|
|
|
|
Path: "mount/unmount",
|
|
|
|
AuthRequired: true,
|
|
|
|
Fn: unMountRc,
|
2020-06-04 19:13:10 +02:00
|
|
|
Title: "Unmount selected active mount",
|
2020-04-25 07:03:07 +02:00
|
|
|
Help: `
|
|
|
|
rclone allows Linux, FreeBSD, macOS and Windows to
|
|
|
|
mount any of Rclone's cloud storage systems as a file system with
|
|
|
|
FUSE.
|
|
|
|
|
2021-08-16 11:30:01 +02:00
|
|
|
This takes the following parameters:
|
2020-04-25 07:03:07 +02:00
|
|
|
|
|
|
|
- mountPoint: valid path on the local machine where the mount was created (required)
|
|
|
|
|
2021-08-16 11:30:01 +02:00
|
|
|
Example:
|
2020-04-25 07:03:07 +02:00
|
|
|
|
|
|
|
rclone rc mount/unmount mountPoint=/home/<user>/mountPoint
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-04 19:13:10 +02:00
|
|
|
// unMountRc allows the umount command to be run from rc
|
2020-04-25 07:03:07 +02:00
|
|
|
func unMountRc(_ context.Context, in rc.Params) (out rc.Params, err error) {
|
|
|
|
mountPoint, err := in.GetString("mountPoint")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-05 20:32:56 +02:00
|
|
|
mountMu.Lock()
|
|
|
|
defer mountMu.Unlock()
|
2021-01-03 01:05:52 +01:00
|
|
|
mountInfo, found := liveMounts[mountPoint]
|
|
|
|
if !found {
|
|
|
|
return nil, errors.New("mount not found")
|
|
|
|
}
|
|
|
|
if err = mountInfo.Unmount(); err != nil {
|
2020-06-04 19:13:10 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-03 01:05:52 +01:00
|
|
|
delete(liveMounts, mountPoint)
|
2020-06-04 19:13:10 +02:00
|
|
|
return nil, nil
|
|
|
|
}
|
2020-04-25 07:03:07 +02:00
|
|
|
|
2020-05-14 17:05:23 +02:00
|
|
|
func init() {
|
|
|
|
rc.Add(rc.Call{
|
|
|
|
Path: "mount/types",
|
|
|
|
AuthRequired: true,
|
|
|
|
Fn: mountTypesRc,
|
|
|
|
Title: "Show all possible mount types",
|
|
|
|
Help: `This shows all possible mount types and returns them as a list.
|
2020-04-25 07:03:07 +02:00
|
|
|
|
2020-05-14 17:05:23 +02:00
|
|
|
This takes no parameters and returns
|
2020-04-25 07:03:07 +02:00
|
|
|
|
2020-05-14 17:05:23 +02:00
|
|
|
- mountTypes: list of mount types
|
2020-04-25 07:03:07 +02:00
|
|
|
|
2020-05-14 17:05:23 +02:00
|
|
|
The mount types are strings like "mount", "mount2", "cmount" and can
|
|
|
|
be passed to mount/mount as the mountType parameter.
|
2020-04-25 07:03:07 +02:00
|
|
|
|
2020-05-14 17:05:23 +02:00
|
|
|
Eg
|
|
|
|
|
|
|
|
rclone rc mount/types
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// mountTypesRc returns a list of available mount types.
|
|
|
|
func mountTypesRc(_ context.Context, in rc.Params) (out rc.Params, err error) {
|
|
|
|
var mountTypes = []string{}
|
2020-06-05 20:32:56 +02:00
|
|
|
mountMu.Lock()
|
|
|
|
defer mountMu.Unlock()
|
2020-05-14 17:05:23 +02:00
|
|
|
for mountType := range mountFns {
|
|
|
|
mountTypes = append(mountTypes, mountType)
|
2020-04-25 07:03:07 +02:00
|
|
|
}
|
2020-05-14 17:05:23 +02:00
|
|
|
sort.Strings(mountTypes)
|
|
|
|
return rc.Params{
|
|
|
|
"mountTypes": mountTypes,
|
|
|
|
}, nil
|
2020-04-25 07:03:07 +02:00
|
|
|
}
|
2020-06-04 19:13:10 +02:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
rc.Add(rc.Call{
|
|
|
|
Path: "mount/listmounts",
|
|
|
|
AuthRequired: true,
|
|
|
|
Fn: listMountsRc,
|
|
|
|
Title: "Show current mount points",
|
2021-08-16 11:30:01 +02:00
|
|
|
Help: `This shows currently mounted points, which can be used for performing an unmount.
|
2020-06-04 19:13:10 +02:00
|
|
|
|
|
|
|
This takes no parameters and returns
|
|
|
|
|
|
|
|
- mountPoints: list of current mount points
|
|
|
|
|
|
|
|
Eg
|
|
|
|
|
|
|
|
rclone rc mount/listmounts
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-03 01:05:52 +01:00
|
|
|
// MountInfo is a transitional structure for json marshaling
|
|
|
|
type MountInfo struct {
|
|
|
|
Fs string `json:"Fs"`
|
|
|
|
MountPoint string `json:"MountPoint"`
|
|
|
|
MountedOn time.Time `json:"MountedOn"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// listMountsRc returns a list of current mounts sorted by mount path
|
2020-06-04 19:13:10 +02:00
|
|
|
func listMountsRc(_ context.Context, in rc.Params) (out rc.Params, err error) {
|
2020-06-05 20:32:56 +02:00
|
|
|
mountMu.Lock()
|
|
|
|
defer mountMu.Unlock()
|
2021-01-03 01:05:52 +01:00
|
|
|
var keys []string
|
|
|
|
for key := range liveMounts {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
mountPoints := []MountInfo{}
|
|
|
|
for _, k := range keys {
|
|
|
|
m := liveMounts[k]
|
|
|
|
info := MountInfo{
|
2022-10-20 03:40:20 +02:00
|
|
|
Fs: fs.ConfigString(m.Fs),
|
2021-01-03 01:05:52 +01:00
|
|
|
MountPoint: m.MountPoint,
|
|
|
|
MountedOn: m.MountedOn,
|
|
|
|
}
|
|
|
|
mountPoints = append(mountPoints, info)
|
2020-06-04 19:13:10 +02:00
|
|
|
}
|
|
|
|
return rc.Params{
|
2021-01-03 01:05:52 +01:00
|
|
|
"mountPoints": mountPoints,
|
2020-06-04 19:13:10 +02:00
|
|
|
}, nil
|
|
|
|
}
|
2020-06-05 20:32:56 +02:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
rc.Add(rc.Call{
|
|
|
|
Path: "mount/unmountall",
|
|
|
|
AuthRequired: true,
|
|
|
|
Fn: unmountAll,
|
2022-10-16 06:42:12 +02:00
|
|
|
Title: "Unmount all active mounts",
|
|
|
|
Help: `
|
|
|
|
rclone allows Linux, FreeBSD, macOS and Windows to
|
|
|
|
mount any of Rclone's cloud storage systems as a file system with
|
|
|
|
FUSE.
|
2020-06-05 20:32:56 +02:00
|
|
|
|
|
|
|
This takes no parameters and returns error if unmount does not succeed.
|
|
|
|
|
|
|
|
Eg
|
|
|
|
|
|
|
|
rclone rc mount/unmountall
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// unmountAll unmounts all the created mounts
|
|
|
|
func unmountAll(_ context.Context, in rc.Params) (out rc.Params, err error) {
|
|
|
|
mountMu.Lock()
|
|
|
|
defer mountMu.Unlock()
|
2021-01-03 01:05:52 +01:00
|
|
|
for mountPoint, mountInfo := range liveMounts {
|
|
|
|
if err = mountInfo.Unmount(); err != nil {
|
|
|
|
fs.Debugf(nil, "Couldn't unmount : %s", mountPoint)
|
2020-06-05 20:32:56 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
delete(liveMounts, mountPoint)
|
|
|
|
}
|
2021-01-03 01:05:52 +01:00
|
|
|
return nil, nil
|
2020-06-05 20:32:56 +02:00
|
|
|
}
|