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!
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

4 anos atrás
4 anos atrás
4 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:tags', '-H', name])
  99. if not replace:
  100. tags = list(tags)
  101. tags.extend(lst[0][0].split(' '))
  102. tags = list(set(tags))
  103. tags = list(filter(lambda a: a != '-', tags))
  104. if len(tags) > 0:
  105. zfs_run(['zfs', 'set', 'focker:tags=' + ' '.join(tags), name])
  106. else:
  107. zfs_run(['zfs', 'inherit', 'focker:tags', name])
  108. def zfs_untag(tags, focker_type='image'):
  109. # print('zfs_untag(), tags:', tags)
  110. poolname = zfs_poolname()
  111. lst = zfs_parse_output(['zfs', 'list', '-o', 'name,focker:tags', '-H', '-r', poolname + '/focker/' + focker_type + 's'])
  112. lst = filter(lambda a: any([b in a[1].split(' ') for b in tags]), lst)
  113. for row in lst:
  114. cur_tags = row[1].split(' ')
  115. for t in tags:
  116. if t in cur_tags:
  117. cur_tags.remove(t)
  118. zfs_tag(row[0], cur_tags, replace=True)
  119. def zfs_name(path):
  120. lst = zfs_parse_output(['zfs', 'list', '-o', 'name', '-H', path])
  121. if len(lst) == 0:
  122. raise ValueError('Not a ZFS path')
  123. if len(lst) > 1:
  124. raise ValueError('Ambiguous ZFS path')
  125. return lst[0][0]
  126. def zfs_poolname():
  127. poolname = zfs_parse_output(['zfs', 'list', '-H', '/'])
  128. if len(poolname) == 0:
  129. raise ValueError('Not a ZFS root')
  130. poolname = poolname[0][0].split('/')[0]
  131. return poolname
  132. def zfs_init():
  133. poolname = zfs_poolname()
  134. print('poolname:', poolname)
  135. for path in ['/focker', '/focker/images', '/focker/volumes', '/focker/jails']:
  136. if not os.path.exists(path):
  137. os.mkdir(path)
  138. if not zfs_exists(poolname + '/focker'):
  139. zfs_run(['zfs', 'create', '-o', 'canmount=off', '-o', 'mountpoint=/focker', poolname + '/focker'])
  140. if not zfs_exists(poolname + '/focker/images'):
  141. zfs_run(['zfs', 'create', '-o', 'canmount=off', poolname + '/focker/images'])
  142. if not zfs_exists(poolname + '/focker/volumes'):
  143. zfs_run(['zfs', 'create', '-o', 'canmount=off', poolname + '/focker/volumes'])
  144. if not zfs_exists(poolname + '/focker/jails'):
  145. zfs_run(['zfs', 'create', '-o', 'canmount=off', poolname + '/focker/jails'])