dispvm.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. ''' A disposable vm implementation '''
  22. import asyncio
  23. import qubes.vm.qubesvm
  24. import qubes.vm.appvm
  25. import qubes.config
  26. class DispVM(qubes.vm.qubesvm.QubesVM):
  27. '''Disposable VM'''
  28. template = qubes.VMProperty('template',
  29. load_stage=4,
  30. vmclass=qubes.vm.appvm.AppVM,
  31. doc='AppVM, on which this DispVM is based.')
  32. dispid = qubes.property('dispid', type=int, write_once=True,
  33. clone=False,
  34. doc='''Internal, persistent identifier of particular DispVM.''')
  35. def __init__(self, *args, **kwargs):
  36. self.volume_config = {
  37. 'root': {
  38. 'name': 'root',
  39. 'pool': 'default',
  40. 'snap_on_start': True,
  41. 'save_on_stop': False,
  42. 'rw': False,
  43. 'source': None,
  44. },
  45. 'private': {
  46. 'name': 'private',
  47. 'pool': 'default',
  48. 'snap_on_start': True,
  49. 'save_on_stop': False,
  50. 'rw': True,
  51. 'source': None,
  52. },
  53. 'volatile': {
  54. 'name': 'volatile',
  55. 'pool': 'default',
  56. 'snap_on_start': False,
  57. 'save_on_stop': False,
  58. 'rw': True,
  59. 'size': qubes.config.defaults['root_img_size'] +
  60. qubes.config.defaults['private_img_size'],
  61. },
  62. 'kernel': {
  63. 'name': 'kernel',
  64. 'pool': 'linux-kernel',
  65. 'snap_on_start': False,
  66. 'save_on_stop': False,
  67. 'rw': False,
  68. }
  69. }
  70. if 'name' not in kwargs and 'dispid' in kwargs:
  71. kwargs['name'] = 'disp' + str(kwargs['dispid'])
  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] = config.copy()
  85. if 'vid' in self.volume_config[name]:
  86. del self.volume_config[name]['vid']
  87. # by default inherit label from the DispVM template
  88. if 'label' not in kwargs:
  89. kwargs['label'] = template.label
  90. super(DispVM, self).__init__(*args, **kwargs)
  91. @qubes.events.handler('domain-load')
  92. def on_domain_loaded(self, event):
  93. ''' When domain is loaded assert that this vm has a template.
  94. ''' # pylint: disable=unused-argument
  95. assert self.template
  96. @qubes.events.handler('property-pre-set:template')
  97. def on_property_pre_set_template(self, event, name, newvalue,
  98. oldvalue=None):
  99. ''' Disposable VM cannot have template changed '''
  100. # pylint: disable=unused-argument
  101. raise qubes.exc.QubesValueError(self,
  102. 'Cannot change template of Disposable VM')
  103. @classmethod
  104. @asyncio.coroutine
  105. def from_appvm(cls, appvm, **kwargs):
  106. '''Create a new instance from given AppVM
  107. :param qubes.vm.appvm.AppVM appvm: template from which the VM should \
  108. be created
  109. :returns: new disposable vm
  110. *kwargs* are passed to the newly created VM
  111. >>> import qubes.vm.dispvm.DispVM
  112. >>> dispvm = qubes.vm.dispvm.DispVM.from_appvm(appvm).start()
  113. >>> dispvm.run_service('qubes.VMShell', input='firefox')
  114. >>> dispvm.cleanup()
  115. This method modifies :file:`qubes.xml` file.
  116. The qube returned is not started.
  117. '''
  118. if not appvm.dispvm_allowed:
  119. raise qubes.exc.QubesException(
  120. 'Refusing to start DispVM out of this AppVM, because '
  121. 'dispvm_allowed=False')
  122. app = appvm.app
  123. dispvm = app.add_new_vm(
  124. cls,
  125. dispid=app.domains.get_new_unused_dispid(),
  126. template=app.domains[appvm],
  127. **kwargs)
  128. # exclude template
  129. proplist = [prop for prop in dispvm.property_list()
  130. if prop.clone and prop.__name__ not in ['template']]
  131. dispvm.clone_properties(app.domains[appvm], proplist=proplist)
  132. yield from dispvm.create_on_disk()
  133. app.save()
  134. return dispvm
  135. @asyncio.coroutine
  136. def cleanup(self):
  137. '''Clean up after the DispVM
  138. This stops the disposable qube and removes it from the store.
  139. This method modifies :file:`qubes.xml` file.
  140. '''
  141. try:
  142. # pylint: disable=not-an-iterable
  143. yield from self.kill()
  144. except qubes.exc.QubesVMNotStartedError:
  145. pass
  146. yield from self.remove_from_disk()
  147. del self.app.domains[self]
  148. self.app.save()