티스토리 뷰

Study/System >

FUSE 활용하기

Jake Yoon 2013. 12. 19. 00:47

Dark Cloud 시스템을 만들면서 FUSE라는 것을 활용하게 되었는데, 

FUSE는 유닉스 계열 OS에 적재할 수 있는 커널 모듈로 File System in Userspace의 약자이다.

FUSE를 이용하면 컴퓨터에 OS와 같은 권한이 아닌 사용자가 커널 코드를 편집하지 않고도 자신의 파일 시스템을 만들 수 있다.


즉, 유저레벨에서 파일시스템을 만들고 해당 파일시스템에 발생하는 이벤트들을 쉽게 처리할 수 있다는 의미이다.

다시말하면, 사용자 정의의 파일시스템을 만들고 파일시스템에 이벤트가 발생했을 때 ( open, read, write, close ) 사용자의 다른 이벤트로 우회시켜서 처리할 때 유용하다는 것이다. 이 점이 마음에 들어 FUSE를 활용하게 되었다.


ubuntu 에서 fuse를 활용하려면 먼저 설치를 해야한다.


$ sudo apt-get install libfuse-dev


이어서 fuse공식 사이트에 올라와있는 fuse 예제를 통해 FUSE를 이해해보자 


hello.c /* fuse example source 작성 */

출처 : http://fuse.sourceforge.net/doxygen/index.html


/*
FUSE: Filesystem in Userspace
Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
This program can be distributed under the terms of the GNU GPL.
See the file COPYING.
*/
#define FUSE_USE_VERSION 30
#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);
}



$ mkdir mnt

$ ./hello mnt

$ cat mnt/hello

Hello World 메시지를 확인할 수 있다.

'Study > System &gt;' 카테고리의 다른 글

Module 2: Data Center Environment  (0) 2013.04.24
Module 1: Introduction to Information Storage  (0) 2013.04.22
LXR  (0) 2013.04.17
중첩 인터럽트(Nested Interrupt)  (0) 2013.04.01
O(1) 스케쥴러 vs CFS 스케쥴러  (0) 2013.03.14
댓글