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.

zfs.py 6.5KB

4 år sedan
4 år sedan
4 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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, focker_type='image'):
  80. poolname = zfs_poolname()
  81. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256', '-t', 'snap',
  82. '-r', poolname + '/focker/' + focker_type + 's'])
  83. lst = list(filter(lambda a: a[0] == sha256, lst))
  84. if len(lst) == 0:
  85. return False
  86. return True
  87. def zfs_snapshot_by_sha256(sha256, focker_type='image'):
  88. poolname = zfs_poolname()
  89. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256,name',
  90. '-t', 'snap', '-H', '-r', poolname + '/focker/' + focker_type + 's'])
  91. lst = list(filter(lambda a: a[0] == sha256, lst))
  92. if len(lst) == 0:
  93. raise ValueError('Snapshot with given sha256 does not exist: ' + sha256)
  94. if len(lst) > 1:
  95. raise ValueError('Ambiguous snapshot sha256: ' + sha256)
  96. return lst[0][1]
  97. def zfs_tag(name, tags, replace=False):
  98. if any(map(lambda a: ' ' in a, tags)):
  99. raise ValueError('Tags cannot contain spaces')
  100. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:tags', '-H', name])
  101. if not replace:
  102. tags = list(tags)
  103. tags.extend(lst[0][0].split(' '))
  104. tags = list(set(tags))
  105. tags = list(filter(lambda a: a != '-', tags))
  106. if len(tags) > 0:
  107. zfs_run(['zfs', 'set', 'focker:tags=' + ' '.join(tags), name])
  108. else:
  109. zfs_run(['zfs', 'inherit', 'focker:tags', name])
  110. def zfs_untag(tags, focker_type='image'):
  111. if any(map(lambda a: ' ' in a, tags)):
  112. raise ValueError('Tags cannot contain spaces')
  113. # print('zfs_untag(), tags:', tags)
  114. poolname = zfs_poolname()
  115. lst = zfs_parse_output(['zfs', 'list', '-o', 'name,focker:tags', '-H', '-r', poolname + '/focker/' + focker_type + 's'])
  116. lst = filter(lambda a: any([b in a[1].split(' ') for b in tags]), lst)
  117. for row in lst:
  118. cur_tags = row[1].split(' ')
  119. for t in tags:
  120. if t in cur_tags:
  121. cur_tags.remove(t)
  122. zfs_tag(row[0], cur_tags, replace=True)
  123. def zfs_name(path):
  124. lst = zfs_parse_output(['zfs', 'list', '-o', 'name', '-H', path])
  125. if len(lst) == 0:
  126. raise ValueError('Not a ZFS path')
  127. if len(lst) > 1:
  128. raise ValueError('Ambiguous ZFS path')
  129. return lst[0][0]
  130. def zfs_poolname():
  131. poolname = zfs_parse_output(['zfs', 'list', '-H', '/'])
  132. if len(poolname) == 0:
  133. raise ValueError('Not a ZFS root')
  134. poolname = poolname[0][0].split('/')[0]
  135. return poolname
  136. def zfs_init():
  137. poolname = zfs_poolname()
  138. print('poolname:', poolname)
  139. for path in ['/focker', '/focker/images', '/focker/volumes', '/focker/jails']:
  140. if not os.path.exists(path):
  141. os.mkdir(path)
  142. if not zfs_exists(poolname + '/focker'):
  143. zfs_run(['zfs', 'create', '-o', 'canmount=off', '-o', 'mountpoint=/focker', poolname + '/focker'])
  144. if not zfs_exists(poolname + '/focker/images'):
  145. zfs_run(['zfs', 'create', '-o', 'canmount=off', poolname + '/focker/images'])
  146. if not zfs_exists(poolname + '/focker/volumes'):
  147. zfs_run(['zfs', 'create', '-o', 'canmount=off', poolname + '/focker/volumes'])
  148. if not zfs_exists(poolname + '/focker/jails'):
  149. zfs_run(['zfs', 'create', '-o', 'canmount=off', poolname + '/focker/jails'])