02QubesTemplateHVm.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. return False
  61. def resize_root_img(self, size):
  62. for vm in self.appvms.values():
  63. if vm.is_running():
  64. raise QubesException("Cannot resize root.img while any VM "
  65. "based on this tempate is running")
  66. return super(QubesTemplateHVm, self).resize_root_img()
  67. def start(self, *args, **kwargs):
  68. for vm in self.appvms.values():
  69. if vm.is_running():
  70. raise QubesException("Cannot start HVM template while VMs based on it are running")
  71. return super(QubesTemplateHVm, self).start(*args, **kwargs)
  72. register_qubes_vm_class(QubesTemplateHVm)