library.sh 23.9 KB
Newer Older
1 2 3 4 5 6 7 8
# waypoint vars
WAYPOINTS=""
NUM_STEPS=0

# progress bar vars
export PB_POSITION=0
export PB_WAYPOINT_LENGTH=0

9
# used for setting up apt
10 11 12 13 14
PROTOCOL=
MIRROR=
DIRECTORY=
COMPONENTS=
DISTRIBUTION=
15 16

# used by kernel installation code
17 18
KERNEL=
KERNEL_LIST=/tmp/available_kernels.txt
19 20
KERNEL_NAME=`udpkg --print-os`
case $KERNEL_NAME in
21
	linux)		KERNEL_MAJOR="$(uname -r | cut -d - -f 1 | cut -d . -f 1,2)" ;;
22
	kfreebsd)	KERNEL_MAJOR="$(uname -r | cut -d . -f 1)" ;;
23
	hurd)		KERNEL_NAME=gnumach ; KERNEL_MAJOR="$(uname -v | cut -d ' ' -f 2 | cut -d . -f 1)" ;;
24
esac
25 26
KERNEL_VERSION="$(uname -r | cut -d - -f 1)"
KERNEL_ABI="$(uname -r | cut -d - -f 1,2)"
27
KERNEL_FLAVOUR=$(uname -r | cut -d - -f 3-)
28 29 30
MACHINE="$(uname -m)"
NUMCPUS=$(cat /var/numcpus 2>/dev/null) || true
CPUINFO=/proc/cpuinfo
31
OFCPUS=/proc/device-tree/cpus/
32

33 34 35 36
# files and directories
APT_SOURCES=/target/etc/apt/sources.list
APT_CONFDIR=/target/etc/apt/apt.conf.d
IT_CONFDIR=/target/etc/initramfs-tools/conf.d
37
DPKG_CONFDIR=/target/etc/dpkg/dpkg.cfg.d
38

39 40 41 42
IFS_ORIG="$IFS"
NL="
"

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
log() {
	logger -t base-installer "$@"
}
error() {
	log "error: $*"
}
warning() {
	log "warning: $*"
}
info() {
	log "info: $*"
}

exit_error() {
	error "exiting on error $1"
	db_progress stop
	db_input critical $1 || [ $? -eq 30 ]
	db_go
	exit 1
}

waypoint () {
	WAYPOINTS="$WAYPOINTS $1:$2"
	NUM_STEPS=$(($NUM_STEPS + $1)) || true
}

run_waypoints () {
70
	db_progress START 0 $NUM_STEPS $1
71 72 73 74 75 76 77 78 79 80 81 82 83 84
	for item in $WAYPOINTS; do
		PB_WAYPOINT_LENGTH=$(echo $item | cut -d: -f 1)
		WAYPOINT=$(echo $item | cut -d: -f 2)
		# Not all of the section headers need exist.
		db_progress INFO "base-installer/section/$WAYPOINT" || true
		eval $WAYPOINT
		PB_POSITION=$(($PB_POSITION + $PB_WAYPOINT_LENGTH)) || true
		db_progress SET $PB_POSITION
	done
	db_progress STOP
}

update_progress () {
	# Updates the progress bar to a new position within the space allocated
Frans Pop's avatar
Frans Pop committed
85
	# for the current waypoint.
86 87 88 89 90 91 92
	NW_POS=$(($PB_POSITION + $PB_WAYPOINT_LENGTH * $1 / $2))
	db_progress SET $NW_POS
}

check_target () {
	# Make sure something is mounted on the target.
	# Partconf causes the latter format.
93 94
	if [ -e /proc/mounts ] && \
	   ! grep -q '/target ' /proc/mounts && \
95
	   ! grep -q '/target/ ' /proc/mounts; then
96 97
		exit_error base-installer/no_target_mounted
	fi
Frans Pop's avatar
Frans Pop committed
98

99 100
	# Warn about installation over an existing unix.
	if [ -e /target/bin/sh -o -L /target/bin/sh ]; then
101
		warning "attempting to install to unclean target"
102 103 104 105 106 107 108 109 110 111 112
		db_capb ""
		db_input high base-installer/use_unclean_target || true
		db_go || exit 10
		db_capb backup
		db_get base-installer/use_unclean_target
		if [ "$RET" = false ]; then
			db_reset base-installer/use_unclean_target
			exit_error base-installer/unclean_target_cancel
		fi
		db_reset base-installer/use_unclean_target
	fi
113 114

	# Undo dev bind mounts for idempotency.
115 116 117
	if grep -qE '^[^ ]+ /target/dev' /proc/mounts; then
		umount /target/dev
	fi
118 119 120 121 122 123
	# Unmount /dev/.static/dev if mounted on same device as /target
	mp_stdev=$(grep -E '^[^ ]+ /dev/\.static/dev' /proc/mounts | \
		   cut -d" " -f1)
	if [ "$mp_stdev" ] && grep -q "^$mp_stdev /target " /proc/mounts; then
		umount /dev/.static/dev
	fi
124 125
}

