createramdisk.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/python3
  2. """
  3. Generates, from this source repository, a "tarramdisk" - a ustar archive
  4. suitable for booting ToaruOS.
  5. """
  6. import tarfile
  7. users = {
  8. 'root': 0,
  9. 'local': 1000,
  10. }
  11. restricted_files = {
  12. 'etc/master.passwd': 0o600,
  13. 'etc/sudoers': 0o600,
  14. 'tmp': 0o777,
  15. 'var': 0o755,
  16. 'bin/sudo': 0o4555,
  17. 'bin/gsudo': 0o4555,
  18. }
  19. def file_filter(tarinfo):
  20. # Root owns files by default.
  21. tarinfo.uid = 0
  22. tarinfo.gid = 0
  23. if tarinfo.name.startswith('home/'):
  24. # Home directory contents are owned by their users.
  25. user = tarinfo.name.split('/')[1]
  26. tarinfo.uid = users.get(user,0)
  27. tarinfo.gid = tarinfo.uid
  28. elif tarinfo.name in restricted_files:
  29. tarinfo.mode = restricted_files[tarinfo.name]
  30. if tarinfo.name.startswith('src'):
  31. # Let local own the files here
  32. tarinfo.uid = users.get('local')
  33. tarinfo.gid = tarinfo.uid
  34. # Skip object files
  35. if tarinfo.name.endswith('.so') or tarinfo.name.endswith('.o'):
  36. return None
  37. return tarinfo
  38. with tarfile.open('fatbase/ramdisk.img','w') as ramdisk:
  39. ramdisk.add('base',arcname='/',filter=file_filter)
  40. ramdisk.add('.',arcname='/src',filter=file_filter,recursive=False) # Add a src directory
  41. ramdisk.add('apps',arcname='/src/apps',filter=file_filter)
  42. ramdisk.add('kernel',arcname='/src/kernel',filter=file_filter)
  43. ramdisk.add('linker',arcname='/src/linker',filter=file_filter)
  44. ramdisk.add('lib',arcname='/src/lib',filter=file_filter)
  45. ramdisk.add('libc',arcname='/src/libc',filter=file_filter)
  46. ramdisk.add('boot',arcname='/src/boot',filter=file_filter)
  47. ramdisk.add('modules',arcname='/src/modules',filter=file_filter)
  48. ramdisk.add('util/build-the-world.py',arcname='/usr/bin/build-the-world.py',filter=file_filter)