__init__.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # -*- encoding: utf8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. '''Qubes VM objects.'''
  21. import qubesmgmt.base
  22. import qubesmgmt.storage
  23. class QubesVM(qubesmgmt.base.PropertyHolder):
  24. '''Qubes domain.'''
  25. def __init__(self, app, name):
  26. super(QubesVM, self).__init__(app, 'mgmt.vm.property.', name)
  27. self._volumes = None
  28. @property
  29. def name(self):
  30. '''Domain name'''
  31. return self._method_dest
  32. @name.setter
  33. def name(self, new_value):
  34. self.qubesd_call(
  35. self._method_dest,
  36. self._method_prefix + 'Set',
  37. 'name',
  38. str(new_value).encode('utf-8'))
  39. self._method_dest = new_value
  40. self._volumes = None
  41. self.app.domains.clear_cache()
  42. def start(self):
  43. '''
  44. Start domain.
  45. :return:
  46. '''
  47. self.qubesd_call(self._method_dest, 'mgmt.vm.Start')
  48. def shutdown(self):
  49. '''
  50. Shutdown domain.
  51. :return:
  52. '''
  53. # TODO: force parameter
  54. # TODO: wait parameter (using event?)
  55. self.qubesd_call(self._method_dest, 'mgmt.vm.Shutdown')
  56. def kill(self):
  57. '''
  58. Kill domain (forcefuly shutdown).
  59. :return:
  60. '''
  61. self.qubesd_call(self._method_dest, 'mgmt.vm.Kill')
  62. def pause(self):
  63. '''
  64. Pause domain.
  65. Pause its execution without any prior notification.
  66. :return:
  67. '''
  68. self.qubesd_call(self._method_dest, 'mgmt.vm.Pause')
  69. def unpause(self):
  70. '''
  71. Unpause domain.
  72. Opposite to :py:meth:`pause`.
  73. :return:
  74. '''
  75. self.qubesd_call(self._method_dest, 'mgmt.vm.Unpause')
  76. def suspend(self):
  77. '''
  78. Suspend domain.
  79. Give domain a chance to prepare for suspend - for example suspend
  80. used PCI devices.
  81. :return:
  82. '''
  83. raise NotImplementedError
  84. #self.qubesd_call(self._method_dest, 'mgmt.vm.Suspend')
  85. def resume(self):
  86. '''
  87. Resume domain.
  88. Opposite to :py:meth:`suspend`.
  89. :return:
  90. '''
  91. raise NotImplementedError
  92. #self.qubesd_call(self._method_dest, 'mgmt.vm.Resume')
  93. @property
  94. def volumes(self):
  95. '''VM disk volumes'''
  96. if self._volumes is None:
  97. volumes_list = self.qubesd_call(
  98. self._method_dest, 'mgmt.vm.volume.List')
  99. self._volumes = {}
  100. for volname in volumes_list.decode('ascii').splitlines():
  101. if not volname:
  102. continue
  103. self._volumes[volname] = qubesmgmt.storage.Volume(self.app,
  104. vm=self.name, vm_name=volname)
  105. return self._volumes
  106. # pylint: disable=abstract-method
  107. class AdminVM(QubesVM):
  108. '''Dom0'''
  109. pass
  110. class AppVM(QubesVM):
  111. '''Application VM'''
  112. pass
  113. class StandaloneVM(QubesVM):
  114. '''Standalone Application VM'''
  115. pass
  116. class TemplateVM(QubesVM):
  117. '''Template for AppVM'''
  118. pass
  119. class DispVM(QubesVM):
  120. '''Disposable VM'''
  121. pass