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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. #!/usr/bin/env sh
  2. #
  3. # While this script does assist with installing Gentoo, it is NOT a
  4. # replacement for reading and understanding the Gentoo Handbook for your
  5. # system. See https://gentoo.org/get-started/ for information on getting
  6. # started with Gentoo.
  7. #
  8. # This file uses inline documentation whenever possible to preserve
  9. # context, situational or otherwise. Please read the entire file before
  10. # continuing to ensure you are aware of how this script functions, as
  11. # undesired behaviors for your use-case may exist.
  12. #
  13. # This script does NOT automatically partition your disk(s). You MUST
  14. # partition and mount your disk(s) before running this script.
  15. #
  16. # This file uses a modified 0-clause MIT license. If you did not receive
  17. # a copy of the license, please visit https://gein.io/License.md for a
  18. # copy.
  19. #
  20. ###
  21. # This section describes variables that will define the resulting
  22. # system. '$Hostname', '$Locale', and '$TimeZone' are mostly obvious in
  23. # terms of their use, though '$VideoCards' is used as the value of
  24. # 'VIDEO_CARDS' within '/etc/portage/make.conf'.
  25. cpu_cores="$(grep -c ^processor /proc/cpuinfo)"
  26. hostname="gein"
  27. locale="en_US.UTF-8 UTF-8"
  28. timezone="America/New_York"
  29. video_cards="false"
  30. # This section contains aliases for commands which are used throughout
  31. # the script. By default commands have their output and prompts
  32. # suppressed, though should you want to review or debug this script you
  33. # may want to adjust these as desired.
  34. curl="curl -sSf"
  35. emerge="emerge -v --quiet-build"
  36. emerge_sync="emerge -q --sync"
  37. make="make -s -j$cpu_cores"
  38. mkdir="mkdir -p"
  39. rm="rm"
  40. wget="wget -q"
  41. # This section describes how the kernel will be built, and whether the
  42. # user will be prompted to configure their kernel. The two mentioned
  43. # variables may be used together to suit a variety of use-cases.
  44. #
  45. # kernel_autobuild
  46. #
  47. # When 'true', this implies that the user does not want to perform
  48. # any manual configuration of the kernel, regardless of whether a
  49. # configuration file was provided. '$ make menuconfig' will NOT be
  50. # run before compiling the kernel.
  51. #
  52. # When any value OTHER than 'true', this implies that the use DOES
  53. # want to perform manual configuration on the kernel, regardless
  54. # of whether a configuration file was provided. '$ make menuconfig'
  55. # will be run before compiling the kernel.
  56. #
  57. # kernel_config
  58. #
  59. # The URL location of a kernel configuration file. If this variable
  60. # is unset, no attempt to download a configuration file will be
  61. # made, instead using the kernel defconfig.
  62. kernel_autobuild="true"
  63. #kernel_config=""
  64. # I would like this section to be where a user defines their partitions,
  65. #
  66. #partition_boot=""
  67. #partition_uefi=""
  68. #
  69. #
  70. config_source="https://gein.io"
  71. CONFIG() {
  72. config_files="
  73. /etc/portage/make.conf
  74. /etc/portage/package.accept_keywords/development
  75. /etc/portage/package.accept_keywords/lxqt
  76. /etc/portage/package.accept_keywords/media
  77. /etc/portage/package.accept_keywords/system
  78. /etc/portage/package.env
  79. /etc/portage/package.license
  80. /etc/portage/package.use/global
  81. /etc/portage/package.use/local
  82. /etc/portage/package.use/multilib
  83. /etc/portage/sets/gein-base
  84. /etc/portage/sets/gein-i3wm
  85. /etc/portage/sets/gein-lxqt
  86. /etc/portage/sets/gein-laptop
  87. /usr/local/sbin/gpkg
  88. /usr/local/sbin/kbuild
  89. "
  90. config_dirs="
  91. /etc/portage/package.accept_keywords
  92. /etc/portage/package.use
  93. /etc/portage/sets
  94. "
  95. for dir in $config_dirs; do
  96. if [ ! -d "$dir" ]; then
  97. $rm "$dir"
  98. fi
  99. if [ ! -e "$dir" ]; then
  100. $mkdir "$dir"
  101. fi
  102. done
  103. unset config_dirs dir
  104. for file in $config_files; do
  105. $wget "$config_source/$file" -O "$file"
  106. done
  107. unset config_files file
  108. config_complete="true"
  109. }
  110. # This section determines the current system architecture, which is
  111. # used to download the correct Stage 3 archive in the following section.
  112. case "$(uname -m)" in
  113. i486|i586)
  114. cpu_dir="x86"
  115. cpu_arch="i486";;
  116. i686|x86|x86_32)
  117. cpu_dir="x86"
  118. cpu_arch="i686";;
  119. amd64|x86_64)
  120. cpu_dir="amd64"
  121. cpu_arch="amd64";;
  122. *)
  123. echo "gein: error: your architecture has not been defined yet." \
  124. "Submit an issue with the output of 'uname -m'" | fold -s
  125. echo "gein: Exiting..." \
  126. | fold -s
  127. exit 1
  128. esac
  129. #
  130. #
  131. PREREQUISITES() {
  132. if [ -z "$partition_boot" ] || [ -z "$video_cards" ]; then
  133. echo "gein: error: required variables are unset!" | fold -s
  134. echo "gein: please ensure you have partitioned and mounted" \
  135. "your disks, as well as updated the variables associated" \
  136. "with the required partitions. You must also declare" \
  137. "your VideoCard. Please see gein.sh for instructions." \
  138. | fold -s
  139. echo "gein: Exiting..." | fold -s
  140. exit 1
  141. fi
  142. prerequisites_complete="true"
  143. }
  144. #
  145. #
  146. PARTITION() {
  147. if [ ! -e /mnt/gentoo ]; then
  148. echo "gein: error: '/mnt/gentoo' does not exist!" | fold -s
  149. echo "gein: '/mnt/gentoo' is referred to later in this script," \
  150. "and is required to continue. Please ensure your mounted" \
  151. "partitions are correct." | fold -s
  152. echo "gein: Exiting..." | fold -s
  153. exit 1
  154. fi
  155. partition_complete="true"
  156. }
  157. #
  158. #
  159. BOOTSTRAP() {
  160. echo "gein: Ensuring we are in /mnt/gentoo..."
  161. if [ ! -e /mnt/gentoo/$(basename "$0") ]; then
  162. cp "$0" /mnt/gentoo/
  163. cd /mnt/gentoo
  164. fi
  165. echo "gein: Setting system time via ntpd..." | fold -s
  166. if [ -x "$(command -v ntpd)" ]; then
  167. ntpd -q -g
  168. fi
  169. echo "gein: Downloading and extracting Stage3 tarball..."
  170. if [ -x "$(command -v curl)" ]; then
  171. stage3_source="http://distfiles.gentoo.org/releases/$cpu_dir/autobuilds"
  172. stage3_release="curl -s $stage3_source/latest-stage3-$cpu_arch.txt"
  173. stage3_current="$($stage3_release|tail -1|awk '{print $1}')"
  174. $wget "$stage3_source/$stage3_current"
  175. tar -xpf stage3-* --xattrs --numeric-owner
  176. rm -rf stage3-*
  177. unset stage3_source stage3_release stage3_current
  178. else
  179. echo "gein: error: curl not present!"
  180. echo "gein: Exiting..."
  181. exit 1
  182. fi
  183. echo "gein: Mounting hardware devices..."
  184. hardware_mountpoints="proc sys dev"
  185. for target in $hardware_mountpoints; do
  186. if [ -e /mnt/gentoo/"$target" ]; then
  187. case "$target" in
  188. proc) mount -t proc /proc /mnt/gentoo/proc ;;
  189. sys ) mount --rbind /sys /mnt/gentoo/sys
  190. mount --make-rslave /mnt/gentoo/sys ;;
  191. dev ) mount --rbind /dev /mnt/gentoo/dev
  192. mount --make-rslave /mnt/gentoo/dev ;;
  193. *) echo "gein: $target: Improper hardware device" \
  194. | fold -s
  195. exit
  196. esac
  197. else
  198. echo "gein: $target unable to be mounted! Exiting..." \
  199. | fold -s
  200. exit
  201. fi
  202. done
  203. unset hardware_mountpoints target
  204. if [ ! -e "$partition_swap" ]; then
  205. echo "gein: Setting up swapfile..." | fold -s
  206. mkswap "$partition_swap"
  207. swapon "$partition_swap"
  208. echo "/swapfile none swap sw 0 0" >> /mnt/gentoo/etc/fstab
  209. fi
  210. echo "gein: Copying '/etc/resolv.conf'..." | fold -s
  211. cp -L /etc/resolv.conf /mnt/gentoo/etc/
  212. echo "gein: Chroot'ing into /mnt/gentoo..." | fold -s
  213. chroot \
  214. /mnt/gentoo /usr/bin/env -i \
  215. HOME="/root" \
  216. TERM="$TERM" \
  217. PS1="[chroot \u@\h \W]$ " \
  218. PATH="/usr/local/sbin/:/usr/local/bin:/usr/sbin" \
  219. PATH="$PATH:/usr/bin:/sbin:/bin:/opt/bin" \
  220. MANPATH="/usr/man:/usr/share/man:/usr/local/man" \
  221. MANPATH="$MANPATH:/usr/local/share/man" \
  222. /bin/bash --login
  223. bootstrap_complete="true"
  224. }
  225. #
  226. #
  227. INSTALL() {
  228. CONFIG
  229. echo "gein: Setting CPU cores and GPU type..." | fold -s
  230. sed -i "s/Video_Cards/$VideoCards/g; s/Make_Opts/-j$CPUCores/g" \
  231. /etc/portage/make.conf
  232. echo "gein: Syncing Portage and selecting profile..." | fold -s
  233. $emerge_sync
  234. eselect profile list | grep -Evi "dev|exp"
  235. echo "gein: Choose the latest stable release" | fold -s
  236. profile_target=""
  237. while [ -z "$profile_target" ]; do
  238. read -p "Which profile?: " profile_target
  239. done
  240. eselect profile set "$profile_target"
  241. $emerge -uDN @world
  242. echo "gein: Setting timezone..." | fold -s
  243. echo "$timezone" > /etc/timezone
  244. $emerge --config sys-libs/timezone-data
  245. echo "gein: Setting locale..." | fold -s
  246. echo "$Locale" > /etc/locale.gen && locale-gen
  247. L="$(echo $Locale | awk -F '[-]' '{print $1}')"
  248. LL="$(eselect locale list|grep -i $L|awk -F '[][]' '{print $2}')"
  249. eselect locale set "$LL"
  250. env-update && source /etc/profile
  251. export PS1="[chroot \u@\h \W]$ "
  252. echo "gein: Emerging base system packages..." | fold -s
  253. $emerge @gein-base
  254. if grep -Rqi 'intel' /proc/cpuinfo; then
  255. echo "gein: Emerging intel-microcode"
  256. $emerge intel-microcode
  257. fi
  258. echo "gein: Configuring Linux kernel..."
  259. cd /usr/src/linux
  260. if [ "$kernel_autobuild" = "true" ]; then
  261. if [ -z "$kernel_config" ]; then
  262. make defconfig
  263. else
  264. $wget "$kernel_config" -O /usr/src/linux/.config
  265. fi
  266. elif [ "$kernel_autobuild" = "false" ]; then
  267. if [ -z "$kernel_config" ]; then
  268. make defconfig
  269. make menuconfig
  270. else
  271. $wget "$kernel_config" -O /usr/src/linux/.config
  272. make menuconfig
  273. fi
  274. else
  275. echo "gein: Error: AutoKernel isn't true or false. Exiting..."
  276. fi
  277. echo "gein: Compiling Linux kernel and modules..."
  278. $make && $make modules && \
  279. $make install && $make modules install && \
  280. $make distclean
  281. cd /
  282. echo "gein: Adding services to OpenRC..."
  283. rc-update add dhcpcd default
  284. rc-update add cronie default
  285. echo "gein: Setting hostname..."
  286. echo "hostname=$hostname" > /etc/conf.d/hostname
  287. echo "gein: Installing Grub to $partition_boot..."
  288. grub-install "$partition_boot"
  289. grub-mkconfig -o /boot/grub/grub.cfg
  290. install_complete="true"
  291. }
  292. #
  293. #
  294. POSTINSTALL() {
  295. echo "gein: Setting root password..."
  296. passwd
  297. # echo "gein: Creating 'power' group"
  298. # groupadd power
  299. # poweroff reboot shutdown
  300. read -p "gein: Setup a standard user? [Y/N]: " user_setup
  301. if echo "$user_setup" | grep -iq "^y"; then
  302. echo "gein: Creating user account"
  303. read -p "Username: " username
  304. useradd -m -G wheel,audio,video \
  305. -s /bin/bash "$username"
  306. passwd "$username"
  307. fi
  308. echo "gein: Installation complete."
  309. postinstall_complete="true"
  310. }
  311. #
  312. #
  313. case $1 in
  314. -p|--partition)
  315. PREREQUISITES
  316. PARTITION
  317. ;;
  318. -b|--bootstrap)
  319. PREREQUISITES
  320. BOOTSTRAP
  321. ;;
  322. -i|--install)
  323. PREREQUISITES
  324. INSTALL
  325. POSTINSTALL
  326. ;;
  327. *)
  328. echo "gein: Linux-based derivative of Gentoo"
  329. echo " -p, partition Partition and mount disk(s)"
  330. echo " -b. bootstrap Bootstrap the stage3 tarball"
  331. echo " -i, install Install Gentoo"
  332. esac