dispvm.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 qubes.vm.qubesvm
  25. import qubes.vm.appvm
  26. import qubes.config
  27. class DispVM(qubes.vm.qubesvm.QubesVM):
  28. '''Disposable VM'''
  29. template = qubes.VMProperty('template',
  30. load_stage=4,
  31. vmclass=qubes.vm.appvm.AppVM,
  32. ls_width=31,
  33. doc='AppVM, on which this DispVM is based.')
  34. dispid = qubes.property('dispid', type=int, write_once=True,
  35. clone=False,
  36. ls_width=3,
  37. doc='''Internal, persistent identifier of particular DispVM.''')
  38. def __init__(self, *args, **kwargs):
  39. self.volume_config = {
  40. 'root': {
  41. 'name': 'root',
  42. 'pool': 'default',
  43. 'volume_type': 'snapshot',
  44. },
  45. 'private': {
  46. 'name': 'private',
  47. 'pool': 'default',
  48. 'volume_type': 'snapshot',
  49. },
  50. 'volatile': {
  51. 'name': 'volatile',
  52. 'pool': 'default',
  53. 'volume_type': 'volatile',
  54. 'size': qubes.config.defaults['root_img_size'] +
  55. qubes.config.defaults['private_img_size'],
  56. },
  57. 'kernel': {
  58. 'name': 'kernel',
  59. 'pool': 'linux-kernel',
  60. 'volume_type': 'read-only',
  61. }
  62. }
  63. super(DispVM, self).__init__(*args, **kwargs)
  64. @qubes.events.handler('domain-load')
  65. def on_domain_loaded(self, event):
  66. ''' When domain is loaded assert that this vm has a template.
  67. ''' # pylint: disable=unused-argument
  68. assert self.template
  69. @classmethod
  70. def from_appvm(cls, appvm, **kwargs):
  71. '''Create a new instance from given AppVM
  72. :param qubes.vm.appvm.AppVM appvm: template from which the VM should \
  73. be created (could also be name or qid)
  74. :returns: new disposable vm
  75. *kwargs* are passed to the newly created VM
  76. >>> import qubes.vm.dispvm.DispVM
  77. >>> dispvm = qubes.vm.dispvm.DispVM.from_appvm(appvm).start()
  78. >>> dispvm.run_service('qubes.VMShell', input='firefox')
  79. >>> dispvm.cleanup()
  80. This method modifies :file:`qubes.xml` file. In fact, the newly created
  81. vm belongs to other :py:class:`qubes.Qubes` instance than the *app*.
  82. The qube returned is not started.
  83. '''
  84. store = appvm.app.store if isinstance(appvm, qubes.vm.BaseVM) else None
  85. app = qubes.Qubes(store)
  86. dispvm = app.add_new_vm(
  87. cls,
  88. dispid=app.domains.get_new_unused_dispid(),
  89. template=app.domains[appvm],
  90. **kwargs)
  91. dispvm.create_on_disk()
  92. app.save()
  93. return dispvm
  94. def cleanup(self):
  95. '''Clean up after the DispVM
  96. This stops the disposable qube and removes it from the store.
  97. This method modifies :file:`qubes.xml` file.
  98. '''
  99. app = qubes.Qubes(self.app.store)
  100. self = app.domains[self.uuid]
  101. self.force_shutdown()
  102. self.remove_from_disk()
  103. del app.domains[self]
  104. app.save()