createramdisk.py 1.9 KB

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