2022-03-06 12:34:13 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
2022-09-25 15:31:16 +00:00
|
|
|
# Bootstrap a new Arch system from an installation ISO.
|
2022-03-06 12:34:13 +00:00
|
|
|
#
|
|
|
|
# Bootable USB:
|
|
|
|
# - [Download](https://archlinux.org/download/) ISO and GPG files
|
|
|
|
# - Verify the ISO file: `$ pacman-key -v archlinux-<version>-x86_64.iso.sig`
|
|
|
|
# - Create a bootable USB with: `# dd if=archlinux*.iso of=/dev/sdX && sync`
|
|
|
|
#
|
|
|
|
# UEFI setup:
|
|
|
|
#
|
|
|
|
# - Set boot mode to UEFI, disable Legacy mode entirely.
|
|
|
|
# - Temporarily disable Secure Boot.
|
|
|
|
# - Make sure a strong UEFI administrator password is set.
|
|
|
|
# - Delete preloaded OEM keys for Secure Boot, allow custom ones.
|
|
|
|
#
|
|
|
|
# Run installation:
|
|
|
|
#
|
2022-09-25 15:31:16 +00:00
|
|
|
# - Connect to wifi via: `# iwctl station wlan0 connect $SSID`
|
2022-03-06 12:34:13 +00:00
|
|
|
# - Run: `# bash <(curl -sL https://link.rafe.li/dot)`
|
|
|
|
#
|
|
|
|
|
2022-09-25 15:31:16 +00:00
|
|
|
set -xeuo pipefail
|
2022-03-06 12:34:13 +00:00
|
|
|
trap 's=$?; echo "$0: Error on line "$LINENO": $BASH_COMMAND"; exit $s' ERR
|
|
|
|
|
2022-09-25 15:31:16 +00:00
|
|
|
exec 1> >(tee "/root/stdout.log")
|
|
|
|
exec 2> >(tee "/root/stderr.log" >&2)
|
|
|
|
|
|
|
|
args=()
|
|
|
|
target_device=""
|
|
|
|
new_hostname=""
|
|
|
|
|
|
|
|
while [[ $# -gt 0 ]]
|
|
|
|
do
|
|
|
|
arg="$1"
|
|
|
|
|
|
|
|
case "$arg" in
|
|
|
|
"--device")
|
|
|
|
target_device="$2"
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
"--hostname")
|
|
|
|
new_hostname="$2"
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
args+=("$arg")
|
|
|
|
shift;
|
|
|
|
esac
|
|
|
|
done
|
2022-03-06 12:34:13 +00:00
|
|
|
|
2022-09-25 15:31:16 +00:00
|
|
|
if [[ -z "$target_device" ]]; then
|
|
|
|
echo "Missing --device <device> argument" >&2
|
|
|
|
exit 2;
|
|
|
|
fi
|
2022-03-06 12:34:13 +00:00
|
|
|
|
2022-09-25 15:31:16 +00:00
|
|
|
if [[ -z "$new_hostname" ]]; then
|
|
|
|
echo "Missing --hostname <hostname> argument" >&2
|
|
|
|
exit 2;
|
|
|
|
fi
|
2022-03-06 12:34:13 +00:00
|
|
|
|
2022-09-25 15:31:16 +00:00
|
|
|
if [[ "${#args[@]}" -ne 0 ]]; then
|
|
|
|
echo "Unexpected extra arguments: ${args[*]}" >&2
|
|
|
|
exit 2
|
|
|
|
fi
|
2022-03-06 12:34:13 +00:00
|
|
|
|
2022-09-25 15:31:16 +00:00
|
|
|
if [ ! -f /sys/firmware/efi/fw_platform_size ]; then
|
|
|
|
echo >&2 "You must boot in UEFI mode to continue"
|
|
|
|
exit 2
|
|
|
|
fi
|
2022-03-06 12:34:13 +00:00
|
|
|
|
2022-09-25 15:31:16 +00:00
|
|
|
if [[ "$UID" -ne 0 ]]; then
|
|
|
|
echo "This script needs to be run as root!" >&2
|
|
|
|
exit 3
|
|
|
|
fi
|
2022-03-06 12:34:13 +00:00
|
|
|
|
2022-09-25 15:31:16 +00:00
|
|
|
read -rp "THIS SCRIPT WILL OVERWRITE ALL CONTENTS OF ${target_device}. Type uppercase yes to continue: " confirmed
|
2022-03-06 12:34:13 +00:00
|
|
|
|
2022-09-25 15:31:16 +00:00
|
|
|
if [[ "$confirmed" != "YES" ]]; then
|
|
|
|
echo "aborted" >&2
|
|
|
|
exit 128
|
|
|
|
fi
|
2022-03-06 12:34:13 +00:00
|
|
|
|
2022-09-25 15:31:16 +00:00
|
|
|
timedatectl set-ntp true
|
|
|
|
hwclock --systohc --utc
|
|
|
|
loadkeys de-latin1
|
2022-03-06 12:34:13 +00:00
|
|
|
|
2022-09-25 15:31:16 +00:00
|
|
|
# Partition
|
|
|
|
sgdisk --zap-all "${target_device}"
|
2022-09-11 15:16:16 +00:00
|
|
|
sgdisk --clear \
|
2022-09-25 15:31:16 +00:00
|
|
|
--new 1:0:+550MiB --typecode 1:ef00 --change-name 1:EFI \
|
|
|
|
--new 2:0:+8GiB --typecode 2:8200 --change-name 2:swap \
|
|
|
|
--new 3 --typecode 3:8304 --change-name 3:system \
|
|
|
|
"${target_device}"
|
2022-03-06 12:34:13 +00:00
|
|
|
|
2022-09-25 15:31:16 +00:00
|
|
|
# Reload partition table
|
|
|
|
sleep 5
|
|
|
|
partprobe -s "${target_device}"
|
|
|
|
sleep 3
|
2022-09-11 15:16:16 +00:00
|
|
|
|
2022-09-25 15:31:16 +00:00
|
|
|
# Encrypt root
|
|
|
|
echo -n "password" | cryptsetup luksFormat --type luks2 --pbkdf argon2id "/dev/disk/by-partlabel/system"
|
|
|
|
echo -n "password" | cryptsetup luksOpen --allow-discards --persistent "/dev/disk/by-partlabel/system" system
|
2022-09-11 15:16:16 +00:00
|
|
|
|
2022-09-25 15:31:16 +00:00
|
|
|
# Create file systems
|
|
|
|
mkfs.fat -F 32 -n "EFI" /dev/disk/by-partlabel/EFI
|
2022-09-11 15:16:16 +00:00
|
|
|
mkfs.btrfs --force --label system /dev/mapper/system
|
2022-03-06 12:34:13 +00:00
|
|
|
|
2022-09-25 15:31:16 +00:00
|
|
|
# Mount system subvolume and create additional subvolumes
|
2022-09-11 15:16:16 +00:00
|
|
|
o=defaults,x-mount.mkdir
|
|
|
|
o_btrfs=$o,compress=zstd,ssd,noatime
|
|
|
|
|
2022-09-11 16:09:29 +00:00
|
|
|
mount -t btrfs LABEL=system /mnt
|
2022-09-25 15:31:16 +00:00
|
|
|
btrfs subvolume create /mnt/@ # /
|
|
|
|
btrfs subvolume create /mnt/@home # /home
|
|
|
|
btrfs subvolume create /mnt/@snapshots # /.snapshots
|
|
|
|
btrfs subvolume create /mnt/@pkg # /var/cache/pacman/pkg
|
|
|
|
btrfs subvolume create /mnt/@aurbuild # /var/lib/aurbuild
|
|
|
|
btrfs subvolume create /mnt/@archbuild # /var/lib/archbuild
|
|
|
|
btrfs subvolume create /mnt/@log # /var/log
|
|
|
|
btrfs subvolume create /mnt/@tmp # /var/tmp
|
2022-09-11 16:41:07 +00:00
|
|
|
|
2022-09-25 15:31:16 +00:00
|
|
|
umount -R /mnt
|
|
|
|
mount -t btrfs -o subvol=@,$o_btrfs LABEL=system /mnt
|
|
|
|
mount -t btrfs -o subvol=@home,$o_btrfs,nodatacow LABEL=system /mnt/home
|
|
|
|
mount -t btrfs -o subvol=@snapshots,$o_btrfs LABEL=system /mnt/.snapshots
|
|
|
|
mount -t btrfs -o subvol=@pkg,$o_btrfs LABEL=system /mnt/var/cache/pacman/pkg
|
|
|
|
mount -t btrfs -o subvol=@aurbuild,$o_btrfs LABEL=system /mnt/var/lib/aurbuild
|
|
|
|
mount -t btrfs -o subvol=@archbuild,$o_btrfs LABEL=system /mnt/var/lib/archbuild
|
|
|
|
mount -t btrfs -o subvol=@log,$o_btrfs LABEL=system /mnt/var/log
|
|
|
|
mount -t btrfs -o subvol=@tmp,$o_btrfs LABEL=system /mnt/var/tmp
|
|
|
|
|
|
|
|
# Mount additional partitions
|
2022-09-11 16:44:26 +00:00
|
|
|
mount -o $o LABEL=EFI /mnt/efi
|
2022-03-06 12:34:13 +00:00
|
|
|
|
2022-09-25 15:31:16 +00:00
|
|
|
# Disable CoW for /home due to large loopback files by systemd-homed
|
|
|
|
chattr +C /mnt/home
|
2022-03-06 12:34:13 +00:00
|
|
|
|
2022-09-25 15:31:16 +00:00
|
|
|
if ! grep "# Installer cache" /etc/pacman.conf > /dev/null; then
|
2022-03-06 12:34:13 +00:00
|
|
|
cat >> /etc/pacman.conf << EOF
|
2022-09-25 15:31:16 +00:00
|
|
|
# Installer cache
|
2022-03-06 12:34:13 +00:00
|
|
|
[options]
|
|
|
|
CacheDir = /mnt/var/cache/pacman/pkg
|
|
|
|
EOF
|
|
|
|
fi
|
|
|
|
|
2022-09-25 15:31:16 +00:00
|
|
|
# Bootstrap new chroot
|
|
|
|
reflector --country 'Germany' --protocol https --sort age --latest 5 --save /etc/pacman.d/mirrorlist
|
|
|
|
pacstrap /mnt base linux linux-firmware intel-ucode btrfs-progs dracut neovim iwd networkmanager
|
|
|
|
|
2022-09-11 15:16:16 +00:00
|
|
|
genfstab -L -p /mnt >> /mnt/etc/fstab
|
|
|
|
|
2022-09-25 15:31:16 +00:00
|
|
|
# Configure timezone, locale, keymap, network
|
2022-09-11 18:05:44 +00:00
|
|
|
sed -i 's/^#en_US\.UTF-8/en_US\.UTF-8/' /mnt/etc/locale.gen
|
|
|
|
sed -i 's/^#de_DE\.UTF-8/de_DE\.UTF-8/' /mnt/etc/locale.gen
|
|
|
|
arch-chroot /mnt locale-gen
|
|
|
|
arch-chroot /mnt systemd-firstboot \
|
2022-09-11 15:16:16 +00:00
|
|
|
--locale="en_US.UTF-8" \
|
|
|
|
--keymap="de-latin1" \
|
|
|
|
--timezone="Europe/Berlin" \
|
2022-09-25 15:31:16 +00:00
|
|
|
--hostname="${new_hostname}" \
|
2022-09-11 15:16:16 +00:00
|
|
|
--setup-machine-id
|
2022-09-11 18:05:44 +00:00
|
|
|
echo -e "127.0.0.1\tlocalhost" >> /mnt/etc/hosts
|
2022-09-25 15:31:16 +00:00
|
|
|
echo -e "127.0.1.1\t$new_hostname" >> /mnt/etc/hosts
|
2022-09-11 18:05:44 +00:00
|
|
|
echo -e "\n::1\tlocalhost" >> /mnt/etc/hosts
|
2022-03-06 12:34:13 +00:00
|
|
|
|
2022-09-25 15:31:16 +00:00
|
|
|
# Use systemd-resolved as dns backend for NetworkManager (auto-detected)
|
|
|
|
ln -sf /run/systemd/resolve/stub-resolv.conf /mnt/etc/resolv.conf
|
2022-03-06 12:34:13 +00:00
|
|
|
|
2022-09-25 15:31:16 +00:00
|
|
|
# Enable iwd as wifi backend for NetworkManager
|
|
|
|
cat > /mnt/etc/NetworkManager/conf.d/wifi-backend.conf <<EOF
|
2022-09-11 15:16:16 +00:00
|
|
|
[device]
|
|
|
|
wifi.backend=iwd
|
|
|
|
EOF
|
2022-09-25 15:31:16 +00:00
|
|
|
|
|
|
|
# Install dracut opt deps required to build unified kernel images
|
|
|
|
arch-chroot /mnt pacman -S --noconfirm --asdeps binutils elfutils
|
|
|
|
arch-chroot /mnt dracut -f --uefi --regenerate-all
|
|
|
|
# Install bootloader
|
|
|
|
arch-chroot /mnt bootctl install
|
|
|
|
|
|
|
|
# Enable resolved
|
|
|
|
systemctl --root /mnt enable systemd-resolved
|
|
|
|
# Enable homed
|
|
|
|
systemctl --root /mnt enable systemd-homed
|
|
|
|
|
|
|
|
# Set root password
|
|
|
|
echo -n "password" | passwd -R /mnt root
|
|
|
|
|
|
|
|
echo "BOOTSTRAPPING FINISHED"
|