126
setup_dev_linux () {
127 128 129 130 131 132 133
	# Ensure static device nodes created during install are preserved
	# Tests in MAKEDEV require this is done in the D-I environment
	mkdir -p /dev/.static/dev
	chmod 700 /dev/.static/
	mount --bind /target/dev /dev/.static/dev
	# Mirror device nodes in D-I environment to target
	mount --bind /dev /target/dev/
134
}
135

136 137 138 139 140 141 142 143
setup_dev_kfreebsd() {
	mount -t devfs devfs /target/dev
}

setup_dev() {
	case "$OS" in
		linux) setup_dev_linux ;;
		kfreebsd) setup_dev_kfreebsd ;;
144
		hurd) : ;;
145
		*) warning "setup_dev called for an unknown OS ($OS)." ;;
146 147 148
	esac	
}

149
configure_apt_preferences () {
150
	[ ! -d "$APT_CONFDIR" ] && mkdir -p "$APT_CONFDIR"
Frans Pop's avatar
Frans Pop committed
151

152 153
	# Don't install Recommends during base-installer
	cat >$APT_CONFDIR/00InstallRecommends <<EOT
154 155 156
APT::Install-Recommends "false";
EOT

157 158
	# Make apt trust Debian CDs. This is not on by default (we think).
	# This will be left in place on the installed system.
159
	cat > $APT_CONFDIR/00trustcdrom <<EOT
160 161 162 163 164
APT::Authentication::TrustCDROM "true";
EOT

	# Avoid clock skew causing gpg verification issues.
	# This file will be left in place until the end of the install.
165
	cat > $APT_CONFDIR/00IgnoreTimeConflict << EOT
166 167 168 169 170
Acquire::gpgv::Options { "--ignore-time-conflict"; };
EOT

	if db_get debian-installer/allow_unauthenticated && [ "$RET" = true ]; then
		# This file will be left in place until the end of the install.
171
		cat > $APT_CONFDIR/00AllowUnauthenticated << EOT
172 173 174 175
APT::Get::AllowUnauthenticated "true";
Aptitude::CmdLine::Ignore-Trust-Violations "true";
EOT
	fi
176

177 178 179
	if [ "$PROTOCOL" = https ] && db_get debian-installer/allow_unauthenticated_ssl && [ "$RET" = true ]; then
		# This file will be left in place on the installed system.
		cat > $APT_CONFDIR/00AllowUnauthenticatedSSL << EOT
180
Acquire::https::Verify-Host "false";
181 182 183 184
Acquire::https::Verify-Peer "false";
EOT
	fi

185 186
	[ ! -d "$DPKG_CONFDIR" ] && mkdir -p "$DPKG_CONFDIR"

187 188 189 190 191 192
	# Disable all syncing; it's unnecessary in an installation context,
	# and can slow things down quite a bit.
	# This file will be left in place until the end of the install.
	cat > $DPKG_CONFDIR/force-unsafe-io <<EOT
force-unsafe-io
EOT
193 194
}

195 196 197 198 199 200 201 202
final_apt_preferences () {
	# From here on install Recommends as configured
	db_get base-installer/install-recommends
	if [ "$RET" = true ]; then
		rm -f $APT_CONFDIR/00InstallRecommends
	fi
}

203 204 205
apt_update () {
	log-output -t base-installer chroot /target apt-get update \
		|| apt_update_failed=$?
Frans Pop's avatar
Frans Pop committed
206

207 208 209 210 211 212
	if [ "$apt_update_failed" ]; then
		warning "apt update failed: $apt_update_failed"
	fi
}

