summaryrefslogtreecommitdiff
path: root/scripts/bootstrap.sh
blob: 48ed539e81869c4ac9a0f5d7a217cefa194a7617 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/bin/bash
# Copyright 1999-2021 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

# Maintainer: releng@gentoo.org

file_version="2021.0"		# update manually: <year>.<counter>

# people who were here:
# (drobbins, 06 Jun 2003)
# (solar, Jul 2004)
# (vapier, Aug 2004)
# (compnerd, Nov 2004)
# (wolf31o2, Jan 2005)
# (azarah, Mar 2005)
# (uberlord, May 2007)
# (kumba, May 2007)
# (williamh, Mar 2014)
# (kumba, Feb 2015)

# sanity check
[[ -e /etc/profile ]] && . /etc/profile

if [[ -e /lib/gentoo/functions.sh ]] ; then
	source /lib/gentoo/functions.sh
	elif [[ -e /etc/init.d/functions.sh ]] ; then
	source /etc/init.d/functions.sh
else
	eerror() { echo "!!! $*"; }
	einfo() { echo "* $*"; }
fi

# Use our own custom script, else logger cause things to
# 'freeze' if we do not have a system logger running
esyslog() {
	:
}

show_status() {
	local num=$1
	shift
	echo "  [[ ($num/3) $* ]]"
}

# Track progress of the bootstrap process to allow for
# semi-transparent resuming
progressfile=/var/run/bootstrap-progress
[[ -e ${progressfile} ]] && source ${progressfile}
export BOOTSTRAP_STAGE=${BOOTSTRAP_STAGE:-1}

set_bootstrap_stage() {
	[[ -z ${STRAP_RUN} ]] && return 0
	export BOOTSTRAP_STAGE=$1
	echo "BOOTSTRAP_STAGE=$1" > ${progressfile}
}

v_echo() {
	einfo "Executing: $*"
	env "$@"
}

file_copyright=$(sed -n '/Copyright/!b;s/^# *//;p;q' $0)

usage() {
	echo -e "Usage: ${HILITE}${0##*/}${NORMAL} ${GOOD}[options]${NORMAL}"
	echo -e "  ${GOOD}--debug (-d)${NORMAL}     Run with debug information turned on"
	echo -e "  ${GOOD}--fetchonly (-f)${NORMAL} Just download all the source files"
	echo -e "  ${GOOD}--info (-i)${NORMAL}      Show system related information"
	echo -e "  ${GOOD}--pretend (-p)${NORMAL}   Display the packages that will be merged"
	echo -e "  ${GOOD}--quiet (-q)${NORMAL}     Reduced or condensed output from portage"
	echo -e "  ${GOOD}--tree (-t)${NORMAL}      Display the dependency tree, forces -p"
	echo -e "  ${GOOD}--resume (-r)${NORMAL}    Build/use binary packages"
	echo -e "  ${GOOD}--verbose (-v)${NORMAL}   Verbose output from portage"
}

STRAP_EMERGE_OPTS="--oneshot"
STRAP_RUN=1
V_ECHO=env
DEBUG=0

for opt in "$@" ; do
	case ${opt} in
		--fetchonly|-f)
			echo "Running in fetch-only mode ..."
			STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} -f"
			unset STRAP_RUN;;
		--help|-h)
			usage
			exit 0;;
		--debug|-d)   STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} --debug"; DEBUG=1;;
		--info|-i)    STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} --info" ; unset STRAP_RUN ;;
		--pretend|-p) STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} -p"     ; unset STRAP_RUN ;;
		--quiet|-q)   STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} -q"     ; unset STRAP_RUN ;;
		--tree|-t)    STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} -p -t"  ; unset STRAP_RUN ;;
		--resume|-r)  STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} --usepkg --buildpkg";;
		--verbose|-v) STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} -v"     ; V_ECHO=v_echo;;
		--version|-V)
			einfo "Gentoo Linux bootstrap ${file_version}"
			exit 0
			;;
		*)
			eerror "Unknown option '${opt}'"
			usage
			exit 1;;
	esac
