#!/bin/bash set -e echo "=====================================" echo "= Alpine Zero-OS Cleanup Script =" echo "=====================================" echo "" # Define directories and files to clean OUTPUT_DIR="/build/output" KERNEL_DIR="/build/kernel" CACHE_DIR="/build/cache" BOOT_ROOT="/build/boot" ALPINE_ROOT="/build/initramfs" COMPONENTS_DIR="/build/components" # Function to safely remove directory with size info cleanup_dir() { local dir="$1" local description="$2" if [ -d "$dir" ]; then local size=$(du -sh "$dir" 2>/dev/null | cut -f1 || echo "unknown") echo " Removing $description: $dir ($size)" rm -rf "$dir" else echo " $description: $dir (not found)" fi } # Function to safely remove file with size info cleanup_file() { local file="$1" local description="$2" if [ -f "$file" ]; then local size=$(du -sh "$file" 2>/dev/null | cut -f1 || echo "unknown") echo " Removing $description: $file ($size)" rm -f "$file" else echo " $description: $file (not found)" fi } echo "[+] Cleaning build outputs..." cleanup_dir "$OUTPUT_DIR" "Build outputs" echo "" echo "[+] Cleaning kernel build artifacts..." cleanup_dir "$KERNEL_DIR" "Kernel build directory" echo "" echo "[+] Cleaning cache files..." cleanup_dir "$CACHE_DIR" "Build cache" echo "" echo "[+] Cleaning boot initramfs..." cleanup_dir "$BOOT_ROOT" "Boot filesystem" echo "" echo "[+] Cleaning Alpine initramfs..." cleanup_dir "$ALPINE_ROOT" "Alpine initramfs root" echo "" echo "[+] Cleaning component build artifacts..." # Clean Rust target directories in components if [ -d "$COMPONENTS_DIR" ]; then echo " Scanning components for build artifacts..." # Clean Rust target directories find "$COMPONENTS_DIR" -type d -name "target" 2>/dev/null | while read target_dir; do if [ -d "$target_dir" ]; then local size=$(du -sh "$target_dir" 2>/dev/null | cut -f1 || echo "unknown") echo " Removing Rust target: $target_dir ($size)" rm -rf "$target_dir" fi done # Clean Cargo.lock files (except in git repos where they should be kept) find "$COMPONENTS_DIR" -name "Cargo.lock" -not -path "*/.git/*" 2>/dev/null | while read lock_file; do # Only remove if not in a git repository if ! git -C "$(dirname "$lock_file")" rev-parse --git-dir >/dev/null 2>&1; then echo " Removing Cargo.lock: $lock_file" rm -f "$lock_file" fi done # Clean Go build artifacts find "$COMPONENTS_DIR" -type f -name "go.sum" 2>/dev/null | while read go_sum; do echo " Removing Go sum: $go_sum" rm -f "$go_sum" done # Clean any temporary build files find "$COMPONENTS_DIR" -type f \( -name "*.tmp" -o -name "*.temp" -o -name ".build-*" \) 2>/dev/null | while read temp_file; do echo " Removing temp file: $temp_file" rm -f "$temp_file" done else echo " Components directory not found: $COMPONENTS_DIR" fi echo "" echo "[+] Cleaning Docker build artifacts..." # Clean any Docker build artifacts if docker is available if command -v docker >/dev/null 2>&1; then echo " Removing dangling Docker images..." docker image prune -f >/dev/null 2>&1 || echo " No dangling images to remove" echo " Removing stopped containers..." docker container prune -f >/dev/null 2>&1 || echo " No stopped containers to remove" # Remove specific build containers if they exist for container in alpine-initramfs-builder alpine-cached-builder; do if docker ps -a --format "table {{.Names}}" | grep -q "^${container}$"; then echo " Removing container: $container" docker rm -f "$container" >/dev/null 2>&1 || true fi done else echo " Docker not available, skipping Docker cleanup" fi echo "" echo "[+] Cleaning temporary files..." # Clean any temporary files in the project root cleanup_file "/build/build.conf" "Build configuration" cleanup_file "/build/.build-timestamp" "Build timestamp" # Clean any log files find /build -name "*.log" -type f 2>/dev/null | while read log_file; do echo " Removing log file: $log_file" rm -f "$log_file" done echo "" echo "[+] Cleaning system package cache..." # Clean APK cache if we're in Alpine if command -v apk >/dev/null 2>&1; then echo " Cleaning APK cache..." apk cache clean 2>/dev/null || echo " APK cache already clean" # Remove APK cache directory if it exists and is not needed if [ -d "/var/cache/apk" ]; then local cache_size=$(du -sh "/var/cache/apk" 2>/dev/null | cut -f1 || echo "unknown") if [ "$cache_size" != "0" ] && [ "$cache_size" != "unknown" ]; then echo " Removing APK cache directory: /var/cache/apk ($cache_size)" rm -rf /var/cache/apk/* fi fi fi echo "" echo "[+] Cleanup summary:" echo " The following have been cleaned:" echo " • Build outputs (vmlinuz.efi, initramfs.cpio.xz, etc.)" echo " • Kernel source and build artifacts" echo " • Component build artifacts (Rust target/, Go artifacts)" echo " • Cache files and temporary files" echo " • Docker build artifacts" echo " • System package cache" echo "" echo " The following have been preserved:" echo " • Source code and git repositories" echo " • Configuration files" echo " • Build scripts" echo " • Git submodules (use git submodule update to restore)" echo "" # Show disk space freed echo "[+] Disk space status:" df -h /build 2>/dev/null | tail -1 | awk '{print " Available space: " $4 " (" $5 " used)"}' echo "" echo "[+] Cleanup completed successfully!" echo "" echo "To rebuild the project after cleanup:" echo " 1. Run: ./build.sh # Full rebuild" echo " 2. Or: docker-compose up --build # Docker rebuild" echo " 3. Or: scripts/setup-submodules.sh # Restore submodules first"