__init__.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #!/usr/bin/python2 -O
  2. '''
  3. Qubes OS
  4. '''
  5. __author__ = 'Invisible Things Lab'
  6. __license__ = 'GPLv2 or later'
  7. __version__ = 'R3'
  8. import qubes._pluginloader
  9. class QubesException(Exception):
  10. '''Exception that can be shown to the user'''
  11. pass
  12. class QubesVMMConnection(object):
  13. '''Connection to Virtual Machine Manager (libvirt)'''
  14. def __init__(self):
  15. self._libvirt_conn = None
  16. self._xs = None
  17. self._xc = None
  18. self._offline_mode = False
  19. @property
  20. def offline_mode(self):
  21. '''Check or enable offline mode (do not actually connect to vmm)'''
  22. return self._offline_mode
  23. @offline_mode.setter
  24. def offline_mode(self, value):
  25. if value and self._libvirt_conn is not None:
  26. raise QubesException("Cannot change offline mode while already connected")
  27. self._offline_mode = value
  28. def _libvirt_error_handler(self, ctx, error):
  29. pass
  30. def init_vmm_connection(self):
  31. '''Initialise connection
  32. This method is automatically called when getting'''
  33. if self._libvirt_conn is not None:
  34. # Already initialized
  35. return
  36. if self._offline_mode:
  37. # Do not initialize in offline mode
  38. raise QubesException("VMM operations disabled in offline mode")
  39. if 'xen.lowlevel.xs' in sys.modules:
  40. self._xs = xen.lowlevel.xs.xs()
  41. self._libvirt_conn = libvirt.open(defaults['libvirt_uri'])
  42. if self._libvirt_conn == None:
  43. raise QubesException("Failed connect to libvirt driver")
  44. libvirt.registerErrorHandler(self._libvirt_error_handler, None)
  45. atexit.register(self._libvirt_conn.close)
  46. @property
  47. def libvirt_conn(self):
  48. '''Connection to libvirt'''
  49. self.init_vmm_connection()
  50. return self._libvirt_conn
  51. @property
  52. def xs(self):
  53. '''Connection to Xen Store
  54. This property in available only when running on Xen.'''
  55. if 'xen.lowlevel.xs' not in sys.modules:
  56. return None
  57. self.init_vmm_connection()
  58. return self._xs
  59. vmm = QubesVMMConnection()
  60. class QubesHost(object):
  61. '''Basic information about host machine'''
  62. def __init__(self):
  63. (model, memory, cpus, mhz, nodes, socket, cores, threads) = vmm.libvirt_conn.getInfo()
  64. self._total_mem = long(memory)*1024
  65. self._no_cpus = cpus
  66. # print "QubesHost: total_mem = {0}B".format (self.xen_total_mem)
  67. # print "QubesHost: free_mem = {0}".format (self.get_free_xen_memory())
  68. # print "QubesHost: total_cpus = {0}".format (self.xen_no_cpus)
  69. @property
  70. def memory_total(self):
  71. '''Total memory, in bytes'''
  72. return self._total_mem
  73. @property
  74. def no_cpus(self):
  75. '''Noumber of CPUs'''
  76. return self._no_cpus
  77. # TODO
  78. def get_free_xen_memory(self):
  79. ret = self.physinfo['free_memory']
  80. return long(ret)
  81. # TODO
  82. def measure_cpu_usage(self, previous=None, previous_time = None,
  83. wait_time=1):
  84. """measure cpu usage for all domains at once"""
  85. if previous is None:
  86. previous_time = time.time()
  87. previous = {}
  88. info = vmm.xc.domain_getinfo(0, qubes_max_qid)
  89. for vm in info:
  90. previous[vm['domid']] = {}
  91. previous[vm['domid']]['cpu_time'] = (
  92. vm['cpu_time'] / vm['online_vcpus'])
  93. previous[vm['domid']]['cpu_usage'] = 0
  94. time.sleep(wait_time)
  95. current_time = time.time()
  96. current = {}
  97. info = vmm.xc.domain_getinfo(0, qubes_max_qid)
  98. for vm in info:
  99. current[vm['domid']] = {}
  100. current[vm['domid']]['cpu_time'] = (
  101. vm['cpu_time'] / max(vm['online_vcpus'], 1))
  102. if vm['domid'] in previous.keys():
  103. current[vm['domid']]['cpu_usage'] = (
  104. float(current[vm['domid']]['cpu_time'] -
  105. previous[vm['domid']]['cpu_time']) /
  106. long(1000**3) / (current_time-previous_time) * 100)
  107. if current[vm['domid']]['cpu_usage'] < 0:
  108. # VM has been rebooted
  109. current[vm['domid']]['cpu_usage'] = 0
  110. else:
  111. current[vm['domid']]['cpu_usage'] = 0
  112. return (current_time, current)
  113. class QubesVmLabel(object):
  114. '''Label definition for virtual machines
  115. Label specifies colour of the padlock displayed next to VM's name.
  116. When this is a :py:class:`qubes.vm.dispvm.DispVM`, padlock is overlayed
  117. with recycling pictogram.
  118. :param int index: numeric identificator of label
  119. :param str color: colour specification as in HTML (``#abcdef``)
  120. :param str name: label's name like "red" or "green"
  121. :param bool dispvm: :py:obj:`True` if this is :py:class:`qubes.vm.dispvm.DispVM` label
  122. '''
  123. def __init__(self, index, color, name, dispvm=False):
  124. #: numeric identificator of label
  125. self.index = index
  126. #: colour specification as in HTML (``#abcdef``)
  127. self.color = color
  128. #: label's name like "red" or "green"
  129. self.name = name
  130. #: :py:obj:`True` if this is :py:class:`qubes.vm.dispvm.DispVM` label
  131. self.dispvm = dispvm
  132. #: freedesktop icon name, suitable for use in :py:meth:`PyQt4.QtGui.QIcon.fromTheme`
  133. self.icon = '{}-{}'.format(('dispvm' if dispvm else 'appvm'), name)
  134. def __repr__(self):
  135. return '{}({!r}, {!r}, {!r}, dispvm={!r})'.format(
  136. self.__class__.__name__,
  137. self.index,
  138. self.color,
  139. self.name,
  140. self.dispvm)
  141. # self.icon_path is obsolete
  142. # use QIcon.fromTheme(label.icon) where applicable
  143. @property
  144. def icon_path(self):
  145. '''Icon path
  146. DEPRECATED --- use :py:meth:`PyQt4.QtGui.QIcon.fromTheme` and :py:attr:`QubesVmLabel.icon`'''
  147. return os.path.join(system_path['qubes_icon_dir'], self.icon) + ".png"
  148. #: Globally defined labels
  149. QubesVmLabels = {
  150. "red": QubesVmLabel(1, "0xcc0000", "red" ),
  151. "orange": QubesVmLabel(2, "0xf57900", "orange" ),
  152. "yellow": QubesVmLabel(3, "0xedd400", "yellow" ),
  153. "green": QubesVmLabel(4, "0x73d216", "green" ),
  154. "gray": QubesVmLabel(5, "0x555753", "gray" ),
  155. "blue": QubesVmLabel(6, "0x3465a4", "blue" ),
  156. "purple": QubesVmLabel(7, "0x75507b", "purple" ),
  157. "black": QubesVmLabel(8, "0x000000", "black" ),
  158. }
  159. #: Globally defined labels for :py:class:`qubes.vm.dispvm.DispVM` s
  160. QubesDispVmLabels = {
  161. "red": QubesVmLabel(1, "0xcc0000", "red", dispvm=True),
  162. "orange": QubesVmLabel(2, "0xf57900", "orange", dispvm=True),
  163. "yellow": QubesVmLabel(3, "0xedd400", "yellow", dispvm=True),
  164. "green": QubesVmLabel(4, "0x73d216", "green", dispvm=True),
  165. "gray": QubesVmLabel(5, "0x555753", "gray", dispvm=True),
  166. "blue": QubesVmLabel(6, "0x3465a4", "blue", dispvm=True),
  167. "purple": QubesVmLabel(7, "0x75507b", "purple", dispvm=True),
  168. "black": QubesVmLabel(8, "0x000000", "black", dispvm=True),
  169. }