003QubesTemplateVm.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/usr/bin/python2
  2. # -*- coding: utf-8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2010 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2013 Marek Marczykowski <marmarek@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License
  11. # as published by the Free Software Foundation; either version 2
  12. # of the License, or (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. #
  23. #
  24. import os
  25. import subprocess
  26. import sys
  27. from qubes.qubes import QubesVm,register_qubes_vm_class,dry_run
  28. from qubes.qubes import QubesVmCollection,QubesException,QubesVmLabels
  29. from qubes.qubes import defaults,system_path,vm_files
  30. class QubesTemplateVm(QubesVm):
  31. """
  32. A class that represents an TemplateVM. A child of QubesVm.
  33. """
  34. # In which order load this VM type from qubes.xml
  35. load_order = 50
  36. def get_attrs_config(self):
  37. attrs_config = super(QubesTemplateVm, self).get_attrs_config()
  38. attrs_config['dir_path']['func'] = \
  39. lambda value: value if value is not None else \
  40. os.path.join(system_path["qubes_templates_dir"], self.name)
  41. attrs_config['label']['default'] = defaults["template_label"]
  42. # New attributes
  43. # Image for template changes
  44. attrs_config['rootcow_img'] = {
  45. 'func': lambda x: os.path.join(self.dir_path, vm_files["rootcow_img"]) }
  46. # Clean image for root-cow and swap (AppVM side)
  47. attrs_config['clean_volatile_img'] = {
  48. 'func': lambda x: os.path.join(self.dir_path, vm_files["clean_volatile_img"]) }
  49. return attrs_config
  50. def __init__(self, **kwargs):
  51. super(QubesTemplateVm, self).__init__(**kwargs)
  52. self.appvms = QubesVmCollection()
  53. @property
  54. def type(self):
  55. return "TemplateVM"
  56. @property
  57. def updateable(self):
  58. return True
  59. def is_template(self):
  60. return True
  61. def get_firewall_defaults(self):
  62. return { "rules": list(), "allow": False, "allowDns": False, "allowIcmp": False, "allowYumProxy": True }
  63. # FIXME: source_template unused
  64. def get_rootdev(self, source_template=None):
  65. return self._format_disk_dev(
  66. "{dir}/root.img:{dir}/root-cow.img".format(
  67. dir=self.dir_path),
  68. "block-origin", "xvda", True)
  69. def clone_disk_files(self, src_vm, verbose):
  70. if dry_run:
  71. return
  72. super(QubesTemplateVm, self).clone_disk_files(src_vm=src_vm, verbose=verbose)
  73. if verbose:
  74. print >> sys.stderr, "--> Copying the template's clean volatile image:\n{0} ==>\n{1}".\
  75. format(src_vm.clean_volatile_img, self.clean_volatile_img)
  76. # We prefer to use Linux's cp, because it nicely handles sparse files
  77. retcode = subprocess.call (["cp", src_vm.clean_volatile_img, self.clean_volatile_img])
  78. if retcode != 0:
  79. raise IOError ("Error while copying {0} to {1}".\
  80. format(src_vm.clean_volatile_img, self.clean_volatile_img))
  81. if verbose:
  82. print >> sys.stderr, "--> Copying the template's volatile image:\n{0} ==>\n{1}".\
  83. format(self.clean_volatile_img, self.volatile_img)
  84. # We prefer to use Linux's cp, because it nicely handles sparse files
  85. retcode = subprocess.call (["cp", self.clean_volatile_img, self.volatile_img])
  86. if retcode != 0:
  87. raise IOError ("Error while copying {0} to {1}".\
  88. format(self.clean_img, self.volatile_img))
  89. # Create root-cow.img
  90. self.commit_changes(verbose=verbose)
  91. def post_rename(self, old_name):
  92. super(QubesTemplateVm, self).post_rename(old_name)
  93. old_dirpath = os.path.join(os.path.dirname(self.dir_path), old_name)
  94. self.clean_volatile_img = self.clean_volatile_img.replace(old_dirpath, self.dir_path)
  95. self.rootcow_img = self.rootcow_img.replace(old_dirpath, self.dir_path)
  96. def verify_files(self):
  97. if dry_run:
  98. return
  99. super(QubesTemplateVm, self).verify_files()
  100. if not os.path.exists (self.volatile_img):
  101. raise QubesException (
  102. "VM volatile image file doesn't exist: {0}".\
  103. format(self.volatile_img))
  104. if not os.path.exists (self.clean_volatile_img):
  105. raise QubesException (
  106. "Clean VM volatile image file doesn't exist: {0}".\
  107. format(self.clean_volatile_img))
  108. return True
  109. def reset_volatile_storage(self, verbose = False):
  110. assert not self.is_running(), "Attempt to clean volatile image of running Template VM!"
  111. if verbose:
  112. print >> sys.stderr, "--> Cleaning volatile image: {0}...".format (self.volatile_img)
  113. if dry_run:
  114. return
  115. if os.path.exists (self.volatile_img):
  116. os.remove (self.volatile_img)
  117. retcode = subprocess.call (["tar", "xf", self.clean_volatile_img, "-C", self.dir_path])
  118. if retcode != 0:
  119. raise IOError ("Error while unpacking {0} to {1}".\
  120. format(self.template.clean_volatile_img, self.volatile_img))
  121. def commit_changes (self, verbose = False):
  122. assert not self.is_running(), "Attempt to commit changes on running Template VM!"
  123. if verbose:
  124. print >> sys.stderr, "--> Commiting template updates... COW: {0}...".format (self.rootcow_img)
  125. if dry_run:
  126. return
  127. if os.path.exists (self.rootcow_img):
  128. os.rename (self.rootcow_img, self.rootcow_img + '.old')
  129. f_cow = open (self.rootcow_img, "w")
  130. f_root = open (self.root_img, "r")
  131. f_root.seek(0, os.SEEK_END)
  132. f_cow.truncate (f_root.tell()) # make empty sparse file of the same size as root.img
  133. f_cow.close ()
  134. f_root.close()
  135. register_qubes_vm_class(QubesTemplateVm)