adminvm.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #
  2. # The Qubes OS Project, https://www.qubes-os.org/
  3. #
  4. # Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  5. # Copyright (C) 2013-2015 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. # Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. #
  9. # This library is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU Lesser General Public
  11. # License as published by the Free Software Foundation; either
  12. # version 2.1 of the License, or (at your option) any later version.
  13. #
  14. # This library 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 GNU
  17. # Lesser General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU Lesser General Public
  20. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  21. #
  22. ''' This module contains the AdminVM implementation '''
  23. import libvirt
  24. import qubes
  25. import qubes.exc
  26. import qubes.vm
  27. class AdminVM(qubes.vm.BaseVM):
  28. '''Dom0'''
  29. dir_path = None
  30. name = qubes.property('name',
  31. default='dom0', setter=qubes.property.forbidden)
  32. qid = qubes.property('qid',
  33. default=0, type=int, setter=qubes.property.forbidden)
  34. uuid = qubes.property('uuid',
  35. default='00000000-0000-0000-0000-000000000000',
  36. setter=qubes.property.forbidden)
  37. default_dispvm = qubes.VMProperty('default_dispvm',
  38. load_stage=4,
  39. allow_none=True,
  40. default=(lambda self: self.app.default_dispvm),
  41. doc='Default VM to be used as Disposable VM for service calls.')
  42. include_in_backups = qubes.property('include_in_backups',
  43. default=True, type=bool,
  44. doc='If this domain is to be included in default backup.')
  45. updateable = qubes.property('updateable',
  46. default=True,
  47. type=bool,
  48. setter=qubes.property.forbidden,
  49. doc='True if this machine may be updated on its own.')
  50. def __init__(self, *args, **kwargs):
  51. super().__init__(*args, **kwargs)
  52. self._qdb_connection = None
  53. self._libvirt_domain = None
  54. if not self.app.vmm.offline_mode:
  55. self.start_qdb_watch()
  56. def __str__(self):
  57. return self.name
  58. def __lt__(self, other):
  59. # order dom0 before anything
  60. return self.name != other.name
  61. @property
  62. def attached_volumes(self):
  63. return []
  64. @property
  65. def xid(self):
  66. '''Always ``0``.
  67. .. seealso:
  68. :py:attr:`qubes.vm.qubesvm.QubesVM.xid`
  69. '''
  70. return 0
  71. @property
  72. def libvirt_domain(self):
  73. '''Libvirt object for dom0.
  74. .. seealso:
  75. :py:attr:`qubes.vm.qubesvm.QubesVM.libvirt_domain`
  76. '''
  77. if self._libvirt_domain is None:
  78. self._libvirt_domain = self.app.vmm.libvirt_conn.lookupByID(0)
  79. return self._libvirt_domain
  80. @staticmethod
  81. def is_running():
  82. '''Always :py:obj:`True`.
  83. .. seealso:
  84. :py:meth:`qubes.vm.qubesvm.QubesVM.is_running`
  85. '''
  86. return True
  87. @staticmethod
  88. def is_halted():
  89. '''Always :py:obj:`False`.
  90. .. seealso:
  91. :py:meth:`qubes.vm.qubesvm.QubesVM.is_halted`
  92. '''
  93. return False
  94. @staticmethod
  95. def get_power_state():
  96. '''Always ``'Running'``.
  97. .. seealso:
  98. :py:meth:`qubes.vm.qubesvm.QubesVM.get_power_state`
  99. '''
  100. return 'Running'
  101. @staticmethod
  102. def get_mem():
  103. '''Get current memory usage of Dom0.
  104. Unit is KiB.
  105. .. seealso:
  106. :py:meth:`qubes.vm.qubesvm.QubesVM.get_mem`
  107. '''
  108. # return psutil.virtual_memory().total/1024
  109. with open('/proc/meminfo') as file:
  110. for line in file:
  111. if line.startswith('MemTotal:'):
  112. return int(line.split(':')[1].strip().split()[0])
  113. raise NotImplementedError()
  114. def get_mem_static_max(self):
  115. '''Get maximum memory available to Dom0.
  116. .. seealso:
  117. :py:meth:`qubes.vm.qubesvm.QubesVM.get_mem_static_max`
  118. '''
  119. if self.app.vmm.offline_mode:
  120. # default value passed on xen cmdline
  121. return 4096
  122. try:
  123. return self.app.vmm.libvirt_conn.getInfo()[1]
  124. except libvirt.libvirtError as e:
  125. self.log.warning('Failed to get memory limit for dom0: %s', e)
  126. return 4096
  127. def verify_files(self):
  128. '''Always :py:obj:`True`
  129. .. seealso:
  130. :py:meth:`qubes.vm.qubesvm.QubesVM.verify_files`
  131. ''' # pylint: disable=no-self-use
  132. return True
  133. def start(self, start_guid=True, notify_function=None,
  134. mem_required=None):
  135. '''Always raises an exception.
  136. .. seealso:
  137. :py:meth:`qubes.vm.qubesvm.QubesVM.start`
  138. ''' # pylint: disable=unused-argument,arguments-differ
  139. raise qubes.exc.QubesVMError(self, 'Cannot start Dom0 fake domain!')
  140. def suspend(self):
  141. '''Does nothing.
  142. .. seealso:
  143. :py:meth:`qubes.vm.qubesvm.QubesVM.suspend`
  144. '''
  145. raise qubes.exc.QubesVMError(self, 'Cannot suspend Dom0 fake domain!')
  146. def shutdown(self):
  147. '''Does nothing.
  148. .. seealso:
  149. :py:meth:`qubes.vm.qubesvm.QubesVM.shutdown`
  150. '''
  151. raise qubes.exc.QubesVMError(self, 'Cannot shutdown Dom0 fake domain!')
  152. def kill(self):
  153. '''Does nothing.
  154. .. seealso:
  155. :py:meth:`qubes.vm.qubesvm.QubesVM.kill`
  156. '''
  157. raise qubes.exc.QubesVMError(self, 'Cannot kill Dom0 fake domain!')
  158. @property
  159. def icon_path(self):
  160. pass
  161. @property
  162. def untrusted_qdb(self):
  163. '''QubesDB handle for this domain.'''
  164. if self._qdb_connection is None:
  165. import qubesdb # pylint: disable=import-error
  166. self._qdb_connection = qubesdb.QubesDB(self.name)
  167. return self._qdb_connection
  168. # def __init__(self, **kwargs):
  169. # super(QubesAdminVm, self).__init__(qid=0, name="dom0",
  170. # dir_path=None,
  171. # private_img = None,
  172. # template = None,
  173. # maxmem = 0,
  174. # vcpus = 0,
  175. # label = defaults["template_label"],
  176. # **kwargs)