Files
Arch-Linux-Installer/install.sh
T

493 lines
11 KiB
Bash

#!/bin/bash
set -euo pipefail
# Write log
exec >> >(tee -i /tmp/install.log)
exec 2>&1
function cleanup() {
umount /mnt/boot
umount /mnt/home
umount /mnt
cryptsetup close /dev/mapper/main
}
trap cleanup EXIT
clear
# Friendly introduction
echo "0. WELCOME"
echo "Welcome to the Arch Linux installation script!"
echo "This script will ERASE ALL DATA on the partition you will choose next!"
echo
read -p "Do you want to continue? Type [Y]es or [N]o. " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
clear
##########
# Selecting disk
echo "1. PARTITIONING"
echo "Please select a disk to partition:"
echo
# Don't separate values by space
IFS=$'\n'
# Set variable containing name and size of all disks
declare -a dsks=( $(lsblk -d | tail -n+2 | awk '{print $1" "$4}') )
# Select value on array
select dev in "${dsks[@]}"; do
break
done
echo
# Separate values by spaces
IFS=' '
# Create new variable of selection
array=(${dev})
dev="${array[0]}"
clear
##########
echo "2. ENCRYPTION"
echo
# Setting encryption password.
echo "Choose a strong password for encrypting the primary partition:"
pwcr=""
while [[ -z "${pwcr}" ]]; do
echo "Please enter a password: "
read -rs pwfr
read -rs -p "Retype a password: " pwsc
if [[ "${pwfr}" == "${pwsc}" ]];
then
pwcr="${pwfr}"
echo
echo "Both passwords are the same. Continuing.."
break
else
echo
echo "You have entered different passwords. Try again.."
echo
fi
done
clear
##########
echo "3. HOSTNAME"
echo
# Setting hostname.
echo "Before installing the system, please enter a hostname:"
read -r host
clear
##########
echo "4. ROOT PASSWORD"
echo
# Setting root password.
echo "Choose a password for the root account:"
pwr=""
while [[ -z "${pwr}" ]]; do
echo "Please enter a password: "
read -rs pwfr
read -rs -p "Retype a password: " pwsc
if [[ "${pwfr}" == "${pwsc}" ]]; then
pwrt="${pwfr}"
echo
echo "Both passwords are the same. Continuing.."
break
else
echo
echo "You have entered different passwords. Try again.."
echo
fi
done
clear
echo "4. USER"
echo
# Setting username
echo "Please enter a username:"
read -r user
echo
# Setting user password
echo "Also please choose a password for your user:"
pwu=""
while [[ -z "${pwu}" ]]; do
echo "Please enter a password: "
read -rs pwfr
read -rs -p "Retype a password: " pwsc
if [[ "${pwfr}" == "${pwsc}" ]];
then
pwur="${pwfr}"
echo
echo "Both passwords are the same. Continuing.."
break
else
echo
echo "You have entered different passwords. Try again.."
echo
fi
done
clear
##########
echo "5. INSTALLING SYSTEM.."
echo
# Starting partitioning
echo "Partitioning /dev/${dev}.."
echo
# Clearing partition table of selected disk
echo "Clearing existing partitioning table."
sgdisk -Z "/dev/${dev}"
echo
# Creating boot partition
echo "Creating boot partition of 512 MB."
sgdisk -n 1:0:+512M "/dev/${dev}"
echo
# Setting type for EFI
echo "Setting partition type."
sgdisk -t 1:ef00 "/dev/${dev}"
echo
# Creating system partition
echo "Creating system partition."
sgdisk -n 2:0:0 "/dev/${dev}"
echo
# Print partitions
echo "This is your new partition table:"
lsblk | grep "${dev}"
echo
# Get new variable
if [[ "${dev}" = "nvme0n1" ]]; then
main="${dev}p2"
else
main="${dev}2"
fi
# Encrypting partition
echo "Encrypting system partition. This might take a while."
echo
# https://wiki.archlinux.org/title/Dm-crypt/Device_encryption#Encryption_options_for_LUKS_mode
echo -en "${pwcr}\n${pwcr}" | cryptsetup -s 512 luksFormat /dev/$main
echo "Partition successfully encrypted."
echo
# Opening encrypted partition and mounting at /dev/mapper/main
echo "Decrypting.. This also might take a while."
echo
echo -en "${pwcr}\n${pwcr}" | cryptsetup luksOpen "/dev/${main}" main
echo "Partition successfully opened."
echo
lsblk | grep "${dev}"
echo
##########
echo "Creating the filesystem."
if [ "${dev}" = "nvme0n1" ]; then
boot="${dev}p1"
else
boot="${dev}1"
fi
# Create boot filesystem
mkfs.fat -F 32 -n UEFI "/dev/${boot}"
echo "Filesystem for boot successfully created."
echo
# Creating btrfs filesystem
mkfs.btrfs "/dev/mapper/main"
mount "/dev/mapper/main" "/mnt"
btrfs subvolume create "/mnt/root"
btrfs subvolume create "/mnt/home"
umount "/mnt"
echo
echo "Mounting.."
mount -o autodefrag,compress=zstd:3,subvol=root "/dev/mapper/main" "/mnt"
mkdir "/mnt/home"
mount -o autodefrag,compress=zstd:3,subvol=home "/dev/mapper/main" "/mnt/home"
mkdir "/mnt/boot"
mount "/dev/${boot}" "/mnt/boot"
echo "Mounting complete."
echo
lsblk -a
echo
##########
# Updating mirrors using reflector
# https://wiki.archlinux.org/title/Installation_guide#Select_the_mirrors
echo "Updating mirrors.."
reflector --verbose --latest 10 --country Germany --age 24 --protocol https --sort rate --save /etc/pacman.d/mirrorlist
echo "Done."
echo
# Check if keyring is up-to-date
if checkupdates | grep -q archlinux-keyring; then
echo "Updating keyring.."
pacman -Sy archlinux-keyring --noconfirm
echo "Done."
fi
echo
# Installing base system
# https://wiki.archlinux.org/title/Installation_guide#Install_essential_packages
echo "Installing basic packages.."
# Base packages
base_packages=(
base # Arch Linux Base
btrfs-progs # Btrfs
firefox
konsole # Terminal
linux
linux-firmware
linux-zen
nano # Editor
networkmanager
plasma-login-manager
sudo
xorg-xwayland
plasma-desktop
plasma-nm # Network manager applet
)
# Necessary Plasma integrations
plasma_integrations=(
plasma-pa # Audio applet
breeze-gtk # Widget theme
kde-gtk-config # Syncs KDE settings to GTK applications
kdeplasma-addons # Misc addons: https://github.com/KDE/kdeplasma-addons
kscreen # Adds screen management to settings
plasma-browser-integration
powerdevil # Power management
)
# KDE Utilities
kde_utils=(
ark # Archiving tool
bluedevil # Bluetooth
discover # GUI package management
dolphin # File browser
filelight # Disk usage analyzer
gwenview # Image viewer
kate # Text editor
kcalc # Calculator
kdeconnect # Mobile synchronisation tool
kinfocenter # System information
#kmag # Doesn't work on Wayland
plasma-systemmonitor
okular # PDF viewer
partitionmanager # partitioning utility
spectacle # Screenshot utility
)
# Additional default software
default_software=(
libreoffice
pipewire
pipewire-pulse
pipewire-alsa
wireplumber
)
# Put all packages together
all_packages=(
"${base_packages[@]}"
"${plasma_integrations[@]}"
"${kde_utils[@]}"
"${default_software[@]}"
)
# Install packages
pacstrap "/mnt" "${all_packages[@]}"
echo "Base system installed."
echo
##########
# Generating fstab
# https://wiki.archlinux.org/title/Installation_guide#Fstab
echo "Generating fstab.."
genfstab -Up "/mnt" > "/mnt/etc/fstab"
echo "Done."
echo
# Set hardware clock
# https://wiki.archlinux.org/title/Installation_guide#Time
arch-chroot "/mnt" hwclock --systohc # Set the Hardware Clock to the current System Time
# Generate locale
# https://wiki.archlinux.org/title/Installation_guide#Localization
echo "en_US.UTF-8" >> "/mnt/etc/locale.gen"
arch-chroot "/mnt" locale-gen
# Set keymap, locale, timezone and hostname
# https://wiki.archlinux.org/title/Systemd-firstboot
# service_dir="/mnt/etc/systemd/system/systemd-firstboot.service.d"
# mkdir "${service_dir}"
rootdir="/mnt"
keymap="de-latin1-nodeadkeys"
locale="en_US.UTF-8"
timezone="Europe/Berlin"
arch-chroot "/mnt" systemd-firstboot --root="${rootdir}" --keymap="${keymap}" --locale="${locale}" --timezone="${timezone}" --hostname="${host}"
# cat << EOF > "${service_dir}/install.conf"
# [Service]
# ExecStart=
# ExecStart=/usr/bin/systemd-firstboot --root="${rootdir}" --keymap="${keymap}" --locale="${locale}" --timezone="${timezone}" --hostname="${host}"
# [Install]
# WantedBy=sysinit.target
# EOF
# systemctl enable systemd-firstboot.service
##########
# Editing mkinitcpio.conf
echo "Editing /etc/mkinitcpio.conf.."
sed -i '55s/.*/HOOKS=(base udev autodetect microcode modconf kms block keyboard keymap encrypt filesystems fsck shutdown)/' /mnt/etc/mkinitcpio.conf
echo "Done."
echo
# Editing taskbar
#sed -i '65s/.*/launchers=applications:systemsettings.desktop,applications:org.kde.konsole.desktop,applications:firefox.desktop/' /mnt/home/${user}/.config/plasma-org.kde.plasma.desktop-appletsrc
# Creating inital ramdisk
# https://wiki.archlinux.org/title/Installation_guide#Initramfs
echo "Creating inital ramdisk for linux-zen.."
arch-chroot /mnt mkinitcpio -p linux-zen
echo "Done."
echo
# Enable network and display manager
echo "Enabling NetworkManager and Plasma Login Manager.."
arch-chroot "/mnt" systemctl enable NetworkManager plasmalogin
echo "Done."
echo
##########
# Set root password
# https://wiki.archlinux.org/title/Installation_guide#Root_password
echo "Setting root password.."
echo -en "${pwrt}\n${pwrt}" | arch-chroot "/mnt" passwd
echo "Done."
echo
# Install bootloader
# https://wiki.archlinux.org/title/Installation_guide#Boot_loader
echo "Installing systemd-boot.."
arch-chroot "/mnt" bootctl --path=/boot install
echo "Done."
echo
# Get UUID for primary partition
uuid=$(ls -l /dev/disk/by-uuid | grep "${main}" | awk '{print $9}')
# Set bootloader entry
echo "Setting up bootloader.."
cat << EOF > "/mnt/boot/loader/entries/arch.conf"
title Arch Linux
linux /vmlinuz-linux-zen
#initrd /amd-ucode.img
#initrd /intel-ucode.img
initrd /initramfs-linux-zen.img
options cryptdevice=UUID=${uuid}:main
options root=/dev/mapper/main rw
options rootflags=subvol=root
options lang=de locale=de_DE.UTF-8
options init=/usr/lib/systemd/systemd
EOF
# Set bootloader fallback entry
cat << EOF > "/mnt/boot/loader/entries/arch-fallback.conf"
title Arch Linux Fallback
linux /vmlinuz-linux
initrd /initramfs-linux-fallback.img
options cryptdevice=UUID=${uuid}:main
options root=/dev/mapper/main rw
options rootflags=subvol=root
options lang=de locale=de_DE.UTF-8
options init=/usr/lib/systemd/systemd
EOF
# Set bootloader
echo "timeout 1" >> "/mnt/boot/loader/loader.conf"
echo "default arch.conf" >> "/mnt/boot/loader/loader.conf"
echo "Done."
echo
# Set username, password and group
echo "Adding user.."
arch-chroot /mnt useradd -m "${user}"
echo -en "${pwur}\n${pwur}" | arch-chroot /mnt passwd "${user}"
arch-chroot /mnt gpasswd -a "${user}" wheel
sed -i '82s/.*/%wheel ALL=(ALL) ALL/' /mnt/etc/sudoers
echo "Done."
echo
# Enabling plasma login manager auto login
echo "Enabling auto login.."
cat << EOF > /mnt/etc/plasmalogin.conf
[Autologin]
Session=plasma.desktop
User=${user}
EOF
echo "Done."
echo
clear
echo "Installation finished!"
#read -p "Do you want to read the log? Type [Y]es or [N]o. " -n 1 -r
#echo
#if [[ ! $REPLY =~ ^[Yy]$ ]]
#then
# less /tmp/install.log
#fi
#echo
#echo "Press [Enter] to shut down, then remove the installation media and start your system."
#poweroff