02QubesTemplateHVm.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 os.path
  25. import subprocess
  26. import stat
  27. import sys
  28. import re
  29. from qubes.qubes import QubesHVm,register_qubes_vm_class,xs,xc,dry_run
  30. from qubes.qubes import QubesException,QubesVmCollection
  31. from qubes.qubes import system_path,defaults
  32. class QubesTemplateHVm(QubesHVm):
  33. """
  34. A class that represents an HVM template. A child of QubesHVm.
  35. """
  36. # In which order load this VM type from qubes.xml
  37. load_order = 50
  38. def get_attrs_config(self):
  39. attrs_config = super(QubesTemplateHVm, self).get_attrs_config()
  40. attrs_config['dir_path']['func'] = \
  41. lambda value: value if value is not None else \
  42. os.path.join(system_path["qubes_templates_dir"], self.name)
  43. attrs_config['label']['default'] = defaults["template_label"]
  44. return attrs_config
  45. def __init__(self, **kwargs):
  46. super(QubesTemplateHVm, self).__init__(**kwargs)
  47. self.appvms = QubesVmCollection()
  48. @property
  49. def type(self):
  50. return "TemplateHVM"
  51. @property
  52. def updateable(self):
  53. return True
  54. def is_template(self):
  55. return True
  56. def is_appvm(self):
  57. return False
  58. @classmethod
  59. def is_template_compatible(cls, template):
  60. if template is None:
  61. return True
  62. return False
  63. def resize_root_img(self, size):
  64. for vm in self.appvms.values():
  65. if vm.is_running():
  66. raise QubesException("Cannot resize root.img while any VM "
  67. "based on this tempate is running")
  68. return super(QubesTemplateHVm, self).resize_root_img(size)
  69. def start(self, *args, **kwargs):
  70. for vm in self.appvms.values():
  71. if vm.is_running():
  72. raise QubesException("Cannot start HVM template while VMs based on it are running")
  73. return super(QubesTemplateHVm, self).start(*args, **kwargs)
  74. register_qubes_vm_class(QubesTemplateHVm)