xen.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 subprocess
  26. import sys
  27. import re
  28. from qubes.storage import QubesVmStorage
  29. from qubes.qubes import QubesException, vm_files
  30. class QubesXenVmStorage(QubesVmStorage):
  31. """
  32. Class for VM storage of Xen VMs.
  33. """
  34. def __init__(self, vm, **kwargs):
  35. super(QubesXenVmStorage, self).__init__(vm, **kwargs)
  36. self.root_dev = "xvda"
  37. self.private_dev = "xvdb"
  38. self.volatile_dev = "xvdc"
  39. self.modules_dev = "xvdd"
  40. if self.vm.is_template():
  41. self.rootcow_img = os.path.join(self.vmdir, vm_files["rootcow_img"])
  42. else:
  43. self.rootcow_img = None
  44. def _format_disk_dev(self, path, script, vdev, rw=True, type="disk", domain=None):
  45. if path is None:
  46. return ''
  47. template = " <disk type='block' device='{type}'>\n" \
  48. " <driver name='phy'/>\n" \
  49. " <source dev='{path}'/>\n" \
  50. " <target dev='{vdev}' bus='xen'/>\n" \
  51. "{params}" \
  52. " </disk>\n"
  53. params = ""
  54. if not rw:
  55. params += " <readonly/>\n"
  56. if domain:
  57. params += " <backenddomain name='%s'/>\n" % domain
  58. if script:
  59. params += " <script path='%s'/>\n" % script
  60. return template.format(path=path, vdev=vdev, type=type,
  61. params=params)
  62. def _get_rootdev(self):
  63. if self.vm.is_template() and \
  64. os.path.exists(os.path.join(self.vmdir, "root-cow.img")):
  65. return self._format_disk_dev(
  66. "{dir}/root.img:{dir}/root-cow.img".format(
  67. dir=self.vmdir),
  68. "block-origin", self.root_dev, True)
  69. elif self.vm.template:
  70. return self._format_disk_dev(
  71. "{dir}/root.img:{dir}/root-cow.img".format(
  72. dir=self.vm.template.dir_path),
  73. "block-snapshot", self.root_dev, False)
  74. else:
  75. return self._format_disk_dev(
  76. "{dir}/root.img".format(dir=self.vmdir),
  77. None, self.root_dev, True)
  78. def get_config_params(self):
  79. args = {}
  80. args['rootdev'] = self._get_rootdev()
  81. args['privatedev'] = \
  82. self._format_disk_dev(self.private_img,
  83. None, self.private_dev, True)
  84. args['volatiledev'] = \
  85. self._format_disk_dev(self.volatile_img,
  86. None, self.volatile_dev, True)
  87. if self.modules_img is not None:
  88. args['otherdevs'] = \
  89. self._format_disk_dev(self.modules_img,
  90. None, self.modules_dev, self.modules_img_rw)
  91. elif self.drive is not None:
  92. (drive_type, drive_domain, drive_path) = self.drive.split(":")
  93. if drive_domain.lower() == "dom0":
  94. drive_domain = None
  95. args['otherdevs'] = self._format_disk_dev(drive_path, None,
  96. self.modules_dev,
  97. rw=True if drive_type == "disk" else False, type=drive_type,
  98. domain=drive_domain)
  99. else:
  100. args['otherdevs'] = ''
  101. return args
  102. def create_on_disk_private_img(self, verbose, source_template = None):
  103. if source_template:
  104. template_priv = source_template.private_img
  105. if verbose:
  106. print >> sys.stderr, "--> Copying the template's private image: {0}".\
  107. format(template_priv)
  108. self._copy_file(template_priv, self.private_img)
  109. else:
  110. f_private = open (self.private_img, "a+b")
  111. f_private.truncate (self.private_img_size)
  112. f_private.close ()
  113. def create_on_disk_root_img(self, verbose, source_template = None):
  114. if source_template:
  115. if not self.vm.updateable:
  116. # just use template's disk
  117. return
  118. else:
  119. template_root = source_template.root_img
  120. if verbose:
  121. print >> sys.stderr, "--> Copying the template's root image: {0}".\
  122. format(template_root)
  123. self._copy_file(template_root, self.root_img)
  124. else:
  125. f_root = open (self.root_img, "a+b")
  126. f_root.truncate (self.root_img_size)
  127. f_root.close ()
  128. if self.vm.is_template():
  129. self.commit_template_changes()
  130. def rename(self, old_name, new_name):
  131. super(QubesXenVmStorage, self).rename(old_name, new_name)
  132. old_dirpath = os.path.join(os.path.dirname(self.vmdir), old_name)
  133. if self.rootcow_img:
  134. self.rootcow_img = self.rootcow_img.replace(old_dirpath,
  135. self.vmdir)
  136. def resize_private_img(self, size):
  137. f_private = open (self.private_img, "a+b")
  138. f_private.truncate (size)
  139. f_private.close ()
  140. # find loop device if any
  141. p = subprocess.Popen (["sudo", "losetup", "--associated", self.private_img],
  142. stdout=subprocess.PIPE)
  143. result = p.communicate()
  144. m = re.match(r"^(/dev/loop\d+):\s", result[0])
  145. if m is not None:
  146. loop_dev = m.group(1)
  147. # resize loop device
  148. subprocess.check_call(["sudo", "losetup", "--set-capacity", loop_dev])
  149. def commit_template_changes(self):
  150. assert self.vm.is_template()
  151. # TODO: move rootcow_img to this class; the same for vm.is_outdated()
  152. if os.path.exists (self.rootcow_img):
  153. os.rename (self.rootcow_img, self.rootcow_img + '.old')
  154. old_umask = os.umask(002)
  155. f_cow = open (self.rootcow_img, "w")
  156. f_root = open (self.root_img, "r")
  157. f_root.seek(0, os.SEEK_END)
  158. f_cow.truncate (f_root.tell()) # make empty sparse file of the same size as root.img
  159. f_cow.close ()
  160. f_root.close()
  161. os.umask(old_umask)