From d34c491209dffc4dce07c88323c3c53619490d02 Mon Sep 17 00:00:00 2001 From: Nikos Skalkotos <skalkoto@grnet.gr> Date: Thu, 10 Jul 2014 12:00:53 +0300 Subject: [PATCH] Add a ismounted property in OSBase class This can be used to check if the media mounting was successful --- image_creator/os_type/__init__.py | 24 ++++++++++++++++++++--- image_creator/os_type/windows/__init__.py | 21 ++++++++++++-------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/image_creator/os_type/__init__.py b/image_creator/os_type/__init__.py index e38c135..8585bc2 100644 --- a/image_creator/os_type/__init__.py +++ b/image_creator/os_type/__init__.py @@ -212,6 +212,7 @@ class OSBase(object): # This will host the error if mount fails self._mount_error = "" self._mount_warnings = [] + self._mounted = False # Many guestfs compilations don't support scrub self._scrub_support = True @@ -357,6 +358,10 @@ class OSBase(object): self.out.output() + @property + def ismounted(self): + return self._mounted + def mount(self, readonly=False, silent=False, fatal=True): """Returns a context manager for mounting an image""" @@ -374,17 +379,30 @@ class OSBase(object): parent._mount_error = "" del parent._mount_warnings[:] - if not parent._do_mount(readonly) and fatal: + try: + parent._mounted = parent._do_mount(readonly) + except: + parent.image.g.umount_all() + raise + + if not parent.ismounted: msg = "Unable to mount the media %s. Reason: %s" % \ (mount_type, parent._mount_error) - raise FatalError(msg) + if fatal: + raise FatalError(msg) + else: + warn(msg) + for warning in parent._mount_warnings: warn(warning) - success('done') + + if parent.ismounted: + success('done') def __exit__(self, exc_type, exc_value, traceback): output("Umounting the media ...", False) parent.image.g.umount_all() + parent._mounted = False success('done') return Mount() diff --git a/image_creator/os_type/windows/__init__.py b/image_creator/os_type/windows/__init__.py index 457ef0b..91a340a 100644 --- a/image_creator/os_type/windows/__init__.py +++ b/image_creator/os_type/windows/__init__.py @@ -525,16 +525,21 @@ class Windows(OSBase): self.out.output("Reverting media boot preparations ...", False) with self.mount(readonly=False, silent=True, fatal=False): - if disabled_uac: - self.registry.update_uac_remote_setting(0) - if not self.sysprepped: - # Reset the old password - admin = self.sysprep_params['admin'].value - self.registry.reset_passwd(admin, v_val) + if not self.ismounted: + self.out.warn("The boot changes cannot be reverted. " + "The snapshot may be in a corrupted state.") + else: + if disabled_uac: + self.registry.update_uac_remote_setting(0) - self.registry.update_firewalls(*firewall_states) - self.out.success("done") + if not self.sysprepped: + # Reset the old password + admin = self.sysprep_params['admin'].value + self.registry.reset_passwd(admin, v_val) + + self.registry.update_firewalls(*firewall_states) + self.out.success("done") def _exec_sysprep_tasks(self): """This function hosts the actual code for executing the enabled -- GitLab