-
-
Notifications
You must be signed in to change notification settings - Fork 604
/
Copy pathgen-rofs-img.py
executable file
·369 lines (297 loc) · 11 KB
/
gen-rofs-img.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/python3
#
# Copyright (c) 2015 Carnegie Mellon University.
# All Rights Reserved.
#
# THIS SOFTWARE IS PROVIDED "AS IS," WITH NO WARRANTIES WHATSOEVER. CARNEGIE
# MELLON UNIVERSITY EXPRESSLY DISCLAIMS TO THE FULLEST EXTENT PERMITTEDBY LAW
# ALL EXPRESS, IMPLIED, AND STATUTORY WARRANTIES, INCLUDING, WITHOUT
# LIMITATION, THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE, AND NON-INFRINGEMENT OF PROPRIETARY RIGHTS.
#
# Released under a modified BSD license. For full terms, please see mfs.txt in
# the licenses folder or contact permi...@sei.cmu.edu.
#
# DM-0002621
#
#
# Copyright (C) 2017 Waldemar Kozaczuk
# Inspired by original MFS implementation by James Root from 2015
#
# This work is open source software, licensed under the terms of the
# BSD license as described in the LICENSE file in the top-level directory.
#
##################################################################################
# The layout of data on the disk in the block order:
#
# Super Block (512 bytes) that contains magic number and specifies meta
# information including block size and location and size of tables containing
# i-nodes, dentries and symbolic links
#
# Files data where each file is padded to 512 bytes block
#
# Table of directory entries referenced by index in directory i-node
# (each entry holds string with direntry name and i-node number)
#
# Table of symlinks referenced by symlink i-node (each entry holds symbolic link
# path string)
#
# Table of inodes where each specifies type (dir,file,symlink) and data offset
# (for files it is a block on a disk, for symlinks and directories it is an
# offset in one of the 2 tables above)
##################################################################################
import os, optparse, io
from struct import *
from ctypes import *
from manifest_common import add_var, expand, unsymlink, read_manifest, defines, strip_file
OSV_BLOCK_SIZE = 512
DIR_MODE = int('0x4000', 16)
REG_MODE = int('0x8000', 16)
LINK_MODE = int('0xA000', 16)
block = 0
class SuperBlock(Structure):
_fields_ = [
('magic', c_ulonglong),
('version', c_ulonglong),
('block_size', c_ulonglong),
('structure_info_first_block', c_ulonglong),
('structure_info_blocks_count', c_ulonglong),
('directory_entries_count', c_ulonglong),
('symlinks_count', c_ulonglong),
('inodes_count', c_ulonglong)
]
# data_offset and count represent different things depending on mode:
# file - number of first block on disk and size in bytes (number of blocks can be deduced)
# directory - index of the first entry in the directory entries array and number of entries
# symlink - index of the entry in the symlink entries array and 1
class Inode(Structure):
_fields_ = [
('mode', c_ulonglong),
('inode_no', c_ulonglong), #redundant
('data_offset', c_ulonglong),
('count', c_ulonglong) # either file size or children count
]
# Represents directory entry - file, subdirectory or symlink
# It has a name and i-node number so to know what type of
# entry it is one has to read the i-node
# filename (length: unsigned short followed by characters)
class DirectoryEntry(object):
def __init__(self,filename,inode_no):
self.filename = filename
self.inode_no = inode_no
def write(self,fp):
pos = fp.tell()
fp.write(c_ulonglong(self.inode_no))
fp.write(c_ushort(len(self.filename)))
fp.write(bytes(self.filename,'utf-8'))
return fp.tell() - pos
class SymbolicLink(object):
def __init__(self,path):
self.path = path
def write(self,fp):
pos = fp.tell()
fp.write(c_ushort(len(self.path)))
fp.write(bytes(self.path,'utf-8'))
return fp.tell() - pos
directory_entries = []
directory_entries_count = 0
symlinks = []
symlinks_count = 0
inodes = []
inodes_count = 1
def next_directory_entry(filename,inode_no):
global directory_entries
global directory_entries_count
directory_entry = DirectoryEntry(filename,inode_no)
directory_entries_count += 1
directory_entries.append(directory_entry)
return directory_entry
def next_symlink(path,directory):
global symlinks
global symlinks_count
symlink = SymbolicLink(path)
symlinks_count += 1
symlinks.append(symlink)
return symlink
def next_inode():
global inodes_count
global inodes
inode = Inode()
inode.inode_no = inodes_count
inodes_count += 1
inodes.append(inode)
return inode
def pad(fp, size):
fp.write(b'\0' * size)
return size
def write_initial_superblock(fp):
global block
pad(fp, OSV_BLOCK_SIZE) # superblock is empty at first
block += 1
def write_file(fp, path):
global block
total = 0
last = 0
with open(path, 'rb') as f:
while True:
chunk = f.read(OSV_BLOCK_SIZE)
if chunk:
last = len(chunk)
total += last
block += 1
fp.write(chunk)
else:
break
if total > 0:
pad(fp, OSV_BLOCK_SIZE - last)
return total
def write_inodes(fp):
global inodes
for inode in inodes:
fp.write(inode)
return len(inodes) * sizeof(Inode)
def write_array(fp, vals):
bytes_written = 0
for val in vals:
bytes_written += val.write(fp)
return bytes_written
def write_dir(fp, manifest, dirpath, parent_dir):
global directory_entries_count
directory_entry_inodes = []
for entry in manifest:
inode = next_inode()
directory_entry_inodes.append((entry,inode))
val = manifest.get(entry)
if type(val) is dict: # directory
inode.mode = DIR_MODE
count, directory_entries_index = write_dir(fp, val, dirpath + '/' + entry, manifest)
inode.count = count
inode.data_offset = directory_entries_index
else: # file or symlink
if val.startswith('->'): #symlink
inode.mode = LINK_MODE
global symlinks_count
inode.data_offset = symlinks_count
inode.count = 1
next_symlink(val[2:],manifest)
print('Link %s to %s' % (dirpath + '/' + entry, val[2:]))
else: #file
inode.mode = REG_MODE
global block
inode.data_offset = block
inode.count = write_file(fp, val)
print('Adding %s' % (dirpath + '/' + entry))
# This needs to be added so that later we can walk the tree
# when fining symlinks
manifest['.'] = manifest
manifest['..'] = parent_dir
this_directory_entries_index = directory_entries_count
for directory_entry_inode in directory_entry_inodes:
next_directory_entry(directory_entry_inode[0],directory_entry_inode[1].inode_no)
this_directory_entries_count = len(directory_entry_inodes)
return (this_directory_entries_count, this_directory_entries_index)
def write_fs(fp, manifest):
global block
global inodes
global directory_entries
global symlinks
root_inode = next_inode()
root_inode.mode = DIR_MODE
count, directory_entries_index = write_dir(fp, manifest.get(''), '', manifest)
root_inode.count = count
root_inode.data_offset = directory_entries_index
block_no = block
# Write directories entries array
bytes_written = write_array(fp,directory_entries)
bytes_written += write_array(fp,symlinks)
# Write inodes!
write_inodes(fp)
bytes_written += len(inodes) * sizeof(Inode)
return (block_no, bytes_written)
def gen_image(out, manifest):
print('Writing image')
fp = open(out, 'wb')
# write the initial superblock
write_initial_superblock(fp)
system_structure_block, bytes_written = write_fs(fp, manifest)
structure_info_last_block_bytes = bytes_written % OSV_BLOCK_SIZE
structure_info_blocks_count = bytes_written // OSV_BLOCK_SIZE + (1 if structure_info_last_block_bytes > 0 else 0)
pad(fp,OSV_BLOCK_SIZE - structure_info_last_block_bytes)
global inodes
global directory_entries
global symlinks
sb = SuperBlock()
sb.version = 1
sb.magic = int('0xDEADBEAD', 16)
sb.block_size = OSV_BLOCK_SIZE
sb.structure_info_first_block = system_structure_block
sb.structure_info_blocks_count = structure_info_blocks_count
sb.directory_entries_count = len(directory_entries)
sb.symlinks_count = len(symlinks)
sb.inodes_count = len(inodes)
print('First block: %d, blocks count: %d' % (sb.structure_info_first_block, sb.structure_info_blocks_count))
print('Directory entries count %d' % sb.directory_entries_count)
print('Symlinks count %d' % sb.symlinks_count)
print('Inodes count %d' % sb.inodes_count)
fp.seek(0)
fp.write(sb)
fp.close()
def parse_manifest(manifest):
manifest = [(x, y % defines) for (x, y) in manifest]
files = list(expand(manifest))
files = [(x, unsymlink(y)) for (x, y) in files]
file_dict = {}
def populate_with_directory_path(path,directory):
tokens = path.rstrip('/').split('/')
dictionary = directory
for token in tokens[:-1]:
dictionary = dictionary.setdefault(token, {})
return (dictionary,tokens)
for name, hostname in files:
p = file_dict
if hostname.startswith('->'):
p, tokens = populate_with_directory_path(name,p)
entry = tokens[len(tokens)-1]
p[entry] = hostname
else:
if hostname.endswith('-stripped.so'):
continue
hostname = strip_file(hostname)
if os.path.islink(hostname):
p, tokens = populate_with_directory_path(name,p)
entry = tokens[len(tokens)-1]
link = os.readlink(hostname)
p[entry] = '->%s' % link
elif os.path.isdir(hostname):
for token in name.rstrip('/').split('/'):
p = p.setdefault(token, {})
else:
p, tokens = populate_with_directory_path(name,p)
entry = tokens[len(tokens)-1]
p[entry] = hostname
return file_dict
def main():
make_option = optparse.make_option
opt = optparse.OptionParser(option_list=[
make_option('-o',
dest='output',
help='write to FILE',
metavar='FILE'),
make_option('-m',
dest='manifest',
help='read manifest from FILE',
metavar='FILE'),
make_option('-D',
type='string',
help='define VAR=DATA',
metavar='VAR=DATA',
action='callback',
callback=add_var),
])
(options, args) = opt.parse_args()
manifest = read_manifest(options.manifest)
outfile = os.path.abspath(options.output)
manifest = parse_manifest(manifest)
gen_image(outfile, manifest)
if __name__ == '__main__':
main()