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.

64 lines
2.1KB

  1. import subprocess
  2. import csv
  3. import io
  4. import os
  5. def zfs_run(command):
  6. # print('Running:', command)
  7. out = subprocess.check_output(command, stderr=subprocess.STDOUT)
  8. return out
  9. def zfs_parse_output(command):
  10. out = zfs_run(command)
  11. s = io.StringIO(out.decode('utf-8'))
  12. r = csv.reader(s, delimiter='\t')
  13. return [a for a in r]
  14. def zfs_get_type(name):
  15. lst = zfs_parse_output(['zfs', 'list', '-o', 'name,type', '-H', name])
  16. return lst[0][1]
  17. def zfs_snapshot_by_tag_or_sha256(s):
  18. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256,focker:tags,type,name', '-H', '-t', 'snapshot'])
  19. lst = list(filter(lambda a: (a[0] == s or s in a[1].split(' ')) and a[2] == 'snapshot', lst))
  20. if len(lst) == 0:
  21. raise ValueError('Reference not found: ' + s)
  22. if len(lst) > 1:
  23. raise ValueError('Ambiguous reference: ' + s)
  24. return lst[0][3]
  25. def zfs_clone(name, target_name):
  26. zfs_run(['zfs', 'clone', name, target_name])
  27. def zfs_exists(name):
  28. try:
  29. zfs_run(['zfs', 'list', name])
  30. except subprocess.CalledProcessError as e:
  31. return False
  32. return True
  33. def zfs_init():
  34. poolname = zfs_parse_output(['zfs', 'list', '-H', '/'])
  35. if len(poolname) == 0:
  36. raise ValueError('Not a ZFS root')
  37. poolname = poolname[0][0].split('/')[0]
  38. print('poolname:', poolname)
  39. for path in ['/focker', '/focker/images', '/focker/volumes', '/focker/jails']:
  40. if not os.path.exists(path):
  41. os.mkdir(path)
  42. if not zfs_exists(poolname + '/focker'):
  43. zfs_run(['zfs', 'create', '-o', 'canmount=off', '-o', 'mountpoint=/focker', poolname + '/focker'])
  44. if not zfs_exists(poolname + '/focker/images'):
  45. zfs_run(['zfs', 'create', '-o', 'canmount=off', poolname + '/focker/images'])
  46. if not zfs_exists(poolname + '/focker/volumes'):
  47. zfs_run(['zfs', 'create', '-o', 'canmount=off', poolname + '/focker/volumes'])
  48. if not zfs_exists(poolname + '/focker/volumes'):
  49. zfs_run(['zfs', 'create', '-o', 'canmount=off', poolname + '/focker/jails'])