done

RESUME=0
if [[ -n ${STRAP_RUN} ]]  ; then
	if [ ${BOOTSTRAP_STAGE} -ge 3 ] ; then
		echo
		einfo "System has been bootstrapped already!"
		einfo "If you re-bootstrap the system, you must complete the entire bootstrap process"
		einfo "otherwise you will have a broken system."
		einfo "Press enter to continue or CTRL+C to abort ..."
		read
		set_bootstrap_stage 1
	elif [ ${BOOTSTRAP_STAGE} -gt 1 ] ; then
		einfo "Resuming bootstrap at internal stage #${BOOTSTRAP_STAGE} ..."
		RESUME=1
	fi
else
	export BOOTSTRAP_STAGE=0
fi

for p in /etc/portage /etc ; do
	p+="/make.profile"
	[[ -e ${p} ]] || continue
	if type -P realpath >/dev/null ; then
		MYPROFILEDIR=$(realpath ${p})
	else
		MYPROFILEDIR=$(readlink -f ${p})
	fi
done
if [[ ! -d ${MYPROFILEDIR} ]] ; then
	eerror "Error:  '${MYPROFILEDIR}' does not exist.  Exiting."
	exit 1
fi

echo -e "\n${GOOD}Gentoo Linux; ${BRACKET}http://www.gentoo.org/${NORMAL}"
echo -e "${file_copyright}; Distributed under the GPLv2"
if [[ " ${STRAP_EMERGE_OPTS} " == *" -f "* ]] ; then
	echo "Fetching all bootstrap-related archives ..."
elif [[ -n ${STRAP_RUN} ]] ; then
	if [ ${BOOTSTRAP_STAGE} -gt 2 ] ; then
		echo "Resuming Bootstrap of base system ..."
	else
		echo "Starting Bootstrap of base system ..."
	fi
fi
echo -------------------------------------------------------------------------------
show_status 0 Locating packages

# This should not be set to get glibc to build properly. See bug #7652.
unset LD_LIBRARY_PATH

# We do not want stray $TMP, $TMPDIR or $TEMP settings
unset TMP TMPDIR TEMP

cleanup() {
	if [[ -n ${STRAP_RUN} ]] ; then
		if [ ${BOOTSTRAP_STAGE} -le 2 ] ; then
			cp -f /var/cache/edb/mtimedb /var/run/bootstrap-mtimedb
		else
			rm -f /var/run/bootstrap-mtimedb
		fi
	fi
	exit $1
}

pycmd() {
	[[ ${DEBUG} = "1" ]] && echo /usr/bin/python -c "$@" > /dev/stderr
	/usr/bin/python -c "$@"
}

# TSTP messes ^Z of bootstrap up, so we don't trap it anymore.
trap "cleanup" TERM INT QUIT ABRT

# Bug #50158 (don't use `which` in a bootstrap).
if ! type -path portageq &>/dev/null ; then
	echo -------------------------------------------------------------------------------
	eerror "Your portage version is too old.  Please use a newer stage1 image."
	echo
	cleanup 1
fi

# USE may be set from the environment so we back it up for later.
export ORIGUSE=$(portageq envvar USE)

