adminvm.py 5.9 KB

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