appvm.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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, config in template.volume_config.items():
  74. # in case the template vm has more volumes add them to own
  75. # config
  76. if name not in self.volume_config:
  77. self.volume_config[name] = config.copy()
  78. if 'vid' in self.volume_config[name]:
  79. del self.volume_config[name]['vid']
  80. super(AppVM, self).__init__(app, xml, **kwargs)
  81. @qubes.events.handler('domain-load')
  82. def on_domain_loaded(self, event):
  83. ''' When domain is loaded assert that this vm has a template.
  84. ''' # pylint: disable=unused-argument
  85. assert self.template
  86. @qubes.events.handler('property-pre-set:template')
  87. def on_property_pre_set_template(self, event, name, newvalue,
  88. oldvalue=None):
  89. '''Forbid changing template of running VM
  90. ''' # pylint: disable=unused-argument
  91. if not self.is_halted():
  92. raise qubes.exc.QubesVMNotHaltedError(self,
  93. 'Cannot change template while qube is running')
  94. @qubes.events.handler('property-set:template')
  95. def on_property_set_template(self, event, name, newvalue, oldvalue=None):
  96. ''' Adjust root (and possibly other snap_on_start=True) volume
  97. on template change.
  98. ''' # pylint: disable=unused-argument
  99. for volume_name, conf in self.default_volume_config.items():
  100. if conf.get('snap_on_start', False) and \
  101. conf.get('source', None) is None:
  102. config = conf.copy()
  103. self.volume_config[volume_name] = config
  104. self.storage.init_volume(volume_name, config)