proxmox-server-setup/setup.sh
wagesj45 3dbbf57ab2 Add setup.sh
Initial commit.
2025-07-25 14:50:10 -05:00

165 lines
8.3 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
###############################################################################
# setup.sh Interactive Debian VM bootstrap wizard #
# Jordanfriendly edition (whiptail UI, aliases, neofetch, CIFS, etc.) #
###############################################################################
set -euo pipefail
# =============================================================================
# Default SSH key (paste your key between the quotes)
# =============================================================================
DEFAULT_SSH_KEY="[paste key here]"
###############################################################################
# Early root escalation #
###############################################################################
if [[ $EUID -ne 0 ]]; then
SCRIPT=$(readlink -f "$0")
if command -v whiptail &>/dev/null; then
PW=$(whiptail --passwordbox "Root privileges required.\nEnter root password:" 10 60 3>&1 1>&2 2>&3) || exit 1
else
read -rsp "Root password: " PW; echo
fi
exec su -c "$SCRIPT $*" - root <<<"$PW"
fi
###############################################################################
# Logging #
###############################################################################
LOGFILE="/var/log/freshbox.log"
exec &> >(tee -a "$LOGFILE")
###############################################################################
# Colours & helpers #
###############################################################################
RED=$'\e[31m'; YLW=$'\e[33m'; CLR=$'\e[0m'
die() { echo -e "${RED}[ERROR] $*${CLR}" >&2; exit 1; }
ensure_pkgs() { apt-get -qq update; apt-get -y install "$@"; }
msg() { if command -v whiptail &>/dev/null; then whiptail --title "Setup" --msgbox "$1" 10 60; else echo -e "${YLW}$1${CLR}"; fi; }
confirm() { whiptail --yesno "$1" 10 60; }
gauge_run() {
local MESSAGE=$1; shift
(
"$@" & CMD_PID=$!
while kill -0 "$CMD_PID" 2>/dev/null; do
echo "XXX"; echo 50; echo "$MESSAGE"; echo "XXX"; sleep 1
done
) | whiptail --gauge "$MESSAGE" 6 60 0
}
###############################################################################
# Ensure whiptail exists #
###############################################################################
if ! command -v whiptail &>/dev/null; then
echo "Installing whiptail …"
ensure_pkgs whiptail
fi
###############################################################################
# Welcome #
###############################################################################
whiptail --title "VM Setup Wizard" --yesno "\
Welcome!\n\nThis wizard will update the system, configure sudo, hostname,\naliases, neofetch, optional tools, CIFS mounts, and SSH keys.\n\nContinue?" 12 70 || exit 0
###############################################################################
# Core packages & sudo #
###############################################################################
msg "Installing core tools…"
ensure_pkgs sudo curl gnupg lsb-release
# Choose sudo users
mapfile -t USERS < <(awk -F: '$3>=1000 && $1!="nobody"{print $1}' /etc/passwd)
SEL_SUDO=""
if ((${#USERS[@]})); then
OPTS=(); for u in "${USERS[@]}"; do OPTS+=("$u" "" OFF); done
SEL_SUDO=$(whiptail --title "Sudo Access" --checklist \
"Select users to grant passwordless sudo:" 15 60 6 \
"${OPTS[@]}" 3>&1 1>&2 2>&3) || true
for u in $SEL_SUDO; do usermod -aG sudo "${u//\"/}"; done
fi
###############################################################################
# System upgrade #
###############################################################################
gauge_run "Applying system updates…" apt-get -y dist-upgrade
###############################################################################
# Hostname #
###############################################################################
MACS=$(ip -brief link | awk '$1!~"lo"{print $1": "$3}')
NEW_HOST=$(whiptail --title "Hostname" --inputbox "\
Current MAC addresses:\n$MACS\n\nEnter new hostname:" 15 70 "$(hostname)" 3>&1 1>&2 2>&3) || die "Hostname required"
OLD_HOST=$(hostname)
[[ -n $NEW_HOST ]] || die "Hostname cannot be blank"
sed -i "s/$OLD_HOST/$NEW_HOST/g" /etc/hosts
hostnamectl set-hostname "$NEW_HOST"
msg "Hostname set to $NEW_HOST"
###############################################################################
# SSH keys #
###############################################################################
install -d -m 700 /root/.ssh
chmod 700 /root/.ssh
# Add default key if provided
if [[ -n "$DEFAULT_SSH_KEY" ]]; then
grep -qxF "$DEFAULT_SSH_KEY" /root/.ssh/authorized_keys 2>/dev/null || \
echo "$DEFAULT_SSH_KEY" >> /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
msg "Default SSH key added to authorized_keys"
fi
# Optionally add more keys
if confirm "Add additional SSH public key?"; then
KEY=$(whiptail --title "SSH Key" --inputbox "Paste your public key:" 12 70 3>&1 1>&2 2>&3) || die "Cancelled"
grep -qxF "$KEY" /root/.ssh/authorized_keys 2>/dev/null || \
echo "$KEY" >> /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
msg "Additional SSH key added"
fi
###############################################################################
# Optional utilities #
###############################################################################
TOOLS=(htop curl wget jq git tree)
OPTS=(); for t in "${TOOLS[@]}"; do OPTS+=("$t" "" OFF); done
SEL_RAW=$(whiptail --title "Extra Utilities" --checklist \
"Select additional packages to install:" 15 60 8 \
"${OPTS[@]}" 3>&1 1>&2 2>&3) || true
IFS=' ' read -r -a SEL_PKGS <<< "${SEL_RAW//\"/}"
ensure_pkgs bat ncdu neofetch cifs-utils "${SEL_PKGS[@]}"
# Aliases
echo "alias cat='batcat --paging=never'" >/etc/profile.d/10-bat_alias.sh
echo "alias du='ncdu'" >/etc/profile.d/10-ncdu_alias.sh
chmod +x /etc/profile.d/10-*alias.sh
# Neofetch MOTD
echo 'neofetch' >/etc/profile.d/90-neofetch.sh
chmod +x /etc/profile.d/90-neofetch.sh
###############################################################################
# CIFS mounts #
###############################################################################
CIFS_HOSTS=(jimmu keiko keitai)
OPTS=(); for h in "${CIFS_HOSTS[@]}"; do OPTS+=("$h" "" OFF); done
SEL_HOSTS=$(whiptail --title "CIFS Mounts" --checklist \
"Select NAS shares to mount under /media:" 15 60 6 \
"${OPTS[@]}" 3>&1 1>&2 2>&3) || true
mkdir -p /media
for host in "${CIFS_HOSTS[@]}"; do
TEMPLATE="//${host}.wageshouse/Data /media/${host} cifs username=%s,password=%s,_netdev,vers=2.0 0 0"
if [[ $SEL_HOSTS == *\"$host\"* ]]; then
U=$(whiptail --inputbox "[$host] CIFS username:" 8 50 3>&1 1>&2 2>&3) || exit 1
P=$(whiptail --passwordbox "[$host] CIFS password:" 8 50 3>&1 1>&2 2>&3) || exit 1
printf -v LINE "$TEMPLATE" "$U" "$P"
mkdir -p "/media/$host"
grep -q "${host}.wageshouse" /etc/fstab && sed -i "#${host}\.wageshouse#d" /etc/fstab
echo "$LINE" >> /etc/fstab
else
grep -q "${host}.wageshouse" /etc/fstab || printf "# $TEMPLATE\n" "username" "password" >> /etc/fstab
fi
done
if ! mount -a 2>>"$LOGFILE"; then msg "⚠️ Some CIFS mounts failed. Rerun the script to correct credentials."; fi
###############################################################################
# Summary #
###############################################################################
msg "\
Setup complete!\n\n• Hostname: $NEW_HOST\n• Sudoers: ${SEL_SUDO//\"/ }\n• Packages: bat ncdu neofetch ${SEL_PKGS[*]}\n• CIFS: ${SEL_HOSTS//\"/ }\n\nLog saved to $LOGFILE\n\nEnjoy your new Debian VM!"