#!/bin/bash # Common functions and utilities for bootc image creation modules # This module provides shared functionality used across all other modules set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Logging functions log_info() { echo -e "${GREEN}[INFO]${NC} $1" } log_warn() { echo -e "${YELLOW}[WARN]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } log_debug() { echo -e "${BLUE}[DEBUG]${NC} $1" } # Global variables (will be set by calling script) WORK_DIR="" loop_dev="" # Cleanup function cleanup() { if [ -n "$WORK_DIR" ] && [ -d "$WORK_DIR" ]; then log_info "Cleaning up work directory..." sudo umount "$WORK_DIR/mnt" 2>/dev/null || true sudo losetup -d "$loop_dev" 2>/dev/null || true sudo rm -rf "$WORK_DIR" fi } # Set trap for cleanup setup_cleanup() { trap cleanup EXIT INT TERM } # Check prerequisites check_prerequisites() { log_info "Checking prerequisites..." local required_commands=("podman" "qemu-img" "parted" "losetup" "mkfs.ext4" "grub-install" "grub-mkconfig") for cmd in "${required_commands[@]}"; do if ! command -v "$cmd" >/dev/null 2>&1; then log_error "Required command '$cmd' not found" exit 1 fi done # Check if container image exists if ! podman image exists "$CONTAINER_IMAGE"; then log_error "Container image '$CONTAINER_IMAGE' not found" log_info "Please build the image first with: podman build -t $CONTAINER_IMAGE -f debian_bootc_dockerfile.txt ." exit 1 fi log_info "All prerequisites satisfied" } # Create output directory create_output_dir() { log_info "Creating output directory..." mkdir -p "$OUTPUT_DIR" } # Create work directory create_work_dir() { WORK_DIR="./work-$$" log_info "Creating work directory: $WORK_DIR" mkdir -p "$WORK_DIR" } # Validate configuration validate_config() { if [ -z "$CONTAINER_IMAGE" ]; then log_error "CONTAINER_IMAGE not set" exit 1 fi if [ -z "$OUTPUT_DIR" ]; then log_error "OUTPUT_DIR not set" exit 1 fi if [ -z "$IMAGE_SIZE_GB" ]; then log_error "IMAGE_SIZE_GB not set" exit 1 fi log_debug "Configuration validated successfully" } # Show progress show_progress() { local step="$1" local total="$2" local current="$3" local percentage=$((current * 100 / total)) local bar_length=30 local filled=$((bar_length * current / total)) local empty=$((bar_length - filled)) printf "\r[" printf "%${filled}s" | tr ' ' '#' printf "%${empty}s" | tr ' ' '-' printf "] %d%% - %s" "$percentage" "$step" if [ "$current" -eq "$total" ]; then echo fi } # Check disk space check_disk_space() { # Get the directory where the main script is located local script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" local required_space=$((IMAGE_SIZE_GB * 1024 * 1024 * 1024)) # Convert GB to bytes local available_space=$(df "$script_dir" | awk 'NR==2 {print $4}') # Convert available space to GB for comparison local available_gb=$((available_space / 1024 / 1024 / 1024)) log_debug "Disk space check: Required: ${IMAGE_SIZE_GB}GB, Available: ${available_gb}GB" if [ "$available_gb" -lt "$IMAGE_SIZE_GB" ]; then log_error "Insufficient disk space. Required: ${IMAGE_SIZE_GB}GB, Available: ${available_gb}GB" exit 1 fi log_debug "Disk space check passed" } # Wait for user confirmation confirm_action() { local message="$1" local default="${2:-n}" if [ "$default" = "y" ]; then read -p "$message [Y/n]: " -r if [[ $REPLY =~ ^[Nn]$ ]]; then return 1 fi else read -p "$message [y/N]: " -r if [[ ! $REPLY =~ ^[Yy]$ ]]; then return 1 fi fi return 0 } # Print module usage print_module_usage() { local module_name="$1" echo "Usage: source modules/$module_name.sh" echo "This module provides common functionality for bootc image creation." echo "" echo "Available functions:" echo " log_info, log_warn, log_error, log_debug - Logging functions" echo " cleanup, setup_cleanup - Cleanup management" echo " check_prerequisites - Validate system requirements" echo " create_output_dir, create_work_dir - Directory management" echo " validate_config - Configuration validation" echo " show_progress - Progress display" echo " check_disk_space - Disk space validation" echo " confirm_action - User confirmation prompts" }