builder_peppermint_void/builder/core/install_kernel.py
2025-04-28 08:30:23 +00:00

117 lines
5.6 KiB
Python

# -*- coding: utf-8 -*-
"""
SPDX-FileCopyrightText: 2023-2025 PeppermintOS Team
(peppermintosteam@proton.me)
SPDX-License-Identifier: GPL-3.0-or-later
This module provides a function to install the specified kernel package within different
environments of the PeppermintOS build process, such as the root filesystem, the pep-target,
or the pep-host, using the host-execution strategy. It reads kernel package information
from a YAML configuration file, handling architecture-specific package names, and
uses the XBPS package manager for installation.
Credits:
- PeppermintOS Team (peppermintosteam@proton.me) - Development and maintenance of the project.
License:
This code is distributed under the GNU General Public License version 3 or later (GPL-3.0-or-later).
For more details, please refer to the LICENSE file included in the project or visit:
https://www.gnu.org/licenses/gpl-3.0.html
"""
import os
import sys
import logging
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.insert(0, BASE_DIR)
try:
from builder.core.command_runner import run_command
from builder.configs import logger_config
from builder.core.bootstrap import bootstrap
logger = logger_config.setup_logger('install_kernel')
except ImportError as e:
try:
basic_logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.ERROR)
basic_logger.error(f"Error importing necessary modules for install_kernel: {e}. Ensure your environment is set up correctly.")
except Exception:
print(f"Error importing necessary modules for install_kernel: {e}. Ensure your environment is set up correctly.")
sys.exit(1)
def install_kernel(arch, kernel_type, kernels_config, target_env='rootfs', paths=None, host_arch=None, repositories_data=None):
"""
Installs the specified kernel in the target environment (rootfs, pep-target, pep-host),
using the host-execution strategy and handling architecture-specific package names.
Args:
arch (str): Architecture (e.g., 'aarch64').
kernel_type (str): Kernel type (e.g., 'current', 'lts', 'mainline').
kernels_config (dict): Kernel configuration loaded from the YAML file.
Expected structure: {kernels: {<kernel_type>: {package_names: {<architecture>: package_name_string}}}}
target_env (str): Target environment to install the kernel in ('rootfs', 'pep-target', 'pep-host').
Defaults to 'rootfs'.
paths (dict): Dictionary of build paths. Required to get target path and cache dir.
host_arch (str): The architecture of the host system.
repositories_data (list): List of repository dictionaries from YAML config.
Expected structure: [{'name': '...', 'uri': '...', 'architectures': [...]}, ...]
Raises:
subprocess.CalledProcessError: If the xbps-install command fails.
ValueError: If configuration data is missing or incorrect.
KeyError: If required paths are not found.
"""
logger.info(f"=> Starting kernel installation: {kernel_type} for {arch} in environment: {target_env}...")
if paths is None or not isinstance(paths, dict):
logger.error("Paths dictionary is missing or incorrect.")
raise ValueError("Paths dictionary is required.")
kernel_package_name_string = None
if kernels_config and 'kernels' in kernels_config and kernel_type in kernels_config['kernels']:
kernel_info_for_type = kernels_config['kernels'][kernel_type]
if isinstance(kernel_info_for_type, dict):
package_names_dict = kernel_info_for_type.get('package_names', {})
if isinstance(package_names_dict, dict):
kernel_package_name_string = package_names_dict.get(arch)
if kernel_package_name_string:
logger.info(f"=> Selected Void Linux kernel package name string (YAML) for {arch} / {kernel_type}: '{kernel_package_name_string}'")
kernel_packages_list = kernel_package_name_string.split()
else:
default_package_name_string = "linux"
logger.warning(f"Kernel type '{kernel_type}' or architecture '{arch}' not found in 'package_names' section of the YAML configuration. Using '{default_package_name_string}' as default package name string.")
kernel_packages_list = [default_package_name_string]
if not kernel_packages_list:
logger.error(f"Kernel package list is empty even after fallback. Cannot install kernel.")
raise ValueError("Kernel package list is not defined.")
adapted_package_structure = {target_env: {arch: kernel_packages_list}}
kernel_packages_args = bootstrap.construct_xbps_install_args(
target_env,
arch,
repositories_data,
adapted_package_structure
)
if not kernel_packages_args:
logger.warning("Skipping kernel installation due to failure in constructing xbps-install arguments.")
else:
kernel_install_command = ["/usr/bin/xbps-install"] + kernel_packages_args
logger.info(f"Executing kernel installation command (host) [TARGETED AT: {target_env}]: {' '.join(kernel_install_command)}")
try:
run_command(kernel_install_command)
logger.info(f"=> Kernel installed successfully in the environment: {target_env}.")
except Exception as e:
logger.error(f"Error installing the kernel in the environment: {target_env}: {e}")
raise
logger.info(f"=> Kernel installation: {kernel_type} ({target_env}) completed.")
logger.info(f"--------------------------------------------------------------------\n")