build-alpine/build.sh

71 lines
2.1 KiB
Bash

#!/bin/bash
# Exit immediately if a command exits with a non-zero status
set -e
# Define variables
WORK_DIR="/root/build-alpine"
INPUT_DIR="$WORK_DIR/input" # Directory containing files to be included in the image
OUTPUT_DIR="$WORK_DIR/output"
SCRIPT_NAME="alpine-make-vm-image"
SCRIPT_URL="https://raw.githubusercontent.com/alpinelinux/alpine-make-vm-image/v0.13.0/$SCRIPT_NAME"
SCRIPT_SHA1="0fe2deca927bc91eb8ab32584574eee72a23d033"
ALPINE_IMAGE_NAME="alpine-custom.raw"
SETUP_SCRIPT="$INPUT_DIR/vm-setup.sh" # Correct path to vm-setup.sh
# Ensure a custom TMPDIR is used to avoid filling up /tmp
export TMPDIR="$WORK_DIR/tmp"
mkdir -p "$TMPDIR"
# Create working, input, and output directories
mkdir -p "$INPUT_DIR"
mkdir -p "$OUTPUT_DIR"
cd "$WORK_DIR"
# Install necessary dependencies
sudo apt-get update
sudo apt-get install -y qemu-utils qemu-system-x86 rsync util-linux e2fsprogs dosfstools
# Check available disk space before proceeding
REQUIRED_SPACE_MB=2048 # Minimum required space in MB
AVAILABLE_SPACE_MB=$(df --output=avail -m "$OUTPUT_DIR" | tail -n 1)
if [ "$AVAILABLE_SPACE_MB" -lt "$REQUIRED_SPACE_MB" ]; then
echo "Error: Not enough space in $OUTPUT_DIR. Required: ${REQUIRED_SPACE_MB}MB, Available: ${AVAILABLE_SPACE_MB}MB."
exit 1
fi
# Download the alpine-make-vm-image script
wget "$SCRIPT_URL" -O "$SCRIPT_NAME"
# Verify the script SHA1 hash
echo "$SCRIPT_SHA1 $SCRIPT_NAME" | sha1sum -c || exit 1
# Make the alpine-make-vm-image script executable
chmod +x "$SCRIPT_NAME"
# Ensure vm-setup.sh is executable
chmod +x "$SETUP_SCRIPT"
# Run the alpine-make-vm-image script to create the VM image
./"$SCRIPT_NAME" \
-f raw \
-s 2G \
-a x86_64 \
-B BIOS \
-b latest-stable \
-p "openssh-server htop curl nano zsh" \
-S "$INPUT_DIR" \
--fs-skel-chown=root:root \
--script-chroot \
"$OUTPUT_DIR/$ALPINE_IMAGE_NAME" \
"$SETUP_SCRIPT" # Use the correct path for the setup script
# Check if the image creation was successful
if [ -f "$OUTPUT_DIR/$ALPINE_IMAGE_NAME" ]; then
echo "Alpine Linux VM image created successfully at: $OUTPUT_DIR/$ALPINE_IMAGE_NAME"
else
echo "Error: Image creation failed."
exit 1
fi