Gentoo installation script
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gein.sh 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. #/bin/env sh
  2. #
  3. # Copyright (C) 2017, 2018
  4. # * Johnathan C Maudlin <jcmdln@gmail.com>
  5. #
  6. # This software is licensed under the Azryn Software Labs Public License
  7. # of version 1.1.0 or later. You should have received a copy of the
  8. # Azryn Software Labs Public License along with this program. If not,
  9. # please see https://apl.azryn.org/ for a copy.
  10. #
  11. # This script does automate the install but you will need to adjust the
  12. # variables in the following section to suit your desired install. The
  13. # installation process will not proceed unless 'PartitionBoot' and
  14. # 'VideoCards' are set though some example values have been provided. If
  15. # you are unsure how to proceed, consult the Gentoo Handbook.
  16. #
  17. # This section defines the basic variables needed in order to complete
  18. # the installation. Your root password is set to the value of $Hostname
  19. # for simplicity. Change your root password with 'passwd' after the
  20. # installation completes. Be sure to set $PartitionBoot to your intended
  21. # Grub destination. The default value will install to the master boot
  22. # record (MBR) of '/dev/sda'. You will also need to uncomment the line
  23. # pertaining to your GPU and modify it as needed.
  24. CPUCores="$(grep -c ^processor /proc/cpuinfo)"
  25. Hostname="gein"
  26. Locale="en_US.UTF-8 UTF-8"
  27. #PartitionBoot="/dev/sda"
  28. SwapSize="2G"
  29. TimeZone="America/New_York"
  30. #VideoCards="i915 i965 intel"
  31. #VideoCards="amdgpu radeonsi"
  32. #VideoCards="nouveau nvidia"
  33. #VideoCards="virtualbox vmware"
  34. # This section defines some command aliases that will be used later on,
  35. # and is primarily used as a mechanism to inhibit or control output in a
  36. # way that can be easily updated if needed.
  37. Emerge="emerge -v --quiet-build"
  38. Make="make -s -j$CPUCores"
  39. Wget="wget -q"
  40. # This script relies on downloading configuration files from the main
  41. # repository. Here we will create the $Source variable to simplify
  42. # future sections.
  43. Source="https://raw.githubusercontent.com/jcmdln/gein/master"
  44. # By default, $AutoKernel is set to 'true' which means that the kernel
  45. # will be built using 'make defconfig'. If you want to run
  46. # 'make defconfig; make menuconfig' then set $AutoKernel to 'false'. You
  47. # may also supply your own URL to $KernelConfig while setting
  48. # $AutoKernel to 'false' to use a pre-built kernel config. An example
  49. # kernel config is provided though commented out.
  50. AutoKernel="true"
  51. #KernelConfig="$Source/usr/src/linux/x.x.config"
  52. # Much work has been done to simplify or in most cases fully automate
  53. # interacting with Portage though this section simply creates easily
  54. # referenced variables that may be called later in this script. At the
  55. # time of writing, the Gentoo stage3 no longer includes 'git' which
  56. # prevents using the GitHub mirror. Please leave this commented unless
  57. # you plan to emerge git ahead of time.
  58. MakeConf="$Source/etc/portage/make.conf"
  59. PackageAcceptKeywords="$Source/etc/portage/package.accept_keywords"
  60. PackageEnv="$Source/etc/portage/package.env"
  61. PackageLicense="$Source/etc/portage/package.license"
  62. PackageUse="$Source/etc/portage/package.use"
  63. #ReposConf="$Source/etc/portage/repos.conf/gentoo.conf"
  64. # This section exists to automate identifying and downloading the latest
  65. # stage3 archive under the condition that cURL is present. This is not
  66. # an issue when using the Gentoo installation CD's though prevents
  67. # errors when executing MINIMAL() or DESKTOP() due to cURL missing
  68. # after completing the BOOTSTRAP().
  69. S3_Arch="amd64"
  70. S3_Source="http://distfiles.gentoo.org/releases/$S3_Arch/autobuilds"
  71. S3_Release="curl -s $S3_Source/latest-stage3-$S3_Arch.txt"
  72. [ -x "$(command -v curl)" ] &&
  73. S3_Current="$($S3_Release|tail -1|awk '{print $1}')" &&
  74. Stage3="$S3_Source/$S3_Current"
  75. # Bootstrapping a Gentoo stage3 archive is a fairly quick process though
  76. # we must also ensure this script will be accessible from the chroot as
  77. # well as other housekeeping tasks. The Portage configuration files are
  78. # setup in this section.
  79. BOOTSTRAP() {
  80. echo "Please ensure that you have performed the following: "
  81. echo " - Edited the environment variables at the top of this script."
  82. echo " - Partitioned and mounted your disk(s)."
  83. read -ep "Proceed with installation? [Y/N]: " Proceed
  84. if echo "$Proceed" | grep -iq "^y"; then
  85. echo "gein: Proceeding with installation..."
  86. else
  87. echo "gein: Exiting..."
  88. exit
  89. fi
  90. if [ -z "$VideoCards" ] || [ -z "$PartitionBoot" ]; then
  91. echo "gein: You didn't read $0 and adjust the variables!"
  92. echo "gein: Exiting..."
  93. exit
  94. fi
  95. echo "gein: Ensuring we are in /mnt/gentoo..."
  96. [ ! -e /mnt/gentoo/$(basename "$0") ] &&
  97. cp "$0" /mnt/gentoo/ &&
  98. cd /mnt/gentoo
  99. echo "gein: Setting system time via ntpd..."
  100. [ -x "$(command -v ntpd)" ] &&
  101. ntpd -q -g
  102. echo "gein: Downloading and extracting Stage3 tarball..."
  103. if [ -n "$Stage3" ]; then
  104. $Wget "$Stage3" &&
  105. tar -xpf stage3-* --xattrs --numeric-owner &&
  106. rm -rf stage3-*
  107. else
  108. echo "gein: 'S3Tgt' is not set! Is cURL missing? Exiting..."
  109. exit
  110. fi
  111. echo "gein: Mounting hardware devices..."
  112. HW="proc sys dev"
  113. for target in "$HW"; do
  114. if [ -e /mnt/gentoo/"$target" ]; then
  115. case "$target" in
  116. proc)
  117. mount -t proc /proc /mnt/gentoo/proc
  118. ;;
  119. sys )
  120. mount --rbind /sys /mnt/gentoo/sys &&
  121. mount --make-rslave /mnt/gentoo/sys
  122. ;;
  123. dev )
  124. mount --rbind /dev /mnt/gentoo/dev &&
  125. mount --make-rslave /mnt/gentoo/dev
  126. ;;
  127. *) echo "gein: $target: Improper hardware device"
  128. exit
  129. esac
  130. else
  131. echo "gein: $target unable to be mounted! Exiting..."
  132. exit
  133. fi
  134. done
  135. SwapFile="/mnt/gentoo/swapfile"
  136. if [ ! -e "$SwapFile" ]; then
  137. echo "gein: Setting up swapfile..." &&
  138. fallocate -l "$SwapSize" "$SwapFile" &&
  139. chmod 0600 "$SwapFile" &&
  140. mkswap "$SwapFile" &&
  141. swapon "$SwapFile" &&
  142. echo "/swapfile none swap sw 0 0" >> /mnt/gentoo/etc/fstab
  143. fi
  144. echo "gein: Copying '/etc/resolv.conf'..." &&
  145. cp -L /etc/resolv.conf /mnt/gentoo/etc/
  146. echo "gein: Downloading Portage configuration files..."
  147. [ -n "$MakeConf" ] &&
  148. $Wget "$MakeConf" \
  149. -O /mnt/gentoo/etc/portage/make.conf
  150. [ -n "$PackageAcceptKeywords" ] &&
  151. $Wget "$PackageAcceptKeywords" \
  152. -O /mnt/gentoo/etc/portage/package.accept_keywords
  153. [ -n "$PackageEnv" ] &&
  154. $Wget "$PackageEnv" \
  155. -O /mnt/gentoo/etc/portage/package.env
  156. [ -n "$PackageLicense" ] &&
  157. $Wget "$PackageLicense" \
  158. -O /mnt/gentoo/etc/portage/package.license
  159. [ -n "$PackageUse" ] &&
  160. rm -rf /mnt/gentoo/etc/portage/package.use &&
  161. $Wget "$PackageUse" \
  162. -O /mnt/gentoo/etc/portage/package.use
  163. [ -n "$ReposConf" ] &&
  164. mkdir -p /mnt/gentoo/etc/portage/repos.conf &&
  165. $Wget "$ReposConf" \
  166. -O /mnt/gentoo/etc/portage/repos.conf/gentoo.conf
  167. echo "gein: Downloading gein Portage package sets..."
  168. mkdir -p /mnt/etc/portage/sets
  169. PortageSets="
  170. /etc/portage/sets/gein-base
  171. /etc/portage/sets/gein-i3wm
  172. /etc/portage/sets/gein-laptop
  173. /etc/portage/sets/gein-lxqt
  174. /etc/portage/sets/gein-steam
  175. "
  176. for Set in "$PortageSets"; do
  177. $Wget "$Source"/"$Set" -O /mnt/gentoo/"$Set"
  178. done
  179. echo "gein: Chroot'ing into /mnt/gentoo..." &&
  180. chroot /mnt/gentoo /usr/bin/env -i \
  181. HOME="/root" TERM="$TERM" PS1="[chroot \u@\h \W]$ " \
  182. PATH="/usr/local/sbin/:/usr/local/bin:/usr/sbin:/usr/bin" \
  183. PATH="$PATH:/sbin:/bin:/opt/bin" \
  184. MANPATH="/usr/man:/usr/share/man:/usr/local/man" \
  185. MANPATH="$MANPATH:/usr/local/share/man" \
  186. /bin/bash --login
  187. }
  188. # In this section we will update various configuration files, select the
  189. # desired profile, compile the kernel, and install some basic packages.
  190. MINIMAL() {
  191. echo "gein: Setting CPU cores and GPU type..." &&
  192. sed -i "s/Video_Cards/$VideoCards/g; s/Make_Opts/-j$CPUCores/g" \
  193. /etc/portage/make.conf
  194. echo "gein: Syncing Portage and selecting profile..." &&
  195. emerge -q --sync &&
  196. eselect profile list &&
  197. echo "gein: Hint: choose the latest 'default/linux/amd64/xx.x'" &&
  198. TargetProfile="" &&
  199. while [ -z "$TargetProfile" ]; do
  200. read -ep "Which profile?: " TargetProfile
  201. done &&
  202. eselect profile set "$TargetProfile" &&
  203. $Emerge -uDN @world
  204. echo "gein: Setting timezone..." &&
  205. echo "$TimeZone" > /etc/timezone &&
  206. $Emerge --config sys-libs/timezone-data
  207. echo "gein: Setting locale..." &&
  208. echo "$Locale" > /etc/locale.gen &&
  209. locale-gen && locale -a && eselect locale list &&
  210. read -ep "Target locale: " TargetLocale &&
  211. eselect locale set "$TargetLocale" &&
  212. env-update && source /etc/profile &&
  213. export PS1="[chroot \u@\h \W]$ "
  214. echo "gein: Emerging minimal packages..." &&
  215. $Emerge @gein-base
  216. if grep -Rqi 'intel' /proc/cpuinfo; then
  217. echo "gein: emerging intel-microcode" &&
  218. $Emerge intel-microcode
  219. fi
  220. echo "gein: Configuring Linux kernel..."
  221. cd /usr/src/linux
  222. if [ "$AutoKernel" = "true" ]; then
  223. if [ -z "$KernelConfig" ]; then
  224. make defconfig
  225. else
  226. $Wget "$KernelConfig" -O /usr/src/linux/.config
  227. fi
  228. elif [ "$AutoKernel" = "false" ]; then
  229. if [ -z "$KernelConfig" ]; then
  230. make defconfig &&
  231. make menuconfig
  232. else
  233. $Wget "$KernelConfig" -O /usr/src/linux/.config &&
  234. make menuconfig
  235. fi
  236. else
  237. echo "gein: Error: AutoKernel isn't true or false. Exiting..."
  238. fi
  239. echo "gein: Compiling Linux kernel, modules, and initramfs..." &&
  240. $Make && $Make modules &&
  241. $Make install && $Make modules install &&
  242. $Make distclean &&
  243. cd /
  244. echo "gein: Adding services to OpenRC..." &&
  245. rc-update add dhcpcd default &&
  246. rc-update add cronie default
  247. echo "gein: Setting hostname..." &&
  248. echo "hostname=$Hostname" > /etc/conf.d/hostname
  249. echo "gein: Installing Grub to $PartitionBoot..." &&
  250. grub-install "$PartitionBoot" &&
  251. grub-mkconfig -o /boot/grub/grub.cfg
  252. echo "gein: Adding userland configurations..."
  253. CfgFiles="
  254. /etc/bash/bashrc
  255. /etc/profile
  256. /etc/profile.d/alias.sh
  257. /etc/profile.d/defaults.sh
  258. /etc/profile.d/gein.sh
  259. /etc/profile.d/golang.sh
  260. /etc/profile.d/kernel.sh
  261. /etc/profile.d/racket.sh
  262. "
  263. for cfg in "$CfgFiles"; do
  264. $Wget "$Source"/"$cfg" -O "$cfg"
  265. done
  266. echo "gein: Setting root password..."
  267. [ -x $(command -v chpasswd) ] && \
  268. echo root:"$Hostname" | chpasswd
  269. }
  270. # In this section we will install the chosen xorg-driver and packages
  271. # for i3wm as the desktop of choice. Some additional packages are added
  272. # as a convenience.
  273. DESKTOP() {
  274. echo "gein: Installing Xorg drivers..." &&
  275. $Emerge x11-base/xorg-drivers &&
  276. env-update && source /etc/profile &&
  277. export PS1="[chroot \u@\h \W]$ "
  278. echo "gein: Installing desktop packages..." &&
  279. $Emerge @gein-base "$DesktopChoice"
  280. if [ -n "$DesktopConfig" ]; then
  281. echo "gein: Adding configuration files..."
  282. for cfg in "$DesktopConfig"; do
  283. $Wget "$Source"/"$cfg" -O "$cfg"
  284. done
  285. fi
  286. }
  287. LAPTOP() {
  288. echo "gein: Installing laptop packages..." &&
  289. $Emerge @gein-laptop
  290. echo "azryn: Add laptop_mode to OpenRC..." &&
  291. rc-update add laptop_mode default
  292. }
  293. # This section is for completing tasks after the installation is
  294. # complete. The user will have a complete system already installed and
  295. # may skip these steps if desired.
  296. POSTINSTALL() {
  297. # echo "gein: Creating 'power' group"
  298. # groupadd power
  299. # poweroff reboot shutdown
  300. read -ep "gein: Install laptop packages? [Y/N]: " SetupUser
  301. if echo "$SetupUser" | grep -iq "^y"; then
  302. $Emerge @gein-laptop
  303. fi
  304. read -ep "gein: Setup a standard user? [Y/N]: " SetupUser
  305. if echo "$SetupUser" | grep -iq "^y"; then
  306. echo "gein: Creating user account" &&
  307. read -ep "Username: " Username &&
  308. read -ep "Password: " Password &&
  309. useradd -m -G wheel,audio,video,power \
  310. -s /bin/bash "$Username" &&
  311. echo "$Username":"$Password" | chpasswd
  312. fi
  313. echo "gein: Installation complete."
  314. }
  315. # This is the CLI that controls what will be run. Keep in mind that the
  316. # BOOTSTRAP() needs to be run to completion before running the MINIMAL()
  317. # or DESKTOP() passes.
  318. shopt -s nocasematch
  319. case $1 in
  320. -b|bootstrap)
  321. BOOTSTRAP
  322. ;;
  323. -m|minimal)
  324. MINIMAL && POSTINSTALL
  325. ;;
  326. -d|desktop)
  327. case $2 in
  328. i3wm)
  329. DesktopChoice="@gein-i3wm"
  330. DesktopConfig="
  331. /etc/i3status.conf
  332. /etc/xinitrc
  333. /etc/Xresources
  334. /etc/i3/config
  335. "
  336. MINIMAL && DESKTOP && POSTINSTALL
  337. ;;
  338. lxqt)
  339. DesktopChoice="@gein-lxqt"
  340. MINIMAL && DESKTOP
  341. echo "azryn: Set SDDM as the display manager" &&
  342. sed -i 's/DISPLAYMANAGER="xdm"/DISPLAYMANAGER="sddm"/g' \
  343. /etc/conf.d/xdm &&
  344. sed -i 's/startl|xqt/"ck-launch-session dbus-launch startlxqt"/g' \
  345. /usr/share/xsessions/lxqt.desktop &&
  346. rc-update add xdm default &&
  347. rc-update add dbus default
  348. POSTINSTALL
  349. ;;
  350. *)
  351. echo "gein: $2 not an available desktop"
  352. echo ""
  353. echo "Available desktops:"
  354. echo " i3wm A complete i3wm desktop"
  355. echo " lxqt A complete LXQT desktop"
  356. esac
  357. ;;
  358. *)
  359. echo "gein: Linux-based derivative of Gentoo"
  360. echo " -h, help Shows this output"
  361. echo " -b, bootstrap Bootstrap the stage3 tarball"
  362. echo ""
  363. echo "Post-bootstrap:"
  364. echo " -m, minimal Perform a basic Gentoo installation"
  365. echo " -d, desktop Install a gein desktop"
  366. echo " i3wm A complete i3wm desktop"
  367. echo " lxqt A complete LXQT desktop"
  368. esac
  369. shopt -u nocasematch