dispvm.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. #
  23. ''' A disposable vm implementation '''
  24. import copy
  25. import qubes.vm.qubesvm
  26. import qubes.vm.appvm
  27. import qubes.config
  28. class DispVM(qubes.vm.qubesvm.QubesVM):
  29. '''Disposable VM'''
  30. template = qubes.VMProperty('template',
  31. load_stage=4,
  32. vmclass=qubes.vm.appvm.AppVM,
  33. ls_width=31,
  34. doc='AppVM, on which this DispVM is based.')
  35. dispid = qubes.property('dispid', type=int, write_once=True,
  36. clone=False,
  37. ls_width=3,
  38. doc='''Internal, persistent identifier of particular DispVM.''')
  39. def __init__(self, *args, **kwargs):
  40. self.volume_config = {
  41. 'root': {
  42. 'name': 'root',
  43. 'pool': 'default',
  44. 'snap_on_start': True,
  45. 'save_on_stop': False,
  46. 'rw': False,
  47. 'internal': True
  48. },
  49. 'private': {
  50. 'name': 'private',
  51. 'pool': 'default',
  52. 'snap_on_start': True,
  53. 'save_on_stop': False,
  54. 'internal': True,
  55. 'rw': True,
  56. },
  57. 'volatile': {
  58. 'name': 'volatile',
  59. 'pool': 'default',
  60. 'internal': True,
  61. 'rw': True,
  62. 'size': qubes.config.defaults['root_img_size'] +
  63. qubes.config.defaults['private_img_size'],
  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 'name' not in kwargs and 'dispid' in kwargs:
  74. kwargs['name'] = 'disp' + str(kwargs['dispid'])
  75. template = kwargs.get('template', None)
  76. if template is not None:
  77. # template is only passed if the AppVM is created, in other cases we
  78. # don't need to patch the volume_config because the config is
  79. # coming from XML, already as we need it
  80. for name, conf in self.volume_config.items():
  81. tpl_volume = template.volumes[name]
  82. conf['size'] = tpl_volume.size
  83. conf['pool'] = tpl_volume.pool
  84. has_source = ('source' in conf and conf['source'] is not None)
  85. is_snapshot = 'snap_on_start' in conf and conf['snap_on_start']
  86. if is_snapshot and not has_source:
  87. if tpl_volume.source is not None:
  88. conf['source'] = tpl_volume.source
  89. else:
  90. conf['source'] = tpl_volume.vid
  91. for name, config in template.volume_config.items():
  92. # in case the template vm has more volumes add them to own
  93. # config
  94. if name not in self.volume_config:
  95. self.volume_config[name] = copy.deepcopy(config)
  96. if 'vid' in self.volume_config[name]:
  97. del self.volume_config[name]['vid']
  98. # by default inherit label from the DispVM template
  99. if 'label' not in kwargs:
  100. kwargs['label'] = template.label
  101. super(DispVM, self).__init__(*args, **kwargs)
  102. @qubes.events.handler('domain-load')
  103. def on_domain_loaded(self, event):
  104. ''' When domain is loaded assert that this vm has a template.
  105. ''' # pylint: disable=unused-argument
  106. assert self.template
  107. @classmethod
  108. def from_appvm(cls, appvm, **kwargs):
  109. '''Create a new instance from given AppVM
  110. :param qubes.vm.appvm.AppVM appvm: template from which the VM should \
  111. be created (could also be name or qid)
  112. :returns: new disposable vm
  113. *kwargs* are passed to the newly created VM
  114. >>> import qubes.vm.dispvm.DispVM
  115. >>> dispvm = qubes.vm.dispvm.DispVM.from_appvm(appvm).start()
  116. >>> dispvm.run_service('qubes.VMShell', input='firefox')
  117. >>> dispvm.cleanup()
  118. This method modifies :file:`qubes.xml` file. In fact, the newly created
  119. vm belongs to other :py:class:`qubes.Qubes` instance than the *app*.
  120. The qube returned is not started.
  121. '''
  122. store = appvm.app.store if isinstance(appvm, qubes.vm.BaseVM) else None
  123. app = qubes.Qubes(store)
  124. dispvm = app.add_new_vm(
  125. cls,
  126. dispid=app.domains.get_new_unused_dispid(),
  127. template=app.domains[appvm],
  128. **kwargs)
  129. # exclude template
  130. proplist = [prop for prop in dispvm.property_list()
  131. if prop.clone and prop.__name__ not in ['template']]
  132. dispvm.clone_properties(app.domains[appvm], proplist=proplist)
  133. dispvm.create_on_disk()
  134. app.save()
  135. return dispvm
  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. app = qubes.Qubes(self.app.store)
  142. self = app.domains[self.uuid]
  143. self.force_shutdown()
  144. self.remove_from_disk()
  145. del app.domains[self]
  146. app.save()