-
Notifications
You must be signed in to change notification settings - Fork 2
/
fuse_driver.py
143 lines (105 loc) · 3.6 KB
/
fuse_driver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from errno import ENOENT
import errno
from fuse import FUSE, FuseOSError, Operations
import logging
import argparse
import fs.squashfs
import fs.jffs2
import sys
log = logging.getLogger("imageIO")
log.setLevel(logging.INFO)
supported_filesystems = [fs.squashfs.SquashImage,
fs.jffs2.JffsImage]
class FSDriver(Operations):
def __init__(self, imgObj):
self.image = imgObj
self.fd = 0
# Filesystem methods
# ==================
def access(self, path, mode):
pass
def chmod(self, path, mode):
raise FuseOSError(errno.EROFS)
def chown(self, path, uid, gid):
raise FuseOSError(errno.EROFS)
def getattr(self, path, fh=None):
log.debug("[getattr] %s" % path)
attrs = self.image.getAttrs(path)
if attrs:
return attrs
raise FuseOSError(ENOENT)
def readdir(self, path, fh):
yield '.'
yield ".."
for item in self.image.listPath(path):
yield item
def readlink(self, path):
trg = self.image.getLnkTarget(path)
if trg:
return trg
return FuseOSError(errno.EROFS)
def mknod(self, path, mode, dev):
raise FuseOSError(errno.EROFS)
def rmdir(self, path):
raise FuseOSError(errno.EROFS)
def mkdir(self, path, mode):
raise FuseOSError(errno.EROFS)
def statfs(self, path):
return self.image.getStatFs()
def unlink(self, path):
raise FuseOSError(errno.EROFS)
def symlink(self, target, name):
raise FuseOSError(errno.EROFS)
def rename(self, old, new):
raise FuseOSError(errno.EROFS)
def link(self, target, name):
raise FuseOSError(errno.EROFS)
def utimens(self, path, times=None):
raise FuseOSError(errno.EROFS)
# File methods
# ============
def open(self, path, flags):
self.fd += 1
return self.fd
def create(self, path, mode, fi=None):
raise FuseOSError(errno.EROFS)
def read(self, path, length, offset, fh):
data = self.image.getFileData(path)
if data:
return data[offset: (offset + length)]
raise FuseOSError(errno.ENOENT)
def write(self, path, buf, offset, fh):
raise FuseOSError(errno.EROFS)
def truncate(self, path, length, fh=None):
raise FuseOSError(errno.EROFS)
def flush(self, path, fh):
pass
def release(self, path, fh):
pass
def fsync(self, path, fdatasync, fh):
pass
def main(fusebox, mountpoint, conf_file=None):
# Need to set user_allow_other in /etc/fuse.conf for
# allow_other option to work (or run this process as root)
# #fusebox = FuseBox(conf_file)
FUSE(fusebox, mountpoint, foreground=True, allow_other=True, nothreads=True)
if __name__ == '__main__':
p = argparse.ArgumentParser()
logging.basicConfig(level=logging.INFO)
p.add_argument("-d", "--debug", action='store_true', dest='debug',
help="turn on debugging output")
p.add_argument("-m", "--mount_point", required=True, help="Mount directory")
p.add_argument("rootfs", help="Image file to mount")
args = p.parse_args()
loglevel = logging.INFO
if args.debug:
loglevel = logging.DEBUG
log.setLevel(level=loglevel)
if args.mount_point and args.rootfs:
for fscls in supported_filesystems:
imgObj = fscls.createObject(args.rootfs, loglevel)
if imgObj:
main(FSDriver(imgObj), args.mount_point)
sys.exit(0)
log.warning("Unsupported image type!")
log.error("Check your parameters!")