__init__.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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()