From: Michael Tremer Date: Sat, 8 May 2021 13:22:15 +0000 (+0000) Subject: disks: Shut down storage when leaving the installer X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f193c98aedf4b7aa278337da8d16bedc2a7518ef;p=people%2Fms%2Fbricklayer.git disks: Shut down storage when leaving the installer Signed-off-by: Michael Tremer --- diff --git a/src/python/__init__.py b/src/python/__init__.py index 38661f7..d62cb12 100644 --- a/src/python/__init__.py +++ b/src/python/__init__.py @@ -88,55 +88,60 @@ class Bricklayer(object): def __call__(self): with self.tui: - # Walk through all steps - for step in self.steps: - try: - self._run_step(step) - - # End installer if aborted - except InstallAbortedError: - return 1 - - # Catch any failed commands - except subprocess.CalledProcessError as e: - args = { - "command" : " ".join(e.cmd), - "output" : e.output.decode(), - "returncode" : e.returncode, - } - - # Log the error - log.error("Command \"%(command)s\" failed with error code " - "%(returncode)s:\n%(output)s" % args) - - # Format the error message - error = _("Command \"%(command)s\" failed with error code " - "%(returncode)s:\n\n%(output)s" % args) - - # Show it - self.tui.error(_("An Unexpected Error Occured"), error, - buttons=[_("Exit")], width=78) - - # Exit - return e.returncode - - # Catch all other exceptions and show an error - except: - type, value, tb = sys.exc_info() - - # Log the error - log.error("An unexpected error occured:", exc_info=True) - - # Format the exception - error = _("The installation cannot be continued due to an error:" - "\n\n%s") % "".join(traceback.format_exception(type, value, tb)) - - # Show an error message - self.tui.error(_("An Unexpected Error Occured"), - "".join(error), buttons=[_("Exit")], width=78) - - # Exit - return 1 + try: + # Walk through all steps + for step in self.steps: + try: + self._run_step(step) + + # End installer if aborted + except InstallAbortedError: + return 1 + + # Catch any failed commands + except subprocess.CalledProcessError as e: + args = { + "command" : " ".join(e.cmd), + "output" : e.output.decode(), + "returncode" : e.returncode, + } + + # Log the error + log.error("Command \"%(command)s\" failed with error code " + "%(returncode)s:\n%(output)s" % args) + + # Format the error message + error = _("Command \"%(command)s\" failed with error code " + "%(returncode)s:\n\n%(output)s" % args) + + # Show it + self.tui.error(_("An Unexpected Error Occured"), error, + buttons=[_("Exit")], width=78) + + # Exit + return e.returncode + + # Catch all other exceptions and show an error + except: + type, value, tb = sys.exc_info() + + # Log the error + log.error("An unexpected error occured:", exc_info=True) + + # Format the exception + error = _("The installation cannot be continued due to an error:" + "\n\n%s") % "".join(traceback.format_exception(type, value, tb)) + + # Show an error message + self.tui.error(_("An Unexpected Error Occured"), + "".join(error), buttons=[_("Exit")], width=78) + + # Exit + return 1 + + # Cleanup when we leave + finally: + self.disks.tear_down() def _run_step(self, stepcls): """ diff --git a/src/python/disk.py b/src/python/disk.py index e3e069f..d1888ee 100644 --- a/src/python/disk.py +++ b/src/python/disk.py @@ -121,6 +121,13 @@ class Disks(object): # Create one giant root partition root.create_system_partitions() + def tear_down(self): + """ + Shuts down any storage + """ + for disk in self.disks: + disk.tear_down() + class Disk(object): def __init__(self, bricklayer, device): @@ -280,6 +287,17 @@ class Disk(object): if self.path.startswith("/dev/loop"): self.bricklayer.command(["kpartx", "-av", self.path]) + def tear_down(self): + """ + Shuts down this disk + """ + # Unmap partitions on loop devices + if self.path.startswith("/dev/loop"): + self.bricklayer.command(["kpartx", "-dv", self.path]) + + # Free the loop device + self.bricklayer.command(["losetup", "-d", self.path]) + class Partition(object): def __init__(self, bricklayer, parted):