[PATCH 5/6] test: Add a helper class to create disk images
Simon Glass
sjg at chromium.org
Wed Feb 25 17:23:49 CET 2026
From: Simon Glass <simon.glass at canonical.com>
Provide a way to create disk images which consist of multiple filesystem
images.
Provide an option to use the current directory for images, instead of
persistent-data directory. This makes it easy for C tests to locate the
image.
Signed-off-by: Simon Glass <simon.glass at canonical.com>
---
test/py/tests/fs_helper.py | 97 ++++++++++++++++++++++++++++++++++++++
1 file changed, 97 insertions(+)
diff --git a/test/py/tests/fs_helper.py b/test/py/tests/fs_helper.py
index 531974d7dbe..f27a3945aa9 100644
--- a/test/py/tests/fs_helper.py
+++ b/test/py/tests/fs_helper.py
@@ -157,6 +157,103 @@ class FsHelper:
self.cleanup()
+class DiskHelper:
+ """Helper class for creating disk images containing filesystems
+
+ Usage:
+ with DiskHelper(ubman.config, 0, 'mmc') as img, \
+ FsHelper(ubman.config, 'ext4', 1, 'mmc') as fsh:
+ # Write files to fsh.srcdir
+ ...
+
+ # Create the filesystem
+ fsh.mk_fs()
+
+ # Add this filesystem to the disk
+ img.add_fs(fsh, DiskHelper.VFAT)
+
+ # Add more filesystems as needed (add another 'with' clause)
+ ...
+
+ # Get the final disk image
+ data = img.create()
+ """
+
+ # Partition-type codes
+ VFAT = 0xc
+ EXT4 = 0x83
+
+ def __init__(self, config, devnum, prefix, cur_dir=False):
+ """Set up a new disk image
+
+ Args:
+ config (u_boot_config): U-Boot configuration
+ devnum (int): Device number (for filename)
+ prefix (str): Prefix string of volume's file name
+ cur_dir (bool): True to put the file in the current directory,
+ instead of the persistent-data directory
+ """
+ self.fs_list = []
+ self.fname = os.path.join('' if cur_dir else config.persistent_data_dir,
+ f'{prefix}{devnum}.img')
+ self.remove_img = True
+ self._do_cleanup = False
+
+ def add_fs(self, fs_img, part_type, bootable=False):
+ """Add a new filesystem
+
+ Args:
+ fs_img (FsHelper): Filesystem to add
+ part_type (DiskHelper.VFAT or DiskHelper.EXT4): Partition type
+ bootable (bool): True to set the 'bootable' flag
+ """
+ self.fs_list.append([fs_img, part_type, bootable])
+
+ def create(self):
+ """Create the disk image
+
+ Create an image with a partition table and the filesystems
+ """
+ spec = ''
+ pos = 1 # Reserve 1MB for the partition table itself
+ for fsi, part_type, bootable in self.fs_list:
+ if spec:
+ spec += '\n'
+ spec += f'type={part_type:x}, size={fsi.size_mb}M, start={pos}M'
+ if bootable:
+ spec += ', bootable'
+ pos += fsi.size_mb
+
+ img_size = pos
+ try:
+ check_call(f'qemu-img create {self.fname} {img_size}M', shell=True)
+ self._do_cleanup = True
+ check_call(f'printf "{spec}" | sfdisk {self.fname}', shell=True)
+ except CalledProcessError:
+ os.remove(self.fname)
+ raise
+
+ pos = 1 # Reserve 1MB for the partition table itself
+ for fsi, part_type, bootable in self.fs_list:
+ check_call(
+ f'dd if={fsi.fs_img} of={self.fname} bs=1M seek={pos} conv=notrunc',
+ shell=True)
+ pos += fsi.size_mb
+ return self.fname
+
+ def cleanup(self):
+ """Remove created file"""
+ if self._do_cleanup:
+ os.remove(self.fname)
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, extype, value, traceback):
+ if self.remove_img:
+ self.cleanup()
+
+
def mk_fs(config, fs_type, size, prefix, src_dir=None, size_gran = 0x100000,
fs_img=None, quiet=False):
"""Create a file system volume
--
2.43.0
More information about the U-Boot
mailing list