#!/bin/bash # build-with-cache.sh - Enhanced build script with apt-cacher-ng support set -euo pipefail # Configuration IMAGE_NAME="${CONTAINER_IMAGE:-localhost/debian-bootc:ostree-transformed-v2}" IMAGE_TAG="latest" OUTPUT_DIR="./output" BUILD_DIR="." # apt-cacher-ng configuration APT_CACHER_NG_URL="${APT_CACHER_NG_URL:-}" APT_CACHER_NG_DISCOVERY=true # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color 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" } # Detect apt-cacher-ng automatically detect_apt_cacher_ng() { if [ -n "$APT_CACHER_NG_URL" ]; then log_info "Using apt-cacher-ng from environment: $APT_CACHER_NG_URL" return 0 fi if [ "$APT_CACHER_NG_DISCOVERY" = true ]; then log_info "Auto-detecting apt-cacher-ng..." # Common apt-cacher-ng URLs to try local urls=( "http://192.168.1.101:3142" "http://localhost:3142" "http://apt-cacher-ng:3142" "http://cache:3142" "http://192.168.1.100:3142" ) for url in "${urls[@]}"; do if curl -s --connect-timeout 5 "$url/acng-report.html" > /dev/null 2>&1; then APT_CACHER_NG_URL="$url" log_info "Found apt-cacher-ng at: $APT_CACHER_NG_URL" return 0 fi done log_warn "No apt-cacher-ng found, using direct connections" return 1 fi return 1 } # Check prerequisites check_prerequisites() { log_info "Checking prerequisites..." local missing_tools=() for tool in podman buildah qemu-img curl; do if ! command -v "$tool" &> /dev/null; then missing_tools+=("$tool") fi done if [ ${#missing_tools[@]} -ne 0 ]; then log_error "Missing required tools: ${missing_tools[*]}" log_info "Install them with:" log_info " sudo apt install podman buildah qemu-utils curl" exit 1 fi log_info "All prerequisites satisfied." } # Create build directory structure setup_build_environment() { log_info "Setting up build environment..." mkdir -p "$OUTPUT_DIR" # Check if Dockerfile exists in current directory if [ ! -f "debian_bootc_dockerfile.txt" ]; then log_error "debian_bootc_dockerfile.txt not found in current directory" exit 1 fi log_info "Using existing debian_bootc_dockerfile.txt" # Create the compatibility script cat > "$BUILD_DIR/bootc-compat.sh" << 'EOF' #!/bin/bash set -euo pipefail if [ ! -d "/ostree/repo/objects" ]; then echo "Initializing ostree repository..." ostree admin init-fs / || true ostree admin stateroot-init debian || true fi setup_boot() { echo "Setting up boot configuration..." if command -v bootctl &> /dev/null; then bootctl install --path=/boot || true fi if command -v update-grub &> /dev/null; then update-grub || true fi } setup_deployment() { echo "Setting up deployment structure..." mkdir -p /var/lib/containers /etc/containers /usr/lib/bootc cat > /usr/lib/bootc/status.json << EOJSON { "apiVersion": "org.containers.bootc/v1alpha1", "kind": "BootcHost", "spec": { "image": { "image": "localhost/debian-bootc:latest" } }, "status": { "booted": { "image": { "image": "localhost/debian-bootc:latest" } } } } EOJSON } main() { echo "Starting Debian bootc compatibility layer..." setup_deployment setup_boot echo "Bootc compatibility setup complete." if [ "${1:-}" = "--init" ]; then exec /sbin/init else exec "$@" fi } main "$@" EOF # Create configuration file cat > "$BUILD_DIR/config.toml" << 'EOF' [[users]] name = "debian" password = "$6$salt123$zKz/hA0eCEjVpNJC4T.nD/OXucJwPh5z9BRNDguk3EAU4isYPV3hEppKfCgZ/XZnSDdk2jxngc05ejbo.FLxV." groups = ["wheel", "sudo"] [customizations] hostname = "debian-bootc" [customizations.kernel] append = "console=ttyS0,115200n8 console=tty0" [customizations.disk] root_fs_type = "ext4" [customizations.services] enabled = ["sshd", "systemd-resolved"] EOF log_info "Build environment ready in $BUILD_DIR" } # Build container image with apt-cacher-ng support build_container_image() { log_info "Building container image with apt-cacher-ng support..." local build_args="" if [ -n "$APT_CACHER_NG_URL" ]; then build_args="--build-arg APT_CACHER_NG_URL=$APT_CACHER_NG_URL" log_info "Building with apt-cacher-ng: $APT_CACHER_NG_URL" else log_info "Building without apt-cacher-ng (direct connections)" fi # Extract image name without tag for podman build local image_name_only=$(echo "$IMAGE_NAME" | cut -d':' -f1) podman build $build_args -t "$image_name_only" -f debian_bootc_dockerfile.txt . log_info "Container image built: $image_name_only" } # Create QCOW2 image using bootc-image-builder create_qcow2_image() { log_info "Creating QCOW2 image using bootc-image-builder..." # Check if bootc-image-builder is available if ! podman image exists quay.io/centos-bootc/bootc-image-builder; then log_info "Pulling bootc-image-builder..." podman pull quay.io/centos-bootc/bootc-image-builder fi # Create the bootable image sudo podman run --rm -it --privileged \ -v ./output:/output \ -v ./build/config.toml:/config/config.toml:ro \ -v /var/lib/containers/storage:/var/lib/containers/storage \ quay.io/centos-bootc/bootc-image-builder \ --type qcow2 \ localhost/debian-bootc:latest log_info "QCOW2 image created successfully!" } # Export container image export_container_image() { log_info "Exporting container image..." podman save -o "$OUTPUT_DIR/debian-bootc-container.tar" "$IMAGE_NAME:$IMAGE_TAG" log_info "Container image exported to $OUTPUT_DIR/debian-bootc-container.tar" } # Show build information show_build_info() { log_info "=== Build Information ===" log_info "Image: $IMAGE_NAME:$IMAGE_TAG" log_info "Output Directory: $OUTPUT_DIR" log_info "Build Directory: $BUILD_DIR" if [ -n "$APT_CACHER_NG_URL" ]; then log_info "apt-cacher-ng: $APT_CACHER_NG_URL" log_info "Cache Status: Enabled" else log_info "apt-cacher-ng: Not configured" log_info "Cache Status: Disabled (direct connections)" fi log_info "========================" } # Main execution main() { log_info "Starting Debian bootc container build with apt-cacher-ng support..." # Detect apt-cacher-ng detect_apt_cacher_ng # Show build information show_build_info check_prerequisites setup_build_environment build_container_image log_info "Container build process completed!" if [ -n "$APT_CACHER_NG_URL" ]; then log_info "Container built with apt-cacher-ng caching enabled!" log_info "Cache URL: $APT_CACHER_NG_URL" fi } # Run main function main "$@"