appvm.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2014-2016 Wojtek Porczyk <woju@invisiblethingslab.com>
  7. # Copyright (C) 2016 Marek Marczykowski <marmarek@invisiblethingslab.com>)
  8. # Copyright (C) 2016 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License along
  21. # with this program; if not, write to the Free Software Foundation, Inc.,
  22. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. #
  24. ''' This module contains the AppVM implementation '''
  25. import copy
  26. import qubes.events
  27. import qubes.vm.qubesvm
  28. from qubes.config import defaults
  29. class AppVM(qubes.vm.qubesvm.QubesVM):
  30. '''Application VM'''
  31. template = qubes.VMProperty('template',
  32. load_stage=4,
  33. vmclass=qubes.vm.templatevm.TemplateVM,
  34. ls_width=31,
  35. doc='Template, on which this AppVM is based.')
  36. def __init__(self, app, xml, template=None, **kwargs):
  37. self.volume_config = {
  38. 'root': {
  39. 'name': 'root',
  40. 'pool': 'default',
  41. 'snap_on_start': True,
  42. 'save_on_stop': False,
  43. 'rw': False,
  44. 'internal': True
  45. },
  46. 'private': {
  47. 'name': 'private',
  48. 'pool': 'default',
  49. 'snap_on_start': False,
  50. 'save_on_stop': True,
  51. 'rw': True,
  52. 'source': None,
  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. 'snap_on_start': True,
  67. 'rw': False,
  68. 'internal': True
  69. }
  70. }
  71. if template is not None:
  72. # template is only passed if the AppVM is created, in other cases we
  73. # don't need to patch the volume_config because the config is
  74. # coming from XML, already as we need it
  75. for name, conf in self.volume_config.items():
  76. tpl_volume = template.volumes[name]
  77. conf['size'] = tpl_volume.size
  78. conf['pool'] = tpl_volume.pool
  79. has_source = ('source' in conf and conf['source'] is not None)
  80. is_snapshot = 'snap_on_start' in conf and conf['snap_on_start']
  81. if is_snapshot and not has_source:
  82. if tpl_volume.source is not None:
  83. conf['source'] = tpl_volume.source
  84. else:
  85. conf['source'] = tpl_volume.vid
  86. for name, config in template.volume_config.items():
  87. # in case the template vm has more volumes add them to own
  88. # config
  89. if name not in self.volume_config:
  90. self.volume_config[name] = copy.deepcopy(config)
  91. if 'vid' in self.volume_config[name]:
  92. del self.volume_config[name]['vid']
  93. super(AppVM, self).__init__(app, xml, **kwargs)
  94. if not hasattr(self, 'template') and template is not None:
  95. self.template = template
  96. if 'source' not in self.volume_config['root']:
  97. msg = 'missing source for root volume'
  98. raise qubes.exc.QubesException(msg)
  99. @qubes.events.handler('domain-load')
  100. def on_domain_loaded(self, event):
  101. ''' When domain is loaded assert that this vm has a template.
  102. ''' # pylint: disable=unused-argument
  103. assert self.template