003QubesTemplateVm.py 6.2 KB

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