__init__.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. # Additional drive (currently used only by HVM)
  62. self.drive = None
  63. def get_config_params(self):
  64. raise NotImplementedError
  65. def _copy_file(self, source, destination):
  66. """
  67. Effective file copy, preserving sparse files etc.
  68. """
  69. # TODO: Windows support
  70. # We prefer to use Linux's cp, because it nicely handles sparse files
  71. retcode = subprocess.call (["cp", source, destination])
  72. if retcode != 0:
  73. raise IOError ("Error while copying {0} to {1}".\
  74. format(source, destination))
  75. def get_disk_utilization(self):
  76. return qubes.qubesutils.get_disk_usage(self.vmdir)
  77. def get_disk_utilization_private_img(self):
  78. return qubes.qubesutils.get_disk_usage(self.private_img)
  79. def get_private_img_sz(self):
  80. if not os.path.exists(self.private_img):
  81. return 0
  82. return os.path.getsize(self.private_img)
  83. def resize_private_img(self, size):
  84. raise NotImplementedError
  85. def create_on_disk_private_img(self, verbose, source_template = None):
  86. raise NotImplementedError
  87. def create_on_disk_root_img(self, verbose, source_template = None):
  88. raise NotImplementedError
  89. def create_on_disk(self, verbose, source_template = None):
  90. if source_template is None:
  91. source_template = self.vm.template
  92. old_umask = os.umask(002)
  93. if verbose:
  94. print >> sys.stderr, "--> Creating directory: {0}".format(self.vmdir)
  95. os.mkdir (self.vmdir)
  96. self.create_on_disk_private_img(verbose, source_template)
  97. self.create_on_disk_root_img(verbose, source_template)
  98. self.reset_volatile_storage(verbose, source_template)
  99. os.umask(old_umask)
  100. def clone_disk_files(self, src_vm, verbose):
  101. if verbose:
  102. print >> sys.stderr, "--> Creating directory: {0}".format(self.vmdir)
  103. os.mkdir (self.vmdir)
  104. if src_vm.private_img is not None and self.private_img is not None:
  105. if verbose:
  106. print >> sys.stderr, "--> Copying the private image:\n{0} ==>\n{1}".\
  107. format(src_vm.private_img, self.private_img)
  108. self._copy_file(src_vm.private_img, self.private_img)
  109. if src_vm.updateable and src_vm.root_img is not None and self.root_img is not None:
  110. if verbose:
  111. print >> sys.stderr, "--> Copying the root image:\n{0} ==>\n{1}".\
  112. format(src_vm.root_img, self.root_img)
  113. self._copy_file(src_vm.root_img, self.root_img)
  114. # TODO: modules?
  115. def rename(self, old_name, new_name):
  116. old_vmdir = self.vmdir
  117. new_vmdir = os.path.join(os.path.dirname(self.vmdir), new_name)
  118. os.rename(self.vmdir, new_vmdir)
  119. self.vmdir = new_vmdir
  120. if self.private_img:
  121. self.private_img = self.private_img.replace(old_vmdir, new_vmdir)
  122. if self.root_img:
  123. self.root_img = self.root_img.replace(old_vmdir, new_vmdir)
  124. if self.volatile_img:
  125. self.volatile_img = self.volatile_img.replace(old_vmdir, new_vmdir)
  126. def verify_files(self):
  127. if not os.path.exists (self.vmdir):
  128. raise QubesException (
  129. "VM directory doesn't exist: {0}".\
  130. format(self.vmdir))
  131. if self.root_img and not os.path.exists (self.root_img):
  132. raise QubesException (
  133. "VM root image file doesn't exist: {0}".\
  134. format(self.root_img))
  135. if self.private_img and not os.path.exists (self.private_img):
  136. raise QubesException (
  137. "VM private image file doesn't exist: {0}".\
  138. format(self.private_img))
  139. if self.modules_img is not None:
  140. if not os.path.exists(self.modules_img):
  141. raise QubesException (
  142. "VM kernel modules image does not exists: {0}".\
  143. format(self.modules_img))
  144. def remove_from_disk(self):
  145. shutil.rmtree (self.vmdir)
  146. def reset_volatile_storage(self, verbose = False, source_template = None):
  147. if source_template is None:
  148. source_template = self.vm.template
  149. # Re-create only for template based VMs
  150. if source_template is not None and self.volatile_img:
  151. if os.path.exists(self.volatile_img):
  152. os.remove(self.volatile_img)
  153. # For StandaloneVM create it only if not already exists (eg after backup-restore)
  154. if self.volatile_img and not os.path.exists(self.volatile_img):
  155. if verbose:
  156. print >> sys.stderr, "--> Creating volatile image: {0}...".\
  157. format(self.volatile_img)
  158. subprocess.check_call([system_path["prepare_volatile_img_cmd"],
  159. self.volatile_img, str(self.root_img_size / 1024 / 1024)])
  160. def prepare_for_vm_startup(self, verbose):
  161. self.reset_volatile_storage(verbose=verbose)
  162. if self.private_img and not os.path.exists (self.private_img):
  163. print >>sys.stderr, "WARNING: Creating empty VM private image file: {0}".\
  164. format(self.private_img)
  165. self.storage.create_on_disk_private_img(verbose=False)