[elbe-devel] [PATCH 22/32] Nuke ASCIIDocLog from hdimg

dion at linutronix.de dion at linutronix.de
Fri Jun 14 22:13:36 CEST 2019


From: Olivier Dion <dion at linutronix.de>

Signed-off-by: Olivier Dion <dion at linutronix.de>
---
 elbepack/efilesystem.py |  12 ++-
 elbepack/hdimg.py       | 201 +++++++++++++++++++++++-------------------------
 2 files changed, 102 insertions(+), 111 deletions(-)

diff --git a/elbepack/efilesystem.py b/elbepack/efilesystem.py
index 1f6428e0..3d2e3d6d 100644
--- a/elbepack/efilesystem.py
+++ b/elbepack/efilesystem.py
@@ -307,13 +307,11 @@ class TargetFs(ChRootFilesystem):
     def part_target(self, targetdir, grub_version, grub_fw_type=None):
 
         # create target images and copy the rfs into them
-        self.images = do_hdimg(
-            self.log,
-            self.xml,
-            targetdir,
-            self,
-            grub_version,
-            grub_fw_type)
+        self.images = do_hdimg(self.xml,
+                               targetdir,
+                               self,
+                               grub_version,
+                               grub_fw_type)
 
         for i in self.images:
             self.image_packers[i] = default_packer
diff --git a/elbepack/hdimg.py b/elbepack/hdimg.py
index 771c9a35..b1c02449 100644
--- a/elbepack/hdimg.py
+++ b/elbepack/hdimg.py
@@ -15,11 +15,11 @@ import parted
 import _ped
 
 from elbepack.fstab import fstabentry, mountpoint_dict
-from elbepack.asciidoclog import CommandError
 from elbepack.filesystem import size_to_int
+from elbepack.shellhelper import do, CommandError
 
 
-def mkfs_mtd(outf, mtd, fslabel, target):
+def mkfs_mtd(mtd, fslabel, target):
 
     # generated files
     img_files = []
@@ -43,7 +43,7 @@ def mkfs_mtd(outf, mtd, fslabel, target):
             continue
 
         try:
