appvm.py 4.3 KB

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