在内存FUSE文件系统中

Gau*_*tam 12 linux filesystems fuse

编写一个存储在内存中的简单FUSE文件系统.文件系统必须支持以下命令:

ls,mkdir,cp

这个问题最近在接受采访时被问到,我无法回答.所以我决定学习它.

做了一些搜索,找到了一些关于构建我自己的FUSE文件系统的指南.我真的对如何在内存中实现文件系统毫无头绪.

我的问题是

  • 我正朝着正确的方向前进吗?
  • 我还应该读些什么?
  • 解决办法是什么 ?

我正在阅读的链接:

在最后一个链接中,提到了使用PyFileSystem进行内存中缓存.我不确定这可能会有什么帮助.

PS:这是一个书面采访问题,所以答案必须足够简单,在10-15分钟内写在纸上.

Sus*_*ant 5

我已经开始了一个课程,我们必须建立一个类似于Frangipani设计的内存分布式文件系统.该课程受麻省理工学院分布式系统课程的启发.做他们的前几个实验作业将是一个很好的练习.

本教程也非常有用.


bbe*_*ort 5

您没有指定编程语言,尽管 FUSE 是本机 C++,但有本机 Golang 绑定,在bazil.org/fuse实现。

我想说答案的主要部分需要包括以下内容:

  1. 处理内存中文件系统树的数据结构
  2. 节点的描述及其与 iNode 的关系
  3. 用于捕获 FUSE 服务器请求以处理 cli 命令的挂钩
  4. 使用 FUSE 服务器安装文件夹的说明。

我最近使用此适配器编写了一个内存文件系统:github.com/bbengfort/memfs。我关于其性能的文章在这里:带有 FUSE 的内存文件系统。很快,我做出了一些选择:

内存数据结构包含 2 个主要结构,dir 和 file,它们都是节点:

type Node struct {
    ID uint64 
    Name string 
    Attrs fuse.Attr 
    Parent *Dir 
}

type Dir struct {
    Node
    Children map[string]Node
}

type File struct {
    Node
    Data []byte 
}
Run Code Online (Sandbox Code Playgroud)

Children正如您所看到的,这是一个简单的树,可以通过和链接上下遍历Parent。File的属性Data保存了文件的所有内容。因此,文件系统只需要创建一个"\"在挂载点调用的“根”目录,然后将 on mkdiraDir添加到其子目录,然后添加on cpa 。File在 Go 中,这很简单:

type FS struct {
    root *Dir 
}

func Mount(path string) error {

    // Unmount the FS in case it was mounted with errors.
    fuse.Unmount(path)

    // Mount the FS with the specified options
    conn, err := fuse.Mount(path)
    if err != nil {
        return err
    }

    // Ensure that the file system is shutdown
    defer conn.Close()

    // Create the root dir and file system 
    memfs := FS{
        root: &Dir{
            ID: 1, 
            Name: "\", 
            Parent: nil, 
        },
    }

    // Serve the file system
    if err := fs.Serve(conn, memfs); err != nil {
        return err
    }
}
Run Code Online (Sandbox Code Playgroud)

现在您需要钩子来实现各种 FUSE 请求和调用。这是一个示例mkdir

func (d *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
    // Update the directory Atime
    d.Attrs.Atime = time.Now()

    // Create the child directory
    c := new(Dir)
    c.Init(req.Name, req.Mode, d)

    // Set the directory's UID and GID to that of the caller
    c.Attrs.Uid = req.Header.Uid
    c.Attrs.Gid = req.Header.Gid

    // Add the directory to the directory
    d.Children[c.Name] = c

    // Update the directory Mtime
    d.Attrs.Mtime = time.Now()

    return c, nil
}
Run Code Online (Sandbox Code Playgroud)

最后,通过讨论如何编译和运行服务器、安装到路径以及 FUSE 如何拦截内核调用并将它们传递到用户空间中的进程来结束面试问题。