挂载保险丝时出现无效参数错误
我写了一段Python代码来挂载一个点挂载的文件系统,但总是出现无效参数的错误。我用C语言写了同样的程序,结果运行得很好。有没有Python高手能帮我找出问题所在?我把代码贴在这里了。
#!/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")
挂载的语法是:
int mount(const char *source, const char *target,
const char *filesystemtype, unsigned long mountflags,
const void *data);
我尝试过使用swig,也用C语言写程序,然后从中创建了一个.so文件,结果都能正常工作。但我还是想用纯Python来写。提前谢谢大家。
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
1 个回答
2
ctypes.c_void_p
不能用字符串来初始化。你只需要直接使用字符串,不用加 c_void_p
。
然后你可以比较以下两个输出:
strace -v -e mount python mymount.py
和
strace -v -e mount ./mymount-c
直到它们匹配为止。
另外,确保在你调用 mount 的时候,文件句柄 fd
仍然是打开的。使用 file("/dev/fuse", 'w+')
的话,某些 Python 实现(包括 cpython)会自动把它回收并关闭。你可以通过把 file("/dev/fuse")
的结果赋值给一个变量来防止这种情况发生。