xen.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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():
  64. return self._format_disk_dev(
  65. "{dir}/root.img:{dir}/root-cow.img".format(
  66. dir=self.vmdir),
  67. "block-origin", self.root_dev, True)
  68. elif self.vm.template:
  69. return self._format_disk_dev(
  70. "{dir}/root.img:{dir}/root-cow.img".format(
  71. dir=self.vm.template.dir_path),
  72. "block-snapshot", self.root_dev, False)
  73. else:
  74. return self._format_disk_dev(
  75. "{dir}/root.img".format(dir=self.vmdir),
  76. None, self.root_dev, True)
  77. def get_config_params(self):
  78. args = {}
  79. args['rootdev'] = self._get_rootdev()
  80. args['privatedev'] = \
  81. self._format_disk_dev(self.private_img,
  82. None, self.private_dev, True)
  83. args['volatiledev'] = \
  84. self._format_disk_dev(self.volatile_img,
  85. None, self.volatile_dev, True)
  86. if self.modules_img is not None:
  87. args['otherdevs'] = \
  88. self._format_disk_dev(self.modules_img,
  89. None, self.modules_dev, self.modules_img_rw)
  90. elif self.drive is not None:
  91. (drive_type, drive_domain, drive_path) = self.drive.split(":")
  92. if drive_domain.lower() == "dom0":
  93. drive_domain = None
  94. args['otherdevs'] = self._format_disk_dev(drive_path, None,
  95. self.modules_dev,
  96. rw=True if drive_type == "disk" else False, type=drive_type,
  97. domain=drive_domain)
  98. else:
  99. args['otherdevs'] = ''
  100. return args
  101. def create_on_disk_private_img(self, verbose, source_template = None):
  102. if source_template:
  103. template_priv = source_template.private_img
  104. if verbose:
  105. print >> sys.stderr, "--> Copying the template's private image: {0}".\
  106. format(template_priv)
  107. self._copy_file(template_priv, self.private_img)
  108. else:
  109. f_private = open (self.private_img, "a+b")
  110. f_private.truncate (self.private_img_size)
  111. f_private.close ()
  112. def create_on_disk_root_img(self, verbose, source_template = None):
  113. if source_template:
  114. if not self.vm.updateable:
  115. # just use template's disk
  116. return
  117. else:
  118. template_root = source_template.root_img
  119. if verbose:
  120. print >> sys.stderr, "--> Copying the template's root image: {0}".\
  121. format(template_root)
  122. self._copy_file(template_root, self.root_img)
  123. else:
  124. f_root = open (self.root_img, "a+b")
  125. f_root.truncate (self.root_img_size)
  126. f_root.close ()
  127. if self.vm.is_template():
  128. self.commit_template_changes()
  129. def rename(self, old_name, new_name):
  130. super(QubesXenVmStorage, self).rename(old_name, new_name)
  131. old_dirpath = os.path.join(os.path.dirname(self.vmdir), old_name)
  132. if self.rootcow_img:
  133. self.rootcow_img = self.rootcow_img.replace(old_dirpath,
  134. self.vmdir)
  135. def resize_private_img(self, size):
  136. f_private = open (self.private_img, "a+b")
  137. f_private.truncate (size)
  138. f_private.close ()
  139. # find loop device if any
  140. p = subprocess.Popen (["sudo", "losetup", "--associated", self.private_img],
  141. stdout=subprocess.PIPE)
  142. result = p.communicate()
  143. m = re.match(r"^(/dev/loop\d+):\s", result[0])
  144. if m is not None:
  145. loop_dev = m.group(1)
  146. # resize loop device
  147. subprocess.check_call(["sudo", "losetup", "--set-capacity", loop_dev])
  148. def commit_template_changes(self):
  149. assert self.vm.is_template()
  150. # TODO: move rootcow_img to this class; the same for vm.is_outdated()
  151. if os.path.exists (self.rootcow_img):
  152. os.rename (self.rootcow_img, self.rootcow_img + '.old')
  153. old_umask = os.umask(002)
  154. f_cow = open (self.rootcow_img, "w")
  155. f_root = open (self.root_img, "r")
  156. f_root.seek(0, os.SEEK_END)
  157. f_cow.truncate (f_root.tell()) # make empty sparse file of the same size as root.img
  158. f_cow.close ()
  159. f_root.close()
  160. os.umask(old_umask)