appvm.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #
  2. # The Qubes OS Project, http://www.qubes-os.org
  3. #
  4. # Copyright (C) 2014-2016 Wojtek Porczyk <woju@invisiblethingslab.com>
  5. # Copyright (C) 2016 Marek Marczykowski <marmarek@invisiblethingslab.com>)
  6. # Copyright (C) 2016 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
  7. #
  8. # This library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2.1 of the License, or (at your option) any later version.
  12. #
  13. # This library 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 GNU
  16. # Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public
  19. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  20. #
  21. ''' This module contains the AppVM implementation '''
  22. import copy
  23. import qubes.events
  24. import qubes.vm.qubesvm
  25. from qubes.config import defaults
  26. class AppVM(qubes.vm.qubesvm.QubesVM):
  27. '''Application VM'''
  28. template = qubes.VMProperty('template',
  29. load_stage=4,
  30. vmclass=qubes.vm.templatevm.TemplateVM,
  31. doc='Template, on which this AppVM is based.')
  32. template_for_dispvms = qubes.property('template_for_dispvms',
  33. type=bool,
  34. default=False,
  35. doc='Should this VM be allowed to start as Disposable VM')
  36. default_volume_config = {
  37. 'root': {
  38. 'name': 'root',
  39. 'snap_on_start': True,
  40. 'save_on_stop': False,
  41. 'rw': True,
  42. 'source': None,
  43. },
  44. 'private': {
  45. 'name': 'private',
  46. 'snap_on_start': False,
  47. 'save_on_stop': True,
  48. 'rw': True,
  49. 'size': defaults['private_img_size'],
  50. },
  51. 'volatile': {
  52. 'name': 'volatile',
  53. 'snap_on_start': False,
  54. 'save_on_stop': False,
  55. 'size': defaults['root_img_size'],
  56. 'rw': True,
  57. },
  58. 'kernel': {
  59. 'name': 'kernel',
  60. 'snap_on_start': False,
  61. 'save_on_stop': False,
  62. 'rw': False,
  63. }
  64. }
  65. def __init__(self, app, xml, **kwargs):
  66. # migrate renamed properties
  67. if xml is not None:
  68. node_dispvm_allowed = xml.find(
  69. './properties/property[@name=\'dispvm_allowed\']')
  70. if node_dispvm_allowed is not None:
  71. kwargs['template_for_dispvms'] = \
  72. qubes.property.bool(None, None, node_dispvm_allowed.text)
  73. node_dispvm_allowed.getparent().remove(node_dispvm_allowed)
  74. self.volume_config = copy.deepcopy(self.default_volume_config)
  75. template = kwargs.get('template', None)
  76. if template is not None:
  77. # template is only passed if the AppVM is created, in other cases we
  78. # don't need to patch the volume_config because the config is
  79. # coming from XML, already as we need it
  80. for name, config in template.volume_config.items():
  81. # in case the template vm has more volumes add them to own
  82. # config
  83. if name not in self.volume_config:
  84. self.volume_config[name] = config.copy()
  85. if 'vid' in self.volume_config[name]:
  86. del self.volume_config[name]['vid']
  87. super(AppVM, self).__init__(app, xml, **kwargs)
  88. @property
  89. def dispvms(self):
  90. ''' Returns a generator containing all Disposable VMs based on the
  91. current AppVM.
  92. '''
  93. for vm in self.app.domains:
  94. if hasattr(vm, 'template') and vm.template is self:
  95. yield vm
  96. @qubes.events.handler('domain-load')
  97. def on_domain_loaded(self, event):
  98. ''' When domain is loaded assert that this vm has a template.
  99. ''' # pylint: disable=unused-argument
  100. assert self.template
  101. @qubes.events.handler('property-pre-del:template')
  102. def on_property_pre_del_template(self, event, name, oldvalue=None):
  103. '''Forbid deleting template of running VM
  104. ''' # pylint: disable=unused-argument,no-self-use
  105. raise qubes.exc.QubesValueError('Cannot unset template')
  106. @qubes.events.handler('property-pre-set:template')
  107. def on_property_pre_set_template(self, event, name, newvalue,
  108. oldvalue=None):
  109. '''Forbid changing template of running VM
  110. ''' # pylint: disable=unused-argument
  111. if not self.is_halted():
  112. raise qubes.exc.QubesVMNotHaltedError(self,
  113. 'Cannot change template while qube is running')
  114. if any(self.dispvms):
  115. raise qubes.exc.QubesVMInUseError(self,
  116. 'Cannot change template '
  117. 'while there are DispVMs based on this qube')
  118. @qubes.events.handler('property-set:template')
  119. def on_property_set_template(self, event, name, newvalue, oldvalue=None):
  120. ''' Adjust root (and possibly other snap_on_start=True) volume
  121. on template change.
  122. ''' # pylint: disable=unused-argument
  123. for volume_name, conf in self.default_volume_config.items():
  124. if conf.get('snap_on_start', False) and \
  125. conf.get('source', None) is None:
  126. config = conf.copy()
  127. self.volume_config[volume_name] = config
  128. self.storage.init_volume(volume_name, config)