02QubesTemplateHVm.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 os.path
  26. import subprocess
  27. import stat
  28. import sys
  29. import re
  30. from qubes.qubes import QubesHVm,register_qubes_vm_class,dry_run,vmm
  31. from qubes.qubes import QubesException,QubesVmCollection
  32. from qubes.qubes import system_path,defaults
  33. class QubesTemplateHVm(QubesHVm):
  34. """
  35. A class that represents an HVM template. A child of QubesHVm.
  36. """
  37. # In which order load this VM type from qubes.xml
  38. load_order = 50
  39. def get_attrs_config(self):
  40. attrs_config = super(QubesTemplateHVm, self).get_attrs_config()
  41. attrs_config['dir_path']['func'] = \
  42. lambda value: value if value is not None else \
  43. os.path.join(system_path["qubes_templates_dir"], self.name)
  44. attrs_config['label']['default'] = defaults["template_label"]
  45. return attrs_config
  46. def __init__(self, **kwargs):
  47. super(QubesTemplateHVm, self).__init__(**kwargs)
  48. self.appvms = QubesVmCollection()
  49. @property
  50. def type(self):
  51. return "TemplateHVM"
  52. @property
  53. def updateable(self):
  54. return True
  55. def is_template(self):
  56. return True
  57. def is_appvm(self):
  58. return False
  59. @property
  60. def rootcow_img(self):
  61. return self.storage.rootcow_img
  62. @classmethod
  63. def is_template_compatible(cls, template):
  64. if template is None:
  65. return True
  66. return False
  67. def resize_root_img(self, size):
  68. for vm in self.appvms.values():
  69. if vm.is_running():
  70. raise QubesException("Cannot resize root.img while any VM "
  71. "based on this tempate is running")
  72. return super(QubesTemplateHVm, self).resize_root_img(size)
  73. def start(self, *args, **kwargs):
  74. for vm in self.appvms.values():
  75. if vm.is_running():
  76. raise QubesException("Cannot start HVM template while VMs based on it are running")
  77. return super(QubesTemplateHVm, self).start(*args, **kwargs)
  78. def commit_changes (self, verbose = False):
  79. self.log.debug('commit_changes()')
  80. if not vmm.offline_mode:
  81. assert not self.is_running(), "Attempt to commit changes on running Template VM!"
  82. if verbose:
  83. print >> sys.stderr, "--> Commiting template updates... COW: {0}...".format (self.rootcow_img)
  84. if dry_run:
  85. return
  86. self.storage.commit_template_changes()
  87. register_qubes_vm_class(QubesTemplateHVm)