void-usb/install-stage2.sh

148 lines
3.5 KiB
Bash

#!/usr/bin/env bash
LOGFILE=/tmp/void-usb-install-log
function press_any_key {
echo "Press any key to continue or Ctrl+c to abort..."
read -n1 DISCARD_ME
echo ""
}
function yesno {
unset DISCARD_ME
while [ -z "$DISCARD_ME" ]; do
read -p "[y/n] " -n1 DISCARD_ME
case "$DISCARD_ME" in
y)
echo ""
return 0
;;
n)
echo ""
return 1
;;
*)
echo " Please enter y for yes or n for no."
unset DISCARD_ME
;;
esac
done
}
[ -z "$TERM" ] && export TERM=linux
echo -n "Checking for internet connection..."
INTERNET="down"
WAITING=0
while [ "$INTERNET" = "down" -a "$WAITING" -lt 15 ]; do
sleep 1
if ping -c2 repo-default.voidlinux.org >> $LOGFILE 2>&1; then
INTERNET="up"
fi
WAITING=$((WAITING+1))
echo -n "."
done
echo " $INTERNET"
if [ "$INTERNET" = "down" ]; then
clear
echo "Connect to WiFi?
Any network settings from before the reboot have not been saved and need to be entered again.
Use 'Activate a connection' in the dialog to select your network.
Alternatively, you can continue without internet but the system will be left in a barebones state.
"
if yesno; then
nmtui
clear
echo -n "Checking for internet connection..."
WAITING=0
while [ "$INTERNET" = "down" -a "$WAITING" -lt 15 ]; do
sleep 1
if ping -c2 repo-default.voidlinux.org >> $LOGFILE 2>&1; then
INTERNET="up"
fi
WAITING=$((WAITING+1))
echo -n "."
done
echo " $INTERNET"
fi
fi
if [ "$INTERNET" = "up" ]; then
echo -n "Retrieving package lists... "
xbps-install --sync >>$LOGFILE 2>&1
echo "done"
fi
STAGE2_DIR="/opt/void-usb/installer/" # Change this to your desired directory
CONFIG_DIR="$STAGE2_DIR/custom-scripts"
SCRIPT_LIST="$CONFIG_DIR/scripts.lst"
SCRIPTS=()
NAMES=()
DESCRIPTIONS=()
# Find all scripts and extract their names & descriptions
echo "Scanning for configuration scripts in $CONFIG_DIR..."
for script in "$CONFIG_DIR"/*.sh; do
[ -f "$script" ] || continue # Skip if no .sh files found
SCRIPTS+=("$script")
# Extract name from "# NAME:" (first occurrence)
SCRIPT_NAME=$(grep -m1 '^# NAME:' "$script" | sed 's/^# NAME:[[:space:]]*//')
[ -z "$SCRIPT_NAME" ] && SCRIPT_NAME="$(basename "$script")"
# Extract description from "# DESC:" (first occurrence)
SCRIPT_DESC=$(grep -m1 '^# DESC:' "$script" | sed 's/^# DESC:[[:space:]]*//')
[ -z "$SCRIPT_DESC" ] && SCRIPT_DESC="No description available."
NAMES+=("$SCRIPT_NAME")
DESCRIPTIONS+=("$SCRIPT_DESC")
done
# Check if no scripts were found
if [ ${#SCRIPTS[@]} -eq 0 ]; then
echo "No configuration scripts found in $CONFIG_DIR."
exit 1
fi
# Display menu dynamically
echo "Available configurations:"
for i in "${!SCRIPTS[@]}"; do
echo "$((i+1))) ${NAMES[i]} - ${DESCRIPTIONS[i]}"
done
echo "$(( ${#SCRIPTS[@]} + 1 ))) Exit"
# Get user input
read -p "Select an option (1-${#SCRIPTS[@]}): " choice
# Run selected script
if (( choice >= 1 && choice <= ${#SCRIPTS[@]} )); then
echo "Running: ${NAMES[choice-1]}"
bash "${SCRIPTS[choice-1]}"
elif (( choice == ${#SCRIPTS[@]} + 1 )); then
echo "Exiting..."
exit 0
else
echo "Invalid choice."
exit 1
fi
# Stage 2 Tasks
echo "Setting up user..."
read -p "Enter username: " NEW_USER
useradd -m -G wheel "$NEW_USER"
passwd "$NEW_USER"
echo "Cleaning up installation files..."
rm -rf "$STAGE2_DIR"
echo "Restoring TTY1 configuration..."
mv /mnt/target/etc/sv/agetty-tty1/conf.bak /mnt/target/etc/sv/agetty-tty1/conf >> $LOGFILE 2>&1
bash --norc --noprofile