adminvm.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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, 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. def __init__(self, *args, **kwargs):
  43. super().__init__(*args, **kwargs)
  44. self._qdb_connection = None
  45. self._libvirt_domain = None
  46. if not self.app.vmm.offline_mode:
  47. self.start_qdb_watch()
  48. def __str__(self):
  49. return self.name
  50. def __lt__(self, other):
  51. # order dom0 before anything
  52. return self.name != other.name
  53. @property
  54. def attached_volumes(self):
  55. return []
  56. @property
  57. def xid(self):
  58. '''Always ``0``.
  59. .. seealso:
  60. :py:attr:`qubes.vm.qubesvm.QubesVM.xid`
  61. '''
  62. return 0
  63. @property
  64. def libvirt_domain(self):
  65. '''Libvirt object for dom0.
  66. .. seealso:
  67. :py:attr:`qubes.vm.qubesvm.QubesVM.libvirt_domain`
  68. '''
  69. if self._libvirt_domain is None:
  70. self._libvirt_domain = self.app.vmm.libvirt_conn.lookupByID(0)
  71. return self._libvirt_domain
  72. @staticmethod
  73. def is_running():
  74. '''Always :py:obj:`True`.
  75. .. seealso:
  76. :py:meth:`qubes.vm.qubesvm.QubesVM.is_running`
  77. '''
  78. return True
  79. @staticmethod
  80. def is_halted():
  81. '''Always :py:obj:`False`.
  82. .. seealso:
  83. :py:meth:`qubes.vm.qubesvm.QubesVM.is_halted`
  84. '''
  85. return False
  86. @staticmethod
  87. def get_power_state():
  88. '''Always ``'Running'``.
  89. .. seealso:
  90. :py:meth:`qubes.vm.qubesvm.QubesVM.get_power_state`
  91. '''
  92. return 'Running'
  93. @staticmethod
  94. def get_mem():
  95. '''Get current memory usage of Dom0.
  96. Unit is KiB.
  97. .. seealso:
  98. :py:meth:`qubes.vm.qubesvm.QubesVM.get_mem`
  99. '''
  100. # return psutil.virtual_memory().total/1024
  101. with open('/proc/meminfo') as file:
  102. for line in file:
  103. if line.startswith('MemTotal:'):
  104. return int(line.split(':')[1].strip().split()[0])
  105. raise NotImplementedError()
  106. def get_mem_static_max(self):
  107. '''Get maximum memory available to Dom0.
  108. .. seealso:
  109. :py:meth:`qubes.vm.qubesvm.QubesVM.get_mem_static_max`
  110. '''
  111. if self.app.vmm.offline_mode:
  112. # default value passed on xen cmdline
  113. return 4096
  114. else:
  115. try:
  116. return self.app.vmm.libvirt_conn.getInfo()[1]
  117. except libvirt.libvirtError as e:
  118. self.log.warning('Failed to get memory limit for dom0: %s', e)
  119. return 4096
  120. def verify_files(self):
  121. '''Always :py:obj:`True`
  122. .. seealso:
  123. :py:meth:`qubes.vm.qubesvm.QubesVM.verify_files`
  124. ''' # pylint: disable=no-self-use
  125. return True
  126. def start(self, start_guid=True, notify_function=None,
  127. mem_required=None):
  128. '''Always raises an exception.
  129. .. seealso:
  130. :py:meth:`qubes.vm.qubesvm.QubesVM.start`
  131. ''' # pylint: disable=unused-argument,arguments-differ
  132. raise qubes.exc.QubesVMError(self, 'Cannot start Dom0 fake domain!')
  133. def suspend(self):
  134. '''Does nothing.
  135. .. seealso:
  136. :py:meth:`qubes.vm.qubesvm.QubesVM.suspend`
  137. '''
  138. raise qubes.exc.QubesVMError(self, 'Cannot suspend Dom0 fake domain!')
  139. @property
  140. def icon_path(self):
  141. return None
  142. @property
  143. def untrusted_qdb(self):
  144. '''QubesDB handle for this domain.'''
  145. if self._qdb_connection is None:
  146. import qubesdb # pylint: disable=import-error
  147. self._qdb_connection = qubesdb.QubesDB(self.name)
  148. return self._qdb_connection
  149. # def __init__(self, **kwargs):
  150. # super(QubesAdminVm, self).__init__(qid=0, name="dom0",
  151. # dir_path=None,
  152. # private_img = None,
  153. # template = None,
  154. # maxmem = 0,
  155. # vcpus = 0,
  156. # label = defaults["template_label"],
  157. # **kwargs)