#!/bin/sh set -e ALPINE_ROOT="/build/initramfs" COMPONENTS_DIR="/build/components" CACHE_DIR="/build/cache/source" echo "[+] Compiling Zero-OS components from git submodules..." # Create build directories mkdir -p "$CACHE_DIR" # Check if submodules are available if [ ! -d "$COMPONENTS_DIR" ]; then echo "Error: Components directory not found. Run setup-submodules.sh first." exit 1 fi # Function to prepare component for building prepare_component() { local component_name="$1" local component_dir="$COMPONENTS_DIR/$component_name" if [ ! -d "$component_dir" ]; then echo " Warning: $component_name submodule not found at $component_dir" return 1 fi echo " Preparing $component_name..." cd "$component_dir" # Update submodule if needed if [ -d ".git" ]; then git pull origin main 2>/dev/null || git pull origin master 2>/dev/null || echo " Using existing checkout" fi return 0 } # Function to build Rust project from submodule build_rust_component() { local component_name="$1" local binary_name="$2" local install_path="$3" echo " Building Rust component: $component_name" if ! prepare_component "$component_name"; then echo " Skipping $component_name (not available)" return 1 fi # Build the project echo " Compiling with cargo for musl target..." # Source cargo environment . ~/.cargo/env # Set git version for components that use git_version macro export GIT_DESCRIBE=$(cd "$COMPONENTS_DIR/$component_name" && git describe --tags --always --dirty=-modified 2>/dev/null || echo "unknown") # Build with musl target for static linking cargo build --release --target x86_64-unknown-linux-musl # 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 } # Function to build Go project from submodule build_go_component() { local component_name="$1" local binary_name="$2" local install_path="$3" local build_cmd="$4" echo " Building Go component: $component_name" if ! prepare_component "$component_name"; then echo " Skipping $component_name (not available)" return 1 fi # Build the project echo " Compiling with go..." if command -v go >/dev/null; then if [ -n "$build_cmd" ]; then eval "$build_cmd" else go build -o "$binary_name" fi # Install binary to Alpine root mkdir -p "$ALPINE_ROOT$install_path" cp "$binary_name" "$ALPINE_ROOT$install_path/" chmod +x "$ALPINE_ROOT$install_path/$binary_name" echo " Success: $install_path/$binary_name installed to Alpine root" return 0 else echo " Error: go not found - installing Go..." apk add --no-cache go if [ -n "$build_cmd" ]; then eval "$build_cmd" else go build -o "$binary_name" fi mkdir -p "$ALPINE_ROOT$install_path" cp "$binary_name" "$ALPINE_ROOT$install_path/" chmod +x "$ALPINE_ROOT$install_path/$binary_name" echo " Success: $install_path/$binary_name installed to Alpine root" return 0 fi } # Install build dependencies if needed echo " Checking build dependencies..." if ! command -v git >/dev/null; then echo " Installing git..." apk add --no-cache git fi echo "" # Ensure Alpine root directory exists if [ ! -d "$ALPINE_ROOT" ]; then echo "Creating Alpine initramfs root at $ALPINE_ROOT" mkdir -p "$ALPINE_ROOT" fi # Build each Zero-OS component from submodules echo "Building Zero-OS components from submodules:" # 1. Zinit - Init system (Rust) - master branch if build_rust_component "zinit" "zinit" "/sbin"; then echo " Zinit built successfully" else echo " Warning: Failed to build zinit" fi # 2. RFS - Rust filesystem (Rust) - v2.0.6 with musl if build_rust_component "rfs" "rfs" "/usr/bin"; then echo " RFS built successfully" else echo " Warning: Failed to build rfs" fi # 3. CoreX - Container control (static binary) - v2.1.4 echo " Installing CoreX static binary..." if [ -f "$COMPONENTS_DIR/corex/corex" ]; then mkdir -p "$ALPINE_ROOT/usr/bin" cp "$COMPONENTS_DIR/corex/corex" "$ALPINE_ROOT/usr/bin/" chmod +x "$ALPINE_ROOT/usr/bin/corex" echo " CoreX installed successfully" 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 echo " Mycelium built successfully" else echo " Warning: Failed to build mycelium" fi echo "" echo "[+] Zero-OS component compilation completed" # Show installed binaries info echo "" echo "[+] Compiled Zero-OS components in Alpine root:" for binary in "/sbin/zinit" "/usr/bin/rfs" "/usr/bin/corex" "/usr/bin/mycelium"; do if [ -x "$ALPINE_ROOT$binary" ]; then size=$(stat -c%s "$ALPINE_ROOT$binary" 2>/dev/null || echo "unknown") echo " $binary (${size} bytes)" else echo " $binary (NOT BUILT)" fi done echo "[+] Zero-OS binaries installed to Alpine root filesystem"