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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 which points to
  32. # the main repository, and a 'Config' variable which is where all
  33. # custom configuration lives.
  34. Source="https://raw.githubusercontent.com/jcmdln/gein/master"
  35. Config="https://raw.githubusercontent.com/jcmdln/cfg/master"
  36. ## System
  37. #
  38. # The 'Hostname' is used to set the 'Hostname'.
  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. case "$(uname -m)" in
  69. amd64|x86_64) CPUArch="amd64" ;;
  70. *)
  71. echo "gein: CPU arch has not been defined yet"
  72. exit
  73. esac
  74. CPUCores="$(grep -c ^processor /proc/cpuinfo)"
  75. ## Command Aliases
  76. #
  77. # This section defines some command aliases that will be used later on,
  78. # and is primarily used as a mechanism to inhibit or control output in a
  79. # way that can be easily updated or changed if needed.
  80. Emerge="emerge -v --quiet-build"
  81. Make="make -s -j$CPUCores"
  82. Wget="wget -q"
  83. ## Gentoo Stage3
  84. #
  85. # This section exists to automate identifying and downloading the latest
  86. # stage3 archive under the condition that cURL is present. This is not
  87. # an issue when using the Gentoo installation CD's though prevents
  88. # errors when executing MINIMAL() or DESKTOP() due to cURL missing
  89. # after completing the BOOTSTRAP().
  90. S3_Source="http://distfiles.gentoo.org/releases/$CPUArch/autobuilds"
  91. S3_Release="curl -s $S3_Source/latest-stage3-$CPUArch.txt"
  92. if [ -x "$(command -v curl)" ]; then
  93. S3_Current="$($S3_Release|tail -1|awk '{print $1}')"
  94. Stage3="$S3_Source/$S3_Current"
  95. fi
  96. # Bootstrapping a Gentoo stage3 archive is a fairly quick process though
  97. # we must also ensure this script will be accessible from the chroot as
  98. # well as other housekeeping tasks. The Portage configuration files are
  99. # setup in this section.
  100. BOOTSTRAP() {
  101. echo "Please ensure that you have performed the following: "
  102. echo " - Edited the environment variables at the top of this script."
  103. echo " - Partitioned and mounted your disk(s)."
  104. read -ep "Proceed with installation? [Y/N]: " Proceed
  105. if echo "$Proceed" | grep -iq "^y"; then
  106. echo "gein: Proceeding with installation..."
  107. else
  108. echo "gein: Exiting..."
  109. exit
  110. fi
  111. if [ -z "$VideoCards" ] || [ -z "$PartitionBoot" ]; then
  112. echo "gein: You didn't read $0 and adjust the variables!"
  113. echo "gein: Exiting..."
  114. exit
  115. fi
  116. echo "gein: Ensuring we are in /mnt/gentoo..."
  117. [ ! -e /mnt/gentoo/$(basename "$0") ] &&
  118. cp "$0" /mnt/gentoo/ &&
  119. cd /mnt/gentoo &&
  120. echo "gein: Setting system time via ntpd..."
  121. [ -x "$(command -v ntpd)" ] &&
  122. ntpd -q -g &&
  123. echo "gein: Downloading and extracting Stage3 tarball..."
  124. if [ -n "$Stage3" ]; then
  125. $Wget "$Stage3" &&
  126. tar -xpf stage3-* --xattrs --numeric-owner &&
  127. rm -rf stage3-*
  128. else
  129. echo "gein: 'Stage3' variable is not set! Is cURL missing?"
  130. echo "gein: Exiting..."
  131. exit
  132. fi
  133. echo "gein: Mounting hardware devices..."
  134. HW="proc sys dev"
  135. for target in $HW; do
  136. if [ -e /mnt/gentoo/"$target" ]; then
  137. case "$target" in
  138. proc) mount -t proc /proc /mnt/gentoo/proc ;;
  139. sys ) mount --rbind /sys /mnt/gentoo/sys &&
  140. mount --make-rslave /mnt/gentoo/sys ;;
  141. dev ) mount --rbind /dev /mnt/gentoo/dev &&
  142. mount --make-rslave /mnt/gentoo/dev ;;
  143. *) echo "gein: $target: Improper hardware device"
  144. exit
  145. esac
  146. else
  147. echo "gein: $target unable to be mounted! Exiting..."
  148. exit
  149. fi
  150. done
  151. SwapFile="/mnt/gentoo/swapfile"
  152. if [ ! -e "$SwapFile" ]; then
  153. echo "gein: Setting up swapfile..." &&
  154. fallocate -l "$SwapSize" "$SwapFile" &&
  155. chmod 0600 "$SwapFile" &&
  156. mkswap "$SwapFile" &&
  157. swapon "$SwapFile" &&
  158. echo "/swapfile none swap sw 0 0" >> /mnt/gentoo/etc/fstab
  159. fi
  160. echo "gein: Copying '/etc/resolv.conf'..." &&
  161. cp -L /etc/resolv.conf /mnt/gentoo/etc/
  162. echo "gein: Chroot'ing into /mnt/gentoo..." &&
  163. chroot /mnt/gentoo /usr/bin/env -i \
  164. HOME="/root" TERM="$TERM" PS1="[chroot \u@\h \W]$ " \
  165. PATH="/usr/local/sbin/:/usr/local/bin:/usr/sbin" \
  166. PATH="$PATH:/usr/bin:/sbin:/bin:/opt/bin" \
  167. MANPATH="/usr/man:/usr/share/man:/usr/local/man" \
  168. MANPATH="$MANPATH:/usr/local/share/man" \
  169. /bin/bash --login
  170. }
  171. # In this section we will update various configuration files, select the
  172. # desired profile, compile the kernel, and install some basic packages.
  173. MINIMAL() {
  174. echo "gein: getting configuration files from 'cfg'..." &&
  175. $Wget $Config/cfg.sh &&
  176. source ./cfg.sh -g
  177. echo "gein: Setting CPU cores and GPU type..." &&
  178. sed -i "s/Video_Cards/$VideoCards/g; s/Make_Opts/-j$CPUCores/g" \
  179. /etc/portage/make.conf
  180. echo "gein: Syncing Portage and selecting profile..." &&
  181. emerge -q --sync &&
  182. eselect profile list | grep -Evi "dev|exp"
  183. echo "gein: choose the latest stable release" &&
  184. TargetProfile="" &&
  185. while [ -z "$TargetProfile" ]; do
  186. read -ep "Which profile?: " TargetProfile
  187. done &&
  188. eselect profile set "$TargetProfile" &&
  189. $Emerge -uDN @world
  190. echo "gein: Setting timezone..." &&
  191. echo "$TimeZone" > /etc/timezone &&
  192. $Emerge --config sys-libs/timezone-data
  193. echo "gein: Setting locale..." &&
  194. echo "$Locale" > /etc/locale.gen &&
  195. locale-gen && locale -a &&
  196. LocaleMain=$(echo $Locale | awk -F '[-]' '{print $1}') &&
  197. LocaleSet=$(eselect locale list | grep -i $LocaleMain | \
  198. awk -F '[][]' '{print $2}') &&
  199. eselect locale set $LocaleSet &&
  200. env-update && source /etc/profile &&
  201. export PS1="[chroot \u@\h \W]$ "
  202. echo "gein: Emerging minimal packages..." &&
  203. $Emerge @gein-base
  204. if grep -Rqi 'intel' /proc/cpuinfo; then
  205. echo "gein: emerging intel-microcode" &&
  206. $Emerge intel-microcode
  207. fi
  208. echo "gein: Configuring Linux kernel..."
  209. cd /usr/src/linux
  210. if [ "$AutoKernel" = "true" ]; then
  211. if [ -z "$KernelConfig" ]; then
  212. make defconfig
  213. else
  214. $Wget "$KernelConfig" -O /usr/src/linux/.config
  215. fi
  216. elif [ "$AutoKernel" = "false" ]; then
  217. if [ -z "$KernelConfig" ]; then
  218. make defconfig &&
  219. make menuconfig
  220. else
  221. $Wget "$KernelConfig" -O /usr/src/linux/.config &&
  222. make menuconfig
  223. fi
  224. else
  225. echo "gein: Error: AutoKernel isn't true or false. Exiting..."
  226. fi
  227. echo "gein: Compiling Linux kernel and modules..." &&
  228. $Make && $Make modules &&
  229. $Make install && $Make modules install &&
  230. $Make distclean &&
  231. cd /
  232. echo "gein: Adding services to OpenRC..." &&
  233. rc-update add dhcpcd default &&
  234. rc-update add cronie default
  235. echo "gein: Setting hostname..." &&
  236. echo "hostname=$Hostname" > /etc/conf.d/hostname
  237. echo "gein: Installing Grub to $PartitionBoot..." &&
  238. grub-install "$PartitionBoot" &&
  239. grub-mkconfig -o /boot/grub/grub.cfg
  240. }
  241. # In this section we will install the chosen xorg-driver and packages
  242. # for i3wm as the desktop of choice. Some additional packages are added
  243. # as a convenience.
  244. DESKTOP() {
  245. echo "gein: Installing Xorg drivers..." &&
  246. $Emerge x11-base/xorg-drivers &&
  247. env-update && source /etc/profile &&
  248. export PS1="[chroot \u@\h \W]$ "
  249. echo "gein: Installing desktop packages..." &&
  250. $Emerge @gein-base "$DesktopChoice"
  251. read -ep "gein: Install laptop packages? [Y/N]: " SetupUser
  252. if echo "$SetupUser" | grep -iq "^y"; then
  253. $Emerge @gein-laptop
  254. rc-update add laptop_mode default
  255. fi
  256. }
  257. # This section is for completing tasks after the installation is
  258. # complete. The user will have a complete system already installed and
  259. # may skip these steps if desired.
  260. POSTINSTALL() {
  261. echo "gein: Setting root password..."
  262. passwd
  263. # echo "gein: Creating 'power' group"
  264. # groupadd power
  265. # poweroff reboot shutdown
  266. read -ep "gein: Setup a standard user? [Y/N]: " SetupUser
  267. if echo "$SetupUser" | grep -iq "^y"; then
  268. echo "gein: Creating user account" &&
  269. read -ep "Username: " Username &&
  270. useradd -m -G wheel,audio,video \
  271. -s /bin/bash "$Username" &&
  272. passwd $Username
  273. fi
  274. echo "gein: Installation complete."
  275. }
  276. # This is the CLI that controls what will be run. Keep in mind that the
  277. # BOOTSTRAP() needs to be run to completion before running the MINIMAL()
  278. # or DESKTOP() passes.
  279. shopt -s nocasematch
  280. case $1 in
  281. -b|bootstrap)
  282. BOOTSTRAP
  283. ;;
  284. -m|minimal)
  285. MINIMAL && POSTINSTALL
  286. ;;
  287. -d|desktop)
  288. case $2 in
  289. i3wm)
  290. sed -i '2,$s/^# //g' /etc/portage/package.use/defaults
  291. DesktopChoice="@gein-i3wm"
  292. MINIMAL && DESKTOP && POSTINSTALL
  293. ;;
  294. lxqt)
  295. sed -i '2,$s/^# //g' /etc/portage/package.use/defaults
  296. DesktopChoice="@gein-lxqt"
  297. MINIMAL && DESKTOP
  298. echo "azryn: Set SDDM as the display manager" &&
  299. sed -i 's/DISPLAYMANAGER="xdm"/DISPLAYMANAGER="sddm"/g' \
  300. /etc/conf.d/xdm &&
  301. sed -i 's/startl|xqt/"ck-launch-session dbus-launch startlxqt"/g' \
  302. /usr/share/xsessions/lxqt.desktop &&
  303. rc-update add xdm default &&
  304. rc-update add dbus default
  305. POSTINSTALL
  306. ;;
  307. *)
  308. echo "gein: $2 not an available desktop"
  309. echo ""
  310. echo "Available desktops:"
  311. echo " i3wm A complete i3wm desktop"
  312. echo " lxqt A complete LXQT desktop"
  313. esac
  314. ;;
  315. *)
  316. echo "gein: Linux-based derivative of Gentoo"
  317. echo " -h, help Shows this output"
  318. echo " -b, bootstrap Bootstrap the stage3 tarball"
  319. echo ""
  320. echo "Post-bootstrap:"
  321. echo " -m, minimal Perform a basic Gentoo installation"
  322. echo " -d, desktop Install a gein desktop"
  323. echo " i3wm A complete i3wm desktop"
  324. echo " lxqt A complete LXQT desktop"
  325. esac
  326. shopt -u nocasematch