appvm.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. )
  38. def __init__(self, app, xml, template=None, **kwargs):
  39. self.volume_config = {
  40. 'root': {
  41. 'name': 'root',
  42. 'pool': 'default',
  43. 'snap_on_start': True,
  44. 'save_on_stop': False,
  45. 'rw': False,
  46. 'internal': True
  47. },
  48. 'private': {
  49. 'name': 'private',
  50. 'pool': 'default',
  51. 'snap_on_start': False,
  52. 'save_on_stop': True,
  53. 'rw': True,
  54. 'source': None,
  55. 'size': defaults['private_img_size'],
  56. 'internal': True
  57. },
  58. 'volatile': {
  59. 'name': 'volatile',
  60. 'pool': 'default',
  61. 'size': defaults['root_img_size'],
  62. 'internal': True,
  63. 'rw': True,
  64. },
  65. 'kernel': {
  66. 'name': 'kernel',
  67. 'pool': 'linux-kernel',
  68. 'snap_on_start': True,
  69. 'rw': False,
  70. 'internal': True
  71. }
  72. }
  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. conf['size'] = tpl_volume.size
  80. conf['pool'] = tpl_volume.pool
  81. has_source = ('source' in conf and conf['source'] is not None)
  82. is_snapshot = 'snap_on_start' in conf and conf['snap_on_start']
  83. if is_snapshot and not has_source:
  84. if tpl_volume.source is not None:
  85. conf['source'] = tpl_volume.source
  86. else:
  87. conf['source'] = tpl_volume.vid
  88. for name, config in template.volume_config.items():
  89. # in case the template vm has more volumes add them to own
  90. # config
  91. if name not in self.volume_config:
  92. self.volume_config[name] = copy.deepcopy(config)
  93. if 'vid' in self.volume_config[name]:
  94. del self.volume_config[name]['vid']
  95. super(AppVM, self).__init__(app, xml, **kwargs)
  96. if not hasattr(self, 'template') and template is not None:
  97. self.template = template
  98. if 'source' not in self.volume_config['root']:
  99. msg = 'missing source for root volume'
  100. raise qubes.exc.QubesException(msg)
  101. @qubes.events.handler('domain-load')
  102. def on_domain_loaded(self, event):
  103. ''' When domain is loaded assert that this vm has a template.
  104. ''' # pylint: disable=unused-argument
  105. assert self.template