storage.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. # -*- encoding: utf8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. '''Storage subsystem.'''
  21. class Volume(object):
  22. '''Storage volume.'''
  23. def __init__(self, app, pool=None, vid=None, vm=None, vm_name=None):
  24. '''Construct a Volume object.
  25. Volume may be identified using pool+vid, or vm+vm_name. Either of
  26. those argument pairs must be given.
  27. :param Qubes app: application instance
  28. :param str pool: pool name
  29. :param str vid: volume id (within pool)
  30. :param str vm: owner VM name
  31. :param str vm_name: name within owning VM (like 'private', 'root' etc)
  32. '''
  33. self.app = app
  34. if pool is None and vm is None:
  35. raise ValueError('Either pool or vm must be given')
  36. if pool is not None and vid is None:
  37. raise ValueError('If pool is given, vid must be too.')
  38. if vm is not None and vm_name is None:
  39. raise ValueError('If vm is given, vm_name must be too.')
  40. self._pool = pool
  41. self._vid = vid
  42. self._vm = vm
  43. self._vm_name = vm_name
  44. self._info = None
  45. def _qubesd_call(self, func_name, payload=None):
  46. '''Make a call to qubesd regarding this volume
  47. :param str func_name: API function name, like `Info` or `Resize`
  48. :param bytes payload: Payload to send.
  49. '''
  50. if self._vm is not None:
  51. method = 'mgmt.vm.volume.' + func_name
  52. dest = self._vm
  53. arg = self._vm_name
  54. else:
  55. method = 'mgmt.pool.volume.' + func_name
  56. dest = 'dom0'
  57. arg = self._pool
  58. if payload is not None:
  59. payload = self._vid.encode('ascii') + b' ' + payload
  60. else:
  61. payload = self._vid.encode('ascii')
  62. return self.app.qubesd_call(dest, method, arg, payload)
  63. def _fetch_info(self, force=True):
  64. '''Fetch volume properties
  65. Populate self._info dict
  66. :param bool force: refresh self._info, even if already populated.
  67. '''
  68. if not force and self._info is not None:
  69. return
  70. info = self._qubesd_call('Info')
  71. info = info.decode('ascii')
  72. self._info = dict([line.split('=', 1) for line in info.splitlines()])
  73. def __eq__(self, other):
  74. if isinstance(other, Volume):
  75. return self.pool == other.pool and self.vid == other.vid
  76. return NotImplemented
  77. @property
  78. def pool(self):
  79. '''Storage volume pool name.'''
  80. if self._pool is not None:
  81. return self._pool
  82. self._fetch_info()
  83. return str(self._info['pool'])
  84. @property
  85. def vid(self):
  86. '''Storage volume id, unique within given pool.'''
  87. if self._vid is not None:
  88. return self._vid
  89. self._fetch_info()
  90. return str(self._info['vid'])
  91. @property
  92. def size(self):
  93. '''Size of volume, in bytes.'''
  94. self._fetch_info(True)
  95. return int(self._info['size'])
  96. @property
  97. def usage(self):
  98. '''Used volume space, in bytes.'''
  99. self._fetch_info(True)
  100. return int(self._info['usage'])
  101. @property
  102. def rw(self):
  103. '''True if volume is read-write.'''
  104. self._fetch_info()
  105. return self._info['rw'] == 'True'
  106. @property
  107. def snap_on_start(self):
  108. '''Create a snapshot from source on VM start.'''
  109. self._fetch_info()
  110. return self._info['snap_on_start'] == 'True'
  111. @property
  112. def save_on_stop(self):
  113. '''Commit changes to original volume on VM stop.'''
  114. self._fetch_info()
  115. return self._info['save_on_stop'] == 'True'
  116. @property
  117. def source(self):
  118. '''Volume ID of source volume (for :py:attr:`snap_on_start`).
  119. If None, this volume itself will be used.
  120. '''
  121. self._fetch_info()
  122. if self._info['source']:
  123. return self._info['source']
  124. return None
  125. @property
  126. def internal(self):
  127. '''If `True` volume is hidden when qvm-block is used'''
  128. self._fetch_info()
  129. return self._info['internal'] == 'True'
  130. @property
  131. def revisions_to_keep(self):
  132. '''Number of revisions to keep around'''
  133. self._fetch_info()
  134. return int(self._info['revisions_to_keep'])
  135. def resize(self, size):
  136. '''Resize volume.
  137. Currently only extending is supported.
  138. :param int size: new size in bytes.
  139. '''
  140. self._qubesd_call('Resize', str(size).encode('ascii'))
  141. @property
  142. def revisions(self):
  143. ''' Returns iterable containing revision identifiers'''
  144. revisions = self._qubesd_call('ListSnapshots')
  145. return revisions.decode('ascii').splitlines()
  146. def revert(self, revision):
  147. ''' Revert volume to previous revision
  148. :param str revision: Revision identifier to revert to
  149. '''
  150. if not isinstance(revision, str):
  151. raise TypeError('revision must be a str')
  152. self._qubesd_call('Revert', revision.encode('ascii'))
  153. class Pool(object):
  154. ''' A Pool is used to manage different kind of volumes (File
  155. based/LVM/Btrfs/...).
  156. '''
  157. def __init__(self, app, name=None):
  158. ''' Initialize storage pool wrapper
  159. :param app: Qubes() object
  160. :param name: name of the pool
  161. '''
  162. self.app = app
  163. self.name = name
  164. self._config = None
  165. def __str__(self):
  166. return self.name
  167. def __eq__(self, other):
  168. if isinstance(other, Pool):
  169. return self.name == other.name
  170. elif isinstance(other, str):
  171. return self.name == other
  172. return NotImplemented
  173. def __lt__(self, other):
  174. if isinstance(other, Pool):
  175. return self.name < other.name
  176. return NotImplemented
  177. @property
  178. def config(self):
  179. ''' Storage pool config '''
  180. if self._config is None:
  181. pool_info_data = self.app.qubesd_call(
  182. 'dom0', 'mgmt.pool.Info', self.name, None)
  183. pool_info_data = pool_info_data.decode('utf-8')
  184. assert pool_info_data.endswith('\n')
  185. pool_info_data = pool_info_data[:-1]
  186. self._config = dict(
  187. l.split('=', 1) for l in pool_info_data.splitlines())
  188. return self._config
  189. @property
  190. def driver(self):
  191. ''' Storage pool driver '''
  192. return self.config['driver']
  193. @property
  194. def volumes(self):
  195. ''' Volumes managed by this pool '''
  196. volumes_data = self.app.qubesd_call(
  197. 'dom0', 'mgmt.pool.volume.List', self.name, None)
  198. assert volumes_data.endswith(b'\n')
  199. volumes_data = volumes_data[:-1].decode('ascii')
  200. for vid in volumes_data.splitlines():
  201. yield Volume(self.app, self.name, vid)