build-alpine/archive/build-alpine.sh

61 lines
2.2 KiB
Bash
Executable File

#!/bin/sh
set -e
# Install necessary packages in the container
apk update
apk add alpine-sdk squashfs-tools xorriso wget p7zip
# Define variables
ISO_OUTPUT_DIR="/output"
ARCH="x86_64"
WORK_DIR="/mnt/alpine-rootfs" # Use a directory within the container
# Create directories
mkdir -p "$ISO_OUTPUT_DIR"
mkdir -p "$WORK_DIR"
# Fetch the latest standard image filename using a BusyBox-compatible command
LATEST_STANDARD_ISO=$(wget -qO- http://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/$ARCH/latest-releases.yaml | grep -o 'alpine-standard-[0-9.]*-x86_64.iso' | head -n 1)
# Construct the download URL using the filename
STANDARD_ISO_URL="http://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/$ARCH/$LATEST_STANDARD_ISO"
# Download the latest Alpine standard ISO
wget "$STANDARD_ISO_URL" -O alpine-standard.iso
# Extract ISO contents using 7z
7z x alpine-standard.iso -o"$WORK_DIR"
# Ensure necessary directories exist in the extracted root filesystem
mkdir -p "$WORK_DIR/proc" "$WORK_DIR/sys" "$WORK_DIR/dev" "$WORK_DIR/root/.ssh" "$WORK_DIR/etc/init.d"
# Initialize APK in the root filesystem
mkdir -p "$WORK_DIR/etc/apk" "$WORK_DIR/var/lib/apk" "$WORK_DIR/var/cache/apk"
cp -r /etc/apk/repositories "$WORK_DIR/etc/apk/"
# Update repository index in the root filesystem and install `openssh`
apk update --root="$WORK_DIR"
apk add --root="$WORK_DIR" openssh
# Modify SSH configuration directly in the WORK_DIR
echo 'PermitRootLogin prohibit-password' >> "$WORK_DIR/etc/ssh/sshd_config"
echo 'PasswordAuthentication no' >> "$WORK_DIR/etc/ssh/sshd_config"
echo 'PermitRootLogin yes' >> "$WORK_DIR/etc/ssh/sshd_config"
echo "$SSH_PUBLIC_KEY" > "$WORK_DIR/root/.ssh/authorized_keys"
chmod 700 "$WORK_DIR/root/.ssh"
chmod 600 "$WORK_DIR/root/.ssh/authorized_keys"
# Add OpenSSH server to the list of services to start automatically
echo "/etc/init.d/sshd start" >> "$WORK_DIR/etc/local.d/ssh.start"
chmod +x "$WORK_DIR/etc/local.d/ssh.start"
# Create squashfs
mksquashfs "$WORK_DIR" "$ISO_OUTPUT_DIR/alpine-rootfs.squashfs" -comp xz
# Create ISO using xorriso
xorriso -as mkisofs -R -J -l -o "$ISO_OUTPUT_DIR/alpine-custom.iso" \
-b boot/grub/grub.cfg \
"$ISO_OUTPUT_DIR"
echo "Custom Alpine ISO built successfully and stored at $ISO_OUTPUT_DIR/alpine-custom.iso"