Skip to content

Commit 619f0cb

Browse files
committed
commands: mount - add NFS host support
1 parent 7afc91b commit 619f0cb

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed

internal/commands/mount.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ func makeHostCommands() []command.Command {
273273
var (
274274
commandMakers = []makeCommand{
275275
makeFUSECommand,
276+
makeNFSCommand,
276277
}
277278
commands = make([]command.Command, 0, len(commandMakers))
278279
)

internal/commands/mountpoint.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func makeMountPointHosts(path ninePath, autoUnlink bool) mountPointHosts {
9090
var (
9191
hostMakers = []makeHostsFunc{
9292
makeFUSEHost,
93+
makeNFSHost,
9394
}
9495
hosts = make(mountPointHosts, len(hostMakers))
9596
)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//go:build !nonfs
2+
3+
package commands
4+
5+
import (
6+
"encoding/json"
7+
"errors"
8+
"flag"
9+
10+
"github.com/djdv/go-filesystem-utils/internal/command"
11+
"github.com/djdv/go-filesystem-utils/internal/filesystem"
12+
p9fs "github.com/djdv/go-filesystem-utils/internal/filesystem/9p"
13+
"github.com/djdv/go-filesystem-utils/internal/filesystem/nfs"
14+
"github.com/djdv/go-filesystem-utils/internal/generic"
15+
"github.com/multiformats/go-multiaddr"
16+
)
17+
18+
type (
19+
nfsHostSettings nfs.Host
20+
nfsHostOption func(*nfsHostSettings) error
21+
nfsHostOptions []nfsHostOption
22+
)
23+
24+
func makeNFSCommand() command.Command {
25+
return makeMountSubcommand(
26+
nfs.HostID,
27+
makeGuestCommands[nfsHostOptions, nfsHostSettings](nfs.HostID),
28+
)
29+
}
30+
31+
func makeNFSHost(path ninePath, autoUnlink bool) (filesystem.Host, p9fs.MakeGuestFunc) {
32+
guests := makeMountPointGuests[nfs.Host](path)
33+
return nfs.HostID, newMakeGuestFunc(guests, path, autoUnlink)
34+
}
35+
36+
func (on *nfsHostOptions) BindFlags(flagSet *flag.FlagSet) { /* NOOP */ }
37+
38+
func (on nfsHostOptions) make() (nfsHostSettings, error) {
39+
return makeWithOptions(on...)
40+
}
41+
42+
func (*nfsHostOptions) usage(guest filesystem.ID) string {
43+
return string(nfs.HostID) + " hosts " +
44+
string(guest) + " as an NFS server"
45+
}
46+
47+
func (set nfsHostSettings) marshal(arg string) ([]byte, error) {
48+
if arg == "" {
49+
err := command.UsageError{
50+
Err: generic.ConstError(
51+
"expected server multiaddr",
52+
),
53+
}
54+
return nil, err
55+
}
56+
maddr, err := multiaddr.NewMultiaddr(arg)
57+
if err != nil {
58+
return nil, err
59+
}
60+
set.Maddr = maddr
61+
return json.Marshal(set)
62+
}
63+
64+
func unmarshalNFS() (filesystem.Host, decodeFunc) {
65+
return nfs.HostID, func(b []byte) (string, error) {
66+
var host nfs.Host
67+
if err := json.Unmarshal(b, &host); err != nil {
68+
return "", err
69+
}
70+
if maddr := host.Maddr; maddr != nil {
71+
return host.Maddr.String(), nil
72+
}
73+
return "", errors.New("NFS host address was not present in the mountpoint data")
74+
}
75+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//go:build nonfs
2+
3+
package commands
4+
5+
import (
6+
"github.com/djdv/go-filesystem-utils/internal/command"
7+
"github.com/djdv/go-filesystem-utils/internal/filesystem"
8+
p9fs "github.com/djdv/go-filesystem-utils/internal/filesystem/9p"
9+
)
10+
11+
const nfsHost = filesystem.Host("")
12+
13+
func makeNFSCommand() command.Command {
14+
return nil
15+
}
16+
17+
func makeNFSHost(ninePath, bool) (filesystem.Host, p9fs.MakeGuestFunc) {
18+
return nfsHost, nil
19+
}
20+
21+
func unmarshalNFS() (filesystem.Host, decodeFunc) {
22+
return nfsHost, nil
23+
}

internal/commands/unmount.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ func newDecodeTargetFunc() p9fs.DecodeTargetFunc {
146146
var (
147147
decoderMakers = []makeDecoderFunc{
148148
unmarshalFUSE,
149+
unmarshalNFS,
149150
}
150151
decoders = make(decoders, len(decoderMakers))
151152
)

0 commit comments

Comments
 (0)