__init__.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/usr/bin/python2
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2013 Marek Marczykowski <marmarek@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License
  9. # as published by the Free Software Foundation; either version 2
  10. # of the License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. #
  21. #
  22. from __future__ import absolute_import
  23. import os
  24. import os.path
  25. import re
  26. import shutil
  27. import subprocess
  28. import sys
  29. from qubes.qubes import vm_files,system_path,defaults
  30. from qubes.qubes import QubesException
  31. import qubes.qubesutils
  32. class QubesVmStorage(object):
  33. """
  34. Class for handling VM virtual disks. This is base class for all other
  35. implementations, mostly with Xen on Linux in mind.
  36. """
  37. def __init__(self, vm,
  38. private_img_size = None,
  39. root_img_size = None,
  40. modules_img = None,
  41. modules_img_rw = False):
  42. self.vm = vm
  43. self.vmdir = vm.dir_path
  44. if private_img_size:
  45. self.private_img_size = private_img_size
  46. else:
  47. self.private_img_size = defaults['private_img_size']
  48. if root_img_size:
  49. self.root_img_size = root_img_size
  50. else:
  51. self.root_img_size = defaults['root_img_size']
  52. self.private_img = os.path.join(self.vmdir, vm_files["private_img"])
  53. if self.vm.template:
  54. self.root_img = self.vm.template.root_img
  55. else:
  56. self.root_img = os.path.join(self.vmdir, vm_files["root_img"])
  57. self.volatile_img = os.path.join(self.vmdir, vm_files["volatile_img"])
  58. # For now compute this path still in QubesVm
  59. self.modules_img = modules_img
  60. self.modules_img_rw = modules_img_rw
  61. def get_config_params(self):
  62. raise NotImplementedError
  63. def _copy_file(self, source, destination):
  64. """
  65. Effective file copy, preserving sparse files etc.
  66. """
  67. # TODO: Windows support
  68. # We prefer to use Linux's cp, because it nicely handles sparse files
  69. retcode = subprocess.call (["cp", source, destination])
  70. if retcode != 0:
  71. raise IOError ("Error while copying {0} to {1}".\
  72. format(source, destination))
  73. def get_disk_utilization(self):
  74. return qubes.qubesutils.get_disk_usage(self.vmdir)
  75. def get_disk_utilization_private_img(self):
  76. return qubes.qubesutils.get_disk_usage(self.private_img)
  77. def get_private_img_sz(self):
  78. if not os.path.exists(self.private_img):
  79. return 0
  80. return os.path.getsize(self.private_img)
  81. def resize_private_img(self, size):
  82. raise NotImplementedError
  83. def create_on_disk_private_img(self, verbose, source_template = None):
  84. raise NotImplementedError
  85. def create_on_disk_root_img(self, verbose, source_template = None):
  86. raise NotImplementedError
  87. def create_on_disk(self, verbose, source_template = None):
  88. if source_template is None:
  89. source_template = self.vm.template
  90. old_umask = os.umask(002)
  91. if verbose:
  92. print >> sys.stderr, "--> Creating directory: {0}".format(self.vmdir)
  93. os.mkdir (self.vmdir)
  94. self.create_on_disk_private_img(verbose, source_template)
  95. self.create_on_disk_root_img(verbose, source_template)
  96. self.reset_volatile_storage(verbose, source_template)
  97. os.umask(old_umask)
  98. def clone_disk_files(self, src_vm, verbose):
  99. if verbose:
  100. print >> sys.stderr, "--> Creating directory: {0}".format(self.vmdir)
  101. os.mkdir (self.vmdir)
  102. if src_vm.private_img is not None and self.private_img is not None:
  103. if verbose:
  104. print >> sys.stderr, "--> Copying the private image:\n{0} ==>\n{1}".\
  105. format(src_vm.private_img, self.private_img)
  106. self._copy_file(src_vm.private_img, self.private_img)
  107. if src_vm.updateable and src_vm.root_img is not None and self.root_img is not None:
  108. if verbose:
  109. print >> sys.stderr, "--> Copying the root image:\n{0} ==>\n{1}".\
  110. format(src_vm.root_img, self.root_img)
  111. self._copy_file(src_vm.root_img, self.root_img)
  112. # TODO: modules?
  113. def verify_files(self):
  114. if not os.path.exists (self.vmdir):
  115. raise QubesException (
  116. "VM directory doesn't exist: {0}".\
  117. format(self.vmdir))
  118. if self.vm.updateable and not os.path.exists (self.root_img):
  119. raise QubesException (
  120. "VM root image file doesn't exist: {0}".\
  121. format(self.root_img))
  122. if not os.path.exists (self.private_img):
  123. raise QubesException (
  124. "VM private image file doesn't exist: {0}".\
  125. format(self.private_img))
  126. if self.modules_img is not None:
  127. if not os.path.exists(self.modules_img):
  128. raise QubesException (
  129. "VM kernel modules image does not exists: {0}".\
  130. format(self.modules_img))
  131. def remove_from_disk(self):
  132. shutil.rmtree (self.vmdir)
  133. def reset_volatile_storage(self, verbose = False, source_template = None):
  134. if source_template is None:
  135. source_template = self.vm.template
  136. # Re-create only for template based VMs
  137. if source_template is not None:
  138. if os.path.exists(self.volatile_img):
  139. os.remove(self.volatile_img)
  140. # For StandaloneVM create it only if not already exists (eg after backup-restore)
  141. if not os.path.exists(self.volatile_img):
  142. if verbose:
  143. print >> sys.stderr, "--> Creating volatile image: {0}...".\
  144. format(self.volatile_img)
  145. subprocess.check_call([system_path["prepare_volatile_img_cmd"],
  146. self.volatile_img, str(self.root_img_size / 1024 / 1024)])
  147. def prepare_for_vm_startup(self, verbose):
  148. self.reset_volatile_storage(verbose=verbose)
  149. pass