adminvm.py 5.4 KB

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