toaruos/util/createramdisk.py

76 lines
2.3 KiB
Python
Raw Normal View History

2018-12-11 06:44:23 +03:00
#!/usr/bin/python3
"""
Generates, from this source repository, a "tarramdisk" - a ustar archive
suitable for booting ToaruOS.
"""
2019-07-09 15:18:20 +03:00
import os
2018-12-11 06:44:23 +03:00
import tarfile
users = {
'root': 0,
'local': 1000,
'guest': 1001,
2018-12-11 06:44:23 +03:00
}
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
2018-12-11 06:44:23 +03:00
if tarinfo.name.startswith('src'):
# Let local own the files here
tarinfo.uid = users.get('local')
tarinfo.gid = tarinfo.uid
# Skip object files
2021-12-11 00:48:21 +03:00
if tarinfo.name.endswith('.so') or tarinfo.name.endswith('.o') or tarinfo.name.endswith('.sys'):
2018-12-11 06:44:23 +03:00
return None
return tarinfo
2023-05-05 16:30:33 +03:00
def symlink(file,target):
ti = tarfile.TarInfo(file)
ti.type = tarfile.SYMTYPE
ti.linkname = target
return ti
2021-05-31 04:47:02 +03:00
with tarfile.open('ramdisk.igz','w:gz') as ramdisk:
2018-12-11 06:44:23 +03:00
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)
2021-12-11 00:48:21 +03:00
ramdisk.add('boot',arcname='/src/boot',filter=file_filter)
ramdisk.add('modules',arcname='/src/modules',filter=file_filter)
2019-07-09 15:18:20 +03:00
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)
2023-05-05 16:30:33 +03:00
ramdisk.addfile(symlink('bin/sh','esh'))
ramdisk.addfile(symlink('bin/mandelbrot','julia'))
2018-12-11 06:44:23 +03:00