dispvm.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 copy
  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. ls_width=31,
  32. doc='AppVM, on which this DispVM is based.')
  33. dispid = qubes.property('dispid', type=int, write_once=True,
  34. clone=False,
  35. ls_width=3,
  36. doc='''Internal, persistent identifier of particular DispVM.''')
  37. def __init__(self, *args, **kwargs):
  38. self.volume_config = {
  39. 'root': {
  40. 'name': 'root',
  41. 'pool': 'default',
  42. 'snap_on_start': True,
  43. 'save_on_stop': False,
  44. 'rw': False,
  45. 'internal': True
  46. },
  47. 'private': {
  48. 'name': 'private',
  49. 'pool': 'default',
  50. 'snap_on_start': True,
  51. 'save_on_stop': False,
  52. 'internal': True,
  53. 'rw': True,
  54. },
  55. 'volatile': {
  56. 'name': 'volatile',
  57. 'pool': 'default',
  58. 'internal': True,
  59. 'rw': True,
  60. 'size': qubes.config.defaults['root_img_size'] +
  61. qubes.config.defaults['private_img_size'],
  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 'name' not in kwargs and 'dispid' in kwargs:
  72. kwargs['name'] = 'disp' + str(kwargs['dispid'])
  73. template = kwargs.get('template', None)
  74. if template is not None:
  75. # template is only passed if the AppVM is created, in other cases we
  76. # don't need to patch the volume_config because the config is
  77. # coming from XML, already as we need it
  78. for name, conf in self.volume_config.items():
  79. tpl_volume = template.volumes[name]
  80. conf['size'] = tpl_volume.size
  81. conf['pool'] = tpl_volume.pool
  82. has_source = ('source' in conf and conf['source'] is not None)
  83. is_snapshot = 'snap_on_start' in conf and conf['snap_on_start']
  84. if is_snapshot and not has_source:
  85. if tpl_volume.source is not None:
  86. conf['source'] = tpl_volume.source
  87. else:
  88. conf['source'] = tpl_volume.vid
  89. for name, config in template.volume_config.items():
  90. # in case the template vm has more volumes add them to own
  91. # config
  92. if name not in self.volume_config:
  93. self.volume_config[name] = copy.deepcopy(config)
  94. if 'vid' in self.volume_config[name]:
  95. del self.volume_config[name]['vid']
  96. # by default inherit label from the DispVM template
  97. if 'label' not in kwargs:
  98. kwargs['label'] = template.label
  99. super(DispVM, self).__init__(*args, **kwargs)
  100. @qubes.events.handler('domain-load')
  101. def on_domain_loaded(self, event):
  102. ''' When domain is loaded assert that this vm has a template.
  103. ''' # pylint: disable=unused-argument
  104. assert self.template
  105. @classmethod
  106. def from_appvm(cls, appvm, **kwargs):
  107. '''Create a new instance from given AppVM
  108. :param qubes.vm.appvm.AppVM appvm: template from which the VM should \
  109. be created (could also be name or qid)
  110. :returns: new disposable vm
  111. *kwargs* are passed to the newly created VM
  112. >>> import qubes.vm.dispvm.DispVM
  113. >>> dispvm = qubes.vm.dispvm.DispVM.from_appvm(appvm).start()
  114. >>> dispvm.run_service('qubes.VMShell', input='firefox')
  115. >>> dispvm.cleanup()
  116. This method modifies :file:`qubes.xml` file. In fact, the newly created
  117. vm belongs to other :py:class:`qubes.Qubes` instance than the *app*.
  118. The qube returned is not started.
  119. '''
  120. store = appvm.app.store if isinstance(appvm, qubes.vm.BaseVM) else None
  121. app = qubes.Qubes(store)
  122. dispvm = app.add_new_vm(
  123. cls,
  124. dispid=app.domains.get_new_unused_dispid(),
  125. template=app.domains[appvm],
  126. **kwargs)
  127. # exclude template
  128. proplist = [prop for prop in dispvm.property_list()
  129. if prop.clone and prop.__name__ not in ['template']]
  130. dispvm.clone_properties(app.domains[appvm], proplist=proplist)
  131. dispvm.create_on_disk()
  132. app.save()
  133. return dispvm
  134. def cleanup(self):
  135. '''Clean up after the DispVM
  136. This stops the disposable qube and removes it from the store.
  137. This method modifies :file:`qubes.xml` file.
  138. '''
  139. app = qubes.Qubes(self.app.store)
  140. self = app.domains[self.uuid]
  141. try:
  142. self.force_shutdown()
  143. except qubes.exc.QubesVMNotStartedError:
  144. pass
  145. self.remove_from_disk()
  146. del app.domains[self]
  147. app.save()