adminvm.py 5.9 KB

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