install_extra () {
213
	local IFS
214
	info "Installing queued packages into /target/."
Frans Pop's avatar
Frans Pop committed
215

216
	if [ -f /var/lib/apt-install/queue ]; then
217
		# We need to install these one by one in case one fails.
218
		PKG_COUNT=$(cat /var/lib/apt-install/queue | wc -w)
219
		CURR_PKG=0
220 221 222 223 224
		IFS="$NL"
		for LINE in $(cat /var/lib/apt-install/queue); do
			IFS="$IFS_ORIG"
			PKG=${LINE%% *}
			OPTS=$(echo "$LINE" | sed "s/$PKG *//")
225 226 227
			db_subst base-installer/section/install_extra_package SUBST0 "$PKG"
			db_progress INFO base-installer/section/install_extra_package

228
			log-output -t base-installer apt-install $OPTS $PKG || \
229 230 231 232 233 234 235 236 237 238
				warning "Failed to install $PKG into /target/: $?"

			# Advance progress bar within space allocated for install_extra
			CURR_PKG=$(($CURR_PKG + 1))
			update_progress $CURR_PKG $PKG_COUNT
		done
	fi
}

pre_install_hooks () {
239
	# avoid apt-install installing things; apt is not configured yet
240
	rm -f $APT_SOURCES
241

242 243
	partsdir="/usr/lib/base-installer.d"
	if [ -d "$partsdir" ]; then
244
		for script in `ls "$partsdir"/* 2>/dev/null`; do
245 246 247 248 249
			base=$(basename $script | sed 's/[0-9]*//')
			if ! db_progress INFO base-installer/progress/$base; then
				db_subst base-installer/progress/fallback SCRIPT "$base"
				db_progress INFO base-installer/progress/fallback
		    	fi
Frans Pop's avatar
Frans Pop committed
250

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
		    	if [ -x "$script" ] ; then
				# be careful to preserve exit code
				if log-output -t base-installer "$script"; then
					:
				else
			    		warning "$script returned error code $?"
				fi
		    	else
				error "Unable to execute $script"
		    	fi
		done
	fi
}

post_install_hooks () {
Frans Pop's avatar
Frans Pop committed
266 267 268
	# locales will now be installed, so unset
	unset IT_LANG_OVERRIDE

269 270
	partsdir="/usr/lib/post-base-installer.d"
	if [ -d "$partsdir" ]; then
271
		scriptcount=`ls "$partsdir"/* 2>/dev/null | wc -l`
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
		scriptcur=0
		for script in "$partsdir"/*; do
			base="$(basename "$script" | sed 's/[0-9]*//')"
			if ! db_progress INFO base-installer/progress/$base && \
			   ! db_progress INFO finish-install/progress/$base; then
				db_subst base-installer/progress/fallback SCRIPT "$base"
				db_progress INFO base-installer/progress/fallback
			fi

			if [ -x "$script" ]; then
				# be careful to preserve exit code
				if log-output -t base-installer "$script"; then
					:
				else
					warning "$script returned error code $?"
				fi
			else
				error "Unable to execute $script"
			fi

			scriptcur="$(($scriptcur + 1))"
			update_progress "$scriptcur" "$scriptcount"
		done
	fi
}
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

get_mirror_info () {
	if [ -f /cdrom/.disk/base_installable ]; then
		if db_get cdrom/codename && [ "$RET" ] ; then
			DISTRIBUTION=$RET
		else
			exit_error base-installer/no_codename
		fi

		PROTOCOL=file
		MIRROR=""
		DIRECTORY="/cdrom/"
		if [ -s /cdrom/.disk/base_components ]; then
			COMPONENTS=`grep -v '^#' /cdrom/.disk/base_components | tr '\n' , | sed 's/,$//'`
		else
			COMPONENTS="*"
		fi

315 316
		# Sanity check: an error reading /cdrom/.disk/base_components can
		# cause ugly errors in debootstrap because $COMPONENTS will be empty
317 318 319 320 321 322 323 324 325 326 327
		if [ -z "$COMPONENTS" ]; then
			exit_error base-installer/cannot_install
		fi
	else
		if db_get mirror/codename && [ "$RET" ] ; then
			DISTRIBUTION=$RET
		else
			exit_error base-installer/no_codename
		fi

		mirror_error=""
Frans Pop's avatar
Frans Pop committed
328

329 330
		db_get mirror/protocol || mirror_error=1
		PROTOCOL="$RET"
331 332 333 334 335 336 337

		db_get mirror/$PROTOCOL/hostname || mirror_error=1
		MIRROR="$RET"

		db_get mirror/$PROTOCOL/directory || mirror_error=1
		DIRECTORY="$RET"

338
		COMPONENTS="main"
339 340 341 342 343 344 345 346

		if [ "$mirror_error" = 1 ] || [ -z "$PROTOCOL" ] || [ -z "$MIRROR" ]; then
			exit_error base-installer/cannot_install
		fi
	fi
}

