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!
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

169 lines
5.8KB

  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], lst[0][0])
  25. def zfs_find(reference, focker_type='image', zfs_type='filesystem'):
  26. poolname = zfs_poolname()
  27. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256,focker:tags,type,name', '-H', '-t', zfs_type, '-r', poolname + '/focker/' + focker_type + 's'])
  28. def match(sha256, tags, type, name):
  29. if sha256.startswith(reference) or \
  30. any(map(lambda a: a.startswith(reference), tags.split(' '))) or \
  31. name.split('/')[-1].startswith(reference):
  32. return True
  33. return False
  34. lst = list(filter(lambda a: match(*a), lst))
  35. if len(lst) == 0:
  36. raise ValueError('Reference not found: ' + reference)
  37. if len(lst) > 1:
  38. raise ValueError('Ambiguous reference: ' + reference)
  39. return (lst[0][3], lst[0][0])
  40. def zfs_list(fields=['name'], focker_type='image', zfs_type='filesystem'):
  41. poolname = zfs_poolname()
  42. fields.append('focker:sha256')
  43. lst = zfs_parse_output(['zfs', 'list', '-o', ','.join(fields),
  44. '-H', '-t', zfs_type, '-r', poolname + '/focker/' + focker_type + 's'])
  45. lst = list(filter(lambda a: a[-1] != '-', lst))
  46. return lst
  47. def zfs_prune(focker_type='image'):
  48. poolname = zfs_poolname()
  49. again = True
  50. while again:
  51. again = False
  52. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256,focker:tags,origin,name', '-H', '-r', poolname + '/focker/' + focker_type + 's'])
  53. used = set()
  54. for r in lst:
  55. if r[2] == '-':
  56. continue
  57. used.add(r[2].split('@')[0])
  58. for r in lst:
  59. if r[0] == '-' or r[1] != '-':
  60. continue
  61. if r[3] not in used:
  62. print('Removing:', r[3])
  63. zfs_run(['zfs', 'destroy', '-r', '-f', r[3]])
  64. again = True
  65. def zfs_clone(name, target_name):
  66. zfs_run(['zfs', 'clone', name, target_name])
  67. def zfs_exists(name):
  68. try:
  69. zfs_run(['zfs', 'list', name])
  70. except subprocess.CalledProcessError as e:
  71. return False
  72. return True
  73. def zfs_set_props(name, props):
  74. for (k, v) in props.items():
  75. zfs_run(['zfs', 'set', k + '=' + v, name])
  76. def zfs_mountpoint(name):
  77. lst = zfs_parse_output(['zfs', 'list', '-o', 'mountpoint', '-H', name])
  78. return lst[0][0]
  79. def zfs_exists_snapshot_sha256(sha256):
  80. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256', '-t', 'snap'])
  81. lst = list(filter(lambda a: a[0] == sha256, lst))
  82. if len(lst) == 0:
  83. return False
  84. return True
  85. def zfs_snapshot_by_sha256(sha256):
  86. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256,name', '-t', 'snap', '-H'])
  87. lst = list(filter(lambda a: a[0] == sha256, lst))
  88. if len(lst) == 0:
  89. raise ValueError('Snapshot with given sha256 does not exist: ' + sha256)
  90. if len(lst) > 1:
  91. raise ValueError('Ambiguous snapshot sha256: ' + sha256)
  92. return lst[0][1]
  93. def zfs_tag(name, tags, replace=False):
  94. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:tags', '-H', name])
  95. if not replace:
  96. tags = list(tags)
  97. tags.extend(lst[0][0].split(' '))
  98. tags = list(set(tags))
  99. tags = list(filter(lambda a: a != '-', tags))
  100. if len(tags) > 0:
  101. zfs_run(['zfs', 'set', 'focker:tags=' + ' '.join(tags), name])
  102. else:
  103. zfs_run(['zfs', 'inherit', 'focker:tags', name])
  104. def zfs_untag(tags, focker_type='image'):
  105. # print('zfs_untag(), tags:', tags)
  106. poolname = zfs_poolname()
  107. lst = zfs_parse_output(['zfs', 'list', '-o', 'name,focker:tags', '-H', '-r', poolname + '/focker/' + focker_type + 's'])
  108. lst = filter(lambda a: any([b in a[1].split(' ') for b in tags]), lst)
  109. for row in lst:
  110. cur_tags = row[1].split(' ')
  111. for t in tags:
  112. if t in cur_tags:
  113. cur_tags.remove(t)
  114. zfs_tag(row[0], cur_tags, replace=True)
  115. def zfs_poolname():
  116. poolname = zfs_parse_output(['zfs', 'list', '-H', '/'])
  117. if len(poolname) == 0:
  118. raise ValueError('Not a ZFS root')
  119. poolname = poolname[0][0].split('/')[0]
  120. return poolname
  121. def zfs_init():
  122. poolname = zfs_poolname()
  123. print('poolname:', poolname)
  124. for path in ['/focker', '/focker/images', '/focker/volumes', '/focker/jails']:
  125. if not os.path.exists(path):
  126. os.mkdir(path)
  127. if not zfs_exists(poolname + '/focker'):
  128. zfs_run(['zfs', 'create', '-o', 'canmount=off', '-o', 'mountpoint=/focker', poolname + '/focker'])
  129. if not zfs_exists(poolname + '/focker/images'):
  130. zfs_run(['zfs', 'create', '-o', 'canmount=off', poolname + '/focker/images'])
  131. if not zfs_exists(poolname + '/focker/volumes'):
  132. zfs_run(['zfs', 'create', '-o', 'canmount=off', poolname + '/focker/volumes'])
  133. if not zfs_exists(poolname + '/focker/jails'):
  134. zfs_run(['zfs', 'create', '-o', 'canmount=off', poolname + '/focker/jails'])