__init__.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, https://www.qubes-os.org/
  5. #
  6. # Copyright (C) 2013-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2013-2015 Marek Marczykowski-Górecki
  8. # <marmarek@invisiblethingslab.com>
  9. # Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation; either version 2 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License along
  22. # with this program; if not, write to the Free Software Foundation, Inc.,
  23. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. #
  25. from __future__ import absolute_import
  26. import importlib
  27. import os
  28. import os.path
  29. import re
  30. import shutil
  31. import subprocess
  32. import sys
  33. import qubes
  34. import qubes.exc
  35. import qubes.utils
  36. BLKSIZE = 512
  37. class VMStorage(object):
  38. '''Class for handling VM virtual disks.
  39. This is base class for all other implementations, mostly with Xen on Linux
  40. in mind.
  41. ''' # pylint: disable=abstract-class-little-used
  42. def __init__(self, vm, private_img_size=None, root_img_size=None):
  43. #: Domain for which we manage storage
  44. self.vm = vm
  45. #: Size of the private image
  46. self.private_img_size = private_img_size \
  47. if private_img_size is not None \
  48. else qubes.config.defaults['private_img_size']
  49. #: Size of the root image
  50. self.root_img_size = root_img_size \
  51. if root_img_size is not None \
  52. else qubes.config.defaults['root_img_size']
  53. #: Additional drive (currently used only by HVM)
  54. self.drive = None
  55. @property
  56. def private_img(self):
  57. '''Path to the private image'''
  58. return self.abspath(qubes.config.vm_files['private_img'])
  59. @property
  60. def root_img(self):
  61. '''Path to the root image'''
  62. return self.vm.template.root_img if hasattr(self.vm, 'template') \
  63. else self.abspath(qubes.config.vm_files['root_img'])
  64. @property
  65. def rootcow_img(self):
  66. '''Path to the root COW image'''
  67. return self.abspath(qubes.config.vm_files['rootcow_img'])
  68. @property
  69. def volatile_img(self):
  70. '''Path to the volatile image'''
  71. return self.abspath(qubes.config.vm_files['volatile_img'])
  72. @property
  73. def kernels_dir(self):
  74. '''Directory where kernel resides.
  75. If :py:attr:`self.vm.kernel` is :py:obj:`None`, the this points inside
  76. :py:attr:`self.vm.dir_path`
  77. '''
  78. return os.path.join(qubes.config.system_path['qubes_base_dir'],
  79. qubes.config.system_path['qubes_kernels_base_dir'], self.vm.kernel)\
  80. if self.vm.kernel is not None \
  81. else os.path.join(self.vm.dir_path,
  82. qubes.config.vm_files['kernels_subdir'])
  83. @property
  84. def modules_img(self):
  85. '''Path to image with modules.
  86. Depending on domain, this may be global or inside domain's dir.
  87. '''
  88. return os.path.join(self.kernels_dir, 'modules.img')
  89. @property
  90. def modules_img_rw(self):
  91. ''':py:obj:`True` if module image should be mounted RW, :py:obj:`False`
  92. otherwise.'''
  93. return self.vm.kernel is None
  94. def abspath(self, path, rel=None):
  95. '''Make absolute path.
  96. If given path is relative, it is interpreted as relative to
  97. :py:attr:`self.vm.dir_path` or given *rel*.
  98. '''
  99. return path if os.path.isabs(path) \
  100. else os.path.join(rel or self.vm.dir_path, path)
  101. def get_config_params(self):
  102. raise NotImplementedError()
  103. @staticmethod
  104. def _copy_file(source, destination):
  105. '''Effective file copy, preserving sparse files etc.
  106. '''
  107. # TODO: Windows support
  108. # We prefer to use Linux's cp, because it nicely handles sparse files
  109. try:
  110. subprocess.check_call(['cp', source, destination])
  111. except subprocess.CalledProcessError:
  112. raise IOError('Error while copying {!r} to {!r}'.format(
  113. source, destination))
  114. def get_disk_utilization(self):
  115. return get_disk_usage(self.vm.dir_path)
  116. def get_disk_utilization_private_img(self):
  117. # pylint: disable=invalid-name
  118. return get_disk_usage(self.private_img)
  119. def get_private_img_sz(self):
  120. if not os.path.exists(self.private_img):
  121. return 0
  122. return os.path.getsize(self.private_img)
  123. def resize_private_img(self, size):
  124. raise NotImplementedError()
  125. def create_on_disk_private_img(self, source_template=None):
  126. raise NotImplementedError()
  127. def create_on_disk_root_img(self, source_template=None):
  128. raise NotImplementedError()
  129. def create_on_disk(self, source_template=None):
  130. if source_template is None:
  131. source_template = self.vm.template
  132. old_umask = os.umask(002)
  133. self.vm.log.info('Creating directory: {0}'.format(self.vm.dir_path))
  134. os.mkdir(self.vm.dir_path)
  135. self.create_on_disk_private_img(source_template)
  136. self.create_on_disk_root_img(source_template)
  137. self.reset_volatile_storage(source_template)
  138. os.umask(old_umask)
  139. def clone_disk_files(self, src_vm):
  140. self.vm.log.info('Creating directory: {0}'.format(self.vm.dir_path))
  141. os.mkdir(self.vm.dir_path)
  142. if hasattr(src_vm, 'private_img'):
  143. self.vm.log.info('Copying the private image: {} -> {}'.format(
  144. src_vm.private_img, self.vm.private_img))
  145. self._copy_file(src_vm.private_img, self.vm.private_img)
  146. if src_vm.updateable and hasattr(src_vm, 'root_img'):
  147. self.vm.log.info('Copying the root image: {} -> {}'.format(
  148. src_vm.root_img, self.root_img))
  149. self._copy_file(src_vm.root_img, self.root_img)
  150. # TODO: modules?
  151. # XXX which modules? -woju
  152. @staticmethod
  153. def rename(newpath, oldpath):
  154. '''Move storage directory, most likely during domain's rename.
  155. .. note::
  156. The arguments are in different order than in :program:`cp` utility.
  157. .. versionchange:: 3.0
  158. This is now dummy method that just passes everything to
  159. :py:func:`os.rename`.
  160. :param str newpath: New path
  161. :param str oldpath: Old path
  162. '''
  163. os.rename(oldpath, newpath)
  164. def verify_files(self):
  165. if not os.path.exists(self.vm.dir_path):
  166. raise qubes.exc.QubesVMError(self.vm,
  167. 'VM directory does not exist: {}'.format(self.vm.dir_path))
  168. if hasattr(self.vm, 'root_img') and not os.path.exists(self.root_img):
  169. raise qubes.exc.QubesVMError(self.vm,
  170. 'VM root image file does not exist: {}'.format(self.root_img))
  171. if hasattr(self.vm, 'private_img') \
  172. and not os.path.exists(self.private_img):
  173. raise qubes.exc.QubesVMError(self.vm,
  174. 'VM private image file does not exist: {}'.format(
  175. self.private_img))
  176. if self.modules_img is not None \
  177. and not os.path.exists(self.modules_img):
  178. raise qubes.exc.QubesVMError(self.vm,
  179. 'VM kernel modules image does not exists: {}'.format(
  180. self.modules_img))
  181. def remove_from_disk(self):
  182. shutil.rmtree(self.vm.dir_path)
  183. def reset_volatile_storage(self, source_template=None):
  184. if source_template is None:
  185. source_template = self.vm.template
  186. # Re-create only for template based VMs
  187. if source_template is not None and self.volatile_img:
  188. if os.path.exists(self.volatile_img):
  189. os.remove(self.volatile_img)
  190. # For StandaloneVM create it only if not already exists
  191. # (eg after backup-restore)
  192. if hasattr(self.vm, 'volatile_img') \
  193. and not os.path.exists(self.vm.volatile_img):
  194. self.vm.log.info(
  195. 'Creating volatile image: {0}'.format(self.volatile_img))
  196. subprocess.check_call(
  197. [qubes.config.system_path["prepare_volatile_img_cmd"],
  198. self.volatile_img,
  199. str(self.root_img_size / 1024 / 1024)])
  200. def prepare_for_vm_startup(self):
  201. self.reset_volatile_storage()
  202. if hasattr(self.vm, 'private_img') \
  203. and not os.path.exists(self.private_img):
  204. self.vm.log.info('Creating empty VM private image file: {0}'.format(
  205. self.private_img))
  206. self.create_on_disk_private_img()
  207. def get_disk_usage_one(st):
  208. '''Extract disk usage of one inode from its stat_result struct.
  209. If known, get real disk usage, as written to device by filesystem, not
  210. logical file size. Those values may be different for sparse files.
  211. :param os.stat_result st: stat result
  212. :returns: disk usage
  213. '''
  214. try:
  215. return st.st_blocks * BLKSIZE
  216. except AttributeError:
  217. return st.st_size
  218. def get_disk_usage(path):
  219. '''Get real disk usage of given path (file or directory).
  220. When *path* points to directory, then it is evaluated recursively.
  221. This function tries estiate real disk usage. See documentation of
  222. :py:func:`get_disk_usage_one`.
  223. :param str path: path to evaluate
  224. :returns: disk usage
  225. '''
  226. try:
  227. st = os.lstat(path)
  228. except OSError:
  229. return 0
  230. ret = get_disk_usage_one(st)
  231. # if path is not a directory, this is skipped
  232. for dirpath, dirnames, filenames in os.walk(path):
  233. for name in dirnames + filenames:
  234. ret += get_disk_usage_one(os.lstat(os.path.join(dirpath, name)))
  235. return ret
  236. def get_storage(vm):
  237. '''Factory yielding storage class instances for domains.
  238. :raises ImportError: when storage class specified in config cannot be found
  239. :raises KeyError: when storage class specified in config cannot be found
  240. '''
  241. pkg, cls = qubes.config.defaults['storage_class'].strip().rsplit('.', 1)
  242. # this may raise ImportError or KeyError, that's okay
  243. return importlib.import_module(pkg).__dict__[cls](vm)