# Check for 'build' or 'bootstrap' in USE ...
INVALID_USE=$(gawk -v ORIGUSE="${ORIGUSE}" '
	BEGIN {
		if (ORIGUSE ~ /[[:space:]](build|bootstrap)[[:space:]]/)
			print "yes"
	}')

# Do not do the check for stage build scripts ...
if [[ ${INVALID_USE} = "yes" ]] ; then
	echo
	eerror "You have 'build' or 'bootstrap' in your USE flags. Please"
	eerror "remove it before trying to continue, since these USE flags"
	eerror "are used for internal purposes and shouldn't be specified"
	eerror "by you."
	echo
	cleanup 1
fi

# since our logic here ignores stuff found in package.use, let's warn the
# user so they can avert disaster early
if [[ -n $(sed -n '/^[ 	]*#/d;/^[ 	]*$/d;p' /etc/portage/package.use 2>/dev/null) ]] ; then
	echo
	ewarn "You appear to have custom USE flags set in /etc/portage/package.use."
	ewarn "Be aware that these settings may be ignored while running this script"
	ewarn "(due to limitations in the bootstrap process).  If you have some USE"
	ewarn "flags you wish to apply to say gcc or glibc, you should hit CTRL+C"
	ewarn "now, export them in your environment (see below), and then restart."
	ewarn " # export USE='some flags i want'"
fi

# gettext should only be needed when used with nls
for opt in ${ORIGUSE} ; do
	case "${opt}" in
		bindist)
			ALLOWED_USE="${ALLOWED_USE} bindist"
			;;
		nls)
			USE_NLS=1
			ALLOWED_USE="${ALLOWED_USE} nls"
			;;
		nptl)
			export MYARCH=$(portageq envvar ARCH)
 			if [[ -z $(portageq best_visible / '>=sys-kernel/linux-headers-2.6.0') ]] ; then
				eerror "You need to have >=sys-kernel/linux-headers-2.6.0 unmasked!"
				eerror "Please edit the latest >=sys-kernel/linux-headers-2.6.0 package,"
				eerror "and add your ARCH to KEYWORDS or change your make.profile link"
				eerror "to a profile which does not have 2.6 headers masked."
				echo
				cleanup 1
			fi
			USE_NPTL=1
			;;
		multilib)
			ALLOWED_USE="${ALLOWED_USE} multilib"
			;;
		userlocales)
			ALLOWED_USE="${ALLOWED_USE} userlocales"
			;;
	esac
done

# With cascading profiles, the packages profile at the leaf is not a
# complete system, just the restrictions to it for the specific profile.
# The complete profile consists of an aggregate of the leaf and all its
# parents.  So we now call portage to read the aggregate profile and store
# that into a variable.

