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!
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

333 wiersze
11KB

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