fuse 虚拟文件系统 的 安装与使用

FUSE 是个好东西, 可以用在特殊的领域里面来实现自己的虚拟文件系统。

 

FUSE的下载与安装 :

从fuse官网下载最新安装包:http://sourceforge.net/projects/fuse/files/fuse-2.X/

 

安装很简单:

./configure

 make

 make install(注意:这一步要在root用户模式下做)

 

挂载 fuse 虚拟文件系统:

 

modprobe fuse

 

查看fuse是否挂载成功:

 

lsmod|grep fuse  执行该命令后会输出

 

[root@localhost fuse]# lsmod | grep fuse
fuse                   49237  2

证明挂载成功

 

 

下来就是使用的问题了, 呵呵。 

 

安装完fuse后,其子文件夹example下有一些做好的文件系统实例,教你挂载和使用该文件系统,推荐使用 fusexmp_fh.c 这个例子

因为他包括了所有的文件操作相关的映射。

 

编译 fusexmp_fh.c 需要用下面的编译命令:

 

gcc -Wall `pkg-config fuse --cflags --libs` -lulockmgr fusexmp_fh.c -o fusexmp_fh.exe

 

编译后生成 fusexmp_fh.exe 可执行文件

 

在 /mnt 目录下面建立文件夹 fuse

 

cd  /mnt

mkdir  fuse

 

然后到 fusexmp_fh.exe 目录去执行

 

./fusexmp_fh.exe  /mnt/fuse  -d

 

即可启动虚拟文件系统, 这个时候执行

 

df  命令,可以看到 虚拟文件系统挂载出来了

 

[root@localhost example]# df
fuse                   6824296   3499688   2972352  55% /mnt/fuse

 

 

这个时候你进入  /mnt/fuse 目录

 

执行  ls , 即可看到映射的文件内容, 在这个目录下面所作的文件操作,都会调用到  fusexmp_fh.exe 这个进程里面注册的

文件操作函数。

 

注册句柄如下:

 

 

 

比如你执行  mkdir  xxx , 则会调用到 fusexmp_fh.exe 进程的 int xmp_mkdir(const char *path, mode_t mode) 这个函数。

 

你可以修改这个函数的实现, 在里面添加你自己的信息。

 

当出现下面的错误提示:

 

fuse: bad mount point `/mnt/fuse': Transport endpoint is not connected

 

你只需要执行

 

umount -l /mnt/fuse  命令即可化解上面的错误, 具体原因就不用说了吧,  umount 这么明显。

 

我们这次使用 fuse 使用, 主要是要实现一个云的功能。

 

 

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
Fuse(Filesystem in Userspace)是一个允许非特权用户在用户空间中实现自己的文件系统的框架。Fuse可以允许用户创建虚拟文件系统,将不同的物理文件夹组合为单个文件夹等等。 在Linux系统中,Fuse可以通过Fuse API实现自定义文件系统Fuse API提供了一组C语言函数,可以实现文件系统的挂载、卸载、读写、文件创建和删除等基本操作。用户可以通过Fuse API编写自己的文件系统模块,然后将其挂载到本地文件系统中。 创建一个Fuse文件系统的基本步骤是: 1. 安装Fuse库和相关的开发工具。 2. 编写Fuse文件系统程序并通过gcc进行编译。 3. 执行Fuse文件系统程序,将其挂载到Linux本地文件系统中。 4. 通过系统的标准文件操作接口来使用Fuse文件系统。 下面是一个示例程序,它可以创建一个简单的Fuse文件系统: 1. 首先,安装Fuse应用程序和开发包。这里以Ubuntu为例: sudo apt-get install fuse libfuse-dev 2. 编写Fuse文件系统程序。下面的程序实现了一个简单的只读文件系统,它将远程的文件读取并映射到本地的文件系统中: ``` #include <fuse.h> #include <stdio.h> #include <string.h> #include <errno.h> #include <fcntl.h> static const char *hello_str = "Hello World!\n"; static const char *hello_path = "/hello"; static int hello_getattr(const char *path, struct stat *stbuf) { int res = 0; memset(stbuf, 0, sizeof(struct stat)); if (strcmp(path, "/") == 0) { stbuf->st_mode = S_IFDIR | 0755; stbuf->st_nlink = 2; } else if (strcmp(path, hello_path) == 0) { stbuf->st_mode = S_IFREG | 0444; stbuf->st_nlink = 1; stbuf->st_size = strlen(hello_str); } else res = -ENOENT; return res; } static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) { (void) offset; (void) fi; if (strcmp(path, "/") != 0) return -ENOENT; filler(buf, ".", NULL, 0); filler(buf, "..", NULL, 0); filler(buf, hello_path + 1, NULL, 0); return 0; } static int hello_open(const char *path, struct fuse_file_info *fi) { if (strcmp(path, hello_path) != 0) return -ENOENT; if ((fi->flags & 3) != O_RDONLY) return -EACCES; return 0; } static int hello_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { size_t len; (void) fi; if(strcmp(path, hello_path) != 0) return -ENOENT; len = strlen(hello_str); if (offset < len) { if (offset + size > len) size = len - offset; memcpy(buf, hello_str + offset, size); } else size = 0; return size; } static struct fuse_operations hello_oper = { .getattr = hello_getattr, .readdir = hello_readdir, .open = hello_open, .read = hello_read, }; int main(int argc, char *argv[]) { return fuse_main(argc, argv, &hello_oper, NULL); } ``` 3. 编译程序: gcc -Wall hello.c -o hello `pkg-config fuse --cflags --libs` 4. 在本地文件系统中创建一个挂载点: mkdir /tmp/myfuse 5. 运行Fuse文件系统程序: ./hello /tmp/myfuse 6. 使用cat或其他标准文件操作接口,读取`/hello`文件: cat /tmp/myfuse/hello 输出:Hello World! Fuse文件系统提供了一种强大而灵活的方法来扩展和定制Linux文件系统,通过定制Fuse文件系统可以实现各种场景下的文件读写、解压缩、加密解密等操作。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

langeldep

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值