#!/usr/bin/env bash # # engram // install.sh # # Turns a clean Arch ISO into a fresh engram host: partitions the disk, # wraps it in LUKS (optional), builds the BTRFS subvolumes, pacstraps the # base system, configures it and installs GRUB. # # This is the automated path of docs/install.md - the doc is the reference # and the fallback if anything goes sideways here. # # Usage (from the Arch ISO): # # curl -fsSLO https://gitea.nevim42.cz/nevim42/engram-boot/raw/branch/main/install.sh # less install.sh # bash install.sh # # Flow: checks -> questions -> partition -> LUKS -> BTRFS -> pacstrap -> # fstab -> chroot setup -> GRUB -> bootstrap.sh -> reboot # set -euo pipefail trap 'printf "\n[x] installer stopped on line %s - check the output above\n" "$LINENO" >&2' ERR # --------------------------------------------------------------------------- # Output helpers - a bit of neon, but nothing that breaks a pipe. # --------------------------------------------------------------------------- if [[ -t 1 ]]; then C_RESET=$'\033[0m' C_CYAN=$'\033[36m' C_YELLOW=$'\033[33m' C_RED=$'\033[31m' C_DIM=$'\033[2m' else C_RESET="" C_CYAN="" C_YELLOW="" C_RED="" C_DIM="" fi info() { printf '%s[+]%s %s\n' "$C_CYAN" "$C_RESET" "$*"; } warn() { printf '%s[!]%s %s\n' "$C_YELLOW" "$C_RESET" "$*"; } die() { printf '%s[x]%s %s\n' "$C_RED" "$C_RESET" "$*" >&2; exit 1; } step() { printf '\n%s==>%s %s\n' "$C_CYAN" "$C_RESET" "$*"; } banner() { cat </dev/null 2>&1; then die "No network. Bring it up first (nmtui or iwctl) and run me again." fi info "UEFI, root, network - all good." } # --------------------------------------------------------------------------- # Questions - the only three things that differ between machines. # --------------------------------------------------------------------------- ask_questions() { step "Target selection" local answer echo "Available disks:" lsblk -dpno NAME,SIZE,MODEL -e7,11 | sed 's/^/ /' echo read -rp " Target disk (name or path, e.g. sda): " answer TARGET_DISK="/dev/${answer#/dev/}" if [[ ! -b $TARGET_DISK ]]; then die "Not a block device: $TARGET_DISK" fi if [[ $(lsblk -dno TYPE "$TARGET_DISK") != "disk" ]]; then die "$TARGET_DISK is not a whole disk." fi if [[ -n $(lsblk -rno MOUNTPOINT "$TARGET_DISK") ]]; then die "$TARGET_DISK (or one of its partitions) is mounted - unmount first." fi echo lsblk -o NAME,SIZE,TYPE,FSTYPE "$TARGET_DISK" | sed 's/^/ /' warn "Everything on $TARGET_DISK will be erased. No backup, no Relic." read -rp " Type '${TARGET_DISK}' to confirm: " answer if [[ $answer != "$TARGET_DISK" ]]; then die "Aborted - the disk was not touched." fi step "Hostname and encryption" while :; do read -rp " Hostname for this machine (e.g. nevim42-mikoshi): " TARGET_HOSTNAME if [[ $TARGET_HOSTNAME =~ ^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$ ]]; then break fi warn "Lowercase letters, digits and hyphens only." done read -rp " Encrypt the disk with LUKS? [Y/n]: " answer if [[ $answer =~ ^[nN] ]]; then ENCRYPTED="no" else ENCRYPTED="yes" info "cryptsetup will ask for the passphrase twice now - and again when opening." fi } # --------------------------------------------------------------------------- # Partitioning - GPT with a 512M ESP and one partition for everything else. # --------------------------------------------------------------------------- partition_disk() { step "Partitioning $TARGET_DISK" wipefs -a "$TARGET_DISK" sfdisk "$TARGET_DISK" < 20 )); then die "Partition $dev never showed up." fi sleep 0.5 done done info "Created $PART_EFI (ESP, $ESP_SIZE) and $PART_ROOT (the rest)." } # --------------------------------------------------------------------------- # LUKS2 container - the Relic. The passphrase is handled by cryptsetup. # --------------------------------------------------------------------------- create_luks() { step "Creating the LUKS2 container on $PART_ROOT" info "The passphrase is not recoverable - pick something worthy." cryptsetup luksFormat "$PART_ROOT" cryptsetup open "$PART_ROOT" "$LUKS_NAME" ROOT_DEV="/dev/mapper/$LUKS_NAME" LUKS_UUID="$(blkid -s UUID -o value "$PART_ROOT")" info "LUKS UUID: $LUKS_UUID" } # --------------------------------------------------------------------------- # Filesystems - FAT for the ESP, BTRFS for everything else. # --------------------------------------------------------------------------- create_filesystems() { step "Creating filesystems" mkfs.fat -F32 "$PART_EFI" mkfs.btrfs -L "$BTRFS_LABEL" "$ROOT_DEV" } # --------------------------------------------------------------------------- # Subvolumes and mounts - same layout as the doc. # --------------------------------------------------------------------------- mount_target() { step "Creating subvolumes and mounting everything" mount "$ROOT_DEV" /mnt local subvol for subvol in "${SUBVOLUMES[@]}"; do btrfs subvolume create "/mnt/$subvol" done umount /mnt mount -o "$MOUNT_OPTS,subvol=@" "$ROOT_DEV" /mnt mkdir -p /mnt/{boot,home,snapshots} mount -o "$MOUNT_OPTS,subvol=@home" "$ROOT_DEV" /mnt/home mount -o "$MOUNT_OPTS,subvol=@snapshots" "$ROOT_DEV" /mnt/snapshots mount "$PART_EFI" /mnt/boot } # --------------------------------------------------------------------------- # The base system. # --------------------------------------------------------------------------- pacstrap_base() { step "Pacstrapping the base system - this is the slow part" pacstrap /mnt "${PACSTRAP_PACKAGES[@]}" } generate_fstab() { step "Generating fstab" genfstab -U -p /mnt >> /mnt/etc/fstab } # --------------------------------------------------------------------------- # Chroot configuration - everything from "Set locale, create user, ..." in # the doc. The inner script is generated with values baked in; if a step # fails, it stays on the target in /root for inspection. # --------------------------------------------------------------------------- configure_system() { step "Configuring the system (chroot)" # The two encryption-dependent steps are prepared here, so the inner # script stays linear and readable. local chroot_hooks chroot_grub if [[ $ENCRYPTED == "yes" ]]; then chroot_hooks="sed -i 's/^HOOKS=.*/HOOKS=(${HOOKS_ENCRYPTED})/' /etc/mkinitcpio.conf" chroot_grub="sed -i 's|^GRUB_CMDLINE_LINUX=.*|GRUB_CMDLINE_LINUX=\"rd.luks.name=${LUKS_UUID}=${LUKS_NAME} root=/dev/mapper/${LUKS_NAME}\"|' /etc/default/grub" else chroot_hooks="# no encryption - the default hooks are fine" chroot_grub="# no encryption - GRUB_CMDLINE_LINUX stays empty" fi cat > /mnt/root/engram-setup.sh < timezone and clock" ln -sf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime hwclock --systohc --utc # --- locale --- echo " -> locale" sed -i 's/^#${LOCALE} UTF-8/${LOCALE} UTF-8/' /etc/locale.gen locale-gen echo "LANG=${LOCALE}" > /etc/locale.conf # --- hostname + hosts --- echo " -> hostname and /etc/hosts" echo "${TARGET_HOSTNAME}" > /etc/hostname cat > /etc/hosts <<'HOSTS' # Static table lookup for hostnames. # See hosts(5) for details. 127.0.0.1 localhost ::1 localhost 127.0.0.1 ${TARGET_HOSTNAME} ::1 ${TARGET_HOSTNAME} # End of file HOSTS # --- user + sudo --- echo " -> user ${USER_NAME} - passwd will ask for the password" useradd -mG wheel -s /bin/fish ${USER_NAME} passwd ${USER_NAME} sed -i 's/^# %wheel ALL=(ALL:ALL) ALL/%wheel ALL=(ALL:ALL) ALL/' /etc/sudoers # --- pacman --- echo " -> pacman colors and multilib" sed -i 's/^#Color/Color/' /etc/pacman.conf sed -i '/^#\[multilib\]/,+1 s/^#//' /etc/pacman.conf # --- networking --- echo " -> NetworkManager" systemctl enable NetworkManager # --- initramfs --- echo " -> initramfs" sed -i 's/^MODULES=()/MODULES=(btrfs)/' /etc/mkinitcpio.conf ${chroot_hooks} mkinitcpio -p linux # --- GRUB --- echo " -> GRUB" grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=GRUB ${chroot_grub} grub-mkconfig -o /boot/grub/grub.cfg echo " -> done" CHROOT arch-chroot /mnt bash /root/engram-setup.sh rm -f /mnt/root/engram-setup.sh } # --------------------------------------------------------------------------- # bootstrap.sh - drop it into the new home, so the next step is one command. # --------------------------------------------------------------------------- fetch() { if command -v curl >/dev/null; then curl -fsSL "$1" else wget -qO- "$1" fi } place_bootstrap() { step "Dropping bootstrap.sh into /home/${USER_NAME}" fetch "$RAW_BASE/bootstrap.sh" > "/mnt/home/${USER_NAME}/bootstrap.sh" \ || die "Could not fetch bootstrap.sh from $RAW_BASE" # A fresh system has exactly one user, so uid/gid 1000 is safe. chown 1000:1000 "/mnt/home/${USER_NAME}/bootstrap.sh" chmod +x "/mnt/home/${USER_NAME}/bootstrap.sh" info "Waiting in the new home - no download needed after reboot." } # --------------------------------------------------------------------------- # The exit - summary and reboot. # --------------------------------------------------------------------------- finish() { step "That is it - the machine is built" cat <