appvm.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. import qubes.vm.mix.dvmtemplate
  26. from qubes.config import defaults
  27. def template_changed_update_storage(self):
  28. '''Update storage configuration for TemplateVM changes'''
  29. for volume_name, conf in self.default_volume_config.items():
  30. if conf.get('snap_on_start', False) and \
  31. conf.get('source', None) is None:
  32. config = conf.copy()
  33. self.volume_config[volume_name] = config
  34. self.storage.init_volume(volume_name, config)
  35. class AppVM(qubes.vm.mix.dvmtemplate.DVMTemplateMixin,
  36. qubes.vm.qubesvm.QubesVM):
  37. '''Application VM'''
  38. template = qubes.VMProperty('template',
  39. load_stage=4,
  40. vmclass=qubes.vm.templatevm.TemplateVM,
  41. doc='Template, on which this AppVM is based.')
  42. default_volume_config = {
  43. 'root': {
  44. 'name': 'root',
  45. 'snap_on_start': True,
  46. 'save_on_stop': False,
  47. 'rw': True,
  48. 'source': None,
  49. },
  50. 'private': {
  51. 'name': 'private',
  52. 'snap_on_start': False,
  53. 'save_on_stop': True,
  54. 'rw': True,
  55. 'size': defaults['private_img_size'],
  56. },
  57. 'volatile': {
  58. 'name': 'volatile',
  59. 'snap_on_start': False,
  60. 'save_on_stop': False,
  61. 'size': defaults['root_img_size'],
  62. 'rw': True,
  63. },
  64. 'kernel': {
  65. 'name': 'kernel',
  66. 'snap_on_start': False,
  67. 'save_on_stop': False,
  68. 'rw': False,
  69. }
  70. }
  71. def __init__(self, app, xml, **kwargs):
  72. # migrate renamed properties
  73. if xml is not None:
  74. node_dispvm_allowed = xml.find(
  75. './properties/property[@name=\'dispvm_allowed\']')
  76. if node_dispvm_allowed is not None:
  77. kwargs['template_for_dispvms'] = \
  78. qubes.property.bool(None, None, node_dispvm_allowed.text)
  79. node_dispvm_allowed.getparent().remove(node_dispvm_allowed)
  80. self.volume_config = copy.deepcopy(self.default_volume_config)
  81. template = kwargs.get('template', None)
  82. if template is not None:
  83. # template is only passed if the AppVM is created, in other cases we
  84. # don't need to patch the volume_config because the config is
  85. # coming from XML, already as we need it
  86. for name, config in template.volume_config.items():
  87. # in case the template vm has more volumes add them to own
  88. # config
  89. if name not in self.volume_config:
  90. self.volume_config[name] = config.copy()
  91. if 'vid' in self.volume_config[name]:
  92. del self.volume_config[name]['vid']
  93. super().__init__(app, xml, **kwargs)
  94. @qubes.events.handler('domain-load')
  95. def on_domain_loaded(self, event):
  96. ''' When domain is loaded assert that this vm has a template.
  97. ''' # pylint: disable=unused-argument
  98. assert self.template
  99. @qubes.events.handler('property-pre-reset:template')
  100. def on_property_pre_reset_template(self, event, name, oldvalue=None):
  101. '''Forbid deleting template of running VM
  102. ''' # pylint: disable=unused-argument,no-self-use
  103. raise qubes.exc.QubesValueError('Cannot unset template')
  104. @qubes.events.handler('property-pre-set:template')
  105. def on_property_pre_set_template(self, event, name, newvalue,
  106. oldvalue=None):
  107. '''Forbid changing template of running VM
  108. ''' # pylint: disable=unused-argument
  109. if not self.is_halted():
  110. raise qubes.exc.QubesVMNotHaltedError(self,
  111. 'Cannot change template while qube is running')
  112. @qubes.events.handler('property-set:template')
  113. def on_property_set_template(self, event, name, newvalue, oldvalue=None):
  114. ''' Adjust root (and possibly other snap_on_start=True) volume
  115. on template change.
  116. ''' # pylint: disable=unused-argument
  117. template_changed_update_storage(self)
  118. for vm in self.dispvms:
  119. vm.on_property_set_template(event, name, newvalue, oldvalue)