stage2.sh 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/bin/bash
  2. # ASSUMED STATE OF TARGET SYSTEM:
  3. # - internet access
  4. # - root user login
  5. # - ~10 GB of free disk space
  6. # working 1.) base 2.) linux/kernel packages
  7. error_exit() {
  8. echo -e "\e[0;30;101m $1\e[0m"
  9. exit 1
  10. }
  11. pacman_error_exit() {
  12. error_exit "Error: Pacman command was not successfull. Exiting ..."
  13. }
  14. cd_error_exit() {
  15. echo -e "\e[0;30;46m Current working directory: \e[0m"
  16. pwd
  17. error_exit "\e[0;30;101m Error: Could not change into '$1'. Exiting ...\e[0m"
  18. }
  19. cd_into() {
  20. cd "$1" || cd_error_exit "$1"
  21. }
  22. sb_status="$(sbctl status)"
  23. echo "$sb_status" | grep "^Setup Mode:" | grep -q "Disabled" || error_exit "Error: Secure Boot in Setup Mode. Please change UEFI settings."
  24. echo "$sb_status" | grep "^Secure Boot:" | grep -q "Enabled" || error_exit "Error: Secure Boot disabled. Please change UEFI settings."
  25. # TODO: re-enable this after stopping the rollout of vendor keys
  26. # echo "$sb_status" | grep "^Vendor Keys:" | grep -q "none" || error_exit "Error: Vendor Keys present. Please change UEFI settings."
  27. grep -q "^2$" /sys/class/tpm/tpm*/tpm_version_major || error_exit "Error: No tpm2 devices found."
  28. systemd-cryptenroll --tpm2-device=auto --tpm2-pcrs=0+7 /dev/sda2 || error_exit "Error: Failed to enroll luks2 key into tpm2"
  29. sed -i 's/block encrypt/block sd-encrypt/' /etc/mkinitcpio.conf
  30. sed -i 's/base udev/base systemd/' /etc/mkinitcpio.conf
  31. sed -i 's/keyboard keymap consolefont/keyboard sd-vconsole/' /etc/mkinitcpio.conf
  32. sda2_uuid="$(blkid | grep sda2 | tr ' ' '\n' | grep ^UUID= | sed 's/^UUID="//; s/"//')"
  33. sed -i "s/cryptdevice=\/dev\/sda2:cryptroot/rd.luks.name=$sda2_uuid=cryptroot/" /etc/kernel/cmdline
  34. mkinitcpio -P || error_exit "Error: Failed to update mkinitcpio"
  35. # install git, vim, stow, opendoas and (base-devel minus sudo)
  36. echo -e "\e[0;30;34mInstalling some initial packages ...\e[0m"
  37. 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"
  38. setup_temporary_doas() {
  39. echo -e "\e[0;30;34mSetting up temporary doas config ...\e[0m"
  40. printf "permit nopass :wheel
  41. permit nopass root as %s\n" "$username" > /etc/doas.conf
  42. chown -c root:root /etc/doas.conf
  43. chmod -c 0400 /etc/doas.conf
  44. }
  45. create_videopc_user() {
  46. if ls /home/ | grep -q "^$username$"; then
  47. return
  48. fi
  49. echo -e "\e[0;30;34mCreating videopc user ...\e[0m"
  50. username="videopc"
  51. useradd -m -g users -G wheel "$username"
  52. while true; do
  53. passwd "$username" && break
  54. done
  55. }
  56. set_rtmp_key() {
  57. echo -e "\e[0;30;34mSetting rtmp key... \e[0m"
  58. while true; do
  59. echo -e "\e[0;30;42m Enter in your RTMP key \e[0m"
  60. read -rp " >>> " rtmp_key
  61. echo "$rtmp_key" > /etc/videopc_rtmp_key
  62. [ -n "$rtmp_key" ] && break
  63. done
  64. }
  65. set_api_key() {
  66. echo -e "\e[0;30;34mSetting api key... \e[0m"
  67. while true; do
  68. echo -e "\e[0;30;42m Enter in your API key \e[0m"
  69. read -rp " >>> " api_key
  70. echo "$api_key" > /etc/videopc_api_key
  71. [ -n "$api_key" ] && break
  72. done
  73. }
  74. add_user_to_groups() {
  75. if ! groups "$username" | grep "input" | grep -q "video"; then
  76. echo -e "\e[0;30;34mAdding $username to video and input groups ... \e[0m"
  77. usermod -aG video "$username"
  78. usermod -aG input "$username"
  79. fi
  80. }
  81. make_user_owner_of_HOME_and_mnt_dirs() {
  82. echo -e "\e[0;30;34mChanging ownership of /home/$username + /mnt ...\e[0m"
  83. chown -R "$username":users /home/"$username"/
  84. chown -R "$username":users /mnt/
  85. }
  86. create_videopc_user
  87. # create ~/ directories
  88. echo -e "\e[0;30;34mCreating ~/ directories ...\e[0m"
  89. mkdir -vp /home/"$username"/dox /home/"$username"/pix /home/"$username"/dl
  90. mkdir -vp /home/"$username"/vids /home/"$username"/mus
  91. mkdir -vp /home/"$username"/.local/bin /home/"$username"/.config
  92. mkdir -vp /home/"$username"/.local/share /home/"$username"/.local/src
  93. echo -e "\e[0;30;34mChanging ownership of /home/$username ...\e[0m"
  94. chown -R "$username":users /home/"$username"/* /home/"$username"/.*
  95. setup_temporary_doas
  96. add_user_to_groups
  97. # add xdg-repo
  98. if ! grep -q "^\s*\[xdg-repo\]\s*$" /etc/pacman.conf; then
  99. echo -e "\e[0;30;34mAdding Noah's xdg-repo ...\e[0m"
  100. pacman-key --recv-keys 7FA7BB604F2A4346 --keyserver keyserver.ubuntu.com
  101. pacman-key --lsign-key 7FA7BB604F2A4346
  102. echo "[xdg-repo]
  103. Server = https://noahvogt.com/\$repo/\$arch" >> /etc/pacman.conf
  104. fi
  105. # add chaotic-aur
  106. if ! grep -q "^\s*\[chaotic-aur\]\s*$" /etc/pacman.conf; then
  107. echo -e "\e[0;30;34mAdding the chaotic aur repo ...\e[0m"
  108. pacman-key --recv-key 3056513887B78AEB --keyserver keyserver.ubuntu.com
  109. pacman-key --lsign-key 3056513887B78AEB
  110. pacman -U --noconfirm --needed 'https://cdn-mirror.chaotic.cx/chaotic-aur/chaotic-keyring.pkg.tar.zst'
  111. pacman -U --noconfirm --needed 'https://cdn-mirror.chaotic.cx/chaotic-aur/chaotic-mirrorlist.pkg.tar.zst'
  112. echo "[chaotic-aur]
  113. Include = /etc/pacman.d/chaotic-mirrorlist" >> /etc/pacman.conf
  114. fi
  115. # fetch + apply dotfiles
  116. if [ ! -d /home/"$username"/.local/src/dotfiles ]; then
  117. echo -e "\e[0;30;34mFetching dotfiles ...\e[0m"
  118. cd_into /home/"$username"/.local/src
  119. while true; do
  120. git clone https://git.noahvogt.com/noah/videopc-infra.git && break
  121. done
  122. else
  123. echo -e "\e[0;30;34mUpdating dotfiles ...\e[0m"
  124. cd_into /home/"$username"/.local/src/dotfiles
  125. while true; do
  126. git pull && break
  127. done
  128. fi
  129. mv /home/"$username"/.local/src/videopc-infra /home/"$username"/.local/src/dotfiles
  130. cd_into /home/"$username"/.local/src/dotfiles
  131. echo -e "\e[0;30;34mApplying dotfiles ...\e[0m"
  132. doas -u "$username" /home/"$username"/.local/src/dotfiles/apply-dotfiles
  133. set_rtmp_key
  134. set_api_key
  135. # download packages from the official repos
  136. echo -e "\e[0;30;34mInstalling packages from repos ...\e[0m"
  137. pacman -Sy --noconfirm --needed neovim ffmpeg pulseaudio-alsa mpv xf86-video-amdgpu xf86-video-intel xf86-video-nouveau coreutils curl hyprland kitty opendoas-sudo adwaita-fake-cursors greetd-agreety openssh uvicorn python-fastapi paru || pacman_error_exit
  138. # install aur packages
  139. echo -e "\e[0;30;34mInstalling packages from AUR ...\e[0m"
  140. doas -u "$username" paru -S --noconfirm --needed mediamtx-bin || pacman_error_exit
  141. # enable mediamtx service
  142. echo -e "\e[0;30;34mEnabling mediamtx daemon ...\e[0m"
  143. systemctl enable mediamtx
  144. make_user_owner_of_HOME_and_mnt_dirs
  145. # setup autologin
  146. echo -e "\e[0;30;34mSetting up Autologin ...\e[0m"
  147. systemctl enable greetd
  148. if ! grep -q "\[initial_session\]" /etc/greetd/config.toml; then
  149. echo '[initial_session]
  150. command = "Hyprland"
  151. user = "videopc"' >> /etc/greetd/config.toml
  152. fi
  153. # enable sshd daemon
  154. echo -e "\e[0;30;34mEnabling sshd daemon ...\e[0m"
  155. systemctl enable sshd
  156. # ~ cleanup
  157. echo -e "\e[0;30;34mCleaning up \$HOME ...\e[0m"
  158. for f in /home/"$username"/.bash*; do
  159. [ -f "$f" ] && rm "$f"
  160. done
  161. for f in /home/"$username"/.less*; do
  162. [ -f "$f" ] && rm "$f"
  163. done