adminvm.py 4.9 KB

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