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!
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

zfs.py 3.5KB

il y a 4 ans
il y a 4 ans
il y a 4 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_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_set_props(name, props):
  34. for (k, v) in props.items():
  35. zfs_run(['zfs', 'set', k + '=' + v, name])
  36. def zfs_mountpoint(name):
  37. lst = zfs_parse_output(['zfs', 'list', '-o', 'mountpoint', '-H', name])
  38. return lst[0][0]
  39. def zfs_exists_snapshot_sha256(sha256):
  40. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256', '-t', 'snap'])
  41. lst = list(filter(lambda a: a[0] == sha256, lst))
  42. if len(lst) == 0:
  43. return False
  44. return True
  45. def zfs_snapshot_by_sha256(sha256):
  46. lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256,name', '-t', 'snap', '-H'])
  47. lst = list(filter(lambda a: a[0] == sha256, lst))
  48. if len(lst) == 0:
  49. raise ValueError('Snapshot with given sha256 does not exist: ' + sha256)
  50. if len(lst) > 1:
  51. raise ValueError('Ambiguous snapshot sha256: ' + sha256)
  52. return lst[0][1]
  53. def zfs_tag(name, tags):
  54. if len(tags) > 0:
  55. zfs_run(['zfs', 'set', 'focker:tags=' + ' '.join(tags), name])
  56. else:
  57. zfs_run(['zfs', 'inherit', 'focker:tags', name])
  58. def zfs_untag(tags):
  59. lst = zfs_parse_output(['zfs', 'list', '-o', 'name,focker:tags', '-H'])
  60. lst = filter(lambda a: any([b in a[1].split(' ') for b in tags]), lst)
  61. for row in lst:
  62. cur_tags = row[1].split(' ')
  63. for t in tags:
  64. cur_tags.remove(t)
  65. zfs_tag(row[0], cur_tags)
  66. def zfs_init():
  67. poolname = zfs_parse_output(['zfs', 'list', '-H', '/'])
  68. if len(poolname) == 0:
  69. raise ValueError('Not a ZFS root')
  70. poolname = poolname[0][0].split('/')[0]
  71. print('poolname:', poolname)
  72. for path in ['/focker', '/focker/images', '/focker/volumes', '/focker/jails']:
  73. if not os.path.exists(path):
  74. os.mkdir(path)
  75. if not zfs_exists(poolname + '/focker'):
  76. zfs_run(['zfs', 'create', '-o', 'canmount=off', '-o', 'mountpoint=/focker', poolname + '/focker'])
  77. if not zfs_exists(poolname + '/focker/images'):
  78. zfs_run(['zfs', 'create', '-o', 'canmount=off', poolname + '/focker/images'])
  79. if not zfs_exists(poolname + '/focker/volumes'):
  80. zfs_run(['zfs', 'create', '-o', 'canmount=off', poolname + '/focker/volumes'])
  81. if not zfs_exists(poolname + '/focker/volumes'):
  82. zfs_run(['zfs', 'create', '-o', 'canmount=off', poolname + '/focker/jails'])