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 16KB

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