dvmtemplate.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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_pre_property_set_template(self, event, name, newvalue,
  44. oldvalue=None):
  45. # pylint: disable=unused-argument
  46. for vm in self.dispvms:
  47. if vm.is_running():
  48. raise qubes.exc.QubesVMNotHaltedError(self,
  49. 'Cannot change template while there are running DispVMs '
  50. 'based on this DVM template')
  51. @qubes.events.handler('property-set:template')
  52. def __on_property_set_template(self, event, name, newvalue,
  53. oldvalue=None):
  54. # pylint: disable=unused-argument
  55. pass
  56. @property
  57. def dispvms(self):
  58. ''' Returns a generator containing all Disposable VMs based on the
  59. current AppVM.
  60. '''
  61. for vm in self.app.domains:
  62. if getattr(vm, 'template', None) == self:
  63. yield vm