From 653c75153596717bdc8ccf158aa63d0de64fadac Mon Sep 17 00:00:00 2001 From: Jan De Landtsheer Date: Sat, 16 Aug 2025 00:51:19 +0200 Subject: [PATCH] Lots of fixes details details details --- build/Dockerfile.cached | 1 + configs/packages-alpine.txt | 1 + scripts/build-boot.sh | 4 +- scripts/build-initramfs.sh | 2 +- scripts/build-kernel.sh | 2 +- scripts/build-smart.sh | 2 +- scripts/cleanup.sh | 180 ++++++++++++++++++++++++++++ scripts/compile-components.sh | 63 ++++++++-- scripts/fetch-github.sh | 2 +- scripts/install-firmware-minimal.sh | 2 +- scripts/install-packages.sh | 2 +- scripts/setup-initramfs.sh | 4 +- scripts/setup-permissions.sh | 2 +- scripts/setup-submodules.sh | 2 +- 14 files changed, 246 insertions(+), 23 deletions(-) create mode 100755 scripts/cleanup.sh diff --git a/build/Dockerfile.cached b/build/Dockerfile.cached index 8c231f2..d10df76 100644 --- a/build/Dockerfile.cached +++ b/build/Dockerfile.cached @@ -33,6 +33,7 @@ RUN apk add --no-cache \ grep \ findutils \ jq \ + diffutils \ # Go for source compilation go \ # Rustup for proper Rust musl builds diff --git a/configs/packages-alpine.txt b/configs/packages-alpine.txt index 3c038ac..4107eab 100644 --- a/configs/packages-alpine.txt +++ b/configs/packages-alpine.txt @@ -15,6 +15,7 @@ findutils grep sed coreutils +diffutils # Crypto and certificates openssl diff --git a/scripts/build-boot.sh b/scripts/build-boot.sh index 15c2761..977cf8a 100755 --- a/scripts/build-boot.sh +++ b/scripts/build-boot.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -e BOOT_ROOT="/build/boot" @@ -72,7 +72,7 @@ chmod 600 "$BOOT_ROOT/dev/console" echo "[+] Creating boot init script..." # Create minimal init script for mounting Alpine runtime cat > "$BOOT_ROOT/init" << 'EOF' -#!/bin/sh +#!/bin/bash # Minimal boot init - establish network and mount Alpine runtime echo "[BOOT] Zero-OS minimal boot starting..." diff --git a/scripts/build-initramfs.sh b/scripts/build-initramfs.sh index 7d81a89..f6478d0 100755 --- a/scripts/build-initramfs.sh +++ b/scripts/build-initramfs.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -e echo "=====================================" diff --git a/scripts/build-kernel.sh b/scripts/build-kernel.sh index 3a029fd..5be3c07 100755 --- a/scripts/build-kernel.sh +++ b/scripts/build-kernel.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -e KERNEL_DIR="/build/kernel" diff --git a/scripts/build-smart.sh b/scripts/build-smart.sh index c04b4d3..6a1db9e 100755 --- a/scripts/build-smart.sh +++ b/scripts/build-smart.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -e CACHE_DIR="/build/cache" diff --git a/scripts/cleanup.sh b/scripts/cleanup.sh new file mode 100755 index 0000000..678d910 --- /dev/null +++ b/scripts/cleanup.sh @@ -0,0 +1,180 @@ +#!/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" \ No newline at end of file diff --git a/scripts/compile-components.sh b/scripts/compile-components.sh index 80c218f..19d6164 100755 --- a/scripts/compile-components.sh +++ b/scripts/compile-components.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -e ALPINE_ROOT="/build/initramfs" @@ -56,14 +56,6 @@ build_rust_component() { # Source cargo environment . ~/.cargo/env - # Set git version for components that use git_version macro - # Many git_version crates check these environment variables first - export VERGEN_GIT_DESCRIBE=$(cd "$COMPONENTS_DIR/$component_name" && git describe --tags --always --dirty=-modified 2>/dev/null || echo "unknown") - export GIT_VERSION=$(cd "$COMPONENTS_DIR/$component_name" && git describe --tags --always --dirty=-modified 2>/dev/null || echo "unknown") - export VERGEN_GIT_BRANCH=$(cd "$COMPONENTS_DIR/$component_name" && git branch --show-current 2>/dev/null || echo "main") - export VERGEN_GIT_COMMIT_TIMESTAMP=$(cd "$COMPONENTS_DIR/$component_name" && git log -1 --format=%ct 2>/dev/null || echo "0") - export VERGEN_GIT_SHA=$(cd "$COMPONENTS_DIR/$component_name" && git rev-parse HEAD 2>/dev/null || echo "unknown") - # Build with musl target for static linking cargo build --release --target x86_64-unknown-linux-musl @@ -124,6 +116,55 @@ build_go_component() { fi } +# Function to build mycelium (special case - builds from myceliumd subdirectory) +build_mycelium_component() { + local component_name="$1" + local binary_name="$2" + local install_path="$3" + + echo " Building Mycelium component: $component_name" + + if ! prepare_component "$component_name"; then + echo " Skipping $component_name (not available)" + return 1 + fi + + # Change to myceliumd subdirectory + if [ ! -d "myceliumd" ]; then + echo " Error: myceliumd subdirectory not found in $component_name" + return 1 + fi + + echo " Building from myceliumd subdirectory..." + cd myceliumd + + # Source cargo environment + . ~/.cargo/env + + # Build with musl target for static linking from myceliumd directory + cargo build --release --target x86_64-unknown-linux-musl + + # Debug: Check what was actually built + echo " Checking build results..." + ls -la target/x86_64-unknown-linux-musl/release/ | grep -v "\.d$" | grep -v "\.rlib$" || true + + # The binary is in target/x86_64-unknown-linux-musl/release/ (relative to myceliumd directory) + if [ ! -f "target/x86_64-unknown-linux-musl/release/$binary_name" ]; then + echo " Error: Binary not found at target/x86_64-unknown-linux-musl/release/$binary_name" + echo " Available files:" + ls -la target/x86_64-unknown-linux-musl/release/ || true + return 1 + fi + + # Install binary to Alpine root + mkdir -p "$ALPINE_ROOT$install_path" + cp "target/x86_64-unknown-linux-musl/release/$binary_name" "$ALPINE_ROOT$install_path/" + chmod +x "$ALPINE_ROOT$install_path/$binary_name" + + echo " Success: $install_path/$binary_name installed to Alpine root (musl static)" + return 0 +} + # Install build dependencies if needed echo " Checking build dependencies..." if ! command -v git >/dev/null; then @@ -167,8 +208,8 @@ else echo " Warning: CoreX binary not found" fi -# 4. Mycelium - Networking layer (Rust) - 0.6.1 with musl -if build_rust_component "mycelium" "mycelium" "/usr/bin"; then +# 4. Mycelium - Networking layer (Rust) - 0.6.1 with musl (special build from myceliumd subdirectory) +if build_mycelium_component "mycelium" "mycelium" "/usr/bin"; then echo " Mycelium built successfully" else echo " Warning: Failed to build mycelium" diff --git a/scripts/fetch-github.sh b/scripts/fetch-github.sh index 33b9bf0..b2c8cb3 100755 --- a/scripts/fetch-github.sh +++ b/scripts/fetch-github.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -e GITHUB_DIR="/build/github" diff --git a/scripts/install-firmware-minimal.sh b/scripts/install-firmware-minimal.sh index 05e9e71..a968bdb 100755 --- a/scripts/install-firmware-minimal.sh +++ b/scripts/install-firmware-minimal.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -e INITRAMFS_ROOT="/build/initramfs" diff --git a/scripts/install-packages.sh b/scripts/install-packages.sh index 3b3f264..ec45041 100755 --- a/scripts/install-packages.sh +++ b/scripts/install-packages.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -e INITRAMFS_ROOT="/build/initramfs" diff --git a/scripts/setup-initramfs.sh b/scripts/setup-initramfs.sh index 3026d66..1736df0 100755 --- a/scripts/setup-initramfs.sh +++ b/scripts/setup-initramfs.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -e INITRAMFS_ROOT="/build/initramfs" @@ -37,7 +37,7 @@ fi # Create debug script for compatibility echo " Creating debug script..." cat > "$INITRAMFS_ROOT/init-debug" << 'EOF' -#!/bin/sh +#!/bin/bash # Debug file injection script (Alpine version) echo "[+] debug mode enabled" diff --git a/scripts/setup-permissions.sh b/scripts/setup-permissions.sh index 49deaf4..e873d36 100755 --- a/scripts/setup-permissions.sh +++ b/scripts/setup-permissions.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # Ensure all scripts are executable chmod +x /build/scripts/*.sh diff --git a/scripts/setup-submodules.sh b/scripts/setup-submodules.sh index 77fda17..d18e9c8 100755 --- a/scripts/setup-submodules.sh +++ b/scripts/setup-submodules.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -e echo "[+] Setting up Zero-OS components..."