]> git.ipfire.org Git - people/ms/bricklayer.git/commitdiff
disks: Shut down storage when leaving the installer
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 May 2021 13:22:15 +0000 (13:22 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 May 2021 13:22:15 +0000 (13:22 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/python/__init__.py
src/python/disk.py

index 38661f7cdc5167536d226e5c4b3aecae6d51ce61..d62cb12414bc59e28266a0dd4e34e73f3e40bf8c 100644 (file)
@@ -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):
                """
index e3e069fa2d591ebf043345cd9641ae9b8e5fe277..d1888eebd7a8f095455973f1ea2eee4e99acaea5 100644 (file)
@@ -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):