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!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import subprocess
  2. from .zfs import *
  3. import random
  4. import shutil
  5. import json
  6. from tabulate import tabulate
  7. import os
  8. import jailconf
  9. from .mount import getmntinfo
  10. import shlex
  11. # import pdb
  12. def jail_fs_create(image=None):
  13. sha256 = bytes([ random.randint(0, 255) for _ in range(32) ]).hex()
  14. lst = zfs_list(fields=['focker:sha256'], focker_type='image')
  15. lst = list(filter(lambda a: a[0] == sha256, lst))
  16. if lst:
  17. raise ValueError('Whew, a collision...')
  18. poolname = zfs_poolname()
  19. for pre in range(7, 32):
  20. name = poolname + '/focker/jails/' + sha256[:pre]
  21. if not zfs_exists(name):
  22. break
  23. if image:
  24. image, _ = zfs_find(image, focker_type='image', zfs_type='snapshot')
  25. zfs_run(['zfs', 'clone', '-o', 'focker:sha256=' + sha256, image, name])
  26. else:
  27. print('Creating empty jail:', name)
  28. zfs_run(['zfs', 'create', '-o', 'focker:sha256=' + sha256, name])
  29. return name
  30. def gen_env_command(command, env):
  31. if any(map(lambda a: ' ' in a, env.keys())):
  32. raise ValueError('Environment variable names cannot contain spaces')
  33. env = [ 'export ' + k + '=' + shlex.quote(v) \
  34. for (k, v) in env.items() ]
  35. command = ' && '.join(env + [ command ])
  36. return command
  37. def quote(s):
  38. s = s.replace('\\', '\\\\')
  39. s = s.replace('\'', '\\\'')
  40. s = '\'' + s + '\''
  41. return s
  42. def jail_create(path, command, env, mounts, hostname=None):
  43. name = os.path.split(path)[-1]
  44. if os.path.exists('/etc/jail.conf'):
  45. conf = jailconf.load('/etc/jail.conf')
  46. else:
  47. conf = jailconf.JailConf()
  48. conf[name] = blk = jailconf.JailBlock()
  49. blk['path'] = path
  50. if command:
  51. command = gen_env_command(command, env)
  52. command = quote(command)
  53. print('command:', command)
  54. blk['exec.start'] = command
  55. prestart = [ 'cp /etc/resolv.conf ' +
  56. shlex.quote(os.path.join(path, 'etc/resolv.conf')) ]
  57. poststop = []
  58. if mounts:
  59. for (from_, on) in mounts:
  60. if not from_.startswith('/'):
  61. from_, _ = zfs_find(from_, focker_type='volume')
  62. from_ = zfs_mountpoint(from_)
  63. prestart.append('mount -t nullfs ' + shlex.quote(from_) +
  64. ' ' + shlex.quote(os.path.join(path, on.strip('/'))))
  65. poststop += [ 'umount -f ' +
  66. os.path.join(path, on.strip('/')) \
  67. for (_, on) in reversed(mounts) ]
  68. if prestart:
  69. blk['exec.prestart'] = quote(' && '.join(prestart))
  70. if poststop:
  71. blk['exec.poststop'] = quote(' && '.join(poststop))
  72. blk['persist'] = True
  73. blk['interface'] = 'lo1'
  74. blk['ip4.addr'] = '127.0.1.0'
  75. blk['mount.devfs'] = True
  76. blk['exec.clean'] = True
  77. blk['host.hostname'] = hostname or name
  78. conf.write('/etc/jail.conf')
  79. return name
  80. def get_jid(path):
  81. data = json.loads(subprocess.check_output(['jls', '--libxo=json']))
  82. lst = data['jail-information']['jail']
  83. lst = list(filter(lambda a: a['path'] == path, lst))
  84. if len(lst) == 0:
  85. raise ValueError('JID not found for path: ' + path)
  86. if len(lst) > 1:
  87. raise ValueError('Ambiguous JID for path: ' + path)
  88. return str(lst[0]['jid'])
  89. def do_mounts(path, mounts):
  90. print('mounts:', mounts)
  91. for (source, target) in mounts:
  92. if source.startswith('/'):
  93. name = source
  94. else:
  95. name, _ = zfs_find(source, focker_type='volume')
  96. name = zfs_mountpoint(name)
  97. while target.startswith('/'):
  98. target = target[1:]
  99. subprocess.check_output(['mount', '-t', 'nullfs',
  100. shlex.quote(name), shlex.quote(os.path.join(path, target))])
  101. def undo_mounts(path, mounts):
  102. for (_, target) in reversed(mounts):
  103. while target.startswith('/'):
  104. target = target[1:]
  105. subprocess.check_output(['umount', '-f',
  106. shlex.quote(os.path.join(path, target))])
  107. def jail_run(path, command, mounts=[]):
  108. 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]
  109. print('Running:', ' '.join(command))
  110. try:
  111. do_mounts(path, mounts)
  112. shutil.copyfile('/etc/resolv.conf', os.path.join(path, 'etc/resolv.conf'))
  113. res = subprocess.run(command)
  114. finally:
  115. try:
  116. subprocess.run(['jail', '-r', get_jid(path)])
  117. except ValueError:
  118. pass
  119. subprocess.run(['umount', '-f', os.path.join(path, 'dev')])
  120. undo_mounts(path, mounts)
  121. if res.returncode != 0:
  122. # subprocess.run(['umount', os.path.join(path, 'dev')])
  123. raise RuntimeError('Command failed')
  124. def jail_stop(path):
  125. try:
  126. jid = get_jid(path)
  127. jailname = os.path.split(path)[-1]
  128. subprocess.run(['jail', '-r', jailname])
  129. except ValueError:
  130. print('JID could not be determined')
  131. # import time
  132. # time.sleep(1)
  133. mi = getmntinfo()
  134. for m in mi:
  135. mntonname = m['f_mntonname'].decode('utf-8')
  136. if mntonname.startswith(path + os.path.sep):
  137. print('Unmounting:', mntonname)
  138. subprocess.run(['umount', '-f', mntonname])
  139. def jail_remove(path):
  140. print('Removing jail:', path)
  141. jail_stop(path)
  142. subprocess.run(['zfs', 'destroy', '-r', '-f', zfs_name(path)])
  143. if os.path.exists('/etc/jail.conf'):
  144. conf = jailconf.load('/etc/jail.conf')
  145. name = os.path.split(path)[-1]
  146. if name in conf:
  147. del conf[name]
  148. conf.write('/etc/jail.conf')
  149. def command_jail_create(args):
  150. name = jail_fs_create(args.image)
  151. if args.tags:
  152. zfs_tag(name, args.tags)
  153. path = zfs_mountpoint(name)
  154. jail_create(path, args.command,
  155. { a.split(':')[0]: ':'.join(a.split(':')[1:]) \
  156. for a in args.env },
  157. [ [a.split(':')[0], ':'.join(a.split(':')[1:])] \
  158. for a in args.mounts ],
  159. args.hostname )
  160. print(sha256)
  161. print(path)
  162. def command_jail_start(args):
  163. name, _ = zfs_find(args.reference, focker_type='jail')
  164. path = zfs_mountpoint(name)
  165. jailname = os.path.split(path)[-1]
  166. subprocess.run(['jail', '-c', jailname])
  167. def command_jail_stop(args):
  168. name, _ = zfs_find(args.reference, focker_type='jail')
  169. path = zfs_mountpoint(name)
  170. jail_stop(path)
  171. def command_jail_remove(args):
  172. name, _ = zfs_find(args.reference, focker_type='jail')
  173. path = zfs_mountpoint(name)
  174. jail_remove(path)
  175. def command_jail_exec(args):
  176. name, _ = zfs_find(args.reference, focker_type='jail')
  177. path = zfs_mountpoint(name)
  178. jid = get_jid(path)
  179. subprocess.run(['jexec', str(jid)] + args.command)
  180. def jail_oneshot(image, command, env, mounts):
  181. # pdb.set_trace()
  182. name = jail_fs_create(image)
  183. path = zfs_mountpoint(name)
  184. jailname = jail_create(path,
  185. ' '.join(map(shlex.quote, command or ['/bin/sh'])),
  186. env, mounts)
  187. subprocess.run(['jail', '-c', jailname])
  188. jail_remove(path)
  189. def command_jail_oneshot(args):
  190. env = { a.split(':')[0]: ':'.join(a.split(':')[1:]) \
  191. for a in args.env }
  192. mounts = [ [ a.split(':')[0], a.split(':')[1] ] \
  193. for a in args.mounts]
  194. jail_oneshot(args.image, args.command, env, mounts)
  195. # Deprecated
  196. def command_jail_oneshot_old():
  197. base, _ = zfs_snapshot_by_tag_or_sha256(args.image)
  198. # root = '/'.join(base.split('/')[:-1])
  199. for _ in range(10**6):
  200. sha256 = bytes([ random.randint(0, 255) for _ in range(32) ]).hex()
  201. name = sha256[:7]
  202. name = base.split('/')[0] + '/focker/jails/' + name
  203. if not zfs_exists(name):
  204. break
  205. zfs_run(['zfs', 'clone', '-o', 'focker:sha256=' + sha256, base, name])
  206. try:
  207. mounts = list(map(lambda a: a.split(':'), args.mounts))
  208. jail_run(zfs_mountpoint(name), args.command, mounts)
  209. # subprocess.check_output(['jail', '-c', 'interface=lo1', 'ip4.addr=127.0.1.0', 'path=' + zfs_mountpoint(name), 'command', command])
  210. finally:
  211. # subprocess.run(['umount', zfs_mountpoint(name) + '/dev'])
  212. zfs_run(['zfs', 'destroy', '-f', name])
  213. # raise
  214. def command_jail_list(args):
  215. lst = zfs_list(fields=['focker:sha256,focker:tags,mountpoint'], focker_type='jail')
  216. jails = subprocess.check_output(['jls', '--libxo=json'])
  217. jails = json.loads(jails)['jail-information']['jail']
  218. jails = { j['path']: j for j in jails }
  219. lst = list(map(lambda a: [ a[1],
  220. a[0] if args.full_sha256 else a[0][:7],
  221. a[2],
  222. jails[a[2]]['jid'] if a[2] in jails else '-' ], lst))
  223. print(tabulate(lst, headers=['Tags', 'SHA256', 'mountpoint', 'JID']))
  224. def command_jail_tag(args):
  225. name, _ = zfs_find(args.reference, focker_type='jail')
  226. zfs_untag(args.tags, focker_type='jail')
  227. zfs_tag(name, args.tags)
  228. def command_jail_untag(args):
  229. zfs_untag(args.tags, focker_type='jail')
  230. def command_jail_prune(args):
  231. jails = subprocess.check_output(['jls', '--libxo=json'])
  232. jails = json.loads(jails)['jail-information']['jail']
  233. used = set()
  234. for j in jails:
  235. used.add(j['path'])
  236. lst = zfs_list(fields=['focker:sha256,focker:tags,mountpoint,name'], focker_type='jail')
  237. for j in lst:
  238. if j[1] == '-' and (j[2] not in used or args.force):
  239. jail_remove(j[2])