adminvm.py 4.9 KB

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