From fa0bde74203881b2d28cfcdfc3be0d2c5b42b424 Mon Sep 17 00:00:00 2001 From: Alexander Kanavin Date: Tue, 5 May 2026 17:58:04 +0200 Subject: [PATCH 1/2] elbepack/hdimg: avoid copying filesystem content with cp when possible Some filesystem types support population of the filesystem directly with respective mkfs command (the specific command line argument is not standardized though). For others like f2fs, a separate command needs to be executed. Both options can be extended further if/when needed. Signed-off-by: Alexander Kanavin --- elbepack/fstab.py | 22 ++++++++++++++++++++++ elbepack/hdimg.py | 12 ++++-------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/elbepack/fstab.py b/elbepack/fstab.py index dc061d321..6cee0d163 100644 --- a/elbepack/fstab.py +++ b/elbepack/fstab.py @@ -155,3 +155,25 @@ def get_label_opt(self): if self.fstype == 'f2fs': return '-l ' + self.label return '' + + def mkfs(self, loopdev, filesystem_tree): + mkfs_fs_copy_argument_dict = { + 'ext2': f'-d {filesystem_tree}', + 'ext3': f'-d {filesystem_tree}', + 'ext4': f'-d {filesystem_tree}', + 'btrfs': f'-r {filesystem_tree}', + 'xfs': f'-p {filesystem_tree}' + } + mkfs_fs_copy_extra_cmd_dict = {'f2fs': f'sload.f2fs -f {filesystem_tree} {loopdev}'} + mkfs_supports_fs_copy = self.fstype in mkfs_fs_copy_argument_dict or \ + self.fstype in mkfs_fs_copy_extra_cmd_dict + + mkfs_fs_copy_argument = mkfs_fs_copy_argument_dict.get(self.fstype, '') + do( + f'mkfs.{self.fstype} {" ".join(self.mkfsopts)} {self.get_label_opt()} ' + f'{mkfs_fs_copy_argument} ' + f'{loopdev}') + if self.fstype in mkfs_fs_copy_extra_cmd_dict: + do(mkfs_fs_copy_extra_cmd_dict[self.fstype]) + + return not mkfs_supports_fs_copy diff --git a/elbepack/hdimg.py b/elbepack/hdimg.py index cde64a25d..315035310 100644 --- a/elbepack/hdimg.py +++ b/elbepack/hdimg.py @@ -304,9 +304,8 @@ def create_label(disk, part, ppart, fslabel, target, grub): grub.add_fs_entry(entry) with entry.losetup() as loopdev: - do( - f'mkfs.{entry.fstype} {" ".join(entry.mkfsopts)} {entry.get_label_opt()} ' - f'{loopdev}') + filesystem_tree = os.path.join(target, 'filesystems', entry.id) + '/.' + needs_cp = entry.mkfs(loopdev, filesystem_tree) _execute_fs_commands(entry.fs_device_commands, dict(device=loopdev)) @@ -314,11 +313,8 @@ def create_label(disk, part, ppart, fslabel, target, grub): with mount(loopdev, mount_path, options=entry.options, force_writable=True): _execute_fs_commands(entry.fs_path_commands, dict(path=mount_path)) - do([ - 'cp', '-a', - os.path.join(target, 'filesystems', entry.id) + '/.', - str(mount_path) + '/', - ]) + if needs_cp: + do(['cp', '-a', filesystem_tree, str(mount_path) + '/']) return ppart From aa0561d9f94eacf04fdb81d188714a02d4fc7813 Mon Sep 17 00:00:00 2001 From: Alexander Kanavin Date: Mon, 11 May 2026 15:00:57 +0200 Subject: [PATCH 2/2] elbepack/hdimg.py: only mount a partition when operations need to be performed on it Signed-off-by: Alexander Kanavin --- elbepack/hdimg.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/elbepack/hdimg.py b/elbepack/hdimg.py index 315035310..f76e04c32 100644 --- a/elbepack/hdimg.py +++ b/elbepack/hdimg.py @@ -311,10 +311,11 @@ def create_label(disk, part, ppart, fslabel, target, grub): mount_path = Path(target, 'imagemnt') - with mount(loopdev, mount_path, options=entry.options, force_writable=True): - _execute_fs_commands(entry.fs_path_commands, dict(path=mount_path)) - if needs_cp: - do(['cp', '-a', filesystem_tree, str(mount_path) + '/']) + if entry.fs_path_commands or needs_cp: + with mount(loopdev, mount_path, options=entry.options, force_writable=True): + _execute_fs_commands(entry.fs_path_commands, dict(path=mount_path)) + if needs_cp: + do(['cp', '-a', filesystem_tree, str(mount_path) + '/']) return ppart