appvm.py 5.1 KB

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