-            outf.do("mkfs.ubifs -r %s -o %s.ubifs -m %s -e %s -c %s %s" % (
+            do("mkfs.ubifs -r %s -o %s.ubifs -m %s -e %s -c %s %s" % (
                 os.path.join(target, "filesystems", fslabel[label].id),
                 os.path.join(target, label),
                 ubivg.text("miniosize"),
@@ -59,7 +59,7 @@ def mkfs_mtd(outf, mtd, fslabel, target):
     return img_files
 
 
-def build_image_mtd(outf, mtd, target):
+def build_image_mtd(mtd, target):
 
     # pylint: disable=too-many-branches
 
@@ -87,7 +87,7 @@ def build_image_mtd(outf, mtd, target):
                     # copy from project directory
                     else:
                         tmp = target + "/" + vol.text("binary")
-                    outf.do(
+                    do(
                         "cp %s %s/%s.ubibin" %
                         (tmp, target, vol.text("label")))
                     img_files.append(vol.text("label") + ".ubibin")
@@ -125,7 +125,7 @@ def build_image_mtd(outf, mtd, target):
         subp = ""
 
     try:
-        outf.do("ubinize %s -o %s -p %s -m %s %s/%s_%s.cfg" % (
+        do("ubinize %s -o %s -p %s -m %s %s/%s_%s.cfg" % (
             subp,
             os.path.join(target, mtd.text("name")),
             ubivg.text("physicaleraseblocksize"),
@@ -144,8 +144,7 @@ def build_image_mtd(outf, mtd, target):
 
 
 class grubinstaller_base(object):
-    def __init__(self, outf, fw_type=None):
-        self.outf = outf
+    def __init__(self, fw_type=None):
         self.root = None
         self.boot = None
         self.boot_efi = None
@@ -173,77 +172,77 @@ class grubinstaller202(grubinstaller_base):
 
         imagemnt = os.path.join(target, "imagemnt")
         try:
-            self.outf.do('cp -a /dev/loop0 /dev/poop0')
+            do('cp -a /dev/loop0 /dev/poop0')
 
-            self.outf.do('losetup /dev/poop0 "%s"' % self.root.filename)
-            self.outf.do('kpartx -as /dev/poop0')
-            self.outf.do(
+            do('losetup /dev/poop0 "%s"' % self.root.filename)
+            do('kpartx -as /dev/poop0')
+            do(
                 'mount /dev/mapper/poop0p%d %s' %
                 (self.root.partnum, imagemnt))
 
             if self.boot:
-                self.outf.do(
+                do(
                     'mount /dev/mapper/poop0p%d %s' %
                     (self.boot.partnum, os.path.join(
                         imagemnt, "boot")))
 
             if self.boot_efi:
-                self.outf.do(
+                do(
                     'mount /dev/mapper/poop0p%d %s' %
                     (self.boot_efi.partnum, os.path.join(
                         imagemnt, "boot/efi")))
 
-            self.outf.do(
+            do(
                 "mount --bind /dev %s" %
                 os.path.join(
                     imagemnt,
                     "dev"))
-            self.outf.do(
+            do(
                 "mount --bind /proc %s" %
                 os.path.join(
                     imagemnt,
                     "proc"))
-            self.outf.do(
+            do(
                 "mount --bind /sys %s" %
                 os.path.join(
                     imagemnt,
                     "sys"))
 
-            self.outf.do('mkdir -p "%s"' % os.path.join(imagemnt, "boot/grub"))
+            do('mkdir -p "%s"' % os.path.join(imagemnt, "boot/grub"))
 
             devmap = open(os.path.join(imagemnt, "boot/grub/device.map"), "w")
             devmap.write("(hd0) /dev/poop0\n")
             devmap.close()
 
-            self.outf.do("chroot %s  update-initramfs -u -k all" % imagemnt)
-            self.outf.do("chroot %s  update-grub2" % imagemnt)
+            do("chroot %s  update-initramfs -u -k all" % imagemnt)
+            do("chroot %s  update-grub2" % imagemnt)
 
             if self.fw_type == "efi" or self.fw_type == "hybrid":
-                self.outf.do(
+                do(
                     "chroot %s grub-install --target=x86_64-efi --removable "
                     "--no-floppy /dev/poop0" %
                     (imagemnt))
             if self.fw_type == "hybrid" or self.fw_type is None:
                 # when we are in hybrid mode, install grub also into MBR
-                self.outf.do(
+                do(
                     "chroot %s grub-install --no-floppy /dev/poop0" %
                     (imagemnt))
 
         finally:
             os.unlink(os.path.join(imagemnt, "boot/grub/device.map"))
-            self.outf.do(
+            do(
                 "umount %s" %
                 os.path.join(
                     imagemnt,
                     "dev"),
                 allow_fail=True)
-            self.outf.do(
+            do(
                 "umount %s" %
                 os.path.join(
                     imagemnt,
                     "proc"),
                 allow_fail=True)
-            self.outf.do(
+            do(
                 "umount %s" %
                 os.path.join(
                     imagemnt,
@@ -251,21 +250,21 @@ class grubinstaller202(grubinstaller_base):
                 allow_fail=True)
 
             if self.boot_efi:
-                self.outf.do(
+                do(
                     'umount /dev/mapper/poop0p%d' %
                     self.boot_efi.partnum, allow_fail=True)
 
             if self.boot:
-                self.outf.do(
+                do(
                     'umount /dev/mapper/poop0p%d' %
                     self.boot.partnum, allow_fail=True)
 
-            self.outf.do(
+            do(
                 'umount /dev/mapper/poop0p%d' %
                 self.root.partnum, allow_fail=True)
 
-            self.outf.do('kpartx -d /dev/poop0', allow_fail=True)
-            self.outf.do("losetup -d /dev/poop0", allow_fail=True)
+            do('kpartx -d /dev/poop0', allow_fail=True)
+            do("losetup -d /dev/poop0", allow_fail=True)
 
 
 class grubinstaller199(grubinstaller_base):
@@ -276,17 +275,17 @@ class grubinstaller199(grubinstaller_base):
 
         imagemnt = os.path.join(target, "imagemnt")
         try:
-            self.outf.do('cp -a /dev/loop0 /dev/poop0')
-            self.outf.do('cp -a /dev/loop1 /dev/poop1')
-            self.outf.do('cp -a /dev/loop2 /dev/poop2')
+            do('cp -a /dev/loop0 /dev/poop0')
+            do('cp -a /dev/loop1 /dev/poop1')
+            do('cp -a /dev/loop2 /dev/poop2')
 
-            self.outf.do('losetup /dev/poop0 "%s"' % self.root.filename)
-            self.root.losetup(self.outf, "poop1")
-            self.outf.do('mount /dev/poop1 %s' % imagemnt)
+            do('losetup /dev/poop0 "%s"' % self.root.filename)
+            self.root.losetup("poop1")
+            do('mount /dev/poop1 %s' % imagemnt)
 
             if self.boot:
-                self.boot.losetup(self.outf, "poop2")
-                self.outf.do('mount /dev/poop2 %s' %
+                self.boot.losetup("poop2")
+                do('mount /dev/poop2 %s' %
                              (os.path.join(imagemnt, "boot")))
 
             devmap = open(os.path.join(imagemnt, "boot/grub/device.map"), "w")
@@ -297,59 +296,59 @@ class grubinstaller199(grubinstaller_base):
 
             devmap.close()
 
-            self.outf.do(
+            do(
                 "mount --bind /dev %s" %
                 os.path.join(
                     imagemnt,
                     "dev"))
-            self.outf.do(
+            do(
                 "mount --bind /proc %s" %
                 os.path.join(
                     imagemnt,
                     "proc"))
-            self.outf.do(
+            do(
                 "mount --bind /sys %s" %
                 os.path.join(
                     imagemnt,
                     "sys"))
 
-            self.outf.do("chroot %s  update-initramfs -u -k all" % imagemnt)
-            self.outf.do("chroot %s  update-grub2" % imagemnt)
+            do("chroot %s  update-initramfs -u -k all" % imagemnt)
+            do("chroot %s  update-grub2" % imagemnt)
 
-            self.outf.do(
+            do(
                 "chroot %s  grub-install --no-floppy /dev/loop0" %
                 imagemnt)
 
         finally:
             os.unlink(os.path.join(imagemnt, "boot/grub/device.map"))
 
-            self.outf.do(
+            do(
                 "umount -l %s" %
                 os.path.join(
                     imagemnt,
                     "dev"),
                 allow_fail=True)
-            self.outf.do(
+            do(
                 "umount -l %s" %
                 os.path.join(
                     imagemnt,
                     "proc"),
                 allow_fail=True)
-            self.outf.do(
+            do(
                 "umount -l %s" %
                 os.path.join(
                     imagemnt,
                     "sys"),
                 allow_fail=True)
 
-            self.outf.do("losetup -d /dev/poop0", allow_fail=True)
+            do("losetup -d /dev/poop0", allow_fail=True)
 
             if self.boot:
-                self.outf.do('umount /dev/poop2', allow_fail=True)
-                self.outf.do('losetup -d /dev/poop2', allow_fail=True)
+                do('umount /dev/poop2', allow_fail=True)
+                do('losetup -d /dev/poop2', allow_fail=True)
 
-            self.outf.do('umount /dev/poop1', allow_fail=True)
-            self.outf.do('losetup -d /dev/poop1', allow_fail=True)
+            do('umount /dev/poop1', allow_fail=True)
+            do('losetup -d /dev/poop1', allow_fail=True)
 
 
 class simple_fstype(object):
@@ -399,7 +398,7 @@ def create_partition(
     return ppart
 
 
-def create_label(outf, disk, part, ppart, fslabel, target, grub):
+def create_label(disk, part, ppart, fslabel, target, grub):
 
     # pylint: disable=too-many-arguments
 
@@ -413,33 +412,31 @@ def create_label(outf, disk, part, ppart, fslabel, target, grub):
     elif entry.mountpoint == "/boot/efi":
         grub.set_boot_efi_entry(entry)
 
-    entry.losetup(outf, "loop0")
-    outf.do(
+    entry.losetup("loop0")
+    do(
         'mkfs.%s %s %s /dev/loop0' %
         (entry.fstype,
          entry.mkfsopt,
          entry.get_label_opt()))
 
-    outf.do('mount /dev/loop0 %s' % os.path.join(target, "imagemnt"))
-    outf.do(
+    do('mount /dev/loop0 %s' % os.path.join(target, "imagemnt"))
+    do(
         'cp -a "%s/." "%s/"' %
         (os.path.join(
             target, "filesystems", entry.id), os.path.join(
             target, "imagemnt")), allow_fail=True)
-    outf.do('umount /dev/loop0')
-    outf.do('losetup -d /dev/loop0')
+    do('umount /dev/loop0')
+    do('losetup -d /dev/loop0')
 
     return ppart
 
 
-def create_logical_partitions(
-        outf,
-        disk,
-        extended,
-        epart,
-        fslabel,
-        target,
-        grub):
+def create_logical_partitions(disk,
+                              extended,
+                              epart,
+                              fslabel,
+                              target,
+                              grub):
 
     # pylint: disable=too-many-arguments
 
@@ -459,12 +456,12 @@ def create_logical_partitions(
             size_in_sectors,
             current_sector)
         if logical.has("label") and logical.text("label") in fslabel:
-            create_label(outf, disk, logical, lpart, fslabel, target, grub)
+            create_label(disk, logical, lpart, fslabel, target, grub)
 
         current_sector += lpart.getLength()
 
 
-def do_image_hd(outf, hd, fslabel, target, grub_version, grub_fw_type=None):
+def do_image_hd(hd, fslabel, target, grub_version, grub_fw_type=None):
 
     # pylint: disable=too-many-arguments
     # pylint: disable=too-many-locals
@@ -475,7 +472,7 @@ def do_image_hd(outf, hd, fslabel, target, grub_version, grub_fw_type=None):
     size_in_sectors = s / sector_size
 
     imagename = os.path.join(target, hd.text("name"))
-    outf.do('rm -f "%s"' % imagename, allow_fail=True)
+    do('rm -f "%s"' % imagename, allow_fail=True)
     f = open(imagename, "wb")
     f.truncate(size_in_sectors * sector_size)
     f.close()
@@ -487,15 +484,15 @@ def do_image_hd(outf, hd, fslabel, target, grub_version, grub_fw_type=None):
         disk = parted.freshDisk(imag, "msdos")
 
     if grub_version == 199:
-        grub = grubinstaller199(outf)
+        grub = grubinstaller199()
     elif grub_version == 202 and grub_fw_type == "efi":
-        grub = grubinstaller202(outf, "efi")
+        grub = grubinstaller202("efi")
     elif grub_version == 202 and grub_fw_type == "hybrid":
-        grub = grubinstaller202(outf, "hybrid")
+        grub = grubinstaller202("hybrid")
     elif grub_version == 202:
-        grub = grubinstaller202(outf)
+        grub = grubinstaller202()
     else:
-        grub = grubinstaller_base(outf)
+        grub = grubinstaller_base()
 
     current_sector = 2048
     for part in hd:
@@ -509,7 +506,7 @@ def do_image_hd(outf, hd, fslabel, target, grub_version, grub_fw_type=None):
                 size_in_sectors,
                 current_sector)
             if part.text("label") in fslabel:
-                create_label(outf, disk, part, ppart, fslabel, target, grub)
+                create_label(disk, part, ppart, fslabel, target, grub)
         elif part.tag == "extended":
             ppart = create_partition(
                 disk,
@@ -518,8 +515,8 @@ def do_image_hd(outf, hd, fslabel, target, grub_version, grub_fw_type=None):
                 fslabel,
                 size_in_sectors,
                 current_sector)
-            create_logical_partitions(
-                outf, disk, part, ppart, fslabel, target, grub)
+            create_logical_partitions(disk, part, ppart,
+                                      fslabel, target, grub)
         else:
             continue
 
@@ -533,7 +530,7 @@ def do_image_hd(outf, hd, fslabel, target, grub_version, grub_fw_type=None):
     return hd.text("name")
 
 
-def add_binary_blob(outf, hd, target):
+def add_binary_blob(hd, target):
 
     imagename = os.path.join(target, hd.text("name"))
 
@@ -559,14 +556,14 @@ def add_binary_blob(outf, hd, target):
             # use file from /var/cache/elbe/<uuid> project dir
             bf = os.path.join(target, binary.et.text)
 
-        outf.do('dd if="%s" of="%s" seek="%s" bs="%s" conv=notrunc' % (
+        do('dd if="%s" of="%s" seek="%s" bs="%s" conv=notrunc' % (
             bf,
             imagename,
             offset,
             bs))
 
 
-def do_hdimg(outf, xml, target, rfs, grub_version, grub_fw_type=None):
+def do_hdimg(xml, target, rfs, grub_version, grub_fw_type=None):
 
     # pylint: disable=too-many-arguments
     # pylint: disable=too-many-locals
@@ -602,52 +599,48 @@ def do_hdimg(outf, xml, target, rfs, grub_version, grub_fw_type=None):
     # create directories, where we want our
     # filesystems later
     fspath = os.path.join(target, "filesystems")
-    outf.do('mkdir -p %s' % fspath)
+    do('mkdir -p %s' % fspath)
 
     imagemnt = os.path.join(target, "imagemnt")
-    outf.do('mkdir -p %s' % imagemnt)
+    do('mkdir -p %s' % imagemnt)
 
     # now move all mountpoints into own directories
     # begin from deepest mountpoints
     for l in reversed(fslist):
-        outf.do('mkdir -p "%s"' % os.path.join(fspath, l.id))
-        outf.do('mkdir -p "%s"' % rfs.fname('') + l.mountpoint)
+        do('mkdir -p "%s"' % os.path.join(fspath, l.id))
+        do('mkdir -p "%s"' % rfs.fname('') + l.mountpoint)
         if rfs.listdir(l.mountpoint):
-            outf.do('mv "%s"/* "%s"' % (rfs.fname(l.mountpoint), os.path.join(
+            do('mv "%s"/* "%s"' % (rfs.fname(l.mountpoint), os.path.join(
                 fspath, l.id)), allow_fail=True)
 
     try:
         # Now iterate over all images and create filesystems and partitions
         for i in xml.tgt.node("images"):
             if i.tag == "msdoshd":
-                img = do_image_hd(
-                    outf,
-                    i,
-                    fslabel,
-                    target,
-                    grub_version,
-                    grub_fw_type)
+                img = do_image_hd(i,
+                                  fslabel,
+                                  target,
+                                  grub_version,
+                                  grub_fw_type)
                 img_files.append(img)
 
             if i.tag == "gpthd":
-                img = do_image_hd(
-                    outf,
-                    i,
-                    fslabel,
-                    target,
-                    grub_version,
-                    grub_fw_type)
+                img = do_image_hd(i,
+                                  fslabel,
+                                  target,
+                                  grub_version,
+                                  grub_fw_type)
                 img_files.append(img)
 
             if i.tag == "mtd":
-                imgs = mkfs_mtd(outf, i, fslabel, target)
+                imgs = mkfs_mtd(i, fslabel, target)
                 img_files.extend(imgs)
     finally:
         # Put back the filesystems into /target
         # most shallow fs first...
         for i in fslist:
             if len(os.listdir(os.path.join(fspath, i.id))) > 0:
-                outf.do(
+                do(
                     'mv "%s"/* "%s"' %
                     (os.path.join(
                         fspath, i.id), rfs.fname(
@@ -656,13 +649,13 @@ def do_hdimg(outf, xml, target, rfs, grub_version, grub_fw_type=None):
     # Files are now moved back. ubinize needs files in place, so we run it now.
     for i in xml.tgt.node("images"):
         if i.tag == "mtd":
-            imgs = build_image_mtd(outf, i, target)
+            imgs = build_image_mtd(i, target)
             img_files.extend(imgs)
 
     # dd binary blobs onto images
     for i in xml.tgt.node("images"):
         if (i.tag == "msdoshd") or (i.tag == "gpthd"):
-            add_binary_blob(outf, i, target)
+            add_binary_blob(i, target)
 
     # use set() to remove duplicates, but
     # return a list
-- 
2.11.0




More information about the elbe-devel mailing list