使用go-fuse开发一个fuse 文件系统

go-fuse 是fuse 的包装,我们可以用来开发fuse 文件系统,以下是一个简单的学习

项目准备

  • go mod
go mod init demoapp
  • 添加依赖
go get github.com/hanwen/go-fuse/v2
  • 简单代码
main.go
package main
import (
    "context"
    "flag"
    "log"
    "syscall"
    "github.com/hanwen/go-fuse/v2/fs"
    "github.com/hanwen/go-fuse/v2/fuse"
)
type HelloRoot struct {
    fs.Inode
}
func (r *HelloRoot) OnAdd(ctx context.Context) {
    ch := r.NewPersistentInode(
        ctx, &fs.MemRegularFile{
            Data: []byte("file.txt"),
            Attr: fuse.Attr{
                Mode: 0644,
            },
        }, fs.StableAttr{Ino: 2})
    r.AddChild("file.txt", ch, false)
}
func (r *HelloRoot) Getattr(ctx context.Context, fh fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
    out.Mode = 0755
    return 0
}
var _ = (fs.NodeGetattrer)((*HelloRoot)(nil))
var _ = (fs.NodeOnAdder)((*HelloRoot)(nil))
func main() {
    debug := flag.Bool("debug", false, "print debug data")
    flag.Parse()
    if len(flag.Args()) < 1 {
        log.Fatal("Usage:
  hello MOUNTPOINT")
    }
    opts := &fs.Options{}
    opts.Debug = *debug
    server, err := fs.Mount(flag.Arg(0), &HelloRoot{}, opts)
    if err != nil {
        log.Fatalf("Mount fail: %v
", err)
    }
    server.Wait()
}

构建&&运行

  • 构建
go build
  • 运行
mkdir /demo
./demoapp /demo
  • 效果

参考资料

https://github.com/hanwen/go-fuse

原文地址:https://www.cnblogs.com/rongfengliang/p/14045393.html