|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# build-custom-alpine.sh |
| 4 | +# |
| 5 | +# This script automates the creation of a custom Alpine Linux minirootfs. |
| 6 | +# It performs the following steps: |
| 7 | +# 1. Dynamically finds the latest stable minirootfs URL from Alpine's servers. |
| 8 | +# 2. Downloads the rootfs. |
| 9 | +# 3. Sets up a chroot environment. |
| 10 | +# 4. Updates all packages to their latest versions. |
| 11 | +# 5. Installs 'cloud-utils-growpart' and 'e2fsprogs-extra'. |
| 12 | +# 6. Cleans up and packages the new, custom rootfs into a tar.gz archive. |
| 13 | + |
| 14 | +set -euo pipefail # Exit on error, undefined variable, or pipe failure |
| 15 | + |
| 16 | +# --- Configuration --- |
| 17 | +ALPINE_RELEASES_URL="https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/x86_64" |
| 18 | +OUTPUT_FILE="/dev/shm/alpine-part-rootfs.tar.gz" |
| 19 | +BUILD_WORK_DIR=/dev/shm/alpine-rootfs |
| 20 | + |
| 21 | +# --- Colors for logging --- |
| 22 | +C_RESET='\033[0m' |
| 23 | +C_GREEN='\033[0;32m' |
| 24 | +C_BLUE='\033[0;34m' |
| 25 | +C_RED='\033[0;31m' |
| 26 | + |
| 27 | +# --- Logging functions --- |
| 28 | +log_info() { |
| 29 | + echo -e "${C_BLUE}==>${C_RESET} ${C_GREEN}$1${C_RESET}" |
| 30 | +} |
| 31 | +log_error() { |
| 32 | + echo -e "${C_RED}[ERROR]${C_RESET} $1" >&2 |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +# --- Cleanup function --- |
| 37 | +do_umount() { |
| 38 | + # Unmount chroot directories in reverse order, checking if they are mounted first |
| 39 | + #if mountpoint -q "${chroot_dir}/etc/ssl/certs"; then |
| 40 | + # umount "${chroot_dir}/etc/ssl/certs" |
| 41 | + #fi |
| 42 | + |
| 43 | + if mountpoint -q "${chroot_dir}/dev"; then |
| 44 | + umount "${chroot_dir}/dev" |
| 45 | + fi |
| 46 | + if mountpoint -q "${chroot_dir}/sys"; then |
| 47 | + umount "${chroot_dir}/sys" |
| 48 | + fi |
| 49 | + if mountpoint -q "${chroot_dir}/proc"; then |
| 50 | + umount "${chroot_dir}/proc" |
| 51 | + fi |
| 52 | +} |
| 53 | + |
| 54 | +cleanup() { |
| 55 | + log_info "Cleaning up..." |
| 56 | + local chroot_dir="${BUILD_WORK_DIR}/rootfs" |
| 57 | + |
| 58 | + |
| 59 | + # Remove the entire temporary directory |
| 60 | + do_umount |
| 61 | + rm -rf "${BUILD_WORK_DIR}" |
| 62 | + |
| 63 | + log_info "Cleanup finished." |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +# Check for root privileges and required dependencies |
| 68 | +check_prerequisites() { |
| 69 | + if [ "$(id -u)" -ne 0 ]; then |
| 70 | + log_error "This script must be run as root to use chroot and mount." |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | + |
| 74 | + for cmd in curl grep sed tar; do |
| 75 | + if ! command -v "$cmd" &> /dev/null; then |
| 76 | + log_error "Required command '$cmd' is not installed." |
| 77 | + exit 1 |
| 78 | + fi |
| 79 | + done |
| 80 | + |
| 81 | + mkdir -p "${BUILD_WORK_DIR}" || true |
| 82 | +} |
| 83 | + |
| 84 | +# Main script logic |
| 85 | +main() { |
| 86 | + check_prerequisites |
| 87 | + trap cleanup EXIT |
| 88 | + |
| 89 | + log_info "Step 1: Finding the latest Alpine minirootfs..." |
| 90 | + local yaml_url="${ALPINE_RELEASES_URL}/latest-releases.yaml" |
| 91 | + |
| 92 | + # Parse the YAML file to find the minirootfs filename |
| 93 | + local minirootfs_file=$(curl -s "$yaml_url" | grep 'file: alpine-minirootfs' | awk '{print $2}' | cut -d ' ' -f2 | head -n 1) |
| 94 | + |
| 95 | + if [ -z "$minirootfs_file" ]; then |
| 96 | + log_error "Could not parse the minirootfs filename from the releases YAML." |
| 97 | + exit 1 |
| 98 | + fi |
| 99 | + |
| 100 | + local download_url="${ALPINE_RELEASES_URL}/${minirootfs_file}" |
| 101 | + log_info "Found: ${minirootfs_file}" |
| 102 | + echo "${minirootfs_file}" > "/dev/shm/alpine_version" |
| 103 | + |
| 104 | + log_info "Step 2: Downloading the base rootfs..." |
| 105 | + curl -L -o "${BUILD_WORK_DIR}/minirootfs.tar.gz" "$download_url" || (echo "Failed to download the minirootfs $download_url." && exit 1) |
| 106 | + |
| 107 | + log_info "Step 3: Setting up the chroot environment..." |
| 108 | + local chroot_dir="${BUILD_WORK_DIR}/rootfs" |
| 109 | + mkdir -p "$chroot_dir" |
| 110 | + tar -xzf "${BUILD_WORK_DIR}/minirootfs.tar.gz" -C "$chroot_dir" |
| 111 | + |
| 112 | + # Mount necessary pseudo-filesystems for chroot to function |
| 113 | + mount --bind /proc "${chroot_dir}/proc" |
| 114 | + mount --bind /sys "${chroot_dir}/sys" |
| 115 | + mount --bind /dev "${chroot_dir}/dev" |
| 116 | + |
| 117 | + # Provide DNS resolution inside the chroot for apk |
| 118 | + #cp /etc/resolv.conf "${chroot_dir}/etc/" |
| 119 | + echo "nameserver 1.1.1.1" >> "${chroot_dir}/etc/resolv.conf" |
| 120 | + |
| 121 | + #echo "https://mirrors.ustc.edu.cn/alpine/v3.22/main" > "${chroot_dir}/etc/apk/repositories" |
| 122 | + #echo "https://mirrors.ustc.edu.cn/alpine/v3.22/community" >> "${chroot_dir}/etc/apk/repositories" |
| 123 | + |
| 124 | + #mount --bind /etc/ssl/certs "${chroot_dir}/etc/ssl/certs" |
| 125 | + #mkdir -p "${chroot_dir}/usr/share/ca-certificates" || true |
| 126 | + #mount --bind /usr/share/ca-certificates "${chroot_dir}/usr/share/ca-certificates" |
| 127 | + |
| 128 | + |
| 129 | + log_info "Step 4: Updating packages and installing tools inside chroot..." |
| 130 | + # We run all commands in a single chroot shell session for efficiency |
| 131 | + # --no-cache is used to avoid filling the rootfs with unnecessary cache files |
| 132 | + # cloud-utils-growpart e2fsprogs-extra for install growpart and mkfs.ext4 |
| 133 | + # iptables busybox-openrc busybox-mdev-openrc for podman |
| 134 | + # dropbear for ssh maybe |
| 135 | + chroot "$chroot_dir" /bin/sh -c "date; ln -s /etc/ssl /usr/lib/ssl; apk --no-check-certificate update && \ |
| 136 | + apk --no-check-certificate upgrade && \ |
| 137 | + apk add -v --no-check-certificate cloud-utils-growpart e2fsprogs-extra iptables busybox-openrc busybox-mdev-openrc dropbear dropbear-ssh && \ |
| 138 | + rc-update add cgroups && \ |
| 139 | + rm -rf /var/cache/apk/*" || { |
| 140 | + log_error "Failed to run commands inside chroot. Check network or package errors." |
| 141 | + exit 1 |
| 142 | + } |
| 143 | + |
| 144 | + log_info "Step 5: Cleaning up the chroot environment..." |
| 145 | + # The apk cache is already clean due to --no-cache. Just remove resolv.conf. |
| 146 | + rm "${chroot_dir}/etc/resolv.conf" |
| 147 | + # Unmounts will be handled by the cleanup trap function to ensure they always run. |
| 148 | + |
| 149 | + log_info "Step 6: Packaging the new custom rootfs..." |
| 150 | + do_umount |
| 151 | + # The -C flag tells tar to change to that directory before archiving. |
| 152 | + # The '.' at the end means "archive everything in this directory". |
| 153 | + # This creates a clean tarball without any leading path components. |
| 154 | + tar -czf "${OUTPUT_FILE}" -C "$chroot_dir" . |
| 155 | + chmod 0666 "${OUTPUT_FILE}" |
| 156 | + |
| 157 | + echo |
| 158 | + log_success "Custom Alpine rootfs created successfully!" |
| 159 | + log_success "Output file: ${OUTPUT_FILE}" |
| 160 | +} |
| 161 | + |
| 162 | +# A small wrapper for success messages |
| 163 | +log_success() { |
| 164 | + echo -e "${C_GREEN}[SUCCESS]${C_RESET} $1" |
| 165 | +} |
| 166 | + |
| 167 | +# Run the main function |
| 168 | +main |
0 commit comments