Writing flexible filesystems with

FUSE-Python

Anurag PatelRed Hat, Pune

Also available at

http://xinh.org/fuse-python

Talk overview

  • UNIX based filesystems
  • What's FUSE?
  • FUSE API overview
  • ToyFS
  • Q&A

On filesystems

“One of the real contributions of UNIX has been the view that everything is a file.”

Filesystems

FS Platform
vfatWindows
hfs+OS X
ext4Linux
xfsIRIX, Linux
nfsLinux*
iso9660CD-ROM
fuseLinux*
# cat /proc/filesystems

Files

Symbol Meaning
dDirectory
lSymbolic link
cCharacter device
bBlock device
sSocket
pNamed pipe
-Regular file

FUSE

Filesystem in USErspace

Virtual memory segregation

Virtual memory

Why FUSE?

  • Usable by unprivileged users
  • Easier development cycle
  • Easy to install (apt-get install sshfs)
  • Multiple language support and bindings
  • C, C++, Python, Java, Ruby, Perl, Golang, Lua
  • Ported to FreeBSD, Mac OSX, OpenSolaris

FUSE overview

FUSE structure

How do I install FUSE?

# yum install fuse fuse-python
# apt-get install fuse python-fuse

Loading fuse

# modprobe fuse
# lsmod | grep fuse

Filesystem class

Subclass fuse.Fuse class and implement a number of methods.

Mount filesystem

Instantiate the Filesystem class and call main()

mount

$ python toyfs.py /tmp/toy

getattr(path)

Defining this method is mandatory for a working filesystem.

The stat structure

Member Description
st_modeInode protection mode
st_inoFile serial number
st_devDevice ID
st_nlinkNumber of hard links
st_uidUser ID of file
st_gidGroup ID of file
st_sizeFile size in bytes
st_atimeTime of last access
st_mtimeTime of last data modification
st_ctimeTime of last status change

ref: <sys/stat.h>

$ stat /etc/fstab

[anurag@zomg toyfs]$ stat /etc/fstab 
  File: ‘/etc/fstab’
  Size: 481             Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 259076      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:etc_t:s0
Access: 2015-02-20 14:46:16.248920273 +0530
Modify: 2015-02-11 20:42:51.750210844 +0530
Change: 2015-02-11 21:56:52.520767293 +0530
 Birth: -
[anurag@zomg toyfs]$

st_mode

st.st_mode = stat.S_IFDIR | 0755
Each bit of the output is 0 if the corresponding bit of x AND of y is 0, otherwise it's 1

ST_MODE flags

Type Flag
dstat.S_IFDIR
lstat.S_IFLNK
cstat.S_IFCHR
bstat.S_IFBLK
sstat.S_IFSOCK
pstat.S_IFIFO
-stat.S_IFREG

ref: Python stat module

readdir(path)

Read directory contents

Reading files

open(path, flags)
read(path, length, offset)

Filesystem methods

General File operation
getattr(path)open(path, flags)
mkdir(path, mode)create(path, flags, mode)
rename(old, new)read(path, length, offset)
mknod(path, mode, rdev)write(path, buf, offset)
link(target, name)fgetattr(path)
symlink(target, name)ftruncate(path, len)
readlink(path)flush(path)
unlink(path)release(path)
fsinit(self)fsync(path, fdatasync)

FUSE filesystems

fuse-zip, rarfs, mysqlfs, cryptfs, httpfs, sshfs, imapfs, gmailfs, flickrfs, ntfs-3g, gitfs, and many many more...

THE END

ToyFS on Github →
FUSE Project →
FUSE Python reference →

@gnuraganurag@redhat.com