Files
zosbuilder/build.sh
Jan De Landtsheer 193662fb67 fix: configure Docker containers to run as current user
- Added user mapping (UID:GID) to Docker Compose services to prevent root-owned files
- Export current user's UID/GID in build.sh for Docker Compose
- Enhanced clean build to handle permission issues gracefully:
  * Try normal cleanup first
  * Fallback to container-based cleanup for root-owned files
  * Ultimate fallback to sudo if needed
- Added cleanup for all Docker volumes (build, source, kernel, target caches)

Fixes 'Permission denied' errors when cleaning output files created by Docker containers.
2025-08-22 18:47:31 +02:00

196 lines
5.7 KiB
Bash
Executable File

#!/bin/bash
# Host build script for Alpine Zero-OS Initramfs
set -e
# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Function to print colored output
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Default values
BUILDMODE="${BUILDMODE:-debug}"
SERVICE="builder"
CLEAN_BUILD=false
DEV_MODE=false
MINIMAL_MODE=false
# Help function
show_help() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -r, --release Build in release mode (default: debug)"
echo " -m, --minimal Build minimal embedded initramfs (~50MB vs 700MB)"
echo " -c, --clean Clean build without cache volumes"
echo " -d, --dev Start development container with shell"
echo " -t, --test Run test build without cache"
echo " -h, --help Show this help message"
echo ""
echo "Examples:"
echo " $0 # Build in debug mode with cache"
echo " $0 --release # Build in release mode"
echo " $0 --minimal # Build minimal embedded initramfs"
echo " $0 --clean # Clean build without cache"
echo " $0 --dev # Start development shell"
echo ""
echo "Environment variables:"
echo " BUILDMODE=release # Override build mode"
echo " TARGETARCH=amd64 # Target architecture"
echo ""
}
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-r|--release)
BUILDMODE="release"
shift
;;
-m|--minimal)
MINIMAL_MODE=true
shift
;;
-c|--clean)
CLEAN_BUILD=true
shift
;;
-d|--dev)
DEV_MODE=true
SERVICE="shell"
shift
;;
-t|--test)
SERVICE="test"
shift
;;
-h|--help)
show_help
exit 0
;;
*)
print_error "Unknown option: $1"
show_help
exit 1
;;
esac
done
print_info "Alpine Zero-OS Initramfs Builder"
print_info "Build mode: $BUILDMODE"
if [ "$MINIMAL_MODE" = true ]; then
print_info "Initramfs type: MINIMAL (~50MB embedded cpio)"
else
print_info "Initramfs type: FULL (~700MB with all packages)"
fi
print_info "Service: $SERVICE"
if [ "$CLEAN_BUILD" = true ]; then
print_info "Clean build: removing cache and artifacts"
fi
# Change to build directory
cd "$(dirname "$0")/build"
# Handle clean build
if [ "$CLEAN_BUILD" = true ]; then
print_info "Cleaning build artifacts and cache..."
# Remove output artifacts (use sudo if needed for root-owned files)
if ! rm -rf ../output/* 2>/dev/null; then
print_info " Some files owned by root, using container to clean..."
export UID=$(id -u)
export GID=$(id -g)
docker compose run --rm builder sh -c "rm -rf /build/output/*" || {
print_warning "Failed to clean output directory, trying with sudo"
sudo rm -rf ../output/*
}
fi
print_info " Removed output artifacts"
# Remove cache directories
rm -rf ../cache/* 2>/dev/null || {
print_info " Using sudo to remove cache directories..."
sudo rm -rf ../cache/*
}
print_info " Removed cache directories"
# Remove Docker volumes
print_info " Removing Docker cache volumes..."
docker volume rm alpine-initramfs_github-cache alpine-initramfs_kernel-cache 2>/dev/null || true
docker volume rm build_build-cache build_source-cache build_kernel-cache build_target-cache 2>/dev/null || true
print_info " Docker cache volumes removed"
print_success "Clean completed successfully"
exit 0
fi
# Create necessary directories
mkdir -p ../output ../cache/github ../cache/packages
# Export environment variables for docker-compose
export BUILDMODE
export MINIMAL_MODE
export TARGETARCH="${TARGETARCH:-amd64}"
export UID=$(id -u)
export GID=$(id -g)
if [ "$DEV_MODE" = true ]; then
print_info "Starting development container..."
docker compose run --rm $SERVICE
else
print_info "Starting build process..."
# Build the Docker image first
print_info "Building Docker image..."
docker compose build $SERVICE
# Run the build
print_info "Running Alpine initramfs build..."
docker compose run --rm $SERVICE
# Check if build was successful
if [ -f "../output/vmlinuz.efi" ]; then
print_success "Build completed successfully!"
print_info "Build artifacts:"
ls -la ../output/
# Show size information
size=$(stat -c%s "../output/vmlinuz.efi" 2>/dev/null || stat -f%z "../output/vmlinuz.efi" 2>/dev/null || echo "unknown")
print_info "Kernel size: $size bytes"
if [ -f "../output/initramfs.cpio.xz" ]; then
initramfs_size=$(stat -c%s "../output/initramfs.cpio.xz" 2>/dev/null || stat -f%z "../output/initramfs.cpio.xz" 2>/dev/null || echo "unknown")
print_info "Initramfs size: $initramfs_size bytes"
fi
print_success "Alpine Zero-OS kernel ready: output/vmlinuz.efi"
print_info ""
print_info "To test with QEMU:"
print_info " qemu-system-x86_64 -kernel output/vmlinuz.efi -m 2048 -enable-kvm -cpu host \\"
print_info " -net nic,model=e1000 -net bridge,br=vm0 -nographic -serial null \\"
print_info " -serial mon:stdio -append console=ttyS1,115200n8"
else
print_error "Build failed - no kernel image found in output/"
exit 1
fi
fi