arch.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #!/bin/bash -e
  2. #
  3. # Author: Casey DeLorme
  4. #
  5. # Description: fully automate installation and config after `arch-chroot`,
  6. # providing the user with a fully configured desktop environment and many
  7. # bells and whistles for developers.
  8. #
  9. # Dependencies: requires `base` and `base-devel` be installed with `pacstrap`,
  10. # and runs as root. Expects `username`, `password`, and `root_password` to be
  11. # supplied.
  12. # verify supplied variables
  13. [ -z "$root_password" ] && echo "missing root password..." && exit 1
  14. [ -z "$username" ] && echo "missing username..." && exit 1
  15. [ -z "$password" ] && echo "missing password..." && exit 1
  16. # print all that transpires henceforth
  17. [ -n "$DEBUG" ] && set -x
  18. # initial time configuration
  19. ln -sf /usr/share/zoneinfo/America/New_York /etc/localtime
  20. hwclock -w --utc
  21. # setup locale
  22. # @todo: determine how to deal with dual-languages
  23. sed -i "/^en_US.UTF-8/d" /etc/locale.gen
  24. # sed -i "/^ja_JP.UTF-8/d" /etc/locale.gen
  25. echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
  26. # echo "ja_JP.UTF-8 UTF-8" >> /etc/locale.gen
  27. locale-gen
  28. echo "LANG=en_US.UTF-8" > /etc/locale.conf
  29. # setup hostname
  30. echo "arch" > /etc/hostname
  31. echo "127.0.0.1 localhost" > /etc/hosts
  32. echo "::1 localhost" >> /etc/hosts
  33. echo "127.0.1.1 $(cat /etc/hostname).localdomain $(cat /etc/hostname)" >> /etc/hosts
  34. # set default virtualconsole font
  35. setfont koi8u_8x16
  36. echo "FONT=koi8u_8x16" > /etc/vconsole.conf
  37. # enable multilib
  38. sed -i "/\[multilib\]/,/Include/"'s/^#//' /etc/pacman.conf
  39. # add sublime text source
  40. curl -O https://download.sublimetext.com/sublimehq-pub.gpg && sudo pacman-key --add sublimehq-pub.gpg && sudo pacman-key --lsign-key 8A8F901A && rm sublimehq-pub.gpg
  41. echo -e "\n[sublime-text]\nServer = https://download.sublimetext.com/arch/stable/x86_64" | sudo tee -a /etc/pacman.conf
  42. # update and install all packages
  43. pacman -Syu --noconfirm sudo bash-completion tmux vim linux-firmware linux-headers dkms smartmontools cryptsetup usbutils btrfs-progs gvfs dmidecode parted pkgfile pkgconf bison gcc gcc-libs cmake ccache ncurses xmlstarlet jq at bc cronie iptables rsync net-tools openssh sshfs ntp wget curl wireless_tools bluez bluez-utils lzop unzip p7zip xz unrar unace lrzip arj git mercurial subversion bzr postgresql mesa lib32-mesa dbus polkit xorg-server xorg-server-devel xorg-xinit xorg-xinit xorg-xdpyinfo xdotool xsel pulseaudio vulkan-tools arandr feh hsetroot openbox openbox-themes obmenu archlinux-xdg-menu compton xarchiver pavucontrol pasystray xdg-utils xdg-user-dirs tint2 conky pcmanfm gmrun rxvt-unicode urxvt-perls gnome-themes-extra gnome-icon-theme arc-gtk-theme gtk-engines gtk-engine-murrine lxappearance graphicsmagick imagemagick lame libwebp libid3tag libvorbis vorbis-tools faac x264 x265 libexif ffmpeg ffmpegthumbnailer tumbler joyutils evtest lm_sensors lshw gparted psensor gparted hardinfo fontconfig ttf-bitstream-vera ttf-droid ttf-dejavu ttf-freefont ttf-liberation ttf-hanazono mpv openshot gimp krita transmission-cli evince viewnior virtualbox-host-modules-arch virtualbox vagrant guvcview dia mednafen mame ppsspp lutris steam wine sublime-text
  44. # enable ccache and optimize cores for AUR
  45. sed -i 's/!ccache/ccache/' /etc/makepkg.conf
  46. sed -i 's/^#MAKEFLAGS.*/MAKEFLAGS="-j$(($(nproc) + 1)) -l$(nproc)"/' /etc/makepkg.conf
  47. # install CPU unicode based on model
  48. [ $(grep -c "Intel" /proc/cpuinfo) -gt 0 ] && pacman -Syu --noconfirm intel-ucode
  49. [ $(grep -c "AMD" /proc/cpuinfo) -gt 0 ] && pacman -Syu --noconfirm amd-ucode
  50. # install nvidia related packages
  51. if [ $(lspci | grep -i " vga" | grep -ci " nvidia") -gt 0 ]; then
  52. pacman -Syu --noconfirm nvidia-dkms libglvnd nvidia-utils opencl-nvidia lib32-libglvnd lib32-nvidia-utils lib32-opencl-nvidia nvidia-settings
  53. # update modules to load
  54. [ $(grep -c "nvidia" /etc/mkinitcpio.conf) -eq 0 ] && sed -i 's/MODULES=(\(.*\))/MODULES=(\1 nvidia nvidia_modeset nvidia_uvm nvidia_drm)/' /etc/mkinitcpio.conf
  55. # automatic updates in pacman.d
  56. echo -e "[Trigger]\nOperation=Install\nOperation=Upgrade\nOperation=Remove\nType=Package\nTarget=nvidia\n\n[Action]\nDepends=mkinitcpio\nWhen=PostTransaction\nExec=/usr/bin/mkinitcpio -P" > /etc/pacman.d/hooks/nvidia.hook
  57. fi
  58. # install AMD/Radeon related packages
  59. if [ $(lspci | grep -i "vga" | grep -ci "amd") -gt 0 ]; then
  60. pacman -Syu --noconfirm xf86-video-amdgpu vulkan-radeon lib32-vulkan-radeon libva-mesa-driver lib32-libva-mesa-driver
  61. # update modules to load
  62. [ $(grep -c "amd" /etc/mkinitcpio.conf) -eq 0 ] && sed -i 's/MODULES=(\(.*\))/MODULES=(\1 amdgpu radeon)/' /etc/mkinitcpio.conf
  63. fi
  64. # temporarily create a user to install aur packages
  65. export aur_username=$(head /dev/urandom | tr -dc a-z | head -c 13 ; echo '')
  66. useradd -r -m -s /bin/bash $aur_username
  67. echo "${aur_username} ALL= NOPASSWD: /usr/bin/pacman" > /etc/sudoers.d/${aur_username}
  68. # install xcursor-chameleon-skyblue
  69. sudo -u $aur_username git clone https://aur.archlinux.org/xcursor-chameleon-skyblue.git /home/${aur_username}/xcursor-chameleon-skyblue
  70. (cd /home/${aur_username}/xcursor-chameleon-skyblue && sudo -u ${aur_username} makepkg -rcsi --noconfirm)
  71. # install numix-icon-theme
  72. sudo -u $aur_username git clone https://aur.archlinux.org/numix-icon-theme-git.git /home/${aur_username}/numix-icon-theme-git
  73. (cd /home/${aur_username}/numix-icon-theme-git && sudo -u ${aur_username} makepkg -rcsi --noconfirm)
  74. # install kazam
  75. sudo -u $aur_username git clone https://aur.archlinux.org/kazam.git /home/${aur_username}/kazam
  76. (cd /home/${aur_username}/kazam && sudo -u ${aur_username} makepkg -rcsi --noconfirm)
  77. # install google-chrome (google-chrome-stable)
  78. sudo -u $aur_username git clone https://aur.archlinux.org/google-chrome.git /home/${aur_username}/google-chrome
  79. (cd /home/${aur_username}/google-chrome && sudo -u ${aur_username} makepkg -rcsi --noconfirm)
  80. # install libc++; a dependency of discord; has broken pgp and tests?
  81. sudo -u $aur_username git clone https://aur.archlinux.org/libc++.git /home/${aur_username}/libc++
  82. (cd /home/${aur_username}/libc++ && sudo -u ${aur_username} makepkg -rcsi --noconfirm --skippgpcheck --nocheck)
  83. # install discord; ignore tests to save time
  84. sudo -u $aur_username git clone https://aur.archlinux.org/discord.git /home/${aur_username}/discord
  85. (cd /home/${aur_username}/discord && sudo -u ${aur_username} makepkg --nocheck -rcsi --noconfirm)
  86. # install laptop packages and optimize system configuration
  87. export chassistype=$(dmidecode --string chassis-type)
  88. if [[ "$chassistype" = "Laptop" || "$chassistype" = "Portable" || "$chassistype" = "Sub Notebook" ]]; then
  89. pacman -Syu --noconfirm xf86-input-synaptics connman
  90. sudo -u $aur_username git clone https://aur.archlinux.org/laptop-mode-tools.git /home/${aur_username}/laptop-mode-tools
  91. (cd /home/${aur_username}/laptop-mode-tools && sudo -u ${aur_username} makepkg -rcsi --noconfirm)
  92. # @todo: configuration of laptop-mode-tools
  93. # @link: https://wiki.archlinux.org/index.php/Laptop_Mode_Tools
  94. fi
  95. # cleanup temporary aur user
  96. rm -rf /etc/sudoers.d/${aur_username}
  97. userdel -fr $aur_username
  98. unset $aur_username
  99. # install youtube-dl
  100. if ! which youtube-dl &> /dev/null; then
  101. curl -Lfs https://yt-dl.org/latest/youtube-dl > /usr/local/bin/youtube-dl
  102. chmod a+rx /usr/local/bin/youtube-dl
  103. fi
  104. # install gifduration
  105. if ! which gifduration &> /dev/null; then
  106. curl -Lfs https://raw.githubusercontent.com/alimony/gifduration-script/master/gifduration.py > /usr/local/bin/gifduration
  107. chmod a+rx /usr/local/bin/gifduration
  108. fi
  109. # install prime95
  110. if ! which mprime &> /dev/null; then
  111. curl -Lfs "http://www.mersenne.org/ftp_root/gimps/p95v294b8.linux64.tar.gz" > /tmp/prime.tar.gz
  112. mkdir -p /usr/local/src/prime/
  113. tar -xf /tmp/prime.tar.gz -C /usr/local/src/prime
  114. ln -sf /usr/local/src/prime/mprime /usr/local/bin/
  115. fi
  116. # install packer
  117. if ! which packer &> /dev/null; then
  118. curl -Lfs "https://releases.hashicorp.com/packer/1.3.5/packer_1.3.5_linux_amd64.zip" > /tmp/packer.zip
  119. unzip /tmp/packer.zip -d /usr/local/bin/
  120. fi
  121. # install 64bit flash projector
  122. if ! which flashplayer &> /dev/null; then
  123. curl -Lfs "https://fpdownload.macromedia.com/pub/flashplayer/updaters/32/flash_player_sa_linux.x86_64.tar.gz" > /tmp/flash.tar.gz
  124. tar -xf /tmp/flash.tar.gz -C /usr/local/bin flashplayer
  125. fi
  126. # install urxvt perl enhancement for font resize support
  127. [ ! -f /usr/lib/urxvt/perl/font ] && curl -Lfs "https://raw.githubusercontent.com/noah/urxvt-font/master/font" > /usr/lib/urxvt/perl/font
  128. # create sudo group and add to sudoers
  129. groupadd -fr sudo
  130. [ ! -f /etc/sudoers.d/sudo ] && echo '%sudo ALL=(ALL) ALL' > /etc/sudoers.d/sudo
  131. # install base configuration files from repository
  132. [ -d /srv/arch-desktop/install ] || git clone https://git.caseydelorme.com/cdelorme/arch-desktop /srv/arch-desktop
  133. rsync -Pav /srv/arch-desktop/install/ /
  134. ln -sf .Xdefaults /etc/skel/.Xresources
  135. mkdir -p /etc/skel/.config/pulse
  136. sed "/module-suspend-on-idle/d" /etc/pulse/default.pa > /etc/skel/.config/pulse/default.pa
  137. rsync -Pav /etc/skel/ $(getent passwd root | cut -d: -f6)/
  138. # update font cache
  139. fc-cache -fr
  140. # secure ssh by disabling root access and only accepting ssh keys
  141. sed -i "/^#\?PermitRootLogin/d" /etc/ssh/sshd_config
  142. sed -i "/^#\?PasswordAuthentication/d" /etc/ssh/sshd_config
  143. sed -i "/^#\?X11Forwarding/d" /etc/ssh/sshd_config
  144. echo "PermitRootLogin no" >> /etc/ssh/sshd_config
  145. echo "PasswordAuthentication no" >> /etc/ssh/sshd_config
  146. echo "X11Forwarding yes" >> /etc/ssh/sshd_config
  147. # set root password
  148. printf "${root_password}\n${root_password}\n" | passwd
  149. # after 4 bad logins lock an account for 10 minutes; or one minute for root
  150. sed -i -re 's/^(auth\s*required\s*pam_tally2.so).*/\1 deny=4 even_deny_root unlock_time=600 root_unlock_time=60 onerr=fail file=\/var\/log\/tallylog/g' /etc/pam.d/system-login
  151. # initialize postgres database
  152. su - postgres -c "initdb --locale en_US.UTF-8 -D '/var/lib/postgres/data'"
  153. # create configuration to fix transmission errors
  154. echo "net.core.rmem_max = 4194304" > /etc/sysctl.d/transmission.conf
  155. echo "net.core.wmem_max = 1048576" >> /etc/sysctl.d/transmission.conf
  156. # check whether username and password are not empty
  157. if [[ -n "$username" && -n "$password" ]]; then
  158. # create user if not exists, else (re)-install dot-files
  159. if ! id $username &> /dev/null; then
  160. useradd -m -s /bin/bash $username
  161. echo "${username}:${password}" | chpasswd -c SHA256
  162. else
  163. [ $EUID -ne 0 ] && rsync -Pav /etc/skel/ $(getent passwd $username | cut -d: -f6)/
  164. fi
  165. # add user to standard groups
  166. usermod -aG users,sudo,adm,input,audio,video,disk,storage,lp,vboxusers $username
  167. # generate postgres user and user database
  168. systemctl start postgresql
  169. su postgres -c "cd && createuser -ds $username" 2> /dev/null && su $username -c "cd && createdb"
  170. # configure home directories
  171. su $username -c "cd && mkdir -p ~/{desktop,downloads,templates,public,documents,music,pictures,videos,git}"
  172. su $username -c 'cd && xdg-user-dirs-update --set DESKTOP $HOME/desktop'
  173. su $username -c 'cd && xdg-user-dirs-update --set DOWNLOAD $HOME/downloads'
  174. su $username -c 'cd && xdg-user-dirs-update --set TEMPLATES $HOME/templates'
  175. su $username -c 'cd && xdg-user-dirs-update --set PUBLICSHARE $HOME/public'
  176. su $username -c 'cd && xdg-user-dirs-update --set DOCUMENTS $HOME/documents'
  177. su $username -c 'cd && xdg-user-dirs-update --set MUSIC $HOME/music'
  178. su $username -c 'cd && xdg-user-dirs-update --set PICTURES $HOME/pictures'
  179. su $username -c 'cd && xdg-user-dirs-update --set VIDEOS $HOME/videos'
  180. su $username -c 'cd && xdg-user-dirs-update'
  181. update-desktop-database
  182. # generate default (passwordless) ed25519 ssh key if none exists
  183. su $username -c "cd; if [ ! -f ~/.ssh/id_ed25519 ]; then ssh-keygen -q -t ed25519 -N '' -f ~/.ssh/id_ed25519 && cd && chmod 600 ~/.ssh/id_ed25519 && chmod 600 ~/.ssh/id_ed25519.pub; fi"
  184. # @note: during arch installation systemd will ignore chroot start for user
  185. # so we have to take the manual route of generating the target symlink.
  186. su $username -c 'cd; mkdir $HOME/.config/systemd/user/default.target.wants; ln -fs $HOME/.config/systemd/user/ssh-agent.service $HOME/.config/systemd/user/default.target.wants/ssh-agent.service'
  187. # install gvm loading from ~/.bash_profile, and the latest go version
  188. su $username -c "if [ ! -d ~/.gvm ]; then GVM_NO_UPDATE_PROFILE=1 bash < <(curl -Ls https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer 2> /dev/null); fi"
  189. su $username -c 'grep "gvm" ~/.bash_profile &> /dev/null || echo -e "\n# load gvm\n! which gvm &> /dev/null && . ~/.gvm/scripts/gvm" >> ~/.bash_profile'
  190. su $username -c ". ~/.gvm/scripts/gvm && gvm install go1.12 -B && gvm use go1.12 --default"
  191. # setup user-space transmission
  192. if [ ! -f "/etc/systemd/system/transmission.service.d/local.conf" ]; then
  193. su $username -c "cd && mkdir -p ~/transmission/{done,incomplete}"
  194. su $username -c 'cd && tmp=$(mktemp) && jq ".[\"download-dir\"] = \"${HOME}/transmission/done\"" $HOME/.config/transmission-daemon/settings.json | jq ".[\"incomplete-dir\"] = \"${HOME}/transmission/incomplete\"" | jq ".[\"watch-dir\"] = \"$(xdg-user-dir DOWNLOAD)\"" > $tmp && mv $tmp $HOME/.config/transmission-daemon/settings.json'
  195. mkdir -p /etc/systemd/system/transmission.service.d
  196. echo "[Service]" > "/etc/systemd/system/transmission.service.d/local.conf"
  197. echo "User=${username}" >> "/etc/systemd/system/transmission.service.d/local.conf"
  198. systemctl daemon-reload
  199. fi
  200. fi
  201. # symlink shceduled maintenance tasks
  202. ln -sf /usr/local/bin/system-updates /etc/cron.daily/
  203. ln -sf /usr/local/bin/disk-maintenance /etc/cron.weekly/
  204. # symlink override vi to vim
  205. ln -sf /usr/bin/vim /usr/local/bin/vi
  206. # load iptables for security
  207. # @note: if during this the kernel was upgraded this may fail
  208. systemctl start iptables
  209. # enable services for next reboot
  210. systemctl enable iptables
  211. systemctl enable sshd
  212. systemctl enable bluetooth
  213. systemctl enable transmission
  214. systemctl enable postgresql
  215. systemctl enable cronie
  216. # extract active network device and if wired enable it so it works on reboot
  217. export active_network_device=$(ip addr | awk '/state UP/ {print $2}' | sed 's/.$//')
  218. [ ! -f "/sys/class/net/${active_network_device}/wireless" ] && systemctl enable dhcpcd@${active_network_device}
  219. # build the init module
  220. mkinitcpio -p linux
  221. # install the bootloader
  222. bootctl install
  223. # create boot loader entry
  224. echo "title arch" > /boot/loader/entries/arch.conf
  225. echo "linux vmlinuz-linux" >> /boot/loader/entries/arch.conf
  226. [ -f /boot/intel-ucode.img ] && echo "initrd /intel-ucode.img" >> /boot/loader/entries/arch.conf
  227. [ -f /boot/amd-ucode.img ] && echo "initrd /amd-ucode.img" >> /boot/loader/entries/arch.conf
  228. echo "initrd /initramfs-linux.img" >> /boot/loader/entries/arch.conf
  229. echo "options root=PARTUUID=$(blkid -s PARTUUID -o value /dev/sda3) rw" >> /boot/loader/entries/arch.conf
  230. [[ $(lspci | grep -i " vga" | grep -ci " nvidia") -gt 0 && $(grep -c "nvidia" /boot/loader/entires/arch.conf) -eq 0 ]] && sed -i 's/rw/rw nvidia-dkm.modeset=1/' /boot/loader/entires/arch.conf
  231. # set boot loader entry as default
  232. sed -i '/^default/d' /boot/loader/loader.conf
  233. echo "default arch" >> /boot/loader/loader.conf
  234. # check boot loader configuration
  235. bootctl status
  236. echo "instalation complete, please exit, umount -R /mnt, and reboot..."