stage2.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/tmp*/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. mkinitcpio -P || error_exit "Error: Failed to update mkinitcpio"
  33. # install git, vim, stow, opendoas and (base-devel minus sudo)
  34. echo -e "\e[0;30;34mInstalling some initial packages ...\e[0m"
  35. 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"
  36. setup_temporary_doas() {
  37. echo -e "\e[0;30;34mSetting up temporary doas config ...\e[0m"
  38. printf "permit nopass :wheel
  39. permit nopass root as %s\n" "$username" > /etc/doas.conf
  40. chown -c root:root /etc/doas.conf
  41. chmod -c 0400 /etc/doas.conf
  42. }
  43. create_videopc_user() {
  44. if ls /home/ | grep -q "^$username$"; then
  45. return
  46. fi
  47. echo -e "\e[0;30;34mCreating videopc user ...\e[0m"
  48. username="videopc"
  49. useradd -m -g users -G wheel "$username"
  50. while true; do
  51. passwd "$username" && break
  52. done
  53. }
  54. set_rtmp_key() {
  55. echo -e "\e[0;30;34mSetting rtmp key... \e[0m"
  56. while true; do
  57. echo -e "\e[0;30;42m Enter in your RTMP key \e[0m"
  58. read -rp " >>> " rtmp_key
  59. echo "$rtmp_key" > /etc/videopc_rtmp_key
  60. [ -n "$rtmp_key" ] && break
  61. done
  62. }
  63. set_api_key() {
  64. echo -e "\e[0;30;34mSetting api key... \e[0m"
  65. while true; do
  66. echo -e "\e[0;30;42m Enter in your API key \e[0m"
  67. read -rp " >>> " api_key
  68. echo "$api_key" > /etc/videopc_api_key
  69. [ -n "$api_key" ] && break
  70. done
  71. }
  72. add_user_to_groups() {
  73. if ! groups "$username" | grep "input" | grep -q "video"; then
  74. echo -e "\e[0;30;34mAdding $username to video and input groups ... \e[0m"
  75. usermod -aG video "$username"
  76. usermod -aG input "$username"
  77. fi
  78. }
  79. make_user_owner_of_HOME_and_mnt_dirs() {
  80. echo -e "\e[0;30;34mChanging ownership of /home/$username + /mnt ...\e[0m"
  81. chown -R "$username":users /home/"$username"/
  82. chown -R "$username":users /mnt/
  83. }
  84. create_videopc_user
  85. # create ~/ directories
  86. echo -e "\e[0;30;34mCreating ~/ directories ...\e[0m"
  87. mkdir -vp /home/"$username"/dox /home/"$username"/pix /home/"$username"/dl
  88. mkdir -vp /home/"$username"/vids /home/"$username"/mus
  89. mkdir -vp /home/"$username"/.local/bin /home/"$username"/.config
  90. mkdir -vp /home/"$username"/.local/share /home/"$username"/.local/src
  91. echo -e "\e[0;30;34mChanging ownership of /home/$username ...\e[0m"
  92. chown -R "$username":users /home/"$username"/* /home/"$username"/.*
  93. setup_temporary_doas
  94. add_user_to_groups
  95. # add xdg-repo
  96. if ! grep -q "^\s*\[xdg-repo\]\s*$" /etc/pacman.conf; then
  97. echo -e "\e[0;30;34mAdding Noah's xdg-repo ...\e[0m"
  98. pacman-key --recv-keys 7FA7BB604F2A4346 --keyserver keyserver.ubuntu.com
  99. pacman-key --lsign-key 7FA7BB604F2A4346
  100. echo "[xdg-repo]
  101. Server = https://noahvogt.com/\$repo/\$arch" >> /etc/pacman.conf
  102. fi
  103. # add chaotic-aur
  104. if ! grep -q "^\s*\[chaotic-aur\]\s*$" /etc/pacman.conf; then
  105. echo -e "\e[0;30;34mAdding the chaotic aur repo ...\e[0m"
  106. pacman-key --recv-key 3056513887B78AEB --keyserver keyserver.ubuntu.com
  107. pacman-key --lsign-key 3056513887B78AEB
  108. pacman -U --noconfirm --needed 'https://cdn-mirror.chaotic.cx/chaotic-aur/chaotic-keyring.pkg.tar.zst'
  109. pacman -U --noconfirm --needed 'https://cdn-mirror.chaotic.cx/chaotic-aur/chaotic-mirrorlist.pkg.tar.zst'
  110. echo "[chaotic-aur]
  111. Include = /etc/pacman.d/chaotic-mirrorlist" >> /etc/pacman.conf
  112. fi
  113. # fetch + apply dotfiles
  114. if [ ! -d /home/"$username"/.local/src/dotfiles ]; then
  115. echo -e "\e[0;30;34mFetching dotfiles ...\e[0m"
  116. cd_into /home/"$username"/.local/src
  117. while true; do
  118. git clone https://git.noahvogt.com/noah/videopc-infra.git && break
  119. done
  120. else
  121. echo -e "\e[0;30;34mUpdating dotfiles ...\e[0m"
  122. cd_into /home/"$username"/.local/src/dotfiles
  123. while true; do
  124. git pull && break
  125. done
  126. fi
  127. mv /home/"$username"/.local/src/videopc-infra /home/"$username"/.local/src/dotfiles
  128. cd_into /home/"$username"/.local/src/dotfiles
  129. echo -e "\e[0;30;34mApplying dotfiles ...\e[0m"
  130. doas -u "$username" /home/"$username"/.local/src/dotfiles/apply-dotfiles
  131. set_rtmp_key
  132. set_api_key
  133. # download packages from the official repos
  134. echo -e "\e[0;30;34mInstalling packages from repos ...\e[0m"
  135. 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
  136. # install aur packages
  137. echo -e "\e[0;30;34mInstalling packages from AUR ...\e[0m"
  138. doas -u "$username" paru -S --noconfirm --needed mediamtx-bin || pacman_error_exit
  139. # enable mediamtx service
  140. echo -e "\e[0;30;34mEnabling mediamtx daemon ...\e[0m"
  141. systemctl enable mediamtx
  142. make_user_owner_of_HOME_and_mnt_dirs
  143. # setup autologin
  144. echo -e "\e[0;30;34mSetting up Autologin ...\e[0m"
  145. systemctl enable greetd
  146. if ! grep -q "\[initial_session\]" /etc/greetd/config.toml; then
  147. echo '[initial_session]
  148. command = "Hyprland"
  149. user = "videopc"' >> /etc/greetd/config.toml
  150. fi
  151. # enable sshd daemon
  152. echo -e "\e[0;30;34mEnabling sshd daemon ...\e[0m"
  153. systemctl enable sshd
  154. # ~ cleanup
  155. echo -e "\e[0;30;34mCleaning up \$HOME ...\e[0m"
  156. for f in /home/"$username"/.bash*; do
  157. [ -f "$f" ] && rm "$f"
  158. done
  159. for f in /home/"$username"/.less*; do
  160. [ -f "$f" ] && rm "$f"
  161. done