[elbe-devel] [PATCH v3 4/5] hdimg: Use losetup wrapper for grubinstaller202

Olivier Dion dion at linutronix.de
Sat Apr 4 19:27:54 CEST 2020


Instead of using fixed loop device /dev/loop0 and /dev/poop0,
grubinstaller uses its losetup wrapper to dynamicaly get a device.
This is because the fixed device could be busy, even though it has
been detached.

Also add an exception catching and log it.  This is because the
finally block can shadow an exception throwed in the try block.

Signed-off-by: Olivier Dion <dion at linutronix.de>
---
 elbepack/hdimg.py | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)

diff --git a/elbepack/hdimg.py b/elbepack/hdimg.py
index a1c77ce7..c33efa08 100644
--- a/elbepack/hdimg.py
+++ b/elbepack/hdimg.py
@@ -9,6 +9,7 @@
 
 from __future__ import print_function
 
+import logging
 import os
 
 import parted
@@ -169,14 +170,16 @@ class grubinstaller202(grubinstaller_base):
         imagemnt = os.path.join(target, "imagemnt")
         imagemntfs = Filesystem(imagemnt)
         try:
-            do('cp -a /dev/loop0 /dev/poop0')
+            loopdev = self.losetup(self.fs['/'].filename)
+            loopnum = loopdev.replace("/dev/loop", "")
+            poopdev = "/dev/poop" + loopnum
 
-            do('losetup /dev/poop0 "%s"' % self.fs['/'].filename)
-            do('kpartx -as /dev/poop0')
+            do('cp -a %s %s' % (loopdev, poopdev))
+            do('kpartx -as %s' % poopdev)
 
             for entry in self.fs.depthlist():
-                do('mount /dev/mapper/poop0p%d %s' %
-                   (entry.partnum, imagemntfs.fname(entry.mountpoint)))
+                do('mount /dev/mapper/poop%sp%d %s' %
+                   (loopnum, entry.partnum, imagemntfs.fname(entry.mountpoint)))
 
             do("mount --bind /dev %s" % imagemntfs.fname("dev"))
             do("mount --bind /proc %s" % imagemntfs.fname("proc"))
@@ -185,7 +188,7 @@ class grubinstaller202(grubinstaller_base):
             do('mkdir -p "%s"' % imagemntfs.fname("boot/grub"))
 
             devmap = open(imagemntfs.fname("boot/grub/device.map"), "w")
-            devmap.write("(hd0) /dev/poop0\n")
+            devmap.write("(hd0) %s\n" % poopdev)
             devmap.close()
 
             chroot(imagemnt, "update-initramfs -u -k all")
@@ -194,8 +197,8 @@ class grubinstaller202(grubinstaller_base):
             if "efi" in self.fw_type:
                 grub_tgt = next(t for t in self.fw_type if t.endswith("-efi"))
                 do("chroot %s grub-install --target=%s --removable "
-                   "--no-floppy /dev/poop0" %
-                   (imagemnt, grub_tgt))
+                   "--no-floppy %s" %
+                   (imagemnt, grub_tgt, poopdev))
             if "shimfix" in self.fw_type:
                 # grub-install is heavily dependent on the running system having
                 # a BIOS or EFI.  The initvm is BIOS-based, so fix the resulting
@@ -206,8 +209,11 @@ class grubinstaller202(grubinstaller_base):
                    "${f[0]} /boot/efi/EFI/debian/${f[0]%%.signed}'"  %
                    imagemnt)
             if not self.fw_type or "bios" in self.fw_type:
-                do("chroot %s grub-install --no-floppy /dev/poop0" %
-                   (imagemnt))
+                do("chroot %s grub-install --no-floppy %s" %
+                   (imagemnt, poopdev))
+
+        except Exception as E:
+            logging.exception(E)
 
         finally:
             os.unlink(imagemntfs.fname("boot/grub/device.map"))
@@ -216,11 +222,11 @@ class grubinstaller202(grubinstaller_base):
             do("umount %s" % imagemntfs.fname("sys"), allow_fail=True)
 
             for entry in reversed(self.fs.depthlist()):
-                do('umount /dev/mapper/poop0p%d' % entry.partnum,
+                do('umount /dev/mapper/poop%sp%d' % (loopnum, entry.partnum),
                    allow_fail=True)
 
-            do('kpartx -d /dev/poop0', allow_fail=True)
-            do("losetup -d /dev/poop0", allow_fail=True)
+            do("kpartx -d %s" % poopdev, allow_fail=True)
+            do("losetup -d %s" % poopdev, allow_fail=True)
 
 
 class grubinstaller97(grubinstaller_base):
-- 
2.26.0




More information about the elbe-devel mailing list