stage2.sh 6.8 KB

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