appvm.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. dispvm_allowed = qubes.property('dispvm_allowed',
  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. self.volume_config = copy.deepcopy(self.default_volume_config)
  68. template = kwargs.get('template', None)
  69. if template is not None:
  70. # template is only passed if the AppVM is created, in other cases we
  71. # don't need to patch the volume_config because the config is
  72. # coming from XML, already as we need it
  73. for name, conf in self.volume_config.items():
  74. tpl_volume = template.volumes[name]
  75. self.config_volume_from_source(conf, tpl_volume)
  76. for name, config in template.volume_config.items():
  77. # in case the template vm has more volumes add them to own
  78. # config
  79. if name not in self.volume_config:
  80. self.volume_config[name] = config.copy()
  81. if 'vid' in self.volume_config[name]:
  82. del self.volume_config[name]['vid']
  83. super(AppVM, self).__init__(app, xml, **kwargs)
  84. @qubes.events.handler('domain-load')
  85. def on_domain_loaded(self, event):
  86. ''' When domain is loaded assert that this vm has a template.
  87. ''' # pylint: disable=unused-argument
  88. assert self.template
  89. @qubes.events.handler('property-pre-set:template')
  90. def on_property_pre_set_template(self, event, name, newvalue,
  91. oldvalue=None):
  92. '''Forbid changing template of running VM
  93. ''' # pylint: disable=unused-argument
  94. if not self.is_halted():
  95. raise qubes.exc.QubesVMNotHaltedError(self,
  96. 'Cannot change template while qube is running')
  97. @qubes.events.handler('property-set:template')
  98. def on_property_set_template(self, event, name, newvalue, oldvalue=None):
  99. ''' Adjust root (and possibly other snap_on_start=True) volume
  100. on template change.
  101. ''' # pylint: disable=unused-argument
  102. for volume_name, conf in self.default_volume_config.items():
  103. if conf.get('snap_on_start', False) and \
  104. conf.get('source', None) is None:
  105. config = conf.copy()
  106. template_volume = newvalue.volumes[volume_name]
  107. self.volume_config[volume_name] = \
  108. self.config_volume_from_source(
  109. config,
  110. template_volume)
  111. self.storage.init_volume(volume_name, config)