02QubesTemplateHVm.py 2.9 KB

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