2023-10-05 01:28:41 +08:00
|
|
|
//go:build unix
|
|
|
|
|
|
|
|
// Package nfs implements a server to serve a VFS remote over NFSv3 protocol
|
|
|
|
//
|
|
|
|
// There is no authentication available on this server
|
|
|
|
// and it is served on loopback interface by default.
|
|
|
|
//
|
|
|
|
// This is primarily used for mounting a VFS remote
|
|
|
|
// in macOS, where FUSE-mounting mechanisms are usually not available.
|
|
|
|
package nfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/rclone/rclone/cmd"
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
|
|
|
"github.com/rclone/rclone/vfs"
|
2024-07-03 18:34:29 +08:00
|
|
|
"github.com/rclone/rclone/vfs/vfscommon"
|
2023-10-05 01:28:41 +08:00
|
|
|
"github.com/rclone/rclone/vfs/vfsflags"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
)
|
|
|
|
|
2024-07-04 02:20:43 +08:00
|
|
|
// OptionsInfo descripts the Options in use
|
|
|
|
var OptionsInfo = fs.Options{{
|
|
|
|
Name: "addr",
|
|
|
|
Default: "",
|
|
|
|
Help: "IPaddress:Port or :Port to bind server to",
|
|
|
|
}, {
|
|
|
|
Name: "nfs_cache_handle_limit",
|
|
|
|
Default: 1000000,
|
|
|
|
Help: "max file handles cached simultaneously (min 5)",
|
|
|
|
}}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
fs.RegisterGlobalOptions(fs.OptionsInfo{Name: "nfs", Opt: &opt, Options: OptionsInfo})
|
|
|
|
}
|
|
|
|
|
2023-10-05 01:28:41 +08:00
|
|
|
// Options contains options for the NFS Server
|
|
|
|
type Options struct {
|
2024-07-04 02:20:43 +08:00
|
|
|
ListenAddr string `config:"addr"` // Port to listen on
|
|
|
|
HandleLimit int `config:"nfs_cache_handle_limit"` // max file handles cached by go-nfs CachingHandler
|
2023-10-05 01:28:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var opt Options
|
|
|
|
|
2024-01-26 00:19:21 +08:00
|
|
|
// AddFlags adds flags for serve nfs (and nfsmount)
|
2023-10-05 01:28:41 +08:00
|
|
|
func AddFlags(flagSet *pflag.FlagSet, Opt *Options) {
|
2024-07-04 02:20:43 +08:00
|
|
|
flags.AddFlagsFromOptions(flagSet, "", OptionsInfo)
|
2023-10-05 01:28:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
vfsflags.AddFlags(Command.Flags())
|
|
|
|
AddFlags(Command.Flags(), &opt)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the command
|
|
|
|
func Run(command *cobra.Command, args []string) {
|
|
|
|
var f fs.Fs
|
|
|
|
cmd.CheckArgs(1, 1, command, args)
|
|
|
|
f = cmd.NewFsSrc(args)
|
|
|
|
cmd.Run(false, true, command, func() error {
|
2024-07-03 18:34:29 +08:00
|
|
|
s, err := NewServer(context.Background(), vfs.New(f, &vfscommon.Opt), &opt)
|
2023-10-05 01:28:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.Serve()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command is the definition of the command
|
|
|
|
var Command = &cobra.Command{
|
|
|
|
Use: "nfs remote:path",
|
|
|
|
Short: `Serve the remote as an NFS mount`,
|
|
|
|
Long: `Create an NFS server that serves the given remote over the network.
|
|
|
|
|
|
|
|
The primary purpose for this command is to enable [mount command](/commands/rclone_mount/) on recent macOS versions where
|
|
|
|
installing FUSE is very cumbersome.
|
|
|
|
|
|
|
|
Since this is running on NFSv3, no authentication method is available. Any client
|
|
|
|
will be able to access the data. To limit access, you can use serve NFS on loopback address
|
|
|
|
and rely on secure tunnels (such as SSH). For this reason, by default, a random TCP port is chosen and loopback interface is used for the listening address;
|
|
|
|
meaning that it is only available to the local machine. If you want other machines to access the
|
|
|
|
NFS mount over local network, you need to specify the listening address and port using ` + "`--addr`" + ` flag.
|
|
|
|
|
|
|
|
Modifying files through NFS protocol requires VFS caching. Usually you will need to specify ` + "`--vfs-cache-mode`" + `
|
|
|
|
in order to be able to write to the mountpoint (full is recommended). If you don't specify VFS cache mode,
|
2024-01-26 00:19:21 +08:00
|
|
|
the mount will be read-only. Note also that ` + "`--nfs-cache-handle-limit`" + ` controls the maximum number of cached file handles stored by the caching handler.
|
|
|
|
This should not be set too low or you may experience errors when trying to access files. The default is ` + "`1000000`" + `, but consider lowering this limit if
|
|
|
|
the server's system resource usage causes problems.
|
2023-10-05 01:28:41 +08:00
|
|
|
|
|
|
|
To serve NFS over the network use following command:
|
|
|
|
|
|
|
|
rclone serve nfs remote: --addr 0.0.0.0:$PORT --vfs-cache-mode=full
|
|
|
|
|
|
|
|
We specify a specific port that we can use in the mount command:
|
|
|
|
|
|
|
|
To mount the server under Linux/macOS, use the following command:
|
|
|
|
|
|
|
|
mount -oport=$PORT,mountport=$PORT $HOSTNAME: path/to/mountpoint
|
|
|
|
|
|
|
|
Where ` + "`$PORT`" + ` is the same port number we used in the serve nfs command.
|
|
|
|
|
|
|
|
This feature is only available on Unix platforms.
|
|
|
|
|
2024-04-05 19:27:33 +08:00
|
|
|
` + vfs.Help(),
|
2023-10-05 01:28:41 +08:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"versionIntroduced": "v1.65",
|
|
|
|
"groups": "Filter",
|
2023-11-27 18:34:21 +08:00
|
|
|
"status": "Experimental",
|
2023-10-05 01:28:41 +08:00
|
|
|
},
|
|
|
|
Run: Run,
|
|
|
|
}
|