|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# ─── COLORS & LOG FUNCTIONS ────────────────────────────────────────────────── |
| 5 | +GREEN="\e[32m"; YELLOW="\e[33m"; RED="\e[31m"; RESET="\e[0m" |
| 6 | +log() { echo -e "${GREEN}[+]${RESET} $1"; } |
| 7 | +warn() { echo -e "${YELLOW}[!]${RESET} $1"; } |
| 8 | +error() { echo -e "${RED}[-]${RESET} $1"; exit 1; } |
| 9 | + |
| 10 | +# ─── ENSURE ROOT & ENABLE TAB COMPLETION ───────────────────────────────────── |
| 11 | +(( EUID == 0 )) || error "Run as root: sudo $0" |
| 12 | +bind 'TAB:complete' |
| 13 | + |
| 14 | +# ─── PROMPT FOR INPUTS ──────────────────────────────────────────────────────── |
| 15 | +read -e -p "Path to Ubuntu ISO: " ISO_PATH |
| 16 | +[[ -f "$ISO_PATH" ]] || error "ISO not found at '$ISO_PATH'" |
| 17 | + |
| 18 | +read -e -p "Path to autoinstall YAML: " YAML_PATH |
| 19 | +[[ -f "$YAML_PATH" ]] || error "YAML not found at '$YAML_PATH'" |
| 20 | + |
| 21 | +read -e -p "USB device (e.g. /dev/sda): " DEV |
| 22 | +[[ -b "$DEV" ]] || error "Block device '$DEV' not found" |
| 23 | +USB_PART="${DEV}1" |
| 24 | + |
| 25 | +cat <<EOF |
| 26 | +
|
| 27 | +You are about to: |
| 28 | +
|
| 29 | + • Wipe & reformat ➜ $DEV |
| 30 | + • Copy ISO ➜ $ISO_PATH |
| 31 | + • Copy autoinstall ➜ $YAML_PATH |
| 32 | +
|
| 33 | +EOF |
| 34 | + |
| 35 | +read -p "Proceed? (yes/[no]) " CONFIRM |
| 36 | +[[ "$CONFIRM" == "yes" ]] || { log "Aborted."; exit 0; } |
| 37 | + |
| 38 | +# ─── PREP: UNMOUNT ─────────────────────────────────────────────────────────── |
| 39 | +log "Unmounting partitions on $DEV if mounted..." |
| 40 | +for p in $(lsblk -lnpo NAME,MOUNTPOINT "${DEV}"* | awk '$2!=""{print $1}'); do |
| 41 | + warn " umount $p" |
| 42 | + umount "$p" || warn "Could not unmount $p, continuing" |
| 43 | +done |
| 44 | + |
| 45 | +# ─── PARTITION & FORMAT ────────────────────────────────────────────────────── |
| 46 | +log "Creating single FAT32 partition on $DEV..." |
| 47 | +parted --script "$DEV" \ |
| 48 | + mklabel msdos \ |
| 49 | + mkpart primary fat32 1MiB 100% \ |
| 50 | + set 1 boot on |
| 51 | + |
| 52 | +log "Formatting ${USB_PART} as FAT32..." |
| 53 | +mkfs.vfat -F32 "$USB_PART" |
| 54 | + |
| 55 | +# ─── MOUNTS ─────────────────────────────────────────────────────────────────── |
| 56 | +MNT_ISO=$(mktemp -d) |
| 57 | +MNT_USB=$(mktemp -d) |
| 58 | + |
| 59 | +log "Mounting ISO -> $MNT_ISO" |
| 60 | +mount -o loop "$ISO_PATH" "$MNT_ISO" |
| 61 | + |
| 62 | +log "Mounting USB -> $MNT_USB" |
| 63 | +mount "$USB_PART" "$MNT_USB" |
| 64 | + |
| 65 | +# ─── COPY FILES (follow symlinks) ──────────────────────────────────────────── |
| 66 | +log "Copying ISO contents to USB..." |
| 67 | +warn "Symbolic link errors are okay" |
| 68 | + |
| 69 | +set +e |
| 70 | +cp -aT "${MNT_ISO}/." "${MNT_USB}/" |
| 71 | +CP_STATUS=$? |
| 72 | +set -e |
| 73 | + |
| 74 | +log "Adding autoinstall.yaml to USB root..." |
| 75 | +cp "$YAML_PATH" "${MNT_USB}/autoinstall.yaml" |
| 76 | + |
| 77 | +# ─── CLEANUP ───────────────────────────────────────────────────────────────── |
| 78 | +log "Syncing disks..." |
| 79 | +sync "$MNT_USB" |
| 80 | + |
| 81 | +log "Unmounting mounts..." |
| 82 | +umount "$MNT_ISO" "$MNT_USB" |
| 83 | +rmdir "$MNT_ISO" "$MNT_USB" |
| 84 | + |
| 85 | +log "USB is ready!" |
0 commit comments