adminvm.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program 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
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. #
  23. ''' This module contains the AdminVM implementation '''
  24. import libvirt
  25. import qubes
  26. import qubes.exc
  27. import qubes.vm
  28. class AdminVM(qubes.vm.BaseVM):
  29. '''Dom0'''
  30. dir_path = None
  31. name = qubes.property('name',
  32. default='dom0', setter=qubes.property.forbidden)
  33. qid = qubes.property('qid',
  34. default=0, setter=qubes.property.forbidden)
  35. uuid = qubes.property('uuid',
  36. default='00000000-0000-0000-0000-000000000000',
  37. setter=qubes.property.forbidden)
  38. default_dispvm = qubes.VMProperty('default_dispvm',
  39. load_stage=4,
  40. allow_none=True,
  41. default=(lambda self: self.app.default_dispvm),
  42. doc='Default VM to be used as Disposable VM for service calls.')
  43. def __init__(self, *args, **kwargs):
  44. super().__init__(*args, **kwargs)
  45. self._qdb_connection = None
  46. self._libvirt_domain = None
  47. if not self.app.vmm.offline_mode:
  48. self.start_qdb_watch()
  49. def __str__(self):
  50. return self.name
  51. def __lt__(self, other):
  52. # order dom0 before anything
  53. return self.name != other.name
  54. @property
  55. def attached_volumes(self):
  56. return []
  57. @property
  58. def xid(self):
  59. '''Always ``0``.
  60. .. seealso:
  61. :py:attr:`qubes.vm.qubesvm.QubesVM.xid`
  62. '''
  63. return 0
  64. @property
  65. def libvirt_domain(self):
  66. '''Libvirt object for dom0.
  67. .. seealso:
  68. :py:attr:`qubes.vm.qubesvm.QubesVM.libvirt_domain`
  69. '''
  70. if self._libvirt_domain is None:
  71. self._libvirt_domain = self.app.vmm.libvirt_conn.lookupByID(0)
  72. return self._libvirt_domain
  73. @staticmethod
  74. def is_running():
  75. '''Always :py:obj:`True`.
  76. .. seealso:
  77. :py:meth:`qubes.vm.qubesvm.QubesVM.is_running`
  78. '''
  79. return True
  80. @staticmethod
  81. def get_power_state():
  82. '''Always ``'Running'``.
  83. .. seealso:
  84. :py:meth:`qubes.vm.qubesvm.QubesVM.get_power_state`
  85. '''
  86. return 'Running'
  87. @staticmethod
  88. def get_mem():
  89. '''Get current memory usage of Dom0.
  90. Unit is KiB.
  91. .. seealso:
  92. :py:meth:`qubes.vm.qubesvm.QubesVM.get_mem`
  93. '''
  94. # return psutil.virtual_memory().total/1024
  95. with open('/proc/meminfo') as file:
  96. for line in file:
  97. if line.startswith('MemTotal:'):
  98. return int(line.split(':')[1].strip().split()[0])
  99. raise NotImplementedError()
  100. def get_mem_static_max(self):
  101. '''Get maximum memory available to Dom0.
  102. .. seealso:
  103. :py:meth:`qubes.vm.qubesvm.QubesVM.get_mem_static_max`
  104. '''
  105. if self.app.vmm.offline_mode:
  106. # default value passed on xen cmdline
  107. return 4096
  108. else:
  109. try:
  110. return self.app.vmm.libvirt_conn.getInfo()[1]
  111. except libvirt.libvirtError as e:
  112. self.log.warning('Failed to get memory limit for dom0: %s', e)
  113. return 4096
  114. def verify_files(self):
  115. '''Always :py:obj:`True`
  116. .. seealso:
  117. :py:meth:`qubes.vm.qubesvm.QubesVM.verify_files`
  118. ''' # pylint: disable=no-self-use
  119. return True
  120. def start(self, start_guid=True, notify_function=None,
  121. mem_required=None):
  122. '''Always raises an exception.
  123. .. seealso:
  124. :py:meth:`qubes.vm.qubesvm.QubesVM.start`
  125. ''' # pylint: disable=unused-argument,arguments-differ
  126. raise qubes.exc.QubesVMError(self, 'Cannot start Dom0 fake domain!')
  127. def suspend(self):
  128. '''Does nothing.
  129. .. seealso:
  130. :py:meth:`qubes.vm.qubesvm.QubesVM.suspend`
  131. '''
  132. raise qubes.exc.QubesVMError(self, 'Cannot suspend Dom0 fake domain!')
  133. @property
  134. def icon_path(self):
  135. return None
  136. @property
  137. def untrusted_qdb(self):
  138. '''QubesDB handle for this domain.'''
  139. if self._qdb_connection is None:
  140. import qubesdb # pylint: disable=import-error
  141. self._qdb_connection = qubesdb.QubesDB(self.name)
  142. return self._qdb_connection
  143. # def __init__(self, **kwargs):
  144. # super(QubesAdminVm, self).__init__(qid=0, name="dom0", netid=0,
  145. # dir_path=None,
  146. # private_img = None,
  147. # template = None,
  148. # maxmem = 0,
  149. # vcpus = 0,
  150. # label = defaults["template_label"],
  151. # **kwargs)