[PATCH v2 4/8] test_ut: Allow running unprivileged

Simon Glass sjg at chromium.org
Thu Nov 21 23:32:10 CET 2024


From: Richard Weinberger <richard at nod.at>

Like for test_fs, no need to mess with loop mounts.

Signed-off-by: Richard Weinberger <richard at nod.at>
Tweaks to reduce diff (keep mnt variable):
Signed-off-by: Simon Glass <sjg at chromium.org>
---

Changes in v2:
- Attempt to reduce the diff, for easier review

 test/py/tests/test_ut.py | 86 ++++++++++++----------------------------
 1 file changed, 26 insertions(+), 60 deletions(-)

diff --git a/test/py/tests/test_ut.py b/test/py/tests/test_ut.py
index 0b716f4029c..d449d0baf5e 100644
--- a/test/py/tests/test_ut.py
+++ b/test/py/tests/test_ut.py
@@ -8,7 +8,6 @@ test one at a time, as well setting up some files needed by the tests.
 # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
 """
 import collections
-import getpass
 import gzip
 import os
 import os.path
@@ -43,44 +42,21 @@ def setup_image(cons, devnum, part_type, img_size=20, second_part=False,
     Returns:
         tuple:
             str: Filename of MMC image
-            str: Directory name of 'mnt' directory
+            str: Directory name of scratch directory
     """
     fname = os.path.join(cons.config.source_dir, f'{basename}{devnum}.img')
-    mnt = os.path.join(cons.config.persistent_data_dir, 'mnt')
+    mnt = os.path.join(cons.config.persistent_data_dir, 'scratch')
     mkdir_cond(mnt)
 
     spec = f'type={part_type:x}, size={img_size - 2}M, start=1M, bootable'
     if second_part:
         spec += '\ntype=c'
 
