diff --git a/core/qubes.py b/core/qubes.py index 107c285f..9ab73cf4 100755 --- a/core/qubes.py +++ b/core/qubes.py @@ -125,70 +125,6 @@ defaults = { qubes_max_qid = 254 qubes_max_netid = 254 -class QubesVMMConnection(object): - def __init__(self): - self._libvirt_conn = None - self._xs = None - self._xc = None - self._offline_mode = False - - @property - def offline_mode(self): - return self._offline_mode - - @offline_mode.setter - def offline_mode(self, value): - if not value and self._libvirt_conn is not None: - raise QubesException("Cannot change offline mode while already connected") - - self._offline_mode = value - - def _libvirt_error_handler(self, ctx, error): - pass - - def init_vmm_connection(self): - if self._libvirt_conn is not None: - # Already initialized - return - if self._offline_mode: - # Do not initialize in offline mode - return - - if 'xen.lowlevel.xs' in sys.modules: - self._xs = xen.lowlevel.xs.xs() - libvirt.virEventRegisterDefaultImpl() - self._libvirt_conn = libvirt.open(defaults['libvirt_uri']) - if self._libvirt_conn == None: - raise QubesException("Failed connect to libvirt driver") - libvirt.registerErrorHandler(self._libvirt_error_handler, None) - atexit.register(self._libvirt_conn.close) - - def _common_getter(self, name): - if self._offline_mode: - # Do not initialize in offline mode - raise QubesException("VMM operations disabled in offline mode") - - if self._libvirt_conn is None: - self.init_vmm_connection() - return getattr(self, name) - - @property - def libvirt_conn(self): - return self._common_getter('_libvirt_conn') - - @property - def xs(self): - if 'xen.lowlevel.xs' in sys.modules: - return self._common_getter('_xs') - else: - return None - - -##### VMM global variable definition ##### - -if not dry_run: - vmm = QubesVMMConnection() - ########################################## class QubesHost(object): diff --git a/qubes/__init__.py b/qubes/__init__.py index db4b724d..3cefbd78 100644 --- a/qubes/__init__.py +++ b/qubes/__init__.py @@ -14,3 +14,73 @@ class QubesException(Exception): '''Exception that can be shown to the user''' pass +class QubesVMMConnection(object): + '''Connection to Virtual Machine Manager (libvirt)''' + def __init__(self): + self._libvirt_conn = None + self._xs = None + self._xc = None + self._offline_mode = False + + @property + def offline_mode(self): + '''Check or enable offline mode (do not actually connect to vmm)''' + return self._offline_mode + + @offline_mode.setter + def offline_mode(self, value): + if not value and self._libvirt_conn is not None: + raise QubesException("Cannot change offline mode while already connected") + + self._offline_mode = value + + def _libvirt_error_handler(self, ctx, error): + pass + + def init_vmm_connection(self): + '''Initialise connection + + This method is automatically called when getting''' + if self._libvirt_conn is not None: + # Already initialized + return + if self._offline_mode: + # Do not initialize in offline mode + return + + if 'xen.lowlevel.xs' in sys.modules: + self._xs = xen.lowlevel.xs.xs() + self._libvirt_conn = libvirt.open(defaults['libvirt_uri']) + if self._libvirt_conn == None: + raise QubesException("Failed connect to libvirt driver") + libvirt.registerErrorHandler(self._libvirt_error_handler, None) + atexit.register(self._libvirt_conn.close) + + def _common_getter(self, name): + if self._offline_mode: + # Do not initialize in offline mode + raise QubesException("VMM operations disabled in offline mode") + + if self._libvirt_conn is None: + self.init_vmm_connection() + return getattr(self, name) + + @property + def libvirt_conn(self): + '''Connection to libvirt''' + return self._common_getter('_libvirt_conn') + + @property + def xs(self): + '''Connection to Xen Store + + This property in available only when running on Xen.''' + + if 'xen.lowlevel.xs' in sys.modules: + return self._common_getter('_xs') + else: + return None + +if not dry_run: + vmm = QubesVMMConnection() +