kernel_update_list () {
347
	# Use 'uniq' to avoid listing the same kernel more then once
348
	chroot /target apt-cache search "^(kernel|$KERNEL_NAME)-image" | \
349
	cut -d" " -f1 | grep -v "linux-image-2.6" | uniq > "$KERNEL_LIST.unfiltered"
350 351 352
	kernels=`sort -r "$KERNEL_LIST.unfiltered" | tr '\n' ' ' | sed -e 's/ $//'`
	for candidate in $kernels; do
		if [ -n "$FLAVOUR" ]; then
353
			if arch_check_usable_kernel "$candidate" "$FLAVOUR"; then
354 355 356 357 358 359 360 361 362 363 364 365
				echo "$candidate"
				info "kernel $candidate usable on $FLAVOUR"
			else
				info "kernel $candidate not usable on $FLAVOUR"
			fi
		else
			info "could not determine kernel flavour"
		fi
	done > "$KERNEL_LIST"
}

kernel_present () {
366
	[ "$1" ] || return 1
367 368 369 370 371
	grep -q "^$1\$" $KERNEL_LIST
}

pick_kernel () {
	kernel_update_list
372
	
373
	db_settitle debian-installer/bootstrap-base/title
374 375 376 377 378

	# For now, only present kernels we believe to be usable. We may have
	# to rethink this later, but if there are no usable kernels it
	# should be accompanied by an error message. The default must still
	# be usable if possible.
379
	kernels=$(sort "$KERNEL_LIST" | tr '\n' ',' | sed -e 's/,$//')
380 381 382

	info "Found kernels '$kernels'"

383 384
	if [ "$kernels" ]; then
		db_subst base-installer/kernel/image KERNELS "$kernels"
385 386 387 388 389 390 391 392 393 394 395 396 397
	else
		db_input high base-installer/kernel/skip-install || true
		db_go || true
		db_get base-installer/kernel/skip-install
		if [ "$RET" != true ]; then
			exit_error base-installer/kernel/no-kernels-found
		else
			db_set base-installer/kernel/image "none"
			KERNEL=none
			return
		fi
	fi

398 399 400 401 402
	# Allow for preseeding first, try to determine a default next.
	db_get base-installer/kernel/image
	if kernel_present "$RET" || [ "$RET" = none ]; then
		KERNEL="$RET"
		info "preseeded/current kernel: $KERNEL"
403
	else
404 405
		# Unset seen flag in case we had an incorrect preseeded value.
		db_fset base-installer/kernel/image seen false || true
406

407
		if [ -n "$FLAVOUR" ]; then
408 409 410 411 412 413 414 415 416
			arch_kernel="$(arch_get_kernel "$FLAVOUR")"

			# Hack to support selection of meta packages with a postfix
			# added to the normal name (for updated kernels in stable).
			if db_get base-installer/kernel/altmeta && [ "$RET" ]; then
				arch_kernel="$(echo "$arch_kernel" | \
					sed "s/$/-$RET/"; \
					echo "$arch_kernel")"
			fi
417 418 419
		else
			arch_kernel=""
		fi
Frans Pop's avatar
Frans Pop committed
420

421 422 423 424 425 426 427 428 429 430 431 432 433 434
		got_arch_kernel=
		if [ "$arch_kernel" ]; then
			info "arch_kernel candidates: $arch_kernel"
			# Walk through recommended list for this architecture in order.
			for candidate in $arch_kernel; do
				if kernel_present "$candidate"; then
					info "arch_kernel: $candidate (present)"
					KERNEL="$candidate"
					break
				else
					info "arch_kernel: $candidate (absent)"
				fi
			done
		fi
435 436
	fi

437 438
	KERNEL_PRIO=high
	if kernel_present "$KERNEL" || [ "$KERNEL" = none ]; then
439
		# Current selection is available
440
		KERNEL_PRIO=medium
441 442
	else
		# No recommendations available; try to guess.
443
		kernels="$(echo "$kernels" | sed 's/,/\n/g')"
444 445

		# Try to default to running kernel version.
446
		KVERS=$(uname -r | cut -d'-' -f 1)
447 448 449 450 451 452 453 454 455 456 457 458
		KERNEL="$(echo "$kernels" | grep -- "-$KVERS" | head -n 1)"
		if [ -z "$KERNEL" ]; then
			# If possible, find one with at least the same major number
			# as the currently running kernel.
			KERNEL="$(echo "$kernels" | grep -- "-$KERNEL_MAJOR" | head -n 1)"
			if [ -z "$KERNEL" ]; then
				# Take the first on the list.
				KERNEL=$(echo "$kernels" | head -n 1)
			fi
		fi
	fi

459 460 461 462 463
	if [ "$KERNEL" ]; then
		db_set base-installer/kernel/image "$KERNEL"
	else
		# We have no reasonable default at all.
		KERNEL_PRIO=critical
464 465
	fi

466
	db_input $KERNEL_PRIO base-installer/kernel/image || [ $? -eq 30 ]
467 468 469 470 471
	if ! db_go; then
		db_progress stop
		exit 10
	fi

472
	db_get base-installer/kernel/image
473 474 475 476
	KERNEL=$RET
	info "Using kernel '$KERNEL'"
}

