bootstrap.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. #!/bin/bash
  2. # Copyright 1999-2014 Gentoo Foundation
  3. # Distributed under the terms of the GNU General Public License v2
  4. # people who were here:
  5. # (drobbins, 06 Jun 2003)
  6. # (solar, Jul 2004)
  7. # (vapier, Aug 2004)
  8. # (compnerd, Nov 2004)
  9. # (wolf31o2, Jan 2005)
  10. # (azarah, Mar 2005)
  11. # (uberlord, May 2007)
  12. # (kumba, May 2007)
  13. # (williamh, Mar 2014)
  14. # (kumba, Feb 2015)
  15. # sanity check
  16. [[ -e /etc/profile ]] && . /etc/profile
  17. if [[ -e /lib/gentoo/functions.sh ]] ; then
  18. source /lib/gentoo/functions.sh
  19. elif [[ -e /etc/init.d/functions.sh ]] ; then
  20. source /etc/init.d/functions.sh
  21. else
  22. eerror() { echo "!!! $*"; }
  23. einfo() { echo "* $*"; }
  24. fi
  25. # Use our own custom script, else logger cause things to
  26. # 'freeze' if we do not have a system logger running
  27. esyslog() {
  28. :
  29. }
  30. show_status() {
  31. local num=$1
  32. shift
  33. echo " [[ ($num/3) $* ]]"
  34. }
  35. # Track progress of the bootstrap process to allow for
  36. # semi-transparent resuming
  37. progressfile=/var/run/bootstrap-progress
  38. [[ -e ${progressfile} ]] && source ${progressfile}
  39. export BOOTSTRAP_STAGE=${BOOTSTRAP_STAGE:-1}
  40. set_bootstrap_stage() {
  41. [[ -z ${STRAP_RUN} ]] && return 0
  42. export BOOTSTRAP_STAGE=$1
  43. echo "BOOTSTRAP_STAGE=$1" > ${progressfile}
  44. }
  45. v_echo() {
  46. einfo "Executing: $*"
  47. env "$@"
  48. }
  49. cvsver="$Id$" # TODO: FIXME for Git era
  50. cvsver=${cvsver##*,v }
  51. cvsver=${cvsver%%Exp*}
  52. cvsyear=${cvsver#* }
  53. cvsyear=${cvsyear%%/*}
  54. usage() {
  55. echo -e "Usage: ${HILITE}${0##*/}${NORMAL} ${GOOD}[options]${NORMAL}"
  56. echo -e " ${GOOD}--debug (-d)${NORMAL} Run with debug information turned on"
  57. echo -e " ${GOOD}--fetchonly (-f)${NORMAL} Just download all the source files"
  58. echo -e " ${GOOD}--info (-i)${NORMAL} Show system related information"
  59. echo -e " ${GOOD}--pretend (-p)${NORMAL} Display the packages that will be merged"
  60. echo -e " ${GOOD}--quiet (-q)${NORMAL} Reduced or condensed output from portage's displays."
  61. echo -e " ${GOOD}--tree (-t)${NORMAL} Display the dependency tree, forces -p"
  62. echo -e " ${GOOD}--resume (-r)${NORMAL} Build/use binary packages"
  63. }
  64. STRAP_EMERGE_OPTS="--oneshot"
  65. STRAP_RUN=1
  66. V_ECHO=env
  67. DEBUG=0
  68. for opt in "$@" ; do
  69. case ${opt} in
  70. --fetchonly|-f)
  71. echo "Running in fetch-only mode ..."
  72. STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} -f"
  73. unset STRAP_RUN;;
  74. --help|-h)
  75. usage
  76. exit 0;;
  77. --debug|-d) STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} --debug"; DEBUG=1;;
  78. --info|-i) STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} --info" ; unset STRAP_RUN ;;
  79. --pretend|-p) STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} -p" ; unset STRAP_RUN ;;
  80. --quiet|-q) STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} -q" ; unset STRAP_RUN ;;
  81. --tree|-t) STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} -p -t" ; unset STRAP_RUN ;;
  82. --resume|-r) STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} --usepkg --buildpkg";;
  83. --verbose|-v) STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} -v"; V_ECHO=v_echo;;
  84. --version|-V)
  85. einfo "Gentoo Linux bootstrap ${cvsver}"
  86. exit 0
  87. ;;
  88. *)
  89. eerror "Unknown option '${opt}'"
  90. usage
  91. exit 1;;
  92. esac
  93. done
  94. RESUME=0
  95. if [[ -n ${STRAP_RUN} ]] ; then
  96. if [ ${BOOTSTRAP_STAGE} -ge 3 ] ; then
  97. echo
  98. einfo "System has been bootstrapped already!"
  99. einfo "If you re-bootstrap the system, you must complete the entire bootstrap process"
  100. einfo "otherwise you will have a broken system."
  101. einfo "Press enter to continue or CTRL+C to abort ..."
  102. read
  103. set_bootstrap_stage 1
  104. elif [ ${BOOTSTRAP_STAGE} -gt 1 ] ; then
  105. einfo "Resuming bootstrap at internal stage #${BOOTSTRAP_STAGE} ..."
  106. RESUME=1
  107. fi
  108. else
  109. export BOOTSTRAP_STAGE=0
  110. fi
  111. for p in /etc/portage /etc ; do
  112. p+="/make.profile"
  113. [[ -e ${p} ]] || continue
  114. if type -P realpath >/dev/null ; then
  115. MYPROFILEDIR=$(realpath ${p})
  116. else
  117. MYPROFILEDIR=$(readlink -f ${p})
  118. fi
  119. done
  120. if [[ ! -d ${MYPROFILEDIR} ]] ; then
  121. eerror "Error: '${MYPROFILEDIR}' does not exist. Exiting."
  122. exit 1
  123. fi
  124. echo -e "\n${GOOD}Gentoo Linux; ${BRACKET}http://www.gentoo.org/${NORMAL}"
  125. echo -e "Copyright 1999-${cvsyear} Gentoo Foundation; Distributed under the GPLv2"
  126. if [[ " ${STRAP_EMERGE_OPTS} " == *" -f "* ]] ; then
  127. echo "Fetching all bootstrap-related archives ..."
  128. elif [[ -n ${STRAP_RUN} ]] ; then
  129. if [ ${BOOTSTRAP_STAGE} -gt 2 ] ; then
  130. echo "Resuming Bootstrap of base system ..."
  131. else
  132. echo "Starting Bootstrap of base system ..."
  133. fi
  134. fi
  135. echo -------------------------------------------------------------------------------
  136. show_status 0 Locating packages
  137. # This should not be set to get glibc to build properly. See bug #7652.
  138. unset LD_LIBRARY_PATH
  139. # We do not want stray $TMP, $TMPDIR or $TEMP settings
  140. unset TMP TMPDIR TEMP
  141. cleanup() {
  142. if [[ -n ${STRAP_RUN} ]] ; then
  143. if [ ${BOOTSTRAP_STAGE} -le 2 ] ; then
  144. cp -f /var/cache/edb/mtimedb /var/run/bootstrap-mtimedb
  145. else
  146. rm -f /var/run/bootstrap-mtimedb
  147. fi
  148. fi
  149. exit $1
  150. }
  151. pycmd() {
  152. [[ ${DEBUG} = "1" ]] && echo /usr/bin/python -c "$@" > /dev/stderr
  153. /usr/bin/python -c "$@"
  154. }
  155. # TSTP messes ^Z of bootstrap up, so we don't trap it anymore.
  156. trap "cleanup" TERM KILL INT QUIT ABRT
  157. # Bug #50158 (don't use `which` in a bootstrap).
  158. if ! type -path portageq &>/dev/null ; then
  159. echo -------------------------------------------------------------------------------
  160. eerror "Your portage version is too old. Please use a newer stage1 image."
  161. echo
  162. cleanup 1
  163. fi
  164. # USE may be set from the environment so we back it up for later.
  165. export ORIGUSE=$(portageq envvar USE)
  166. # Check for 'build' or 'bootstrap' in USE ...
  167. INVALID_USE=$(gawk -v ORIGUSE="${ORIGUSE}" '
  168. BEGIN {
  169. if (ORIGUSE ~ /[[:space:]](build|bootstrap)[[:space:]]/)
  170. print "yes"
  171. }')
  172. # Do not do the check for stage build scripts ...
  173. if [[ ${INVALID_USE} = "yes" ]] ; then
  174. echo
  175. eerror "You have 'build' or 'bootstrap' in your USE flags. Please"
  176. eerror "remove it before trying to continue, since these USE flags"
  177. eerror "are used for internal purposes and shouldn't be specified"
  178. eerror "by you."
  179. echo
  180. cleanup 1
  181. fi
  182. # since our logic here ignores stuff found in package.use, let's warn the
  183. # user so they can avert disaster early
  184. if [[ -n $(sed -n '/^[ ]*#/d;/^[ ]*$/d;p' /etc/portage/package.use 2>/dev/null) ]] ; then
  185. echo
  186. ewarn "You appear to have custom USE flags set in /etc/portage/package.use."
  187. ewarn "Be aware that these settings may be ignored while running this script"
  188. ewarn "(due to limitations in the bootstrap process). If you have some USE"
  189. ewarn "flags you wish to apply to say gcc or glibc, you should hit CTRL+C"
  190. ewarn "now, export them in your environment (see below), and then restart."
  191. ewarn " # export USE='some flags i want'"
  192. fi
  193. # gettext should only be needed when used with nls
  194. for opt in ${ORIGUSE} ; do
  195. case "${opt}" in
  196. bindist)
  197. ALLOWED_USE="${ALLOWED_USE} bindist"
  198. ;;
  199. nls)
  200. USE_NLS=1
  201. ALLOWED_USE="${ALLOWED_USE} nls"
  202. ;;
  203. nptl)
  204. export MYARCH=$(portageq envvar ARCH)
  205. if [[ -z $(portageq best_visible / '>=sys-kernel/linux-headers-2.6.0') ]] ; then
  206. eerror "You need to have >=sys-kernel/linux-headers-2.6.0 unmasked!"
  207. eerror "Please edit the latest >=sys-kernel/linux-headers-2.6.0 package,"
  208. eerror "and add your ARCH to KEYWORDS or change your make.profile link"
  209. eerror "to a profile which does not have 2.6 headers masked."
  210. echo
  211. cleanup 1
  212. fi
  213. USE_NPTL=1
  214. ;;
  215. nptlonly)
  216. USE_NPTLONLY=1
  217. ;;
  218. multilib)
  219. ALLOWED_USE="${ALLOWED_USE} multilib"
  220. ;;
  221. userlocales)
  222. ALLOWED_USE="${ALLOWED_USE} userlocales"
  223. ;;
  224. esac
  225. done
  226. # With cascading profiles, the packages profile at the leaf is not a
  227. # complete system, just the restrictions to it for the specific profile.
  228. # The complete profile consists of an aggregate of the leaf and all its
  229. # parents. So we now call portage to read the aggregate profile and store
  230. # that into a variable.
  231. eval $(pycmd '
  232. import portage
  233. import sys
  234. for atom in portage.settings.packages:
  235. if not isinstance(atom, portage.dep.Atom):
  236. atom = portage.dep.Atom(atom.lstrip("*"))
  237. varname = "my" + portage.catsplit(atom.cp)[1].upper().replace("-", "_")
  238. sys.stdout.write("%s=\"%s\"; " % (varname, atom))
  239. ')
  240. # This stuff should never fail but will if not enough is installed.
  241. [[ -z ${myBASELAYOUT} ]] && myBASELAYOUT=">=$(portageq best_version / sys-apps/baselayout)"
  242. [[ -z ${myPORTAGE} ]] && myPORTAGE="portage"
  243. [[ -z ${myBINUTILS} ]] && myBINUTILS="binutils"
  244. [[ -z ${myGCC} ]] && myGCC="gcc"
  245. [[ -z ${myGETTEXT} ]] && myGETTEXT="gettext"
  246. [[ -z ${myLIBC} ]] && myLIBC="$(portageq expand_virtual / virtual/libc)"
  247. [[ -z ${myTEXINFO} ]] && myTEXINFO="sys-apps/texinfo"
  248. [[ -z ${myZLIB} ]] && myZLIB="zlib"
  249. [[ -z ${myNCURSES} ]] && myNCURSES="ncurses"
  250. # Do we really want gettext/nls?
  251. [[ ${USE_NLS} != 1 ]] && myGETTEXT=
  252. # Do we really have no 2.4.x nptl kernels in portage?
  253. if [[ ${USE_NPTL} = "1" ]] ; then
  254. myOS_HEADERS="$(portageq best_visible / '>=sys-kernel/linux-headers-2.6.0')"
  255. [[ -n ${myOS_HEADERS} ]] && myOS_HEADERS=">=${myOS_HEADERS}"
  256. ALLOWED_USE="${ALLOWED_USE} nptl"
  257. # Should we build with nptl only?
  258. [[ ${USE_NPTLONLY} = "1" ]] && ALLOWED_USE="${ALLOWED_USE} nptlonly"
  259. fi
  260. [[ -z ${myOS_HEADERS} ]] && myOS_HEADERS="virtual/os-headers"
  261. einfo "Using baselayout : ${myBASELAYOUT}"
  262. einfo "Using portage : ${myPORTAGE}"
  263. einfo "Using os-headers : ${myOS_HEADERS}"
  264. einfo "Using binutils : ${myBINUTILS}"
  265. einfo "Using gcc : ${myGCC}"
  266. [[ ${USE_NLS} = "1" ]] && einfo "Using gettext : ${myGETTEXT}"
  267. einfo "Using libc : ${myLIBC}"
  268. einfo "Using texinfo : ${myTEXINFO}"
  269. einfo "Using zlib : ${myZLIB}"
  270. einfo "Using ncurses : ${myNCURSES}"
  271. echo -------------------------------------------------------------------------------
  272. show_status 1 Configuring environment
  273. echo -------------------------------------------------------------------------------
  274. [[ -x /usr/sbin/gcc-config ]] && GCC_CONFIG="/usr/sbin/gcc-config"
  275. [[ -x /usr/bin/gcc-config ]] && GCC_CONFIG="/usr/bin/gcc-config"
  276. # Allow portage to overwrite stuff
  277. export CONFIG_PROTECT="-*"
  278. # disable collision-protection
  279. export FEATURES="${FEATURES} -collision-protect"
  280. # query BOOTSTRAP_USE from the profile
  281. BOOTSTRAP_USE=$(portageq envvar BOOTSTRAP_USE)
  282. if [ ${BOOTSTRAP_STAGE} -le 1 ] ; then
  283. show_status 2 Updating portage
  284. ${V_ECHO} USE="-* build bootstrap ${ALLOWED_USE} ${BOOTSTRAP_USE}" emerge ${STRAP_EMERGE_OPTS} ${myPORTAGE} || cleanup 1
  285. echo -------------------------------------------------------------------------------
  286. set_bootstrap_stage 2
  287. fi
  288. export USE="-* bootstrap ${ALLOWED_USE} ${BOOTSTRAP_USE}"
  289. # We can't unmerge headers which may or may not exist yet. If your
  290. # trying to use nptl, it may be needed to flush out any old headers
  291. # before fully bootstrapping.
  292. if [ ${BOOTSTRAP_STAGE} -le 2 ] ; then
  293. show_status 3 Emerging packages
  294. if [[ ${RESUME} -eq 1 ]] ; then
  295. STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} --resume"
  296. cp /var/run/bootstrap-mtimedb /var/cache/edb
  297. else
  298. STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} \
  299. ${myOS_HEADERS} ${myTEXINFO} ${myGETTEXT} ${myBINUTILS} \
  300. ${myGCC} ${myLIBC} ${myBASELAYOUT} ${myZLIB}"
  301. fi
  302. ${V_ECHO} emerge ${STRAP_EMERGE_OPTS} || cleanup 1
  303. echo -------------------------------------------------------------------------------
  304. set_bootstrap_stage 3
  305. fi
  306. # Basic support for gcc multi version/arch scheme ...
  307. if [[ -n ${STRAP_RUN} ]] ; then
  308. if [[ -x ${GCC_CONFIG} ]] && ${GCC_CONFIG} --get-current-profile &>/dev/null
  309. then
  310. # Make sure we get the old gcc unmerged ...
  311. emerge --prune sys-devel/gcc || cleanup 1
  312. # Make sure the profile and /lib/cpp and /usr/bin/cc are valid ...
  313. ${GCC_CONFIG} "$(${GCC_CONFIG} --get-current-profile)" &>/dev/null
  314. fi
  315. fi
  316. if [[ -n ${STRAP_RUN} ]] ; then
  317. echo -------------------------------------------------------------------------------
  318. einfo "Please note that you should now add the '-e' option for emerge system:"
  319. echo
  320. einfo " # emerge -e system"
  321. echo
  322. fi
  323. cleanup 0