stage2.sh 6.5 KB

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