477
install_kernel_linux () {
478 479 480 481
	if [ "$KERNEL" = none ]; then
		info "Not installing any kernel"
		return
	fi
Frans Pop's avatar
Frans Pop committed
482

483 484 485
	target_kernel_major="$(echo "$KERNEL" | sed 's/^kernel-image-//; s/^linux-image-//; s/-.*//' | cut -d . -f 1,2)"
	case $target_kernel_major in
		2.?)	;;
486 487 488 489 490
		[3-9].*)
			# As far as our debconf templates are concerned,
			# this is essentially 2.6.
			target_kernel_major=2.6
			;;
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
		*)
			# something went wrong; use major version of
			# installation kernel
			target_kernel_major="$(uname -r | cut -d . -f 1,2)"
			;;
	esac

	do_initrd=no
	if db_get "base-installer/kernel/linux/initrd-$target_kernel_major"; then
		if [ "$RET" = true ]; then
			do_initrd=yes
		fi
	# Note: this template currently does not exist
	elif db_get base-installer/kernel/linux/initrd; then
		if [ "$RET" = true ]; then
			do_initrd=yes
		fi
	else
		warning "Failed to get debconf answer 'base-installer/kernel/linux/initrd'."
		do_initrd=yes
	fi

	if db_get base-installer/kernel/linux/link_in_boot ; then
		if [ "$RET" = "true" ]; then
			link_in_boot=yes
		else
			link_in_boot=no
		fi
	else
		warning "Failed to get debconf answer 'base-installer/kernel/linux/link_in_boot'."
		link_in_boot=no
	fi

	# Create configuration file for kernel-package
	if [ -f /target/etc/kernel-img.conf ]; then
		# Backup old kernel-img.conf
		mv /target/etc/kernel-img.conf /target/etc/kernel-img.conf.$$
	fi

	info "Setting do_initrd='$do_initrd'."
	info "Setting link_in_boot='$link_in_boot'."
	cat > /target/etc/kernel-img.conf <<EOF
# Kernel image management overrides
# See kernel-img.conf(5) for details
535
do_symlinks = yes
536 537 538 539 540 541 542
do_bootloader = no
do_initrd = $do_initrd
link_in_boot = $link_in_boot
EOF

	rd_generator=""
	if [ "$do_initrd" = yes ]; then
543
		rd_generator=initramfs-tools
544

545 546
		# initramfs-tools needs busybox pre-installed (and only
		# recommends it)
547 548 549
		if ! log-output -t base-installer apt-install busybox; then
			db_subst base-installer/kernel/failed-package-install PACKAGE busybox
			exit_error base-installer/kernel/failed-package-install
