IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

jail.py 1.9KB

il y a 4 ans
il y a 4 ans
il y a 4 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import subprocess
  2. from .zfs import *
  3. import random
  4. import shutil
  5. import json
  6. def get_jid(path):
  7. data = json.loads(subprocess.check_output(['jls', '--libxo=json']))
  8. lst = data['jail-information']['jail']
  9. lst = list(filter(lambda a: a['path'] == path, lst))
  10. if len(lst) == 0:
  11. raise ValueError('JID not found for path: ' + path)
  12. if len(lst) > 1:
  13. raise ValueError('Ambiguous JID for path: ' + path)
  14. return str(lst[0]['jid'])
  15. def jail_run(path, command):
  16. command = ['jail', '-c', 'host.hostname=' + os.path.split(path)[1], 'persist=1', 'mount.devfs=1', 'interface=lo1', 'ip4.addr=127.0.1.0', 'path=' + path, 'command', '/bin/sh', '-c', command]
  17. print('Running:', ' '.join(command))
  18. try:
  19. res = subprocess.run(command)
  20. finally:
  21. try:
  22. subprocess.run(['jail', '-r', get_jid(path)])
  23. except ValueError:
  24. pass
  25. subprocess.run(['umount', '-f', os.path.join(path, 'dev')])
  26. if res.returncode != 0:
  27. # subprocess.run(['umount', os.path.join(path, 'dev')])
  28. raise RuntimeError('Command failed')
  29. def command_jail_run(args):
  30. base, _ = zfs_snapshot_by_tag_or_sha256(args.image)
  31. # root = '/'.join(base.split('/')[:-1])
  32. for _ in range(10**6):
  33. name = bytes([ random.randint(0, 255) for _ in range(4) ]).hex()[:7]
  34. name = base.split('/')[0] + '/focker/jails/' + name
  35. if not zfs_exists(name):
  36. break
  37. zfs_run(['zfs', 'clone', base, name])
  38. try:
  39. shutil.copyfile('/etc/resolv.conf', os.path.join(zfs_mountpoint(name), 'etc/resolv.conf'))
  40. jail_run(zfs_mountpoint(name), args.command)
  41. # subprocess.check_output(['jail', '-c', 'interface=lo1', 'ip4.addr=127.0.1.0', 'path=' + zfs_mountpoint(name), 'command', command])
  42. finally:
  43. # subprocess.run(['umount', zfs_mountpoint(name) + '/dev'])
  44. zfs_run(['zfs', 'destroy', '-f', name])
  45. # raise