---
title: "FUSE简介，安装，部分代码"
author: "湖大-软件-杨锦涛"
date: "2014-08-02T11:46:02Z"
tags: []
categories: ["开源软件"]
slug: "fuse"
draft: false
lastmod: "2014-08-02T13:20:43Z"
id: 2099345
url: "https://979473618.iteye.com/blog/2099345"
---

**FUSE(用户文件系统)**

**一，fuse介绍**

FUSE实现实现了内核与用户之间的隔离，用户想要写一个文件系统只需修改根据用户想要实现的操作来改写fuse提供的API就可以了，这里还要提一个挂载的概念，就是将一个目录添加到了另一个目录下（类似于U盘一样），然后。可以对这个目录中的数据进行操作，就是挂载

 

二，fuse安装

本文末尾有fuse-2.9.3.tar.gz 下载包

解压：tar -zxvf fuse-2.9.3.tar.gz

进入解压包：cd fuse-2.9.3

安装：./configure (后面可以加  --prefis=/目录  这样可以指定安装路径，有时候系统为64位时，文件会被安装到了/usr/local/bli)

编译:make

安装:make install

卸载摸个目录下的挂载：fusermount -u 目录

 

用python 编辑fuse需要安装fusepy

下载fusepy（本文附带fusepy-2.0.0.tar.gz的下载包）

解压：tar -zxvf fusepy 

进入解压后的文件夹：cd fusepy-2.0.2

这时候你会看到setup.py 

运行fusepy：python setup.py install

然后回到有你的修改好的文件的文件夹中（文件以.py结尾）

运行：python 文件名 挂载文件夹 挂载路径（这个文件夹要为空才行）

如：\[root@localhost fuse]# python test.py /usr  /nn（这里nn文件夹要为空哦）

然后你就看到程序“停止了”其实已经挂载成功了

重新打开一个窗口，进入你的挂载路径，然后ls,你会发现这个文件夹下面的目录和你挂载的文件夹目录一样

 

\[root@localhost usr]# ls

bin  etc  fusetest  games  include  java  lib  lib64  libexec  local  sbin  share  src  tmp

\[root@localhost usr]# cd /yjt/jj/

\[root@localhost jj]# ls

bin  etc  fusetest  games  include  java  lib  lib64  libexec  local  sbin  share  src  tmp

\[root@localhost jj]# 

 

然后还可以里面的文件进行操作哦

 

**三，fuse目录**

 1 ./doc 包含FUSE相关文档

 2 ./include 包含了FUSE API头，对创建文件系统有用，主要用fuse.h

 3 ./lib 存放FUSE库的示代示

 4 ./util 包含了FUSE工具库的示代示

 5 ./example 参考的示子

四，关于fuse API修改

如果你用的是C的话，必须对C相当了解这里重点说python的修改

主要用到了python的os，os.path两个库可以试着去先看一下这些库里面函数的用法API的修改会容易的多

如文件的重命名函数的重写

    def rename(self, old, new):

        return os.rename(self.\_full\_path(old), self.\_full\_path(new))

就只用调用os.rename（）这个函数就可以了

这里提供一个fuse的案例大家可以看一下

```
#!/usr/bin/env python
```

```

```

```
from __future__ import with_statement
```

```

```

```
import os
```

```
import sys
```

```
import errno
```

```

```

```
from fuse import FUSE, FuseOSError, Operations
```

```

```

```

```

```
class Passthrough(Operations):
```

```
    def __init__(self, root):
```

```
        self.root = root
```

```

```

```
    # Helpers
```

```
    # =======
```

```

```

```
    def _full_path(self, partial):
```

```
        if partial.startswith("/"):
```

```
            partial = partial[1:]
```

```
        path = os.path.join(self.root, partial)
```

```
        return path
```

```

```

```
    # Filesystem methods
```

```
    # ==================
```

```

```

```
    def access(self, path, mode):
```

```
        full_path = self._full_path(path)
```

```
        if not os.access(full_path, mode):
```

```
            raise FuseOSError(errno.EACCES)
```

```

```

```
    def chmod(self, path, mode):
```

```
        full_path = self._full_path(path)
```

```
        return os.chmod(full_path, mode)
```

```

```

