02QubesTemplateHVm.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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
  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. def rootcow_img(self):
  60. return self.storage.rootcow_img
  61. @classmethod
  62. def is_template_compatible(cls, template):
  63. if template is None:
  64. return True
  65. return False
  66. def resize_root_img(self, size):
  67. for vm in self.appvms.values():
  68. if vm.is_running():
  69. raise QubesException("Cannot resize root.img while any VM "
  70. "based on this tempate is running")
  71. return super(QubesTemplateHVm, self).resize_root_img(size)
  72. def start(self, *args, **kwargs):
  73. for vm in self.appvms.values():
  74. if vm.is_running():
  75. raise QubesException("Cannot start HVM template while VMs based on it are running")
  76. return super(QubesTemplateHVm, self).start(*args, **kwargs)
  77. def commit_changes (self, verbose = False):
  78. self.log.debug('commit_changes()')
  79. # nothing to do as long as root-cow.img is unused
  80. pass
  81. register_qubes_vm_class(QubesTemplateHVm)