install-arch/install.sh

383 lines
9.3 KiB
Bash
Raw Normal View History

2021-08-12 16:39:06 -04:00
#!/bin/bash
2021-05-14 14:06:20 -04:00
set -e
2021-08-09 14:48:22 -04:00
qpushd() {
pushd $@ > /dev/null
}
qpopd() {
popd $@ > /dev/null
2021-05-14 14:06:20 -04:00
}
2021-08-09 10:44:29 -04:00
quiet() {
local DUMMY
2021-08-10 09:29:03 -04:00
set +e
2021-08-09 10:44:29 -04:00
DUMMY=$($@ 2>&1 > /dev/null)
2021-08-10 09:29:03 -04:00
if [ $? -ne 0 ]; then
echo "$DUMMY"
set -e
return 1
fi
set -e
2021-05-14 14:06:20 -04:00
}
2021-08-10 10:48:14 -04:00
ultra_quiet() {
2021-08-10 09:29:03 -04:00
local DUMMY
DUMMY=$($@ 2>&1 > /dev/null)
2021-08-10 09:08:34 -04:00
}
2021-08-09 14:48:22 -04:00
2021-08-09 19:53:23 -04:00
### FORCE ROOT ###
2021-08-09 14:48:22 -04:00
[ $(whoami) != "root" ] && echo "Please run as root" && exit 1
### CD TO MY DIR ###
2021-08-12 20:03:44 -04:00
cd "$(dirname "$0")"
2021-08-09 19:53:23 -04:00
### URLs ###
FZF_DOWNLOAD="$(curl -s https://api.github.com/repos/junegunn/fzf/releases/latest | grep linux_amd64 | sed -nE 's/^\s*"browser_download_url":\s*"(.*)"\s*$/\1/p')"
2021-08-10 08:35:59 -04:00
PARTED_DOWNLOAD="https://archlinux.org/packages/extra/x86_64/parted/download"
2021-08-10 13:45:40 -04:00
POST_INSTALL_SCRIPT="https://raw.githubusercontent.com/augustogunsch/install-arch/master/post-install.sh"
2021-08-12 20:03:44 -04:00
KEYBOARD_MAP="https://raw.githubusercontent.com/augustogunsch/install-arch/master/keyboard-map.csv"
2021-08-09 19:53:23 -04:00
2021-08-09 14:48:22 -04:00
### COLORS ###
RED='\033[0;31m'
LGREEN='\033[1;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
BOLD=$(tput bold)
NORM=$(tput sgr0)
readonly RED
readonly LGREEN
readonly YELLOW
readonly NC
readonly BOLD
readonly NORM
### VARS ###
CUR_PHASE=1
2021-08-11 18:57:28 -04:00
MAX_PHASE=4
2021-08-09 14:48:22 -04:00
### INFO ###
2021-08-10 09:17:16 -04:00
AVAILABLE_PLATFORMS='Both BIOS and UEFI systems\nOnly x86_64 systems\nDistros:\nArch\nArtix (OpenRC)\n'
2021-08-09 14:48:22 -04:00
readonly AVAILABLE_PLATFORMS
echo "This script can only be run interactively. Make sure you are in a supported platform and have an Internet connection. Available platforms:"
echo -e "$AVAILABLE_PLATFORMS"
2021-05-14 14:06:20 -04:00
2021-08-09 14:48:22 -04:00
### SYSTEM ###
2021-08-09 18:36:39 -04:00
DISTRO=$(cat /etc/os-release | sed -nE 's/^ID=(.*)/\1/p')
2021-08-09 10:44:29 -04:00
INIT_SYS=$(basename $(readlink /bin/init))
2021-08-10 10:34:23 -04:00
set +e
2021-08-10 10:48:14 -04:00
ultra_quiet ls /sys/firmware/efi/efivars
2021-08-09 10:44:29 -04:00
[ $? -eq 0 ] && UEFI=1 || UEFI=0
2021-08-10 10:34:23 -04:00
set -e
2021-08-09 14:48:22 -04:00
readonly DISTRO
readonly INIT_SYS
readonly UEFI
2021-08-10 10:10:24 -04:00
right_chroot() {
2021-08-12 11:02:11 -04:00
[ "$DISTRO" = "arch" ] && arch-chroot $@ || artix-chroot $@
2021-08-10 10:10:24 -04:00
}
right_fstabgen() {
[ "$DISTRO" = "arch" ] && genfstab $@ || fstabgen $@
}
right_basestrap() {
[ "$DISTRO" = "arch" ] && pacstrap $@ || basestrap $@
}
2021-05-15 17:55:45 -04:00
2021-08-09 14:48:22 -04:00
print_phase() {
echo -e "${BOLD}${YELLOW}[$CUR_PHASE/$MAX_PHASE] $1 phase${NC}${NORM}"
CUR_PHASE=$((CUR_PHASE+1))
}
2021-05-15 17:55:45 -04:00
2021-08-09 19:53:23 -04:00
download_fzf() {
2021-08-12 20:42:26 -04:00
[ -f /usr/bin/fzf ] && return 0
2021-08-10 08:35:59 -04:00
echo -n "Downloading fzf (for script use only)..."
2021-08-09 19:53:23 -04:00
curl -sL "$FZF_DOWNLOAD" -o fzf.tar.gz
tar -xf fzf.tar.gz
2021-08-10 08:39:15 -04:00
mv ./fzf /usr/bin/fzf
2021-08-10 08:49:42 -04:00
rm fzf.tar.gz
2021-08-10 08:35:59 -04:00
echo "done"
}
download_parted() {
2021-08-12 20:42:26 -04:00
[ -f /usr/bin/parted ] && return 0
2021-08-10 08:35:59 -04:00
echo -n "Downloading parted (for script use only)..."
2021-08-10 08:49:42 -04:00
curl -sL "$PARTED_DOWNLOAD" -o parted.tar.zst
tar -xf parted.tar.zst
2021-08-10 08:53:35 -04:00
cp -r ./usr /
rm -r ./usr
2021-08-10 08:49:42 -04:00
rm parted.tar.zst
2021-08-10 08:35:59 -04:00
echo "done"
2021-05-15 17:55:45 -04:00
}
prompt() {
2021-08-09 10:44:29 -04:00
echo -n "$1 [Y/n] "
[ $NO_CONFIRM ] && echo "y" && return 1
2021-05-15 17:55:45 -04:00
read ans
case $ans in
2021-08-09 10:44:29 -04:00
n|N) return 0 ;;
*) return 1 ;;
2021-05-15 17:55:45 -04:00
esac
}
2021-08-09 10:44:29 -04:00
prompt_drive() {
local DRIVES="$(lsblk -pno NAME,TYPE,MODEL | awk 'BEGIN {count=1} $1 ~ /^\// {
if ($2 == "disk") {printf("%i'")"' %s \"", count, $1); for(i=3;i<NF;i++) printf("%s ", $i); printf("%s\"\n", $NF); count++ } }')"
echo "Available drives:"
printf " %-12s %s\n" "DISK" "IDENTIFIER"
echo -e "$DRIVES"
echo -n "Choose drive to install $DISTRO into (WARNING: it will be repartitioned and wiped out) (default=1): "
local drive
read drive
[ -z "$drive" ] && drive=1
DRIVE_TARGET=$(echo -e "$DRIVES" | awk '$1 ~ /^'$drive'\)$/ { print $2 }')
if [ -z "$DRIVE_TARGET" ]; then
echo "Invalid target"
exit 1
2021-05-15 19:25:56 -04:00
fi
2021-08-05 21:29:58 -04:00
2021-08-09 10:44:29 -04:00
set +e
2021-08-09 14:48:22 -04:00
prompt "Installing to $DRIVE_TARGET. Confirm?"
2021-08-09 10:44:29 -04:00
[ $? -eq 0 ] && exit 0
2021-08-05 21:29:58 -04:00
set -e
}
2021-08-09 10:44:29 -04:00
partition() {
2021-08-09 14:48:22 -04:00
print_phase "Disk partitioning"
2021-08-12 20:42:26 -04:00
set +e
ultra_quiet swapoff -a
set -e
2021-08-12 11:14:38 -04:00
[ -f /bin/parted ] || download_parted
2021-08-05 21:29:58 -04:00
local rootN
2021-08-09 10:44:29 -04:00
echo -n "Partitioning drive..."
if [ $UEFI -eq 0 ]; then
# Legacy
rootN=2
parted --script "$DRIVE_TARGET" \
mklabel msdos \
mkpart primary linux-swap 0% 4GiB \
mkpart primary ext4 4GiB 100%
echo "done"
else
# EFI
rootN=3
parted --script "$DRIVE_TARGET" \
mklabel gpt \
mkpart swap linux-swap 0% 4GiB \
mkpart boot fat32 4GiB 5Gib \
mkpart root ext4 5GiB 100% \
set 2 esp on
echo "done"
echo -n "Configuring BOOT partition..."
quiet mkfs.fat -F 32 "$DRIVE_TARGET"2
fatlabel "$DRIVE_TARGET"2 BOOT
mkdir /mnt/boot
mount "$DRIVE_TARGET"2 /mnt/boot
echo "done"
fi
2021-08-05 21:29:58 -04:00
2021-08-09 10:44:29 -04:00
echo -n "Configuring SWAP partition..."
quiet mkswap -L SWAP "$DRIVE_TARGET"1
quiet swapon "$DRIVE_TARGET"1
2021-05-15 19:25:56 -04:00
echo "done"
2021-08-09 10:44:29 -04:00
echo -n "Configuring ROOT partition..."
quiet mkfs.ext4 -L ROOT "$DRIVE_TARGET"$rootN
quiet mount "$DRIVE_TARGET"$rootN /mnt
2021-05-15 19:25:56 -04:00
echo "done"
2021-05-14 14:06:20 -04:00
}
2021-08-09 14:48:22 -04:00
install_base() {
print_phase "System installation"
echo -n "Installing base system, kernel, bootloader and vi..."
2021-08-10 10:10:24 -04:00
quiet right_basestrap /mnt base base-devel linux linux-firmware grub vi
2021-08-10 09:17:16 -04:00
echo "done"
2021-08-09 14:48:22 -04:00
2021-08-09 18:33:49 -04:00
if [ "$DISTRO" = "artix" ]; then
2021-08-09 14:48:22 -04:00
if [ "$INIT_SYS" = "openrc-init" ]; then
echo -n "Installing openrc..."
2021-08-10 10:10:24 -04:00
quiet right_basestrap /mnt openrc elogind-openrc
2021-08-09 14:48:22 -04:00
echo "done"
else
echo
echo "Error: Unsupported init system \"$INIT_SYS\""
exit 1
fi
2021-08-10 09:17:16 -04:00
elif [ "$DISTRO" != "arch" ]; then
echo "Error: Unsupported distro \"$DISTRO\""
2021-08-09 14:48:22 -04:00
fi
2021-08-10 09:17:16 -04:00
echo -n "Generating fstab..."
2021-08-10 10:10:24 -04:00
right_fstabgen -U /mnt >> /mnt/etc/fstab
2021-08-10 09:17:16 -04:00
echo "done"
2021-08-09 14:48:22 -04:00
}
2021-08-09 19:53:23 -04:00
set_timezone() {
2021-08-12 11:02:11 -04:00
ln -sf "/usr/share/zoneinfo/$TIMEZONE" /mnt/etc/localtime
2021-08-10 10:10:24 -04:00
quiet right_chroot /mnt hwclock --systohc
2021-08-09 19:53:23 -04:00
}
2021-08-09 14:48:22 -04:00
2021-08-09 19:53:23 -04:00
set_locale() {
2021-08-09 17:58:15 -04:00
echo -n "Configuring locale..."
2021-08-12 16:39:06 -04:00
sed "s/^#$LOCALE/$LOCALE/" < /mnt/etc/locale.gen > /etc/locale.gen
2021-08-12 20:42:26 -04:00
quiet locale-gen
2021-08-12 16:39:06 -04:00
cp -f /usr/lib/locale/locale-archive /mnt/usr/lib/locale/locale-archive
2021-08-09 14:48:22 -04:00
echo "export LANG=\"en_US.UTF-8\"" > /mnt/etc/locale.conf
echo "export LC_COLLATE=\"C\"" >> /mnt/etc/locale.conf
2021-08-09 17:58:15 -04:00
echo "done"
2021-08-12 18:28:05 -04:00
echo -n "Setting keyboard layout..."
2021-08-12 20:42:26 -04:00
echo "KEYMAP=\"$KBD_LAYOUT\"" > /mnt/etc/vconsole.conf
XKBD="$(awk -F, '$1 ~ /^'$KBD_LAYOUT'$/ {print $0; exit 0}' keyboard-map.csv)"
XKBD_LAYOUT="$(echo $XKBD | awk -F, '{print $2}')"
XKBD_MODEL="$(echo $XKBD | awk -F, '{print $3}')"
XKBD_VARIANT="$(echo $XKBD | awk -F, '{print $4}')"
XKBD_OPTIONS="$(echo $XKBD | awk -F, '{print $5}')"
echo "keymap=\"$XKBD_LAYOUT\"" > /mnt/etc/conf.d/keymaps
local XKBD_CONF="/mnt/etc/X11/xorg.conf.d/00-keyboard.conf"
mkdir -p $(dirname $XKBD_CONF)
2021-08-12 18:28:05 -04:00
echo "Section \"InputClass\"" > $XKBD_CONF
echo " Identifier \"system-keyboard\"" >> $XKBD_CONF
echo " MatchIsKeyboard \"on\"" >> $XKBD_CONF
echo " Option \"XkbLayout\" \"$XKBD_LAYOUT\"" >> $XKBD_CONF
2021-08-12 20:03:44 -04:00
echo " Option \"XkbModel\" \"$XKBD_MODEL\"" >> $XKBD_CONF
[ -n $XKBD_VARIANT ] && echo " Option \"XkbVariant\" \"$XKBD_VARIANT\"" >> $XKBD_CONF
[ -n $XKBD_OPTIONS ] && echo " Option \"XkbOptions\" \"$XKBD_OPTIONS\"" >> $XKBD_CONF
2021-08-12 18:28:05 -04:00
echo "EndSection" >> $XKBD_CONF
echo "done"
2021-08-09 19:53:23 -04:00
}
2021-08-09 14:48:22 -04:00
2021-08-09 19:53:23 -04:00
setup_grub() {
2021-08-09 14:48:22 -04:00
echo -n "Configuring boot loader..."
if [ $UEFI -eq 1 ]; then
2021-08-10 10:14:41 -04:00
quiet right_chroot /mnt grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=grub
2021-08-09 14:48:22 -04:00
else
2021-08-10 10:34:23 -04:00
quiet right_chroot /mnt grub-install --target=i386-pc "$DRIVE_TARGET"
2021-08-09 14:48:22 -04:00
fi
2021-08-10 10:34:23 -04:00
quiet right_chroot /mnt grub-mkconfig -o /boot/grub/grub.cfg
2021-08-09 14:48:22 -04:00
echo "done"
2021-08-09 19:53:23 -04:00
}
2021-08-09 14:48:22 -04:00
2021-08-09 19:53:23 -04:00
setup_users() {
2021-08-10 14:04:03 -04:00
echo -n "Configuring users..."
2021-08-12 11:02:11 -04:00
set +e
# There might be a group with the user's name
awk -F: '$1 ~ /^'$PERSONAL_USER'$/ { exit 1 }' /etc/group
[ $? -eq 0 ] && right_chroot /mnt useradd --badnames -m "$PERSONAL_USER" || \
right_chroot /mnt useradd --badnames -m -g "$PERSONAL_USER" "$PERSONAL_USER"
set -e
2021-08-10 14:04:03 -04:00
echo -e "root:$ROOT_PASSWORD\n$PERSONAL_USER:$PERSONAL_PASSWORD" | chpasswd -R /mnt
echo "done"
2021-08-09 19:53:23 -04:00
}
2021-08-09 14:48:22 -04:00
2021-08-09 19:53:23 -04:00
setup_network() {
2021-08-09 17:58:15 -04:00
echo -n "Configuring hostname and network..."
2021-08-10 11:47:14 -04:00
echo "$MACHINE_HOSTNAME" > /mnt/etc/hostname
2021-08-09 14:48:22 -04:00
echo "127.0.0.1 localhost" > /mnt/etc/hosts
echo "::1 localhost" >> /mnt/etc/hosts
2021-08-10 11:47:14 -04:00
echo "127.0.1.1 $MACHINE_HOSTNAME.localdomain $MACHINE_HOSTNAME" >> /mnt/etc/hosts
2021-08-09 14:48:22 -04:00
2021-08-12 11:02:11 -04:00
quiet right_basestrap /mnt dhcpcd wpa_supplicant
2021-08-09 18:33:49 -04:00
if [ "$DISTRO" = "artix" ]; then
2021-08-09 14:48:22 -04:00
if [ "$INIT_SYS" = "openrc-init" ]; then
2021-08-10 11:47:14 -04:00
echo "hostname=\"$MACHINE_HOSTNAME\"" > /mnt/etc/conf.d/hostname
2021-08-12 11:02:11 -04:00
quiet right_basestrap /mnt connman-openrc
2021-08-10 10:34:23 -04:00
quiet right_chroot /mnt rc-update add connmand
2021-08-09 14:48:22 -04:00
fi
2021-08-11 18:57:28 -04:00
else
quiet right_chroot /mnt systemctl enable dhcpcd
2021-08-09 14:48:22 -04:00
fi
2021-08-09 17:58:15 -04:00
echo "done"
2021-08-09 19:53:23 -04:00
}
2021-08-10 11:47:14 -04:00
ask_password() {
echo -n "Type password for $1: "
2021-08-10 13:45:40 -04:00
stty -echo
2021-08-10 11:47:14 -04:00
read USER_PASSWORD
2021-08-10 13:45:40 -04:00
stty echo
2021-08-10 14:04:03 -04:00
echo
2021-08-10 11:47:14 -04:00
echo -n "Confirm password: "
2021-08-10 13:45:40 -04:00
stty -echo
2021-08-10 11:47:14 -04:00
local PASSWORD_CONFIRM
read PASSWORD_CONFIRM
2021-08-10 13:45:40 -04:00
stty echo
2021-08-10 14:04:03 -04:00
echo
2021-08-10 11:47:14 -04:00
if [ "$USER_PASSWORD" != "$PASSWORD_CONFIRM" ]; then
echo "Wrong passwords. Please try again."
ask_password $1
fi
}
prompt_all() {
download_fzf
prompt_drive
echo "Choose timezone:"
qpushd /usr/share/zoneinfo
TIMEZONE="$(fzf --layout=reverse --height=20)"
qpopd
echo "Choose locale:"
2021-08-12 16:39:06 -04:00
LOCALE=$(sed '/^#\s/D' < /etc/locale.gen | sed '/^#$/D' | sed 's/^#//' | fzf --layout=reverse --height=20)
2021-08-10 11:47:14 -04:00
2021-08-12 20:03:44 -04:00
[ -f keyboard-map.csv ] || curl -sL "$KEYBOARD_MAP" -o keyboard-map.csv
echo "Choose keyboard layout:"
2021-08-12 20:42:26 -04:00
KBD_LAYOUT="$(awk -F, '{print $1}' keyboard-map.csv | fzf --layout=reverse --height=20)"
2021-08-12 18:28:05 -04:00
2021-08-10 11:47:14 -04:00
ask_password root
ROOT_PASSWORD="$USER_PASSWORD"
echo -n "Type your personal username: "
read PERSONAL_USER
ask_password "$PERSONAL_USER"
PERSONAL_PASSWORD="$USER_PASSWORD"
echo -n "Type the machine hostname: "
read MACHINE_HOSTNAME
}
2021-08-10 13:45:40 -04:00
post_install() {
curl -sL "$POST_INSTALL_SCRIPT" -o post-install.sh
2021-08-10 14:04:03 -04:00
mv post-install.sh /mnt/root
chmod +x /mnt/root/post-install.sh
2021-08-11 18:57:28 -04:00
print_phase "Post installation"
right_chroot /mnt /root/post-install.sh -nu "$PERSONAL_USER"
2021-08-10 13:45:40 -04:00
}
2021-08-09 19:53:23 -04:00
configure() {
print_phase "System configuration"
set_timezone
set_locale
setup_grub
setup_users
setup_network
2021-08-09 14:48:22 -04:00
}
2021-08-09 10:44:29 -04:00
main() {
2021-08-10 11:47:14 -04:00
prompt_all
2021-08-09 14:48:22 -04:00
partition
install_base
configure
2021-08-10 13:45:40 -04:00
post_install
umount -R /mnt
echo -n "Ready to reboot. Press any key to continue..."
read dummy
reboot
2021-08-09 10:44:29 -04:00
}
2021-05-15 17:55:45 -04:00
2021-08-09 10:44:29 -04:00
main