-    u_boot_utils.run_and_log(cons, f'qemu-img create {fname} {img_size}M')
-    u_boot_utils.run_and_log(cons, f'sudo sfdisk {fname}',
+    u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M')
+    u_boot_utils.run_and_log(cons, f'sfdisk {fname}',
                              stdin=spec.encode('utf-8'))
     return fname, mnt
 
-def mount_image(cons, fname, mnt, fstype):
-    """Create a filesystem and mount it on partition 1
-
-    Args:
-        cons (ConsoleBase): Console to use
-        fname (str): Filename of MMC image
-        mnt (str): Directory name of 'mnt' directory
-        fstype (str): Filesystem type ('vfat' or 'ext4')
-
-    Returns:
-        str: Name of loop device used
-    """
-    out = u_boot_utils.run_and_log(cons, f'sudo losetup --show -f -P {fname}')
-    loop = out.strip()
-    part = f'{loop}p1'
-    u_boot_utils.run_and_log(cons, f'sudo mkfs.{fstype} {part}')
-    opts = ''
-    if fstype == 'vfat':
-        opts += f' -o uid={os.getuid()},gid={os.getgid()}'
-    u_boot_utils.run_and_log(cons, f'sudo mount -o loop {part} {mnt}{opts}')
-    u_boot_utils.run_and_log(cons, f'sudo chown {getpass.getuser()} {mnt}')
-    return loop
-
 def copy_prepared_image(cons, devnum, fname, basename='mmc'):
     """Use a prepared image since we cannot create one
 
@@ -102,13 +78,8 @@ def setup_bootmenu_image(cons):
     mmc_dev = 4
     fname, mnt = setup_image(cons, mmc_dev, 0x83)
 
-    loop = None
-    mounted = False
     complete = False
     try:
-        loop = mount_image(cons, fname, mnt, 'ext4')
-        mounted = True
-
         script = '''# DO NOT EDIT THIS FILE
 #
 # Please edit /boot/armbianEnv.txt to set supported parameters
@@ -212,15 +183,16 @@ booti ${kernel_addr_r} ${ramdisk_addr_r} ${fdt_addr_r}
             cons, f'echo here {kernel} {symlink}')
         os.symlink(kernel, symlink)
 
+        fsfile = 'ext18M.img'
+        u_boot_utils.run_and_log(cons, f'fallocate -l 18M {fsfile}')
+        u_boot_utils.run_and_log(cons, f'mkfs.ext4 {fsfile} -d {mnt}')
+        u_boot_utils.run_and_log(cons, f'dd if={fsfile} of={fname} bs=1M seek=1')
         complete = True
-
     except ValueError as exc:
         print(f'Falled to create image, failing back to prepared copy: {exc}')
     finally:
-        if mounted:
-            u_boot_utils.run_and_log(cons, f'sudo umount --lazy {mnt}')
-        if loop:
-            u_boot_utils.run_and_log(cons, f'sudo losetup -d {loop}')
+        u_boot_utils.run_and_log(cons, f'rm -rf {mnt}')
+        u_boot_utils.run_and_log(cons, f'rm -f {fsfile}')
 
     if not complete:
         copy_prepared_image(cons, mmc_dev, fname)
@@ -230,13 +202,8 @@ def setup_bootflow_image(cons):
     mmc_dev = 1
     fname, mnt = setup_image(cons, mmc_dev, 0xc, second_part=True)
 
-    loop = None
-    mounted = False
     complete = False
     try:
-        loop = mount_image(cons, fname, mnt, 'vfat')
-        mounted = True
-
         vmlinux = 'vmlinuz-5.3.7-301.fc31.armv7hl'
         initrd = 'initramfs-5.3.7-301.fc31.armv7hl.img'
         dtbdir = 'dtb-5.3.7-301.fc31.armv7hl'
@@ -274,19 +241,21 @@ label Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)
         dtb_file = os.path.join(mnt, f'{dtbdir}/sandbox.dtb')
         u_boot_utils.run_and_log(
             cons, f'dtc -o {dtb_file}', stdin=b'/dts-v1/; / {};')
+
+        fsfile = 'vfat18M.img'
+        u_boot_utils.run_and_log(cons, f'fallocate -l 18M {fsfile}')
+        u_boot_utils.run_and_log(cons, f'mkfs.vfat {fsfile}')
+        u_boot_utils.run_and_log(cons, ['sh', '-c', f'mcopy -i {fsfile} {mnt}/* ::/'])
+        u_boot_utils.run_and_log(cons, f'dd if={fsfile} of={fname} bs=1M seek=1')
         complete = True
     except ValueError as exc:
         print(f'Falled to create image, failing back to prepared copy: {exc}')
     finally:
-        if mounted:
-            u_boot_utils.run_and_log(cons, f'sudo umount --lazy {mnt}')
-        if loop:
-            u_boot_utils.run_and_log(cons, f'sudo losetup -d {loop}')
-
+        u_boot_utils.run_and_log(cons, f'rm -rf {mnt}')
+        u_boot_utils.run_and_log(cons, f'rm -f {fsfile}')
     if not complete:
         copy_prepared_image(cons, mmc_dev, fname)
 
-
 def setup_cros_image(cons):
     """Create a 20MB disk image with ChromiumOS partitions"""
     Partition = collections.namedtuple('part', 'start,size,name')
@@ -336,8 +305,6 @@ def setup_cros_image(cons):
     mmc_dev = 5
     fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img')
     u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M')
-    #mnt = os.path.join(cons.config.persistent_data_dir, 'mnt')
-    #mkdir_cond(mnt)
     u_boot_utils.run_and_log(cons, f'cgpt create {fname}')
 
     uuid_state = 'ebd0a0a2-b9e5-4433-87c0-68b6b72699c7'
@@ -559,31 +526,30 @@ def setup_efi_image(cons):
     fname, mnt = setup_image(cons, devnum, 0xc, second_part=True,
                              basename=basename)
 
-    loop = None
-    mounted = False
     complete = False
     try:
-        loop = mount_image(cons, fname, mnt, 'ext4')
-        mounted = True
         efi_dir = os.path.join(mnt, 'EFI')
         mkdir_cond(efi_dir)
         bootdir = os.path.join(efi_dir, 'BOOT')
         mkdir_cond(bootdir)
         efi_src = os.path.join(cons.config.build_dir,
-                               f'lib/efi_loader/testapp.efi')
+                            'lib/efi_loader/testapp.efi')
         efi_dst = os.path.join(bootdir, 'BOOTSBOX.EFI')
         with open(efi_src, 'rb') as inf:
             with open(efi_dst, 'wb') as outf:
                 outf.write(inf.read())
+        fsfile = 'vfat18M.img'
+        u_boot_utils.run_and_log(cons, f'fallocate -l 18M {fsfile}')
+        u_boot_utils.run_and_log(cons, f'mkfs.vfat {fsfile}')
+        u_boot_utils.run_and_log(cons, ['sh', '-c', f'mcopy -vs -i {fsfile} {mnt}/* ::/'])
+        u_boot_utils.run_and_log(cons, f'dd if={fsfile} of={fname} bs=1M seek=1')
         complete = True
     except ValueError as exc:
         print(f'Falled to create image, failing back to prepared copy: {exc}')
 
     finally:
-        if mounted:
-            u_boot_utils.run_and_log(cons, 'sudo umount --lazy %s' % mnt)
-        if loop:
-            u_boot_utils.run_and_log(cons, 'sudo losetup -d %s' % loop)
+        u_boot_utils.run_and_log(cons, f'rm -rf {mnt}')
+        u_boot_utils.run_and_log(cons, f'rm -f {fsfile}')
 
     if not complete:
         copy_prepared_image(cons, devnum, fname, basename)
-- 
2.43.0



More information about the U-Boot mailing list