This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 486
/
createramdisk.py
75 lines (62 loc) · 2.34 KB
/
createramdisk.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
#!/usr/bin/python3
"""
Generates, from this source repository, a "tarramdisk" - a ustar archive
suitable for booting ToaruOS.
"""
import os
import tarfile
users = {
'root': 0,
'local': 1000,
'guest': 1001,
}
restricted_files = {
'etc/master.passwd': 0o600,
'etc/sudoers': 0o600,
'tmp': 0o777,
'var': 0o755,
'bin/sudo': 0o4555,
'bin/gsudo': 0o4555,
}
def file_filter(tarinfo):
# Root owns files by default.
tarinfo.uid = 0
tarinfo.gid = 0
if tarinfo.name.startswith('home/'):
# Home directory contents are owned by their users.
user = tarinfo.name.split('/')[1]
tarinfo.uid = users.get(user,0)
tarinfo.gid = tarinfo.uid
elif tarinfo.name in restricted_files:
tarinfo.mode = restricted_files[tarinfo.name]
if tarinfo.name.startswith('usr/include/kuroko') and tarinfo.type == tarfile.SYMTYPE:
return None
if tarinfo.name.startswith('src'):
# Let local own the files here
tarinfo.uid = users.get('local')
tarinfo.gid = tarinfo.uid
# Skip object files
if tarinfo.name.endswith('.so') or tarinfo.name.endswith('.o') or tarinfo.name.endswith('.sys'):
return None
return tarinfo
def symlink(file,target):
ti = tarfile.TarInfo(file)
ti.type = tarfile.SYMTYPE
ti.linkname = target
return ti
with tarfile.open('ramdisk.igz','w:gz') as ramdisk:
ramdisk.add('base',arcname='/',filter=file_filter)
ramdisk.add('.',arcname='/src',filter=file_filter,recursive=False) # Add a src directory
ramdisk.add('apps',arcname='/src/apps',filter=file_filter)
ramdisk.add('kernel',arcname='/src/kernel',filter=file_filter)
ramdisk.add('linker',arcname='/src/linker',filter=file_filter)
ramdisk.add('lib',arcname='/src/lib',filter=file_filter)
ramdisk.add('libc',arcname='/src/libc',filter=file_filter)
ramdisk.add('boot',arcname='/src/boot',filter=file_filter)
ramdisk.add('modules',arcname='/src/modules',filter=file_filter)
if os.path.exists('tags'):
ramdisk.add('tags',arcname='/src/tags',filter=file_filter)
ramdisk.add('util/auto-dep.krk',arcname='/bin/auto-dep.krk',filter=file_filter)
ramdisk.add('kuroko/src/kuroko',arcname='/usr/include/kuroko',filter=file_filter)
ramdisk.addfile(symlink('bin/sh','esh'))
ramdisk.addfile(symlink('bin/mandelbrot','julia'))