Skip to content

Instantly share code, notes, and snippets.

@irozgar
Last active June 18, 2023 17:24
Show Gist options
  • Save irozgar/a6b60ba1ac533b890985158cf75d7161 to your computer and use it in GitHub Desktop.
Save irozgar/a6b60ba1ac533b890985158cf75d7161 to your computer and use it in GitHub Desktop.
Arch installer
#!/usr/bin/env bash
#
#
# Credits: This script is heavily inspired by this post https://disconnected.systems/blog/archlinux-installer
set -uo pipefail
trap 's=$?; echo "$0: Error happened in script at line $LINENO: $BASH_COMMAND"; exit $s' ERR
KEYMAP=${KEYMAP:-us}
LOCALE=${LOCALE:-en_US}
loadkeys "$KEYMAP"
reflector --threads 4 --fastest 10 --country Spain,France,Italy,Germany > /etc/pacman.d/mirrorlist
pacman -Sy --noconfirm dialog
hostname=$(dialog --stdout --inputbox "Enter hostname" 0 0) || exit 1
clear
: ${hostname:?"The hostname cannot be empty"}
user=$(dialog --stdout --inputbox "Enter username for the admin user" 0 0) || exit 1
clear
: ${user:?"The username cannot be empty"}
password=$(dialog --stdout --passwordbox "Enter password for the user" 0 0) || exit 1
clear
: ${password:?"The password cannot be empty"}
password2=$(dialog --stdout --passwordbox "Repeat password for the user" 0 0) || exit 1
clear
[[ "$password" == "$password2" ]] || (echo "Password did not match"; exit 1;)
devicelist=$(lsblk -dplnx size -o name,size | grep -Ev "boot|rpmb|loop" | tac)
device=$(dialog --stdout --menu "Select installation disk. IMPORTANT: Data will be lost and the disk will be encrypted with luks." 0 0 0 ${devicelist}) || exit 1
### Log stdout and stderr to files to make them easier to navigate
exec 1> >(tee "stdout.log")
exec 2> >(tee "stderr.log")
timedatectl set-ntp true
## System partitions
parted --script "${device}" -- mklabel gpt \
mkpart ESP fat32 1Mib 500MiB \
set 1 boot on \
mkpart primary 500MiB 100%
efi_part="$(ls ${device}* | grep -E "^${device}p?1$")"
encrypted_part="$(ls ${device}* | grep -E "^${device}p?2$")"
wipefs "${efi_part}"
wipefs "${encrypted_part}"
### Filesystems
mkfs.vfat -F32 "${efi_part}"
cryptsetup luksFormat "${encrypted_part}"
cryptsetup open --type luks "${encrypted_part}" lvm
pvcreate --dataalignment 1m /dev/mapper/lvm
vgcreate volgroup0 /dev/mapper/lvm
lvcreate -L 60GB volgroup0 -n lv_root
lvcreate -l 100%FREE volgroup0 -n lv_home
mkfs.ext4 /dev/volgroup0/lv_root
mkfs.ext4 /dev/volgroup0/lv_home
## Install system
mount /dev/volgroup0/lv_root /mnt
mkdir /mnt/home
mount /dev/volgroup0/lv_home /mnt/home
mkdir /mnt/boot
mount "${efi_part}" /mnt/boot
pacstrap /mnt base linux linux-lts linux-firmware sudo zsh lvm2 vim iwd
## Configure
genfstab -t PARTUUID /mnt >> /mnt/etc/fstab
less /mnt/etc/fstab
echo "${hostname}" > /mnt/etc/hostname
sed -i "/${LOCALE}.UTF-8/s/^#//g" /mnt/etc/locale.gen
arch-chroot /mnt locale-gen
echo "LANG=${LOCALE}.UTF-8" > /mnt/etc/locale.conf
echo "KEYMAP=$KEYMAP" > /mnt/etc/vconsole.conf
#echo "An editor will open with the mkinitcpio configuration."
#echo "In this file you have to ensure that the hooks needed for luks and lvm are present".
#echo "Example: HOOKS=(base udev autodetect keyboard keymap modconf block encrypt lvm2 filesystems fsck)"
#echo "Press enter to continue:"
#read
sed -i -e 's/HOOKS=(.*)/HOOKS=(base udev autodetect keyboard keymap modconf block encrypt lvm2 filesystems fsck)/' /mnt/etc/mkinitcpio.conf
#vim /mnt/etc/mkinitcpio.conf
arch-chroot /mnt mkinitcpio -p linux
arch-chroot /mnt mkinitcpio -p linux-lts
arch-chroot /mnt useradd -mU -s /usr/bin/zsh -G wheel "${user}"
echo "${user}:${password}" | chpasswd --root /mnt
echo "root:${password}" | chpasswd --root /mnt
arch-chroot /mnt visudo
arch-chroot /mnt echo "127.0.0.1 localhost" >> /etc/hosts
## Install bootloader
mkdir /mnt/etc/pacman.d/hooks
cat <<EOF > /mnt/etc/pacman.d/hooks/100-systemd-boot.hook
[Trigger]
Type = Package
Operation = Upgrade
Target = systemd
[Action]
Description = Updating systemd-boot
When = PostTransaction
Exec = /usr/bin/bootctl update
EOF
arch-chroot /mnt bootctl install
cat <<EOF > /mnt/boot/loader/loader.conf
default arch
EOF
cat <<EOF > /mnt/boot/loader/entries/arch.conf
title Arch Linux
linux /vmlinuz-linux
initrd /initramfs-linux.img
options cryptdevice=PARTUUID=$(blkid -s PARTUUID -o value "${encrypted_part}"):lvm root=/dev/volgroup0/lv_root rw
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment