adminvm.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. for line in open('/proc/meminfo'):
  80. if line.startswith('MemTotal:'):
  81. return int(line.split(':')[1].strip().split()[0])
  82. raise NotImplementedError()
  83. def get_mem_static_max(self):
  84. '''Get maximum memory available to Dom0.
  85. .. seealso:
  86. :py:meth:`qubes.vm.qubesvm.QubesVM.get_mem_static_max`
  87. '''
  88. if self.app.vmm.offline_mode:
  89. # default value passed on xen cmdline
  90. return 4096
  91. else:
  92. try:
  93. return self.app.vmm.libvirt_conn.getInfo()[1]
  94. except libvirt.libvirtError as e:
  95. self.log.warning(
  96. 'Failed to get memory limit for dom0: {}'.format(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, **kwargs):
  105. '''Always raises an exception.
  106. .. seealso:
  107. :py:meth:`qubes.vm.qubesvm.QubesVM.start`
  108. ''' # pylint: disable=unused-argument
  109. raise qubes.exc.QubesVMError(self, 'Cannot start Dom0 fake domain!')
  110. def suspend(self):
  111. '''Does nothing.
  112. .. seealso:
  113. :py:meth:`qubes.vm.qubesvm.QubesVM.suspend`
  114. '''
  115. raise qubes.exc.QubesVMError(self, 'Cannot suspend Dom0 fake domain!')
  116. @property
  117. def icon_path(self):
  118. return None
  119. # def __init__(self, **kwargs):
  120. # super(QubesAdminVm, self).__init__(qid=0, name="dom0", netid=0,
  121. # dir_path=None,
  122. # private_img = None,
  123. # template = None,
  124. # maxmem = 0,
  125. # vcpus = 0,
  126. # label = defaults["template_label"],
  127. # **kwargs)