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