2023-11-27 11:34:59 +01:00
|
|
|
//go:build unix
|
|
|
|
// +build unix
|
2023-10-04 19:33:12 +02:00
|
|
|
|
|
|
|
// Package nfsmount implements mounting functionality using serve nfs command
|
|
|
|
//
|
2023-11-27 11:34:59 +01:00
|
|
|
// This can potentially work on all unix like systems which can mount NFS.
|
2023-10-04 19:33:12 +02:00
|
|
|
package nfsmount
|
|
|
|
|
|
|
|
import (
|
2023-11-27 11:34:59 +01:00
|
|
|
"bytes"
|
2023-10-04 19:33:12 +02:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/rclone/rclone/cmd/mountlib"
|
|
|
|
"github.com/rclone/rclone/cmd/serve/nfs"
|
2023-11-27 11:34:59 +01:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
2023-10-04 19:33:12 +02:00
|
|
|
"github.com/rclone/rclone/vfs"
|
|
|
|
)
|
|
|
|
|
2023-11-27 11:34:59 +01:00
|
|
|
var (
|
2024-01-25 17:19:21 +01:00
|
|
|
sudo = false
|
|
|
|
nfsServerOpt nfs.Options
|
2023-11-27 11:34:59 +01:00
|
|
|
)
|
|
|
|
|
2023-10-04 19:33:12 +02:00
|
|
|
func init() {
|
2023-11-27 11:34:59 +01:00
|
|
|
name := "nfsmount"
|
|
|
|
cmd := mountlib.NewMountCommand(name, false, mount)
|
|
|
|
cmd.Annotations["versionIntroduced"] = "v1.65"
|
|
|
|
cmd.Annotations["status"] = "Experimental"
|
|
|
|
mountlib.AddRc(name, mount)
|
|
|
|
cmdFlags := cmd.Flags()
|
|
|
|
flags.BoolVarP(cmdFlags, &sudo, "sudo", "", sudo, "Use sudo to run the mount command as root.", "")
|
2024-01-25 17:19:21 +01:00
|
|
|
nfs.AddFlags(cmdFlags, &nfsServerOpt)
|
2023-10-04 19:33:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func mount(VFS *vfs.VFS, mountpoint string, opt *mountlib.Options) (asyncerrors <-chan error, unmount func() error, err error) {
|
2024-01-25 17:19:21 +01:00
|
|
|
s, err := nfs.NewServer(context.Background(), VFS, &nfsServerOpt)
|
2023-10-04 19:33:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
errChan <- s.Serve()
|
|
|
|
}()
|
|
|
|
// The port is always picked at random after the NFS server has started
|
|
|
|
// we need to query the server for the port number so we can mount it
|
|
|
|
_, port, err := net.SplitHostPort(s.Addr().String())
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("cannot find port number in %s", s.Addr().String())
|
|
|
|
return
|
|
|
|
}
|
2023-11-27 11:34:59 +01:00
|
|
|
|
|
|
|
// Options
|
|
|
|
options := []string{
|
|
|
|
"-o", fmt.Sprintf("port=%s", port),
|
|
|
|
"-o", fmt.Sprintf("mountport=%s", port),
|
|
|
|
}
|
|
|
|
for _, option := range opt.ExtraOptions {
|
|
|
|
options = append(options, "-o", option)
|
|
|
|
}
|
|
|
|
options = append(options, opt.ExtraFlags...)
|
|
|
|
|
|
|
|
cmd := []string{}
|
|
|
|
if sudo {
|
|
|
|
cmd = append(cmd, "sudo")
|
|
|
|
}
|
|
|
|
cmd = append(cmd, "mount")
|
|
|
|
cmd = append(cmd, options...)
|
2024-02-08 06:59:13 +01:00
|
|
|
cmd = append(cmd, "localhost:"+opt.VolumeName, mountpoint)
|
2023-11-27 11:34:59 +01:00
|
|
|
fs.Debugf(nil, "Running mount command: %q", cmd)
|
|
|
|
|
|
|
|
out, err := exec.Command(cmd[0], cmd[1:]...).CombinedOutput()
|
2023-10-04 19:33:12 +02:00
|
|
|
if err != nil {
|
2023-11-27 11:34:59 +01:00
|
|
|
out = bytes.TrimSpace(out)
|
|
|
|
err = fmt.Errorf("%s: failed to mount NFS volume: %v", out, err)
|
2023-10-04 19:33:12 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
asyncerrors = errChan
|
|
|
|
unmount = func() error {
|
2024-02-06 08:53:12 +01:00
|
|
|
if s.UnmountedExternally {
|
|
|
|
return nil
|
|
|
|
}
|
2023-10-04 19:33:12 +02:00
|
|
|
var umountErr error
|
2023-11-27 11:34:59 +01:00
|
|
|
var out []byte
|
2023-10-04 19:33:12 +02:00
|
|
|
if runtime.GOOS == "darwin" {
|
2023-11-27 11:34:59 +01:00
|
|
|
out, umountErr = exec.Command("diskutil", "umount", "force", mountpoint).CombinedOutput()
|
2023-10-04 19:33:12 +02:00
|
|
|
} else {
|
2023-11-27 11:34:59 +01:00
|
|
|
out, umountErr = exec.Command("umount", "-f", mountpoint).CombinedOutput()
|
2023-10-04 19:33:12 +02:00
|
|
|
}
|
|
|
|
shutdownErr := s.Shutdown()
|
|
|
|
VFS.Shutdown()
|
|
|
|
if umountErr != nil {
|
2023-11-27 11:34:59 +01:00
|
|
|
out = bytes.TrimSpace(out)
|
|
|
|
return fmt.Errorf("%s: failed to umount the NFS volume %e", out, umountErr)
|
2023-10-04 19:33:12 +02:00
|
|
|
} else if shutdownErr != nil {
|
|
|
|
return fmt.Errorf("failed to shutdown NFS server: %e", shutdownErr)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-02-06 08:53:12 +01:00
|
|
|
|
|
|
|
nfs.OnUnmountFunc = func() {
|
|
|
|
s.UnmountedExternally = true
|
|
|
|
errChan <- nil
|
|
|
|
VFS.Shutdown()
|
|
|
|
}
|
|
|
|
|
2023-10-04 19:33:12 +02:00
|
|
|
return
|
|
|
|
}
|