Search code examples
pythonfuse

mounting fuse gives invalid argument error in python


I have written python code to a mount fuse on a point mount. It always gives invalid argument error. I have tried the same program in C and it works fine. Can any of python Guru's help me out in finding what the problem is, I have pasted the code here.

#!/usr/bin/python

import stat
import os
import ctypes
from ctypes.util import find_library

libc = ctypes.CDLL (find_library ("c"))

def fuse_mount_sys (mountpoint, fsname):
        fd = file.fileno (file ("/dev/fuse", 'w+'))
        if fd < 0:
                raise OSError("Could not open /dev/fuse")

        mnt_param = "%s,fd=%i,rootmode=%o,user_id=%i,group_id=%i" \
                        % ("allow_other,default_permissions,max_read=131072", \
                        fd, stat.S_IFDIR, os.getuid(), os.getgid())

        ret = libc.mount ("fuse", "/mount", "fuse", 0, mnt_param)
        if ret < 0:
                raise OSError("mount failed with code " + str(ret))
        return fd

fds = fuse_mount_sys ("/mount", "fuse")

mount syntax is:

int mount(const char *source, const char *target,
                 const char *filesystemtype, unsigned long mountflags,
                 const void *data);

I tried using swig and also writing the program using in C and then creating a .so from it and they worked. But I am interested in writing in pure python. Thanks in advance.

output of strace:

$ strace -s 100 -v -e mount python fuse-mount.py 
mount("fuse", "/mount", "fuse", 0, "allow_other,default_permissions,max_read=131072,fd=3,rootmode=40000,user_id=0,group_id=0") = -1 EINVAL (Invalid argument)


$ strace -s 100 -v -e mount ./a.out 
mount("fuse", "/mount", "fuse", 0, "allow_other,default_permissions,max_read=131072,fd=3,rootmode=40000,user_id=0,group_id=0") = 0

Solution

  • ctypes.c_void_p cannot be initialized with a string. Instead, just use a string without c_void_p.

    You can then compare the output of

    strace -v -e mount python mymount.py
    

    and

    strace -v -e mount ./mymount-c
    

    until they match.

    Also, make sure the file handle fd is still open when you call mount. file("/dev/fuse", 'w+') will be automatically garbage-collected and closed by some Python implementations, including cpython. You can prevent this by assigning the result of file("/dev/fuse") to a variable.