file.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. #
  2. # The Qubes OS Project, https://www.qubes-os.org/
  3. #
  4. # Copyright (C) 2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  5. # Copyright (C) 2013-2015 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. # Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. #
  9. # This library is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU Lesser General Public
  11. # License as published by the Free Software Foundation; either
  12. # version 2.1 of the License, or (at your option) any later version.
  13. #
  14. # This library is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. # Lesser General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU Lesser General Public
  20. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  21. #
  22. ''' This module contains pool implementations backed by file images'''
  23. import os
  24. import os.path
  25. import re
  26. import subprocess
  27. from contextlib import suppress
  28. import qubes.storage
  29. BLKSIZE = 512
  30. class FilePool(qubes.storage.Pool):
  31. ''' File based 'original' disk implementation
  32. Volumes are stored in sparse files. Additionally device-mapper is used for
  33. applying copy-on-write layer.
  34. Quick reference on device-mapper layers:
  35. snap_on_start save_on_stop layout
  36. yes yes not supported
  37. no yes snapshot-origin(volume.img, volume-cow.img)
  38. yes no snapshot(
  39. snapshot(source.img, source-cow.img),
  40. volume-cow.img)
  41. no no volume.img directly
  42. ''' # pylint: disable=protected-access
  43. driver = 'file'
  44. def __init__(self, *, name, revisions_to_keep=1, dir_path):
  45. super().__init__(name=name, revisions_to_keep=revisions_to_keep)
  46. self.dir_path = os.path.normpath(dir_path)
  47. self._volumes = []
  48. @property
  49. def config(self):
  50. return {
  51. 'name': self.name,
  52. 'dir_path': self.dir_path,
  53. 'driver': FilePool.driver,
  54. 'revisions_to_keep': self.revisions_to_keep
  55. }
  56. def init_volume(self, vm, volume_config):
  57. if volume_config.get('snap_on_start', False) and \
  58. volume_config.get('save_on_stop', False):
  59. raise NotImplementedError(
  60. 'snap_on_start + save_on_stop not supported by file driver')
  61. volume_config['dir_path'] = self.dir_path
  62. if 'vid' not in volume_config:
  63. volume_config['vid'] = os.path.join(
  64. self._vid_prefix(vm), volume_config['name'])
  65. try:
  66. if not volume_config.get('save_on_stop', False):
  67. volume_config['revisions_to_keep'] = 0
  68. except KeyError:
  69. pass
  70. if 'revisions_to_keep' not in volume_config:
  71. volume_config['revisions_to_keep'] = self.revisions_to_keep
  72. volume_config['pool'] = self
  73. volume = FileVolume(**volume_config)
  74. self._volumes += [volume]
  75. return volume
  76. @property
  77. def revisions_to_keep(self):
  78. return self._revisions_to_keep
  79. @revisions_to_keep.setter
  80. def revisions_to_keep(self, value):
  81. value = int(value)
  82. if value > 1:
  83. raise NotImplementedError(
  84. 'FilePool supports maximum 1 volume revision to keep')
  85. self._revisions_to_keep = value
  86. def destroy(self):
  87. pass
  88. def setup(self):
  89. create_dir_if_not_exists(self.dir_path)
  90. appvms_path = os.path.join(self.dir_path, 'appvms')
  91. create_dir_if_not_exists(appvms_path)
  92. vm_templates_path = os.path.join(self.dir_path, 'vm-templates')
  93. create_dir_if_not_exists(vm_templates_path)
  94. @staticmethod
  95. def _vid_prefix(vm):
  96. ''' Helper to create a prefix for the vid for volume
  97. ''' # FIX Remove this if we drop the file backend
  98. import qubes.vm.templatevm # pylint: disable=redefined-outer-name
  99. import qubes.vm.dispvm # pylint: disable=redefined-outer-name
  100. if isinstance(vm, qubes.vm.templatevm.TemplateVM):
  101. subdir = 'vm-templates'
  102. else:
  103. subdir = 'appvms'
  104. return os.path.join(subdir, vm.name)
  105. def target_dir(self, vm):
  106. """ Returns the path to vmdir depending on the type of the VM.
  107. The default QubesOS file storage saves the vm images in three
  108. different directories depending on the ``QubesVM`` type:
  109. * ``appvms`` for ``QubesAppVm`` or ``QubesHvm``
  110. * ``vm-templates`` for ``QubesTemplateVm`` or ``QubesTemplateHvm``
  111. Args:
  112. vm: a QubesVM
  113. pool_dir: the root directory of the pool
  114. Returns:
  115. string (str) absolute path to the directory where the vm files
  116. are stored
  117. """
  118. return os.path.join(self.dir_path, self._vid_prefix(vm))
  119. def list_volumes(self):
  120. return self._volumes
  121. @property
  122. def size(self):
  123. try:
  124. statvfs = os.statvfs(self.dir_path)
  125. return statvfs.f_frsize * statvfs.f_blocks
  126. except FileNotFoundError:
  127. return 0
  128. @property
  129. def usage(self):
  130. try:
  131. statvfs = os.statvfs(self.dir_path)
  132. return statvfs.f_frsize * (statvfs.f_blocks - statvfs.f_bfree)
  133. except FileNotFoundError:
  134. return 0
  135. def included_in(self, app):
  136. ''' Check if there is pool containing this one - either as a
  137. filesystem or its LVM volume'''
  138. return qubes.storage.search_pool_containing_dir(
  139. [pool for pool in app.pools.values() if pool is not self],
  140. self.dir_path)
  141. class FileVolume(qubes.storage.Volume):
  142. ''' Parent class for the xen volumes implementation which expects a
  143. `target_dir` param on initialization. '''
  144. def __init__(self, dir_path, **kwargs):
  145. self.dir_path = dir_path
  146. assert self.dir_path, "dir_path not specified"
  147. self._revisions_to_keep = 0
  148. super().__init__(**kwargs)
  149. if self.snap_on_start:
  150. img_name = self.source.vid + '-cow.img'
  151. self.path_source_cow = os.path.join(self.dir_path, img_name)
  152. @property
  153. def revisions_to_keep(self):
  154. return self._revisions_to_keep
  155. @revisions_to_keep.setter
  156. def revisions_to_keep(self, value):
  157. if int(value) > 1:
  158. raise NotImplementedError(
  159. 'FileVolume supports maximum 1 volume revision to keep')
  160. self._revisions_to_keep = int(value)
  161. def create(self):
  162. assert isinstance(self.size, int) and self.size > 0, \
  163. 'Volume size must be > 0'
  164. if not self.snap_on_start:
  165. create_sparse_file(self.path, self.size)
  166. def remove(self):
  167. if not self.snap_on_start:
  168. _remove_if_exists(self.path)
  169. if self.snap_on_start or self.save_on_stop:
  170. _remove_if_exists(self.path_cow)
  171. def is_dirty(self):
  172. if not self.save_on_stop:
  173. return False
  174. if os.path.exists(self.path_cow):
  175. stat = os.stat(self.path_cow)
  176. return stat.st_blocks > 0
  177. return False
  178. def resize(self, size):
  179. ''' Expands volume, throws
  180. :py:class:`qubst.storage.qubes.storage.StoragePoolException` if
  181. given size is less than current_size
  182. ''' # pylint: disable=no-self-use
  183. if not self.rw:
  184. msg = 'Can not resize reađonly volume {!s}'.format(self)
  185. raise qubes.storage.StoragePoolException(msg)
  186. if size < self.size:
  187. raise qubes.storage.StoragePoolException(
  188. 'For your own safety, shrinking of %s is'
  189. ' disabled. If you really know what you'
  190. ' are doing, use `truncate` on %s manually.' %
  191. (self.name, self.vid))
  192. with open(self.path, 'a+b') as fd:
  193. fd.truncate(size)
  194. p = subprocess.Popen(['losetup', '--associated', self.path],
  195. stdout=subprocess.PIPE)
  196. result = p.communicate()
  197. m = re.match(r'^(/dev/loop\d+):\s', result[0].decode())
  198. if m is not None:
  199. loop_dev = m.group(1)
  200. # resize loop device
  201. subprocess.check_call(['losetup', '--set-capacity',
  202. loop_dev])
  203. self._size = size
  204. def commit(self):
  205. msg = 'Tried to commit a non commitable volume {!r}'.format(self)
  206. assert self.save_on_stop and self.rw, msg
  207. if os.path.exists(self.path_cow):
  208. if self.revisions_to_keep:
  209. old_path = self.path_cow + '.old'
  210. os.rename(self.path_cow, old_path)
  211. else:
  212. os.unlink(self.path_cow)
  213. create_sparse_file(self.path_cow, self.size)
  214. return self
  215. def export(self):
  216. # FIXME: this should rather return snapshot(self.path, self.path_cow)
  217. # if domain is running
  218. return self.path
  219. def import_volume(self, src_volume):
  220. if src_volume.snap_on_start:
  221. raise qubes.storage.StoragePoolException(
  222. "Can not import snapshot volume {!s} in to pool {!s} ".format(
  223. src_volume, self))
  224. if self.save_on_stop:
  225. _remove_if_exists(self.path)
  226. copy_file(src_volume.export(), self.path)
  227. return self
  228. def import_data(self, size):
  229. if not self.save_on_stop:
  230. raise qubes.storage.StoragePoolException(
  231. "Can not import into save_on_stop=False volume {!s}".format(
  232. self))
  233. create_sparse_file(self.path_import, size)
  234. return self.path_import
  235. def import_data_end(self, success):
  236. if success:
  237. os.rename(self.path_import, self.path)
  238. else:
  239. os.unlink(self.path_import)
  240. return self
  241. def reset(self):
  242. ''' Remove and recreate a volatile volume '''
  243. assert not self.snap_on_start and not self.save_on_stop, \
  244. "Not a volatile volume"
  245. assert isinstance(self.size, int) and self.size > 0, \
  246. 'Volatile volume size must be > 0'
  247. _remove_if_exists(self.path)
  248. create_sparse_file(self.path, self.size)
  249. return self
  250. def start(self):
  251. if not self.save_on_stop and not self.snap_on_start:
  252. self.reset()
  253. else:
  254. if not self.save_on_stop:
  255. # make sure previous snapshot is removed - even if VM
  256. # shutdown routine wasn't called (power interrupt or so)
  257. _remove_if_exists(self.path_cow)
  258. if not os.path.exists(self.path_cow):
  259. create_sparse_file(self.path_cow, self.size)
  260. if not self.snap_on_start:
  261. _check_path(self.path)
  262. if hasattr(self, 'path_source_cow'):
  263. if not os.path.exists(self.path_source_cow):
  264. create_sparse_file(self.path_source_cow, self.size)
  265. return self
  266. def stop(self):
  267. if self.save_on_stop:
  268. self.commit()
  269. elif self.snap_on_start:
  270. _remove_if_exists(self.path_cow)
  271. else:
  272. _remove_if_exists(self.path)
  273. return self
  274. @property
  275. def path(self):
  276. if self.snap_on_start:
  277. return os.path.join(self.dir_path, self.source.vid + '.img')
  278. return os.path.join(self.dir_path, self.vid + '.img')
  279. @property
  280. def path_cow(self):
  281. img_name = self.vid + '-cow.img'
  282. return os.path.join(self.dir_path, img_name)
  283. @property
  284. def path_import(self):
  285. img_name = self.vid + '-import.img'
  286. return os.path.join(self.dir_path, img_name)
  287. def verify(self):
  288. ''' Verifies the volume. '''
  289. if not os.path.exists(self.path) and \
  290. (self.snap_on_start or self.save_on_stop):
  291. msg = 'Missing image file: {!s}.'.format(self.path)
  292. raise qubes.storage.StoragePoolException(msg)
  293. return True
  294. @property
  295. def script(self):
  296. if not self.snap_on_start and not self.save_on_stop:
  297. return None
  298. if not self.snap_on_start and self.save_on_stop:
  299. return 'block-origin'
  300. if self.snap_on_start:
  301. return 'block-snapshot'
  302. return None
  303. def block_device(self):
  304. ''' Return :py:class:`qubes.storage.BlockDevice` for serialization in
  305. the libvirt XML template as <disk>.
  306. '''
  307. path = self.path
  308. if self.snap_on_start:
  309. path += ":" + self.path_source_cow
  310. if self.snap_on_start or self.save_on_stop:
  311. path += ":" + self.path_cow
  312. return qubes.storage.BlockDevice(path, self.name, self.script, self.rw,
  313. self.domain, self.devtype)
  314. @property
  315. def revisions(self):
  316. if not hasattr(self, 'path_cow'):
  317. return {}
  318. old_revision = self.path_cow + '.old' # pylint: disable=no-member
  319. if not os.path.exists(old_revision):
  320. return {}
  321. seconds = os.path.getctime(old_revision)
  322. iso_date = qubes.storage.isodate(seconds).split('.', 1)[0]
  323. return {'old': iso_date}
  324. @property
  325. def size(self):
  326. with suppress(FileNotFoundError):
  327. self._size = os.path.getsize(self.path)
  328. return self._size
  329. @size.setter
  330. def size(self, _):
  331. raise qubes.storage.StoragePoolException(
  332. "You shouldn't use volume size setter, use resize method instead")
  333. @property
  334. def usage(self):
  335. ''' Returns the actualy used space '''
  336. usage = 0
  337. if self.save_on_stop or self.snap_on_start:
  338. usage = get_disk_usage(self.path_cow)
  339. if self.save_on_stop or not self.snap_on_start:
  340. usage += get_disk_usage(self.path)
  341. return usage
  342. def create_sparse_file(path, size):
  343. ''' Create an empty sparse file '''
  344. if os.path.exists(path):
  345. raise IOError("Volume %s already exists" % path)
  346. parent_dir = os.path.dirname(path)
  347. if not os.path.exists(parent_dir):
  348. os.makedirs(parent_dir)
  349. with open(path, 'a+b') as fh:
  350. fh.truncate(size)
  351. def get_disk_usage_one(st):
  352. '''Extract disk usage of one inode from its stat_result struct.
  353. If known, get real disk usage, as written to device by filesystem, not
  354. logical file size. Those values may be different for sparse files.
  355. :param os.stat_result st: stat result
  356. :returns: disk usage
  357. '''
  358. try:
  359. return st.st_blocks * BLKSIZE
  360. except AttributeError:
  361. return st.st_size
  362. def get_disk_usage(path):
  363. '''Get real disk usage of given path (file or directory).
  364. When *path* points to directory, then it is evaluated recursively.
  365. This function tries estimate real disk usage. See documentation of
  366. :py:func:`get_disk_usage_one`.
  367. :param str path: path to evaluate
  368. :returns: disk usage
  369. '''
  370. try:
  371. st = os.lstat(path)
  372. except OSError:
  373. return 0
  374. ret = get_disk_usage_one(st)
  375. # if path is not a directory, this is skipped
  376. for dirpath, dirnames, filenames in os.walk(path):
  377. for name in dirnames + filenames:
  378. ret += get_disk_usage_one(os.lstat(os.path.join(dirpath, name)))
  379. return ret
  380. def create_dir_if_not_exists(path):
  381. """ Check if a directory exists in if not create it.
  382. This method does not create any parent directories.
  383. """
  384. if not os.path.exists(path):
  385. os.mkdir(path)
  386. def copy_file(source, destination):
  387. '''Effective file copy, preserving sparse files etc.'''
  388. # We prefer to use Linux's cp, because it nicely handles sparse files
  389. assert os.path.exists(source), \
  390. "Missing the source %s to copy from" % source
  391. assert not os.path.exists(destination), \
  392. "Destination %s already exists" % destination
  393. parent_dir = os.path.dirname(destination)
  394. if not os.path.exists(parent_dir):
  395. os.makedirs(parent_dir)
  396. try:
  397. cmd = ['cp', '--sparse=always',
  398. '--reflink=auto', source, destination]
  399. subprocess.check_call(cmd)
  400. except subprocess.CalledProcessError:
  401. raise IOError('Error while copying {!r} to {!r}'.format(source,
  402. destination))
  403. def _remove_if_exists(path):
  404. ''' Removes a file if it exist, silently succeeds if file does not exist '''
  405. if os.path.exists(path):
  406. os.remove(path)
  407. def _check_path(path):
  408. ''' Raise an StoragePoolException if ``path`` does not exist'''
  409. if not os.path.exists(path):
  410. msg = 'Missing image file: %s' % path
  411. raise qubes.storage.StoragePoolException(msg)