adminvm.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 get_power_state():
  81. '''Always ``'Running'``.
  82. .. seealso:
  83. :py:meth:`qubes.vm.qubesvm.QubesVM.get_power_state`
  84. '''
  85. return 'Running'
  86. @staticmethod
  87. def get_mem():
  88. '''Get current memory usage of Dom0.
  89. Unit is KiB.
  90. .. seealso:
  91. :py:meth:`qubes.vm.qubesvm.QubesVM.get_mem`
  92. '''
  93. # return psutil.virtual_memory().total/1024
  94. with open('/proc/meminfo') as file:
  95. for line in file:
  96. if line.startswith('MemTotal:'):
  97. return int(line.split(':')[1].strip().split()[0])
  98. raise NotImplementedError()
  99. def get_mem_static_max(self):
  100. '''Get maximum memory available to Dom0.
  101. .. seealso:
  102. :py:meth:`qubes.vm.qubesvm.QubesVM.get_mem_static_max`
  103. '''
  104. if self.app.vmm.offline_mode:
  105. # default value passed on xen cmdline
  106. return 4096
  107. else:
  108. try:
  109. return self.app.vmm.libvirt_conn.getInfo()[1]
  110. except libvirt.libvirtError as e:
  111. self.log.warning('Failed to get memory limit for dom0: %s', e)
  112. return 4096
  113. def verify_files(self):
  114. '''Always :py:obj:`True`
  115. .. seealso:
  116. :py:meth:`qubes.vm.qubesvm.QubesVM.verify_files`
  117. ''' # pylint: disable=no-self-use
  118. return True
  119. def start(self, start_guid=True, notify_function=None,
  120. mem_required=None):
  121. '''Always raises an exception.
  122. .. seealso:
  123. :py:meth:`qubes.vm.qubesvm.QubesVM.start`
  124. ''' # pylint: disable=unused-argument,arguments-differ
  125. raise qubes.exc.QubesVMError(self, 'Cannot start Dom0 fake domain!')
  126. def suspend(self):
  127. '''Does nothing.
  128. .. seealso:
  129. :py:meth:`qubes.vm.qubesvm.QubesVM.suspend`
  130. '''
  131. raise qubes.exc.QubesVMError(self, 'Cannot suspend Dom0 fake domain!')
  132. @property
  133. def icon_path(self):
  134. return None
  135. @property
  136. def untrusted_qdb(self):
  137. '''QubesDB handle for this domain.'''
  138. if self._qdb_connection is None:
  139. import qubesdb # pylint: disable=import-error
  140. self._qdb_connection = qubesdb.QubesDB(self.name)
  141. return self._qdb_connection
  142. # def __init__(self, **kwargs):
  143. # super(QubesAdminVm, self).__init__(qid=0, name="dom0", netid=0,
  144. # dir_path=None,
  145. # private_img = None,
  146. # template = None,
  147. # maxmem = 0,
  148. # vcpus = 0,
  149. # label = defaults["template_label"],
  150. # **kwargs)