willscott / go-nfs

golang NFSv3 server
Apache License 2.0
684 stars 74 forks source link
billy golang hacktoberfest nfs nfs-server nfsv3

Golang Network File Server

NFSv3 protocol implementation in pure Golang.

Current Status:

Usage

The most interesting demo is currently in example/osview.

Start the server go run ./example/osview ..

The local folder at . will be the initial view in the mount. mutations to metadata or contents will be stored purely in memory and not written back to the OS. When run, this demo will print the port it is listening on.

The mount can be accessed using a command similar to mount -o port=<n>,mountport=<n> -t nfs localhost:/mount <mountpoint> (For Mac users)

or

mount -o port=<n>,mountport=<n>,nfsvers=3,noacl,tcp -t nfs localhost:/mount <mountpoint> (For Linux users)

API

The NFS server runs on a net.Listener to export a file system to NFS clients. Usage is structured similarly to many other golang network servers.

package main

import (
    "fmt"
    "log"
    "net"

    "github.com/go-git/go-billy/v5/memfs"
    nfs "github.com/willscott/go-nfs"
    nfshelper "github.com/willscott/go-nfs/helpers"
)

func main() {
    listener, err := net.Listen("tcp", ":0")
    panicOnErr(err, "starting TCP listener")
    fmt.Printf("Server running at %s\n", listener.Addr())
    mem := memfs.New()
    f, err := mem.Create("hello.txt")
    panicOnErr(err, "creating file")
    _, err = f.Write([]byte("hello world"))
    panicOnErr(err, "writing data")
    f.Close()
    handler := nfshelper.NewNullAuthHandler(mem)
    cacheHelper := nfshelper.NewCachingHandler(handler, 1)
    panicOnErr(nfs.Serve(listener, cacheHelper), "serving nfs")
}

func panicOnErr(err error, desc ...interface{}) {
    if err == nil {
        return
    }
    log.Println(desc...)
    log.Panicln(err)
}

Notes