feat: add binary optimization with strip and upx compression
- Added upx package to Dockerfile.alpine build dependencies - Added optimize_binary() function to compile-components.sh - Automatically strips debug symbols from all Zero-OS binaries - Applies UPX compression with --best --lzma for maximum size reduction - Optimizes binaries in both initramfs and output directories - Shows size reduction stats (original → stripped → compressed) - Graceful fallback if UPX fails, keeping stripped version This significantly reduces initramfs size by compressing zinit, rfs, and mycelium binaries.
This commit is contained in:
@@ -27,6 +27,7 @@ RUN apk add --no-cache \
|
||||
bc \
|
||||
perl \
|
||||
python3 \
|
||||
upx \
|
||||
# Kernel build dependencies
|
||||
linux-lts-dev \
|
||||
linux-lts \
|
||||
|
||||
@@ -70,6 +70,7 @@ build_component() {
|
||||
}
|
||||
elif [ "$component" = "rfs" ]; then
|
||||
# Build RFS with required features
|
||||
echo " Removing "
|
||||
echo " Running: cargo build --release --target x86_64-unknown-linux-musl --features build-binary"
|
||||
CARGO_TARGET_DIR="$WORKSPACE_ROOT/target" cargo build --release --target x86_64-unknown-linux-musl --features build-binary
|
||||
else
|
||||
@@ -82,6 +83,37 @@ build_component() {
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to optimize binaries (strip + upx)
|
||||
optimize_binary() {
|
||||
local binary_path="$1"
|
||||
local binary_name=$(basename "$binary_path")
|
||||
|
||||
if [ ! -f "$binary_path" ]; then
|
||||
echo " ⚠ Binary not found for optimization: $binary_path"
|
||||
return 1
|
||||
fi
|
||||
|
||||
local original_size=$(stat -c%s "$binary_path")
|
||||
|
||||
# Strip debug symbols and unnecessary sections
|
||||
echo " Stripping $binary_name..."
|
||||
strip --strip-all "$binary_path" 2>/dev/null || {
|
||||
echo " ⚠ Failed to strip $binary_name"
|
||||
}
|
||||
|
||||
local stripped_size=$(stat -c%s "$binary_path")
|
||||
|
||||
# Compress with UPX (with fallback if UPX fails)
|
||||
echo " Compressing $binary_name with UPX..."
|
||||
if upx --best --lzma "$binary_path" 2>/dev/null; then
|
||||
local final_size=$(stat -c%s "$binary_path")
|
||||
echo " ✓ $binary_name optimized: $original_size → $stripped_size → $final_size bytes"
|
||||
else
|
||||
echo " ⚠ UPX compression failed for $binary_name, keeping stripped version"
|
||||
echo " ✓ $binary_name stripped: $original_size → $stripped_size bytes"
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to install workspace binaries
|
||||
install_component_binaries() {
|
||||
local target_dir="$WORKSPACE_ROOT/target/x86_64-unknown-linux-musl/release"
|
||||
@@ -106,7 +138,12 @@ install_component_binaries() {
|
||||
cp "$target_dir/zinit" "$ALPINE_ROOT/sbin/"
|
||||
cp "$target_dir/zinit" "$OUTPUT_DIR/"
|
||||
chmod +x "$ALPINE_ROOT/sbin/zinit"
|
||||
echo " ✓ zinit -> /sbin/zinit"
|
||||
|
||||
# Optimize binaries
|
||||
optimize_binary "$ALPINE_ROOT/sbin/zinit"
|
||||
optimize_binary "$OUTPUT_DIR/zinit"
|
||||
|
||||
echo " ✓ zinit -> /sbin/zinit (optimized)"
|
||||
installed=$((installed + 1))
|
||||
else
|
||||
echo " ✗ zinit binary not found"
|
||||
@@ -125,7 +162,12 @@ install_component_binaries() {
|
||||
cp "$target_dir/$rfs_binary" "$ALPINE_ROOT/usr/bin/rfs"
|
||||
cp "$target_dir/$rfs_binary" "$OUTPUT_DIR/rfs"
|
||||
chmod +x "$ALPINE_ROOT/usr/bin/rfs"
|
||||
echo " ✓ $rfs_binary -> /usr/bin/rfs"
|
||||
|
||||
# Optimize binaries
|
||||
optimize_binary "$ALPINE_ROOT/usr/bin/rfs"
|
||||
optimize_binary "$OUTPUT_DIR/rfs"
|
||||
|
||||
echo " ✓ $rfs_binary -> /usr/bin/rfs (optimized)"
|
||||
installed=$((installed + 1))
|
||||
else
|
||||
echo " ✗ rfs binary not found (checked: rfs, rfs-server, rfs-bin)"
|
||||
@@ -144,7 +186,12 @@ install_component_binaries() {
|
||||
cp "$target_dir/$mycelium_binary" "$ALPINE_ROOT/usr/bin/mycelium"
|
||||
cp "$target_dir/$mycelium_binary" "$OUTPUT_DIR/mycelium"
|
||||
chmod +x "$ALPINE_ROOT/usr/bin/mycelium"
|
||||
echo " ✓ $mycelium_binary -> /usr/bin/mycelium"
|
||||
|
||||
# Optimize binaries
|
||||
optimize_binary "$ALPINE_ROOT/usr/bin/mycelium"
|
||||
optimize_binary "$OUTPUT_DIR/mycelium"
|
||||
|
||||
echo " ✓ $mycelium_binary -> /usr/bin/mycelium (optimized)"
|
||||
installed=$((installed + 1))
|
||||
else
|
||||
echo " ✗ mycelium binary not found (checked: mycelium, myceliumd, mycelium-cli)"
|
||||
|
||||
Reference in New Issue
Block a user