eval $(pycmd '
import portage
from portage.dbapi._expand_new_virt import expand_new_virt
import sys
root = portage.settings["EROOT"]
for atom in portage.settings.packages:
	if not isinstance(atom, portage.dep.Atom):
		atom = portage.dep.Atom(atom.lstrip("*"))
	varname = "my" + portage.catsplit(atom.cp)[1].upper().replace("-", "_")
	atom = list(expand_new_virt(portage.db[root]["vartree"].dbapi, atom))[0]
	sys.stdout.write("%s=\"%s\"; " % (varname, atom))
')

# This stuff should never fail but will if not enough is installed.
[[ -z ${myBASELAYOUT} ]] && myBASELAYOUT=">=$(portageq best_version / sys-apps/baselayout)"
[[ -z ${myPORTAGE}    ]] && myPORTAGE="sys-apps/portage"
[[ -z ${myBINUTILS}   ]] && myBINUTILS="sys-devel/binutils"
[[ -z ${myGCC}        ]] && myGCC="sys-devel/gcc"
[[ -z ${myGETTEXT}    ]] && myGETTEXT="sys-devel/gettext"
[[ -z ${myLIBC}       ]] && myLIBC="$(portageq expand_virtual / virtual/libc)"
[[ -z ${myTEXINFO}    ]] && myTEXINFO="sys-apps/texinfo"
[[ -z ${myZLIB}       ]] && myZLIB="sys-libs/zlib"
[[ -z ${myNCURSES}    ]] && myNCURSES="sys-libs/ncurses"

# Do we really want gettext/nls?
[[ ${USE_NLS} != 1 ]] && myGETTEXT=

if [[ ${USE_NPTL} = "1" ]] ; then
	myOS_HEADERS="$(portageq best_visible / '>=sys-kernel/linux-headers-2.6.0')"
	[[ -n ${myOS_HEADERS} ]] && myOS_HEADERS=">=${myOS_HEADERS}"
	ALLOWED_USE="${ALLOWED_USE} nptl"
fi
[[ -z ${myOS_HEADERS} ]] && myOS_HEADERS="$(portageq expand_virtual / virtual/os-headers)"

einfo "Using baselayout : ${myBASELAYOUT}"
einfo "Using portage    : ${myPORTAGE}"
einfo "Using os-headers : ${myOS_HEADERS}"
einfo "Using binutils   : ${myBINUTILS}"
einfo "Using gcc        : ${myGCC}"
[[ ${USE_NLS} = "1" ]] && einfo "Using gettext    : ${myGETTEXT}"
einfo "Using libc       : ${myLIBC}"
einfo "Using texinfo    : ${myTEXINFO}"
einfo "Using zlib       : ${myZLIB}"
einfo "Using ncurses    : ${myNCURSES}"
echo -------------------------------------------------------------------------------
show_status 1 Configuring environment
echo -------------------------------------------------------------------------------

[[ -x /usr/sbin/gcc-config ]] && GCC_CONFIG="/usr/sbin/gcc-config"
[[ -x /usr/bin/gcc-config  ]] && GCC_CONFIG="/usr/bin/gcc-config"

# Allow portage to overwrite stuff
[[ $CONFIG_PROTECT != "-*"* ]] && export CONFIG_PROTECT="-*"

# disable collision-protection
export FEATURES="${FEATURES} -collision-protect"

# query BOOTSTRAP_USE from the profile
BOOTSTRAP_USE=$(portageq envvar BOOTSTRAP_USE)

if [ ${BOOTSTRAP_STAGE} -le 1 ] ; then
	show_status 2 Updating portage
	${V_ECHO} USE="-* build bootstrap ${ALLOWED_USE} ${BOOTSTRAP_USE}" emerge ${STRAP_EMERGE_OPTS} ${myPORTAGE} || cleanup 1
	echo -------------------------------------------------------------------------------
	set_bootstrap_stage 2
fi
export USE="-* bootstrap ${ALLOWED_USE} ${BOOTSTRAP_USE}"

# We can't unmerge headers which may or may not exist yet. If your
# trying to use nptl, it may be needed to flush out any old headers
# before fully bootstrapping.
if [ ${BOOTSTRAP_STAGE} -le 2 ] ; then
	show_status 3 Emerging packages
	if [[ ${RESUME} -eq 1 ]] ; then
		STRAP_EMERGE_POSARGS=""
		STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} --resume"
		cp /var/run/bootstrap-mtimedb /var/cache/edb
	else
		STRAP_EMERGE_POSARGS="\
			${myOS_HEADERS} ${myTEXINFO} ${myGETTEXT} ${myBINUTILS} \
			${myGCC} ${myLIBC} ${myBASELAYOUT} ${myZLIB}"
	fi
	${V_ECHO} emerge ${STRAP_EMERGE_OPTS} ${STRAP_EMERGE_POSARGS} || cleanup 1
	echo -------------------------------------------------------------------------------
	set_bootstrap_stage 3
fi

# Basic support for gcc multi version/arch scheme ...
if [[ -n ${STRAP_RUN} ]] ; then
	if [[ -x ${GCC_CONFIG} ]] && ${GCC_CONFIG} --get-current-profile &>/dev/null
	then
		output=$(${V_ECHO} emerge ${STRAP_EMERGE_OPTS} --prune --pretend --quiet sys-devel/gcc 2>/dev/null)
		if [[ ${DEBUG} = "1" ]] ; then
			echo "${output}"
		fi
		if [[ "${output}" = *'All selected packages:'* ]] ; then
			# Make sure we get the old gcc unmerged ...
			${V_ECHO} emerge ${STRAP_EMERGE_OPTS} --prune sys-devel/gcc
			# Make sure the profile and /lib/cpp and /usr/bin/cc are valid ...
			${GCC_CONFIG} "$(${GCC_CONFIG} --get-current-profile)" &>/dev/null
		fi
	fi
fi

if [[ -n ${STRAP_RUN} ]] ; then
	echo -------------------------------------------------------------------------------
	einfo "Please note that you should now add the '-e' option for emerge system:"
	echo
	einfo "  # emerge <other_opts> -e @system"
	echo
fi

cleanup 0