appvm.py 4.9 KB

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