From eee4d3f03517fdd85b81dd2cae77a91c864c56e6 Mon Sep 17 00:00:00 2001 From: Manuel Date: Mon, 28 Apr 2025 12:17:18 +0000 Subject: [PATCH] new commit --- builder/core/command_runner.py | 65 ++++++++++++++++++++++++++++++ builder/core/system_reconfigure.py | 2 - builder/iso_builder.py | 9 +++-- 3 files changed, 70 insertions(+), 6 deletions(-) diff --git a/builder/core/command_runner.py b/builder/core/command_runner.py index 2189619e..4522b124 100644 --- a/builder/core/command_runner.py +++ b/builder/core/command_runner.py @@ -57,4 +57,69 @@ def run_command(command, env=None): except FileNotFoundError as e: logger.error(f"Error: Command not found: {command[0] if isinstance(command, list) else command}") logger.error(f"Details: {e}") + raise + +def run_command_in_target(rootfs_path, command, architecture, check=True, shell=False, cwd=None, env=None): + """ + Runs a command within the target root filesystem using chroot. + Sets necessary environment variables for the target architecture. + + Args: + rootfs_path (str): Path to the target root filesystem directory. + command (list): The command and its arguments as a list (command path should be relative to target root). + architecture (str): The target architecture (e.g., 'aarch64'). + check (bool): If True, raise CalledProcessError if the command returns a non-zero exit code. + shell (bool): If True, execute the command through the shell within the chroot. + cwd (str, optional): The current working directory *within the chroot*. (Note: Requires shell=True or `cd` prefix). + env (dict, optional): A dictionary of environment variables to set *inside the chroot*. + XBPS_ARCH is set automatically. + + Returns: + subprocess.CompletedProcess: The result of the command execution. + + Raises: + subprocess.CalledProcessError: If check is True and the command fails. + FileNotFoundError: If the chroot binary or the command within chroot is not found. + ValueError: If rootfs_path is invalid. + """ + if not rootfs_path or not os.path.isdir(rootfs_path): + logger.error(f"Invalid target root filesystem path for chroot: {rootfs_path}") + raise ValueError(f"Invalid target root filesystem path for chroot: {rootfs_path}") + + chroot_env_list = [f"XBPS_ARCH={architecture}"] + if env: + for key, value in env.items(): + if key != 'XBPS_ARCH': + chroot_env_list.append(f"{key}={value}") + + full_command = ["chroot", rootfs_path, "env"] + chroot_env_list + command + + logger.info(f"Executing command IN TARGET ({rootfs_path}): {' '.join(full_command)}") + + try: + result = subprocess.run( + full_command, + capture_output=True, + text=True, + check=check, + shell=shell, + ) + + logger.debug(f"STDOUT:\n{result.stdout.strip()}") + if result.stderr.strip(): + logger.debug(f"STDERR:\n{result.stderr.strip()}") + + return result + + except FileNotFoundError: + logger.error(f"Command not found in target chroot or chroot binary failed: {command[0] if command else 'N/A'}. Ensure base system is correctly installed.") + raise + except subprocess.CalledProcessError as e: + logger.error(f"Command failed in target with exit code {e.returncode}") + logger.error(f"Command: {' '.join(e.cmd)}") + logger.error(f"Stderr:\n{e.stderr.strip()}") + logger.error(f"Stdout:\n{e.stdout.strip()}") + raise + except Exception as e: + logger.error(f"An unexpected error occurred during command execution in target: {e}") raise \ No newline at end of file diff --git a/builder/core/system_reconfigure.py b/builder/core/system_reconfigure.py index da42cfb4..c3c8e4c9 100644 --- a/builder/core/system_reconfigure.py +++ b/builder/core/system_reconfigure.py @@ -110,8 +110,6 @@ def reconfigure_system_in_chroot(rootfs_path, architecture, repositories_list, b result_dkms_check = run_command(check_dkms_cmd) if result_dkms_check.returncode == 0: logger.info("=> DKMS found. Forcing DKMS reconfiguration...") - - # *** ADD LOGGING TO LIST DKMS PACKAGE FILES *** list_dkms_files_cmd = [xbps_commands["XBPS_QUERY_CMD"], '-r', rootfs_path, '-f', 'dkms'] logger.info(f"=> Listing files installed by the dkms package inside the chroot...") result_list_dkms_files = run_command(list_dkms_files_cmd) diff --git a/builder/iso_builder.py b/builder/iso_builder.py index d6964fa5..82c0405e 100644 --- a/builder/iso_builder.py +++ b/builder/iso_builder.py @@ -248,10 +248,11 @@ def iso_builder_main( logger.info("=> Starting system reconfiguration in chroot...") reconfigure_system_in_chroot( - paths['ROOTFS'], - architecture, - host_arch, - repositories, + rootfs_path=paths['ROOTFS'], + target_architecture=architecture, + repositories_data=repositories, + paths=paths, + locale="en_US.UTF-8" ) logger.info("=> System reconfiguration in chroot COMPLETE.")