builder_peppermint_void/builder/core/install_kernel.py
2025-04-25 12:38:42 +00:00

123 lines
4.8 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. It reads kernel package information from a YAML configuration file and
uses the XBPS package manager for installation.
The module allows for flexibility in choosing the kernel type (e.g., current, lts, mainline)
and handles cases where the specified kernel type is not found in the configuration.
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 subprocess
import yaml
import logging
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, BASE_DIR)
try:
from builder.core.xbps_commands import xbps_commands
from builder.core.command_runner import run_command
from builder.core.bootstrap.paths import paths
from builder.configs import logger_config
logger = logger_config.setup_logger('install_kernel')
except ImportError as e:
print(f"Error importing necessary modules: {e}. Ensure your environment is set up correctly.")
sys.exit(1)
def install_kernel(arch, kernel_type, kernels_config, target_env='rootfs'):
"""
Installs the specified kernel in the target environment (rootfs, pep-target, pep-host).
Args:
arch (str): Architecture (e.g., 'x86_64').
kernel_type (str): Kernel type (e.g., 'current', 'lts', 'mainline').
kernels_config (dict): Kernel configuration loaded from the YAML file.
target_env (str): Target environment to install the kernel in ('rootfs', 'pep-target', 'pep-host').
Defaults to 'rootfs'.
"""
logger.info(f"=> Starting kernel installation: {kernel_type} in environment: {target_env}...")
logger.info(f"=> Installing kernel: {kernel_type} ({target_env})")
if target_env == 'rootfs':
target_path = paths['ROOTFS']
elif target_env == 'pep-host':
target_path = paths['PEPHOSTDIR']
elif target_env == 'pep-target':
target_path = paths['PEPTARGETDIR']
else:
logger.error(f"Invalid target environment for kernel installation: {target_env}. Installing in ROOTFS by default.")
target_path = paths['ROOTFS']
target_env = 'rootfs'
kernel_package_name = None
if kernels_config and 'kernels' in kernels_config and kernel_type in kernels_config['kernels']:
kernel_info = kernels_config['kernels'][kernel_type]
kernel_package_name = kernel_info.get('package_name')
if kernel_package_name:
logger.info(f"=> Selected Void Linux kernel package name (YAML): {kernel_package_name}")
else:
kernel_package_name = "linux"
logger.warning(f"Kernel type '{kernel_type}' not found in the YAML configuration. Installing kernel 'linux' by default.")
if not kernel_package_name:
kernel_package_name = "linux"
logger.error(f"Kernel package name not defined, even after checking YAML. Using 'linux' as fallback.")
kernel_packages_list = kernel_package_name.split()
repos_yaml_file = paths['REPOS_YAML_FILE']
try:
with open(repos_yaml_file, 'r') as f:
repo_data = yaml.safe_load(f)
except FileNotFoundError:
logger.error(f"Repository YAML file not found: {repos_yaml_file}")
return
except yaml.YAMLError as e:
logger.error(f"Error reading the repository YAML file: {repos_yaml_file} - {e}")
return
repositories = repo_data.get('repositories', [])
xbps_install_command = [
xbps_commands["XBPS_INSTALL_CMD"], '-S', '-y',
'-r', target_path,
'-c', os.path.join(paths["ISO_CACHE_DIR"], arch)
]
for repo_dict in repositories:
if arch in repo_dict.get('architectures', []):
repo_url = repo_dict['uri']
xbps_install_command.extend(['-R', repo_url])
xbps_install_command.extend(kernel_packages_list)
command_str = " ".join(xbps_install_command)
logger.info(f"=> Executing XBPS_INSTALL_CMD for kernel [TARGETED AT: {target_path}] (Environment: {target_env}): {command_str}")
try:
run_command(xbps_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