#!/usr/bin/env bash # Bootstrap a one-shot GRUB entry for IXFabric automated reinstall. # Usage: curl -fsSL https://install.ixfabric.net/ | sudo bash # # Debian 12 (Bookworm) and Debian 13 (Trixie). # Supports static IPv4 and IPv6 configurations. # BIOS and UEFI preseed selection. # # Bare-metal / VM ISO: # https://cdn.ixfabric.net/iso/ixFabric-Debian13-amd64.iso # Documentation: # https://repo.ixfabric.net/iso/debian13/README.md set -euo pipefail readonly GRUB_ENTRY="/etc/grub.d/50_ixfabric" readonly BOOT_DIR="/boot/ixfabric" readonly REPO_ROOT="https://repo.ixfabric.net" readonly PRESEED_ROOT="https://repo.ixfabric.net/preseed" readonly DNS4="8.8.8.8" readonly DNS6="2001:4860:4860::8888" if [[ "${EUID:-$(id -u)}" -ne 0 ]]; then echo "ERROR: Run this script as root." >&2 exit 1 fi if ! command -v wget >/dev/null 2>&1; then echo "ERROR: wget is required." >&2 exit 1 fi if ! command -v python3 >/dev/null 2>&1; then echo "ERROR: python3 is required for IP address validation." >&2 exit 1 fi detect_deb_release() { local version="" codename="" if [[ -r /etc/os-release ]]; then # shellcheck disable=SC1091 source /etc/os-release version="${VERSION_ID:-}" codename="${VERSION_CODENAME:-}" fi case "$version" in 12|12.*) echo "deb12"; return ;; 13|13.*) echo "deb13"; return ;; esac case "$codename" in bookworm) echo "deb12"; return ;; trixie) echo "deb13"; return ;; esac if [[ -r /etc/debian_version ]]; then version="$(sed 's,/.*,,' /etc/debian_version)" case "$version" in 12|12.*|bookworm) echo "deb12"; return ;; 13|13.*|trixie) echo "deb13"; return ;; esac fi echo "ERROR: Supported releases are Debian 12 and 13." >&2 exit 1 } is_efi() { [[ -d /sys/firmware/efi ]] } read_tty() { if [[ -r /dev/tty ]]; then read -r -p "$1" "$2" 253 or ".." in hostname or not all(label_re.fullmatch(label) for label in hostname.split(".")) ): print(f"ERROR: Invalid hostname: {sys.argv[1]}", file=sys.stderr) print("Expected form: rsID.city.country (e.g. rs1.syd.au, rs5.ams.nl)", file=sys.stderr) sys.exit(1) print(hostname) PY } echo echo "==========================================" echo " ixFabric Debian Reinstallation Preloader" echo "==========================================" echo read_tty "Enter Hostname: " hostname read_tty "Enter IP address (IPv4 or IPv6): " ip read_tty "Enter Subnet (IPv4 mask/CIDR or IPv6 prefix): " subnet read_tty "Enter Gateway: " gateway for var in hostname ip subnet gateway; do if [[ -z "${!var}" ]]; then echo "ERROR: $var is required." >&2 exit 1 fi done hostname="$(valid_hostname "$hostname")" if [[ "$ip" == *:* ]]; then ip_version=6 ip="$(validate_ip "$ip" 6)" gateway="$(validate_ip "$gateway" 6)" netmask="$(ipv6_netmask "$subnet")" nameserver="$DNS6" address_family="IPv6" else ip_version=4 ip="$(validate_ip "$ip" 4)" gateway="$(validate_ip "$gateway" 4)" netmask="$(ipv4_netmask "$subnet")" nameserver="$DNS4" address_family="IPv4" fi # Reject an address and gateway that are identical. if [[ "$ip" == "$gateway" ]]; then echo "ERROR: IP address and gateway must be different." >&2 exit 1 fi deb_release="$(detect_deb_release)" repo_base="${REPO_ROOT}/${deb_release}" if is_efi; then preseed_file="${hostname}.efi.txt" boot_mode="UEFI" else preseed_file="${hostname}.txt" boot_mode="BIOS/Legacy" fi preseed_url="${PRESEED_ROOT}/${preseed_file}" echo echo "Configuration:" echo " Hostname: $hostname" echo " IP version: $address_family" echo " IP address: $ip" echo " Netmask: $netmask" echo " Gateway: $gateway" echo " Nameserver: $nameserver" echo " Debian: $deb_release" echo " Boot mode: $boot_mode" echo " Preseed: $preseed_url" echo # Verify that the required remote resources exist before changing GRUB. echo "Checking repository resources..." for url in "${repo_base}/initrd.gz" "${repo_base}/vmlinuz" "$preseed_url"; do if ! wget --spider --timeout=20 --tries=2 "$url" >/dev/null 2>&1; then echo "ERROR: Resource is unavailable: $url" >&2 echo "No GRUB configuration has been changed." >&2 exit 1 fi done # Download into a staging directory so a failed download does not replace # the previously installed kernel or initrd. mkdir -p "$BOOT_DIR" stage_dir="$(mktemp -d "${BOOT_DIR}/.staging.XXXXXX")" cleanup() { if [[ -n "${stage_dir:-}" && -d "$stage_dir" ]]; then rm -rf -- "$stage_dir" fi } trap cleanup EXIT wget -q --show-progress -O "${stage_dir}/initrd.gz" \ "${repo_base}/initrd.gz" wget -q --show-progress -O "${stage_dir}/vmlinuz" \ "${repo_base}/vmlinuz" for file in initrd.gz vmlinuz; do if [[ ! -s "${stage_dir}/${file}" ]]; then echo "ERROR: Download failed: $file" >&2 exit 1 fi done # Preserve the previous boot files before installing new versions. backup_dir="${BOOT_DIR}/backup-$(date +%Y%m%d-%H%M%S)" mkdir -p "$backup_dir" for file in initrd.gz vmlinuz; do if [[ -f "${BOOT_DIR}/${file}" ]]; then cp -a "${BOOT_DIR}/${file}" "${backup_dir}/${file}" fi done install -m 0644 "${stage_dir}/initrd.gz" "${BOOT_DIR}/initrd.gz" install -m 0644 "${stage_dir}/vmlinuz" "${BOOT_DIR}/vmlinuz" # GRUB paths are relative to the partition containing /boot. if mountpoint -q /boot && [[ "$(stat -c '%d' /)" -ne "$(stat -c '%d' /boot)" ]]; then grub_kernel="/ixfabric/vmlinuz" grub_initrd="/ixfabric/initrd.gz" else grub_kernel="/boot/ixfabric/vmlinuz" grub_initrd="/boot/ixfabric/initrd.gz" fi echo "GRUB kernel path: $grub_kernel" echo "GRUB initrd path: $grub_initrd" # Back up the existing IXFabric GRUB entry before replacing it. if [[ -f "$GRUB_ENTRY" ]]; then cp -a "$GRUB_ENTRY" "${GRUB_ENTRY}.backup-$(date +%Y%m%d-%H%M%S)" fi # Build the entry using the validated address family and network settings. # IPv6 netmasks are passed in the full format expected by Debian Installer. # # Use a shell `cat` heredoc (not `exec tail`) so the grub.d script is valid # shell for bash -n / update-grub, while still emitting GRUB menuentry text. tmp_grub="$(mktemp /etc/grub.d/.50_ixfabric.XXXXXX)" cat > "$tmp_grub" <&2 rm -f "$tmp_grub" exit 1 fi install -m 0755 "$tmp_grub" "$GRUB_ENTRY" rm -f "$tmp_grub" # Generate the updated GRUB menu before scheduling the one-shot boot. if ! update-grub; then echo "ERROR: update-grub failed. The reinstall has NOT been scheduled." >&2 exit 1 fi if ! grub-reboot "ixFabric Reinstallation"; then echo "ERROR: Could not schedule the one-shot reinstall." >&2 exit 1 fi echo echo "==========================================" echo " IXFabric preloader configured successfully" echo "==========================================" echo "Address family: $address_family" echo "Target address: $ip" echo "Next boot: ixFabric Reinstallation" echo echo "The reinstall entry is scheduled for the next boot only." echo "Verify the server's console and network configuration before rebooting."