stage2.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/bin/bash
  2. # ASSUMED STATE OF TARGET SYSTEM:
  3. # - internet access
  4. # - root user login (tty)
  5. # - ~10 GB of free disk space
  6. # - secussfully installed stage1
  7. # - changed firmware settings
  8. error_exit() {
  9. echo -e "\e[0;30;101m $1\e[0m"
  10. exit 1
  11. }
  12. pacman_error_exit() {
  13. error_exit "Error: Pacman command was not successfull. Exiting ..."
  14. }
  15. cd_error_exit() {
  16. echo -e "\e[0;30;46m Current working directory: \e[0m"
  17. pwd
  18. error_exit "\e[0;30;101m Error: Could not change into '$1'. Exiting ...\e[0m"
  19. }
  20. cd_into() {
  21. cd "$1" || cd_error_exit "$1"
  22. }
  23. sb_status="$(sbctl status)"
  24. echo "$sb_status" | grep "^Setup Mode:" | grep -q "Disabled" || error_exit "Error: Secure Boot in Setup Mode. Please change UEFI settings."
  25. echo "$sb_status" | grep "^Secure Boot:" | grep -q "Enabled" || error_exit "Error: Secure Boot disabled. Please change UEFI settings."
  26. # TODO: re-enable this after stopping the rollout of vendor keys
  27. # echo "$sb_status" | grep "^Vendor Keys:" | grep -q "none" || error_exit "Error: Vendor Keys present. Please change UEFI settings."
  28. grep -q "^2$" /sys/class/tpm/tpm*/tpm_version_major || error_exit "Error: No tpm2 devices found."
  29. drive2_uuid="$(sed 's/^.*cryptdevice=UUID=//; s/:cryptroot.*$//' /etc/kernel/cmdline)"
  30. drive2_drive="$(blkid | grep "$drive2_uuid" | tr ' ' '\n' | grep '^.*:$' | sed 's/://')"
  31. systemd-cryptenroll --tpm2-device=auto --tpm2-pcrs=0+7 "$drive2_drive" || error_exit "Error: Failed to enroll luks2 key into tpm2"
  32. sed -i 's/block encrypt/block sd-encrypt/' /etc/mkinitcpio.conf
  33. sed -i 's/base udev/base systemd/' /etc/mkinitcpio.conf
  34. sed -i 's/keyboard keymap consolefont/keyboard sd-vconsole/' /etc/mkinitcpio.conf
  35. sed -i "s/cryptdevice=UUID=$drive2_uuid:cryptroot/rd.luks.name=$drive2_uuid=cryptroot/" /etc/kernel/cmdline
  36. mkinitcpio -P || error_exit "Error: Failed to update mkinitcpio"
  37. # install git, vim, stow, opendoas and (base-devel minus sudo)
  38. echo -e "\e[0;30;34mInstalling some initial packages ...\e[0m"
  39. pacman -Sy --noconfirm --needed git vim opendoas autoconf automake binutils bison fakeroot file findutils flex gawk gcc gettext grep groff gzip libtool m4 make pacman patch pkgconf sed texinfo which libxft stow || error_exit "Error at script start:\n\nAre you sure you're running this as the root user?\n\t(Tip: run 'whoami' to check)\n\nAre you sure you have an internet connection?\n\t(Tip: run 'ip a' to check)\n\e[0m"
  40. setup_temporary_doas() {
  41. echo -e "\e[0;30;34mSetting up temporary doas config ...\e[0m"
  42. printf "permit nopass :wheel
  43. permit nopass root as %s\n" "$username" > /etc/doas.conf
  44. chown -c root:root /etc/doas.conf
  45. chmod -c 0400 /etc/doas.conf
  46. }
  47. create_videopc_user() {
  48. if ls /home/ | grep -q "^$username$"; then
  49. return
  50. fi
  51. while true; do
  52. passwd && break
  53. done
  54. echo -e "\e[0;30;34mCreating videopc user ...\e[0m"
  55. username="videopc"
  56. useradd -m -g users -G wheel "$username"
  57. }
  58. add_user_to_groups() {
  59. if ! groups "$username" | grep "input" | grep -q "video"; then
  60. echo -e "\e[0;30;34mAdding $username to video and input groups ... \e[0m"
  61. usermod -aG video "$username"
  62. usermod -aG input "$username"
  63. fi
  64. }
  65. make_user_owner_of_HOME_and_mnt_dirs() {
  66. echo -e "\e[0;30;34mChanging ownership of /home/$username + /mnt ...\e[0m"
  67. chown -R "$username":users /home/"$username"/
  68. chown -R "$username":users /mnt/
  69. }
  70. aur_build() {
  71. cd_into /home/"$username"/.local/src
  72. doas -u "$username" git clone https://aur.archlinux.org/"$1".git
  73. cd_into "$1"
  74. doas -u "$username" makepkg --noconfirm -si || exit 1
  75. }
  76. create_videopc_user
  77. # create ~/ directories
  78. echo -e "\e[0;30;34mCreating ~/ directories ...\e[0m"
  79. mkdir -vp /home/"$username"/.local/bin /home/"$username"/.config
  80. mkdir -vp /home/"$username"/.local/share /home/"$username"/.local/src
  81. echo -e "\e[0;30;34mChanging ownership of /home/$username ...\e[0m"
  82. chown -R "$username":users /home/"$username"/* /home/"$username"/.*
  83. setup_temporary_doas
  84. add_user_to_groups
  85. # add xdg-repo
  86. if ! grep -q "^\s*\[xdg-repo\]\s*$" /etc/pacman.conf; then
  87. echo -e "\e[0;30;34mAdding Noah's xdg-repo ...\e[0m"
  88. pacman-key --recv-keys 7FA7BB604F2A4346 --keyserver keyserver.ubuntu.com
  89. pacman-key --lsign-key 7FA7BB604F2A4346
  90. echo "[xdg-repo]
  91. Server = https://noahvogt.com/\$repo/\$arch" >> /etc/pacman.conf
  92. fi
  93. # fetch + apply dotfiles
  94. if [ ! -d /home/"$username"/.local/src/dotfiles ]; then
  95. echo -e "\e[0;30;34mFetching dotfiles ...\e[0m"
  96. cd_into /home/"$username"/.local/src
  97. while true; do
  98. git clone https://git.noahvogt.com/noah/videopc-infra.git && break
  99. done
  100. else
  101. echo -e "\e[0;30;34mUpdating dotfiles ...\e[0m"
  102. cd_into /home/"$username"/.local/src/dotfiles
  103. while true; do
  104. git pull && break
  105. done
  106. fi
  107. mv /home/"$username"/.local/src/videopc-infra /home/"$username"/.local/src/dotfiles
  108. cd_into /home/"$username"/.local/src/dotfiles
  109. echo -e "\e[0;30;34mApplying dotfiles ...\e[0m"
  110. doas -u "$username" /home/"$username"/.local/src/dotfiles/apply-dotfiles
  111. # download packages from the official repos
  112. echo -e "\e[0;30;34mInstalling packages from repos ...\e[0m"
  113. pacman -Sy --noconfirm --needed neovim pulseaudio-alsa mpv xf86-video-amdgpu xf86-video-intel xf86-video-nouveau curl hyprland kitty opendoas-sudo adwaita-fake-cursors greetd-agreety openssh uvicorn python-fastapi || pacman_error_exit
  114. # install aur packages
  115. echo -e "\e[0;30;34mInstalling packages from AUR ...\e[0m"
  116. aur_build mediamtx-bin || pacman_error_exit
  117. # enable mediamtx service
  118. echo -e "\e[0;30;34mEnabling mediamtx daemon ...\e[0m"
  119. systemctl enable mediamtx
  120. make_user_owner_of_HOME_and_mnt_dirs
  121. # setup user autologin
  122. echo -e "\e[0;30;34mSetting up Autologin ...\e[0m"
  123. systemctl enable greetd
  124. echo '[initial_session]
  125. command = "Hyprland > /dev/null 2> /dev/null"
  126. user = "videopc"' >> /etc/greetd/config.toml
  127. # enable sshd daemon
  128. echo -e "\e[0;30;34mEnabling sshd daemon ...\e[0m"
  129. systemctl enable sshd
  130. # remove root autologin
  131. echo -e "\e[0;30;34mRemoving root autologin ...\e[0m"
  132. rm -rf /etc/systemd/system/getty@tty1.service.d
  133. systemctl reboot --firmware