dvmtemplate.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # -*- encoding: utf-8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (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 along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. import qubes.events
  21. class DVMTemplateMixin(qubes.events.Emitter):
  22. '''VM class capable of being DVM template'''
  23. template_for_dispvms = qubes.property('template_for_dispvms',
  24. type=bool,
  25. default=False,
  26. doc='Should this VM be allowed to start as Disposable VM')
  27. @qubes.events.handler('property-pre-set:template_for_dispvms')
  28. def __on_pre_set_dvmtemplate(self, event, name,
  29. newvalue, oldvalue=None):
  30. # pylint: disable=unused-argument
  31. if newvalue:
  32. return
  33. if any(self.dispvms):
  34. raise qubes.exc.QubesVMInUseError(self,
  35. 'Cannot change template_for_dispvms to False while there are '
  36. 'some DispVMs based on this DVM template')
  37. @qubes.events.handler('property-pre-del:template_for_dispvms')
  38. def __on_pre_del_dvmtemplate(self, event, name,
  39. oldvalue=None):
  40. self.__on_pre_set_dvmtemplate(
  41. event, name, False, oldvalue)
  42. @qubes.events.handler('property-pre-set:template')
  43. def __on_property_pre_set_template(self, event, name, newvalue,
  44. oldvalue=None):
  45. # pylint: disable=unused-argument
  46. if any(self.dispvms):
  47. raise qubes.exc.QubesVMInUseError(self,
  48. 'Cannot change template '
  49. 'while there are DispVMs based on this qube')
  50. @property
  51. def dispvms(self):
  52. ''' Returns a generator containing all Disposable VMs based on the
  53. current AppVM.
  54. '''
  55. for vm in self.app.domains:
  56. if getattr(vm, 'template', None) == self:
  57. yield vm