appvm.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. 'pool': 'default',
  41. 'snap_on_start': True,
  42. 'save_on_stop': False,
  43. 'rw': False,
  44. 'source': None,
  45. 'internal': True
  46. },
  47. 'private': {
  48. 'name': 'private',
  49. 'pool': 'default',
  50. 'snap_on_start': False,
  51. 'save_on_stop': True,
  52. 'rw': True,
  53. 'size': defaults['private_img_size'],
  54. 'internal': True
  55. },
  56. 'volatile': {
  57. 'name': 'volatile',
  58. 'pool': 'default',
  59. 'size': defaults['root_img_size'],
  60. 'internal': True,
  61. 'rw': True,
  62. },
  63. 'kernel': {
  64. 'name': 'kernel',
  65. 'pool': 'linux-kernel',
  66. 'rw': False,
  67. 'internal': True
  68. }
  69. }
  70. def __init__(self, app, xml, **kwargs):
  71. self.volume_config = copy.deepcopy(self.default_volume_config)
  72. template = kwargs.get('template', None)
  73. if template is not None:
  74. # template is only passed if the AppVM is created, in other cases we
  75. # don't need to patch the volume_config because the config is
  76. # coming from XML, already as we need it
  77. for name, conf in self.volume_config.items():
  78. tpl_volume = template.volumes[name]
  79. self.config_volume_from_source(conf, tpl_volume)
  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] = copy.deepcopy(config)
  85. if 'vid' in self.volume_config[name]:
  86. del self.volume_config[name]['vid']
  87. super(AppVM, self).__init__(app, xml, **kwargs)
  88. @qubes.events.handler('domain-load')
  89. def on_domain_loaded(self, event):
  90. ''' When domain is loaded assert that this vm has a template.
  91. ''' # pylint: disable=unused-argument
  92. assert self.template
  93. @qubes.events.handler('property-pre-set:template')
  94. def on_property_pre_set_template(self, event, name, newvalue,
  95. oldvalue=None):
  96. '''Forbid changing template of running VM
  97. ''' # pylint: disable=unused-argument
  98. if not self.is_halted():
  99. raise qubes.exc.QubesVMNotHaltedError(self,
  100. 'Cannot change template while qube is running')
  101. @qubes.events.handler('property-set:template')
  102. def on_property_set_template(self, event, name, newvalue, oldvalue=None):
  103. ''' Adjust root (and possibly other snap_on_start=True) volume
  104. on template change.
  105. ''' # pylint: disable=unused-argument
  106. for volume_name, conf in self.default_volume_config.items():
  107. if conf.get('snap_on_start', False) and \
  108. conf.get('source', None) is None:
  109. config = copy.deepcopy(conf)
  110. template_volume = newvalue.volumes[volume_name]
  111. self.volume_config[volume_name] = \
  112. self.config_volume_from_source(
  113. config,
  114. template_volume)
  115. self.storage.init_volume(volume_name, config)