550 551
		fi

552 553 554 555 556 557 558 559
		# Make sure the ramdisk creation tool is installed before we
		# change its configuration
		db_subst base-installer/section/install_kernel_package SUBST0 "$rd_generator"
		db_progress INFO base-installer/section/install_kernel_package
		if ! log-output -t base-installer apt-install "$rd_generator"; then
			db_subst base-installer/kernel/failed-package-install PACKAGE "$rd_generator"
			exit_error base-installer/kernel/failed-package-install
		fi
Frans Pop's avatar
Frans Pop committed
560

561 562 563 564 565 566 567 568 569
		# Add modules that have been queued for inclusion in the initrd
		FIRSTMODULE=1
		for QUEUEFILE in /var/lib/register-module/*.initrd; do
			[ ! -e $QUEUEFILE ] && break
			MODULE=$(basename $QUEUEFILE ".initrd")
			addmodule_initramfs_tools "$MODULE" $FIRSTMODULE
			rm $QUEUEFILE
			FIRSTMODULE=0
		done
570 571

		# Select and set driver inclusion policy for initramfs-tools
572 573 574 575 576 577 578 579 580 581 582 583
		if db_get base-installer/initramfs-tools/driver-policy && \
		   [ -z "$RET" ]; then
			# Get default for architecture
			db_get base-installer/kernel/linux/initramfs-tools/driver-policy
			db_set base-installer/initramfs-tools/driver-policy "$RET"
		fi
		db_settitle debian-installer/bootstrap-base/title
		db_input medium base-installer/initramfs-tools/driver-policy || true
		if ! db_go; then
			db_progress stop
			exit 10
		fi
584

585 586 587
		db_get base-installer/initramfs-tools/driver-policy
		if [ "$RET" != most ]; then
			cat > $IT_CONFDIR/driver-policy <<EOF
588 589 590 591 592 593
# Driver inclusion policy selected during installation
# Note: this setting overrides the value set in the file
# /etc/initramfs-tools/initramfs.conf
MODULES=$RET
EOF
		fi
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
	else
		info "Not installing an initrd generator."
	fi

	# Install any extra (kernel related) packages
	EXTRAS=
	if db_get "base-installer/kernel/linux/extra-packages-$target_kernel_major"; then
		EXTRAS="$EXTRAS $RET"
	fi
	if db_get base-installer/kernel/linux/extra-packages; then
		EXTRAS="$EXTRAS $RET"
	fi
	for package in $EXTRAS; do
		info "Installing $package."
		db_subst base-installer/section/install_kernel_package SUBST0 "$package"
		db_progress INFO base-installer/section/install_kernel_package
		# The package might not exist; don't worry about it.
		log-output -t base-installer apt-install "$package" || true
	done

	if [ "$do_initrd" = yes ]; then
		# Set up a default resume partition.
		case $rd_generator in
		    initramfs-tools)
618
			resumeconf=$IT_CONFDIR/resume
619 620 621 622 623 624 625 626 627 628 629
			;;
		    *)
			resumeconf=
			;;
		esac
		resume_devfs="$(get_resume_partition)" || resume_devfs=
		if [ "$resume_devfs" ] && [ -e "$resume_devfs" ]; then
			resume="$(mapdevfs "$resume_devfs")" || resume=
		else
			resume=
		fi
630
		if [ "$resume" ] && ! echo "$resume" | grep -q "^/dev/mapper/"; then
631 632 633 634 635
			resume_uuid="$(block-attr --uuid "$resume" || true)"
			if [ "$resume_uuid" ]; then
				resume="UUID=$resume_uuid"
			fi
		fi
636 637 638 639 640 641 642 643 644 645 646 647
		if [ -n "$resumeconf" ] && [ "$resume" ]; then
			if [ -f $resumeconf ] ; then
				sed -e "s@^#* *RESUME=.*@RESUME=$resume@" < $resumeconf > $resumeconf.new &&
					mv $resumeconf.new $resumeconf
			else
				echo "RESUME=$resume" >> $resumeconf
			fi
		fi

		# Set PReP root partition
		if [ "$ARCH" = powerpc ] && [ "$SUBARCH" = prep ] && \
		   [ "$rd_generator" = initramfs-tools ]; then
648
			prepconf=$IT_CONFDIR/prep-root
649 650 651 652 653 654 655 656 657 658 659
			rootpart_devfs=$(mount | grep "on /target " | cut -d' ' -f1)
			rootpart=$(mapdevfs $rootpart_devfs)
			if [ -f $prepconf ] && grep -q "^#* *ROOT=" $prepconf; then
				sed -e "s@^#* *ROOT=.*@ROOT=$rootpart@" < $prepconf > $prepconf.new &&
					mv $prepconf.new $prepconf
			else
				echo "ROOT=$rootpart" >> $prepconf
			fi
		fi
	fi

660
	# Advance progress bar to 30% of allocated space for install_kernel_linux
661 662 663 664 665 666 667
	update_progress 30 100

	# Install the kernel
	db_subst base-installer/section/install_kernel_package SUBST0 "$KERNEL"
	db_progress INFO base-installer/section/install_kernel_package
	log-output -t base-installer apt-install "$KERNEL" || kernel_install_failed=$?

668
	# Advance progress bar to 90% of allocated space for install_kernel_linux
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
	update_progress 90 100

	if [ -f /target/etc/kernel-img.conf.$$ ]; then
		# Revert old kernel-img.conf
		mv /target/etc/kernel-img.conf.$$ /target/etc/kernel-img.conf
	fi

	if [ "$kernel_install_failed" ]; then
		db_subst base-installer/kernel/failed-install KERNEL "$KERNEL"
		exit_error base-installer/kernel/failed-install
	fi
}

get_resume_partition () {
	biggest_size=0
	biggest_partition=
	while read filename type size other; do
		if [ "$type" != partition ]; then
			continue
		fi
Colin Watson's avatar
Colin Watson committed
689 690 691
		if [ ! -e "$filename" ]; then
			continue
		fi
692 693 694
		if [ "${filename#/dev/ramzswap}" != "$filename" ]; then
			continue
		fi
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
		if [ "$size" -gt "$biggest_size" ]; then
			biggest_size="$size"
			biggest_partition="$filename"
		fi
	done < /proc/swaps
	echo "$biggest_partition"
}

addmodule_easy () {
	if [ -f "$CFILE" ]; then
		if [ "$2" = 1 ]; then
			echo -e "\n# Added by Debian Installer" >>$CFILE
		fi
		echo "$1" >> $CFILE
	fi
}

addmodule_initramfs_tools () {
	CFILE='/target/etc/initramfs-tools/modules'
	addmodule_easy "$1" "$2"
}

717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
install_kernel_kfreebsd() {
	if [ "$KERNEL" = none ]; then
		info "Not installing any kernel"
		return
	fi

	# Create configuration file for kernel-package
	if [ -f /target/etc/kernel-img.conf ]; then
		# Backup old kernel-img.conf
		mv /target/etc/kernel-img.conf /target/etc/kernel-img.conf.$$
	fi

	cat > /target/etc/kernel-img.conf <<EOF
# Kernel image management overrides
# See kernel-img.conf(5) for details
do_symlinks = no
EOF
	# Advance progress bar to 10% of allocated space for install_kfreebsd
	update_progress 10 100

	# Install the kernel
	db_subst base-installer/section/install_kernel_package SUBST0 "$KERNEL"
	db_progress INFO base-installer/section/install_kernel_package
	log-output -t base-installer apt-install "$KERNEL" || kernel_install_failed=$?

	# Advance progress bar to 90% of allocated space for install_kfreebsd
	update_progress 90 100

	if [ -f /target/etc/kernel-img.conf.$$ ]; then
		# Revert old kernel-img.conf
		mv /target/etc/kernel-img.conf.$$ /target/etc/kernel-img.conf
	fi

	if [ "$kernel_install_failed" ]; then
		db_subst base-installer/kernel/failed-install KERNEL "$KERNEL"
		exit_error base-installer/kernel/failed-install
	fi
}

756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
install_kernel_hurd() {
	if [ "$KERNEL" = none ]; then
		info "Not installing any kernel"
		return
	fi

	# Create configuration file for kernel-package
	if [ -f /target/etc/kernel-img.conf ]; then
		# Backup old kernel-img.conf
		mv /target/etc/kernel-img.conf /target/etc/kernel-img.conf.$$
	fi

	cat > /target/etc/kernel-img.conf <<EOF
# Kernel image management overrides
# See kernel-img.conf(5) for details
do_symlinks = no
EOF
	# Advance progress bar to 10% of allocated space for install_kfreebsd
	update_progress 10 100

	# Install the kernel
	db_subst base-installer/section/install_kernel_package SUBST0 "$KERNEL"
	db_progress INFO base-installer/section/install_kernel_package
	log-output -t base-installer apt-install "$KERNEL" || kernel_install_failed=$?

	# Advance progress bar to 90% of allocated space for install_kernel_hurd
	update_progress 90 100

	if [ -f /target/etc/kernel-img.conf.$$ ]; then
		# Revert old kernel-img.conf
		mv /target/etc/kernel-img.conf.$$ /target/etc/kernel-img.conf
	fi

	if [ "$kernel_install_failed" ]; then
		db_subst base-installer/kernel/failed-install KERNEL "$KERNEL"
		exit_error base-installer/kernel/failed-install
	fi
}

795 796 797 798
install_kernel() {
	case "$OS" in
		linux) install_kernel_linux ;;
		kfreebsd) install_kernel_kfreebsd ;;
799
		hurd) install_kernel_hurd ;;
800
		*) warning "install_kernel called for an unknown OS ($OS)." ;;
801 802 803 804
	esac	
}


805
# Assumes the file protocol is only used for CD (image) installs
806
configure_apt () {
807
	if [ "$PROTOCOL" = file ]; then
808
		local tdir=/target/media$DIRECTORY
809 810 811
		rm -f /var/lib/install-cd.id

		# Let apt inside the chroot see the cdrom
812 813 814
		umount $tdir 2>/dev/null || true
		if [ ! -e $tdir ]; then
			mkdir -p $tdir
815 816
		fi

817 818
		# The bind mount is left mounted, for future apt-install
		# calls to use.
819 820 821 822 823 824 825 826 827 828 829 830
		case "$OS" in
			linux)
			if ! mount -o bind $DIRECTORY $tdir; then
				warning "failed to bind mount $tdir"
			fi
			;;
			kfreebsd)
			if ! mount -t nullfs $DIRECTORY $tdir ; then
				warning "failed to bind mount $tdir"
			fi
			;;
			hurd)
831
			if ! mount -t firmlink $DIRECTORY $tdir > /dev/null 2>&1 ; then
832 833 834
				warning "failed to bind mount $tdir"
			fi
			;;
835 836 837
			*)
			warning "configure_apt called with unknown OS ($OS)."
			;;
838
		esac
839

840 841 842 843
		# Define the mount point for apt-cdrom
		cat > $APT_CONFDIR/00CDMountPoint << EOT
Acquire::cdrom {
  mount "/media/cdrom";
844
};
845
Dir::Media::MountPath "/media/cdrom";
846
EOT
847 848 849 850 851
		# Make apt-cdrom and apt not unmount/mount CD-ROMs;
		# needed to support CD images (hd-media installs).
		# This file will be left in place until the end of the
		# install for hd-media installs, but is removed again
		# during apt-setup for installs using real CD/DVDs.
852
		cat > $APT_CONFDIR/00NoMountCDROM << EOT
853 854
APT::CDROM::NoMount "true";
Acquire::cdrom {
855
  "/media/cdrom/" {
856 857 858
    Mount  "true";
    UMount "true";
  };
859
  AutoDetect "false";
860 861
}
EOT
862

863
		# Scan CD-ROM or CD image; start with clean sources.list
864
		# Prevent apt-cdrom from prompting
865
		: > $APT_SOURCES
866 867
		if ! log-output -t base-installer \
		     chroot /target apt-cdrom add </dev/null; then
868 869 870
			error "error while running apt-cdrom"
		fi
	else
871 872 873 874
		# sources.list uses space to separate the components, not comma
		COMPONENTS=$(echo $COMPONENTS | tr , " ")
		APTSOURCE="$PROTOCOL://$MIRROR$DIRECTORY"

875
		echo "deb $APTSOURCE $DISTRIBUTION $COMPONENTS" > $APT_SOURCES
876 877 878 879
	fi
}

cleanup () {
880
	rm -f "$KERNEL_LIST" "$KERNEL_LIST.unfiltered"
881
}