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.

zfs.py 6.7KB

4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import subprocess
  2. import csv
  3. import io
  4. import os
  5. class AmbiguousReference(ValueError):
  6. def __init__(self, msg):
  7. super().__init__(msg)
  8. def zfs_run(command):
  9. # print('Running:', command)
  10. out = subprocess.check_output(command, stderr=subprocess.STDOUT)
  11. return out
  12. def zfs_parse_output(command):
  13. out = zfs_run(command)
  14. s = io.StringIO(out.decode('utf-8'))
  15. r = csv.reader(s, delimiter='\t')
  16. return [a for a in r]
  17. def zfs_get_type(name):
  18. lst = zfs_parse_output(['zfs', 'list', '-o', 'name,type', '-H', name])
  19. return lst[0][1]
  20. def zfs_snapshot_by_tag_or_sha256(s, focker_type='image'):
  21. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256,focker:tags,type,name',
  22. '-H', '-t', 'snapshot', '-r', poolname + '/focker/' + focker_type + 's'])
  23. lst = list(filter(lambda a: (a[0] == s or s in a[1].split(' ')) and a[2] == 'snapshot', lst))
  24. if len(lst) == 0:
  25. raise ValueError('Reference not found: ' + s)
  26. if len(lst) > 1:
  27. raise AmbiguousReference('Ambiguous reference: ' + s)
  28. return (lst[0][3], lst[0][0])
  29. def zfs_find(reference, focker_type='image', zfs_type='filesystem'):
  30. poolname = zfs_poolname()
  31. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256,focker:tags,type,name', '-H', '-t', zfs_type, '-r', poolname + '/focker/' + focker_type + 's'])
  32. def match(sha256, tags, type, name):
  33. if sha256.startswith(reference) or \
  34. any(map(lambda a: a.startswith(reference), tags.split(' '))) or \
  35. name.split('/')[-1].startswith(reference):
  36. return True
  37. return False
  38. lst = list(filter(lambda a: match(*a), lst))
  39. if len(lst) == 0:
  40. raise ValueError('Reference not found: ' + reference)
  41. if len(lst) > 1:
  42. raise AmbiguousReference('Ambiguous reference: ' + reference)
  43. return (lst[0][3], lst[0][0])
  44. def zfs_list(fields=['name'], focker_type='image', zfs_type='filesystem'):
  45. poolname = zfs_poolname()
  46. fields.append('focker:sha256')
  47. lst = zfs_parse_output(['zfs', 'list', '-o', ','.join(fields),
  48. '-H', '-t', zfs_type, '-r', poolname + '/focker/' + focker_type + 's'])
  49. lst = list(filter(lambda a: a[-1] != '-', lst))
  50. return lst
  51. def zfs_prune(focker_type='image'):
  52. poolname = zfs_poolname()
  53. again = True
  54. while again:
  55. again = False
  56. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256,focker:tags,origin,name', '-H', '-r', poolname + '/focker/' + focker_type + 's'])
  57. used = set()
  58. for r in lst:
  59. if r[2] == '-':
  60. continue
  61. used.add(r[2].split('@')[0])
  62. for r in lst:
  63. if r[0] == '-' or r[1] != '-':
  64. continue
  65. if r[3] not in used:
  66. print('Removing:', r[3])
  67. zfs_run(['zfs', 'destroy', '-r', '-f', r[3]])
  68. again = True
  69. def zfs_clone(name, target_name):
  70. zfs_run(['zfs', 'clone', name, target_name])
  71. def zfs_exists(name):
  72. try:
  73. zfs_run(['zfs', 'list', name])
  74. except subprocess.CalledProcessError as e:
  75. return False
  76. return True
  77. def zfs_set_props(name, props):
  78. for (k, v) in props.items():
  79. zfs_run(['zfs', 'set', k + '=' + v, name])
  80. def zfs_mountpoint(name):
  81. lst = zfs_parse_output(['zfs', 'list', '-o', 'mountpoint', '-H', name])
  82. return lst[0][0]
  83. def zfs_exists_snapshot_sha256(sha256, focker_type='image'):
  84. poolname = zfs_poolname()
  85. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256', '-t', 'snap',
  86. '-r', poolname + '/focker/' + focker_type + 's'])
  87. lst = list(filter(lambda a: a[0] == sha256, lst))
  88. if len(lst) == 0:
  89. return False
  90. return True
  91. def zfs_snapshot_by_sha256(sha256, focker_type='image'):
  92. poolname = zfs_poolname()
  93. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256,name',
  94. '-t', 'snap', '-H', '-r', poolname + '/focker/' + focker_type + 's'])
  95. lst = list(filter(lambda a: a[0] == sha256, lst))
  96. if len(lst) == 0:
  97. raise ValueError('Snapshot with given sha256 does not exist: ' + sha256)
  98. if len(lst) > 1:
  99. raise AmbiguousReference('Ambiguous snapshot sha256: ' + sha256)
  100. return lst[0][1]
  101. def zfs_tag(name, tags, replace=False):
  102. if any(map(lambda a: ' ' in a, tags)):
  103. raise ValueError('Tags cannot contain spaces')
  104. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:tags', '-H', name])
  105. if not replace:
  106. tags = list(tags)
  107. tags.extend(lst[0][0].split(' '))
  108. tags = list(set(tags))
  109. tags = list(filter(lambda a: a != '-', tags))
  110. if len(tags) > 0:
  111. zfs_run(['zfs', 'set', 'focker:tags=' + ' '.join(tags), name])
  112. else:
  113. zfs_run(['zfs', 'inherit', 'focker:tags', name])
  114. def zfs_untag(tags, focker_type='image'):
  115. if any(map(lambda a: ' ' in a, tags)):
  116. raise ValueError('Tags cannot contain spaces')
  117. # print('zfs_untag(), tags:', tags)
  118. poolname = zfs_poolname()
  119. lst = zfs_parse_output(['zfs', 'list', '-o', 'name,focker:tags', '-H', '-r', poolname + '/focker/' + focker_type + 's'])
  120. lst = filter(lambda a: any([b in a[1].split(' ') for b in tags]), lst)
  121. for row in lst:
  122. cur_tags = row[1].split(' ')
  123. for t in tags:
  124. if t in cur_tags:
  125. cur_tags.remove(t)
  126. zfs_tag(row[0], cur_tags, replace=True)
  127. def zfs_name(path):
  128. lst = zfs_parse_output(['zfs', 'list', '-o', 'name', '-H', path])
  129. if len(lst) == 0:
  130. raise ValueError('Not a ZFS path')
  131. if len(lst) > 1:
  132. raise AmbiguousReference('Ambiguous ZFS path')
  133. return lst[0][0]
  134. def zfs_poolname():
  135. poolname = zfs_parse_output(['zfs', 'list', '-H', '/'])
  136. if len(poolname) == 0:
  137. raise ValueError('Not a ZFS root')
  138. poolname = poolname[0][0].split('/')[0]
  139. return poolname
  140. def zfs_init():
  141. poolname = zfs_poolname()
  142. print('poolname:', poolname)
  143. for path in ['/focker', '/focker/images', '/focker/volumes', '/focker/jails']:
  144. if not os.path.exists(path):
  145. os.mkdir(path)
  146. if not zfs_exists(poolname + '/focker'):
  147. zfs_run(['zfs', 'create', '-o', 'canmount=off', '-o', 'mountpoint=/focker', poolname + '/focker'])
  148. if not zfs_exists(poolname + '/focker/images'):
  149. zfs_run(['zfs', 'create', '-o', 'canmount=off', poolname + '/focker/images'])
  150. if not zfs_exists(poolname + '/focker/volumes'):
  151. zfs_run(['zfs', 'create', '-o', 'canmount=off', poolname + '/focker/volumes'])
  152. if not zfs_exists(poolname + '/focker/jails'):
  153. zfs_run(['zfs', 'create', '-o', 'canmount=off', poolname + '/focker/jails'])