```
    def chown(self, path, uid, gid):
```

```
        full_path = self._full_path(path)
```

```
        return os.chown(full_path, uid, gid)
```

```

```

```
    def getattr(self, path, fh=None):
```

```
        full_path = self._full_path(path)
```

```
        st = os.lstat(full_path)
```

```
        return dict((key, getattr(st, key)) for key in ('st_atime', 'st_ctime',
```

```
                     'st_gid', 'st_mode', 'st_mtime', 'st_nlink', 'st_size', 'st_uid'))
```

```

```

```
    def readdir(self, path, fh):
```

```
        full_path = self._full_path(path)
```

```

```

```
        dirents = ['.', '..']
```

```
        if os.path.isdir(full_path):
```

```
            dirents.extend(os.listdir(full_path))
```

```
        for r in dirents:
```

```
            yield r
```

```

```

```
    def readlink(self, path):
```

```
        pathname = os.readlink(self._full_path(path))
```

```
        if pathname.startswith("/"):
```

```
            # Path name is absolute, sanitize it.
```

```
            return os.path.relpath(pathname, self.root)
```

```
        else:
```

```
            return pathname
```

```

```

```
    def mknod(self, path, mode, dev):
```

```
        return os.mknod(self._full_path(path), mode, dev)
```

```

```

```
    def rmdir(self, path):
```

```
        full_path = self._full_path(path)
```

```
        return os.rmdir(full_path)
```

```

```

```
    def mkdir(self, path, mode):
```

```
        return os.mkdir(self._full_path(path), mode)
```

```

```

```
    def statfs(self, path):
```

```
        full_path = self._full_path(path)
```

```
        stv = os.statvfs(full_path)
```

```
        return dict((key, getattr(stv, key)) for key in ('f_bavail', 'f_bfree',
```

```
            'f_blocks', 'f_bsize', 'f_favail', 'f_ffree', 'f_files', 'f_flag',
```

```
            'f_frsize', 'f_namemax'))
```

```

```

```
    def unlink(self, path):
```

```
        return os.unlink(self._full_path(path))
```

```

```

```
    def symlink(self, target, name):
```

```
        return os.symlink(self._full_path(target), self._full_path(name))
```

```

```

```
    def rename(self, old, new):
```

```
        return os.rename(self._full_path(old), self._full_path(new))
```

```

```

```
    def link(self, target, name):
```

```
        return os.link(self._full_path(target), self._full_path(name))
```

```

```

```
    def utimens(self, path, times=None):
```

```
        return os.utime(self._full_path(path), times)
```

```

```

```
    # File methods
```

```
    # ============
```

```

```

```
    def open(self, path, flags):
```

```
        full_path = self._full_path(path)
```

```
        return os.open(full_path, flags)
```

```

```

```
    def create(self, path, mode, fi=None):
```

```
        full_path = self._full_path(path)
```

```
        return os.open(full_path, os.O_WRONLY | os.O_CREAT, mode)
```

```

```

```
    def read(self, path, length, offset, fh):
```

```
        os.lseek(fh, offset, os.SEEK_SET)
```

```
        return os.read(fh, length)
```

```

```

```
    def write(self, path, buf, offset, fh):
```

```
        os.lseek(fh, offset, os.SEEK_SET)
```

```
        return os.write(fh, buf)
```

```

```

```
    def truncate(self, path, length, fh=None):
```

```
        full_path = self._full_path(path)
```

```
        with open(full_path, 'r+') as f:
```

```
            f.truncate(length)
```

```

```

```
    def flush(self, path, fh):
```

```
        return os.fsync(fh)
```

```

```

```
    def release(self, path, fh):
```

```
        return os.close(fh)
```

```

```

```
    def fsync(self, path, fdatasync, fh):
```

```
        return self.flush(path, fh)
```

```

```

```

```

```
def main(mountpoint, root):
```

```
   FUSE(Passthrough(root), mountpoint, foreground=True)
```

```
if __name__ == '__main__':
```

```
    main(sys.argv[2], sys.argv[1])
```

将这一个代码复制，然后命名为.py文件,可能会有缩进的问题，大家修改一下，按照上面的提示

\[root@localhost fuse]# python test.py /usr  /nn

即可运行