[elbe-devel] [PATCH 1/7] elbepack: hdimg: correct order of loopdev creation and teardown

Thomas Weißschuh thomas.weissschuh at linutronix.de
Tue May 7 11:54:37 CEST 2024


The loopdev needs to created before the mounts and torn down after the
unmounts. Fix that order.

Fixes: f3e474dc9732 ("elbepack: unify losetup execution into context manager")
Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 elbepack/hdimg.py | 52 ++++++++++++++++++++++++++--------------------------
 1 file changed, 26 insertions(+), 26 deletions(-)

diff --git a/elbepack/hdimg.py b/elbepack/hdimg.py
index 31a4a5190dcd..0175f5695a16 100644
--- a/elbepack/hdimg.py
+++ b/elbepack/hdimg.py
@@ -148,8 +148,8 @@ class grubinstaller202(grubinstaller_base):
 
         imagemnt = os.path.join(target, 'imagemnt')
         imagemntfs = Filesystem(imagemnt)
-        try:
-            with losetup(self.fs['/'].filename) as loopdev:
+        with losetup(self.fs['/'].filename) as loopdev:
+            try:
                 for entry in self.fs.depthlist():
                     do(
                         'mount '
@@ -184,19 +184,19 @@ class grubinstaller202(grubinstaller_base):
                     chroot(imagemnt, ['grub-install', *user_args, '--target', 'i386-pc',
                                       '--no-floppy', loopdev])
 
-        except subprocess.CalledProcessError as E:
-            logging.error('Fail installing grub device: %s', E)
+            except subprocess.CalledProcessError as E:
+                logging.error('Fail installing grub device: %s', E)
 
-        finally:
-            os.unlink(imagemntfs.fname('boot/grub/device.map'))
-            do(f"umount {imagemntfs.fname('dev')}", check=False)
-            do(f"umount {imagemntfs.fname('proc')}", check=False)
-            do(f"umount {imagemntfs.fname('sys')}", check=False)
+            finally:
+                os.unlink(imagemntfs.fname('boot/grub/device.map'))
+                do(f"umount {imagemntfs.fname('dev')}", check=False)
+                do(f"umount {imagemntfs.fname('proc')}", check=False)
+                do(f"umount {imagemntfs.fname('sys')}", check=False)
 
-            for entry in reversed(self.fs.depthlist()):
-                do(
-                    f'umount {loopdev}p{entry.partnum}',
-                    check=False)
+                for entry in reversed(self.fs.depthlist()):
+                    do(
+                        f'umount {loopdev}p{entry.partnum}',
+                        check=False)
 
 
 class grubinstaller97(grubinstaller_base):
@@ -207,8 +207,8 @@ class grubinstaller97(grubinstaller_base):
 
         imagemnt = os.path.join(target, 'imagemnt')
         imagemntfs = Filesystem(imagemnt)
-        try:
-            with losetup(self.fs['/'].filename) as loopdev:
+        with losetup(self.fs['/'].filename) as loopdev:
+            try:
 
                 bootentry = 0
 
@@ -246,19 +246,19 @@ class grubinstaller97(grubinstaller_base):
 
                 chroot(imagemnt, ['grub-install', *user_args, '--no-floppy', loopdev])
 
-        except subprocess.CalledProcessError as E:
-            logging.error('Fail installing grub device: %s', E)
+            except subprocess.CalledProcessError as E:
+                logging.error('Fail installing grub device: %s', E)
 
-        finally:
-            os.unlink(imagemntfs.fname('boot/grub/device.map'))
-            do(f"umount {imagemntfs.fname('dev')}", check=False)
-            do(f"umount {imagemntfs.fname('proc')}", check=False)
-            do(f"umount {imagemntfs.fname('sys')}", check=False)
+            finally:
+                os.unlink(imagemntfs.fname('boot/grub/device.map'))
+                do(f"umount {imagemntfs.fname('dev')}", check=False)
+                do(f"umount {imagemntfs.fname('proc')}", check=False)
+                do(f"umount {imagemntfs.fname('sys')}", check=False)
 
-            for entry in reversed(self.fs.depthlist()):
-                do(
-                    f'umount {loopdev}p{entry.partnum}',
-                    check=False)
+                for entry in reversed(self.fs.depthlist()):
+                    do(
+                        f'umount {loopdev}p{entry.partnum}',
+                        check=False)
 
 
 class simple_fstype:

-- 
2.45.0



More information about the elbe-devel mailing list