8 import stat # for file properties
9 import os # for filesystem modes (O_RDONLY, etc)
10 import errno # for error number codes (ENOENT, etc)
11 # - note: these must be returned as negatives
15 import sipb_xen_database
17 fuse.fuse_python_api = (0, 2)
19 realpath = "/home/machines/"
23 Return the depth of a given path, zero-based from root ('/')
28 return path.count('/')
32 Return the slash-separated parts of a given path as a list
37 # [1:] because otherwise you get an empty list element from the
39 return path[1:].split('/')
55 return (self.st_mode, self.st_ino, self.st_dev, self.st_nlink, self.st_uid, self.st_gid, self.st_size, self.st_atime, self.st_mtime, self.st_ctime)
57 class ConsoleFS(Fuse):
59 ConsoleFS creates a series of subdirectories each mirroring the same real
60 directory, except for a single file - the .k5login - which is dynamically
61 generated for each subdirectory
63 This filesystem only implements the getattr, getdir, read, and readlink
64 calls, beacuse this is a read-only filesystem
67 def __init__(self, *args, **kw):
68 """Initialize the filesystem and set it to allow_other access besides
69 the user who mounts the filesystem (i.e. root)
71 Fuse.__init__(self, *args, **kw)
72 self.lasttime = time()
75 openlog('sipb-xen-consolefs ', LOG_PID, LOG_DAEMON)
77 syslog(LOG_DEBUG, 'Init complete.')
79 def mirrorPath(self, path):
80 """Translate a virtual path to its real path counterpart"""
81 return realpath + "/".join(getParts(path)[1:])
83 def getMachines(self):
84 """Get the list of VMs in the database, clearing the cache if it's
85 older than 15 seconds"""
86 if time() - self.lasttime > 15:
87 self.lasttime = time()
88 sipb_xen_database.clear_cache()
89 return [machine.name for machine in sipb_xen_database.Machine.select()]
91 def getUid(self, machine_name):
92 """Calculate the UID of a machine-account, which is just machine_id+1000
94 return sipb_xen_database.Machine.get_by(name=machine_name).machine_id + 1000
96 def getK5login(self, machine_name):
97 """Build the ACL for a machine and turn it into a .k5login file
99 machine = sipb_xen_database.Machine.get_by(name=machine_name)
100 users = [acl.user for acl in machine.acl]
101 return "\n".join(map(self.userToPrinc, users) + [''])
103 def userToPrinc(self, user):
104 """Convert Kerberos v4-style names to v5-style and append a default
105 realm if none is specified
108 (princ, realm) = user.split('@')
111 realm = "ATHENA.MIT.EDU"
113 return princ.replace('.', '/') + '@' + realm
115 def getattr(self, path):
117 - st_mode (protection bits)
118 - st_ino (inode number)
120 - st_nlink (number of hard links)
121 - st_uid (user ID of owner)
122 - st_gid (group ID of owner)
123 - st_size (size of file, in bytes)
124 - st_atime (time of most recent access)
125 - st_mtime (time of most recent content modification)
126 - st_ctime (platform dependent; time of most recent metadata change on Unix,
127 or the time of creation on Windows).
130 syslog(LOG_DEBUG, "*** getattr: " + path)
132 depth = getDepth(path)
133 parts = getParts(path)
138 st.st_mode = stat.S_IFDIR | 0755
140 # /foo is a directory if foo is a machine - otherwise it doesn't exist
142 if parts[-1] in self.getMachines():
143 st.st_mode = stat.S_IFDIR | 0755
145 # Homedirs should be owned by the user whose homedir it is
146 st.st_uid = st.st_gid = self.getUid(parts[0])
149 # Catch the .k5login file, because it's a special case
150 elif depth == 2 and parts[-1] == '.k5login':
151 st.st_mode = stat.S_IFREG | 0444
153 st.st_size = len(self.getK5login(parts[0]))
154 # The .k5login file should be owned by the user whose homedir it is
155 st.st_uid = st.st_gid = self.getUid(parts[0])
156 # For anything else, we get the mirror path and call out to the OS
158 stats = list(os.lstat(self.mirrorPath(path)))
159 # Shadow the UID and GID from the original homedir
160 stats[4:6] = [self.getUid(parts[0])] * 2
164 # This call isn't actually used in the version of Fuse on console, but we
165 # wanted to leave it implemented to ease the transition in the future
166 def readdir(self, path, offset):
167 """Return a generator with the listing for a directory
169 syslog(LOG_DEBUG, '*** readdir %s %s' % (path, offset))
170 for (value, zero) in self.getdir(path):
171 yield fuse.Direntry(value)
173 def getdir(self, path):
174 """Return a list of tuples of the form (item, 0) with the contents of
177 Fuse doesn't add '.' or '..' on its own, so we have to
179 syslog(LOG_DEBUG, '*** getdir %s' % path)
181 # '/' contains a directory for each machine
183 contents = self.getMachines()
184 # The directory for each machine contains the same files as the realpath
185 # but also the .k5login
187 # The list is converted to a set so that we can handle the case where
188 # there is already a .k5login in the realpath gracefully
189 elif getDepth(path) == 1:
190 contents = set(os.listdir(self.mirrorPath(path)) + ['.k5login'])
191 # If it's not the root of the homedir, just pass the call onto the OS
194 contents = os.listdir(self.mirrorPath(path))
195 # Format the list the way that Fuse wants it - and don't forget to add
197 return [(i, 0) for i in (list(contents) + ['.', '..'])]
199 def read(self, path, length, offset):
200 """Read length bytes starting at offset of path. In most cases, this
201 just gets passed on to the OS
203 syslog(LOG_DEBUG, '*** read %s %s %s' % (path, length, offset))
205 parts = getParts(path)
207 # If the depth is less than 2, then either it's a directory or the file
209 # (realistically this doesn't appear to ever happen)
210 if getDepth(path) < 2:
212 # If we're asking for a real .k5login file, then create it and return
213 # the snippet requested
214 elif parts[1:] == ['.k5login']:
215 if parts[0] not in self.getMachines():
218 return self.getK5login(parts[0])[offset:length + offset]
219 # Otherwise, pass the call onto the OS
220 # (note that the file will get closed when this call returns and the
221 # file descriptor goes out of scope)
223 fname = self.mirrorPath(path)
224 if not os.path.isfile(fname):
229 return f.read(length)
231 def readlink(self, path):
232 syslog(LOG_DEBUG, '*** readlink %s' % path)
234 # There aren't any symlinks here
235 if getDepth(path) < 2:
237 # But there might be here
239 return os.readlink(self.mirrorPath(path))
241 if __name__ == '__main__':
242 sipb_xen_database.connect('postgres://sipb-xen@sipb-xen-dev.mit.edu/sipb_xen')
244 ConsoleFS [mount_path]