debootstrap 18.3 KB
Newer Older
1 2
#!/bin/sh -e

3 4
VERSION='@VERSION@'

5 6
unset TMP TEMP TMPDIR || true

7 8 9
# might not be exported if we're running from init=/bin/sh or similar
export PATH

10 11
###########################################################################

12
if [ -z "$DEBOOTSTRAP_DIR" ]; then
13 14 15
	if [ -x /debootstrap/debootstrap ]; then
		DEBOOTSTRAP_DIR=/debootstrap
	else
16
		DEBOOTSTRAP_DIR=/usr/share/debootstrap
17
	fi
18 19 20 21 22 23 24 25 26 27
fi

DEVICES_TARGZ=$DEBOOTSTRAP_DIR/devices.tar.gz

. $DEBOOTSTRAP_DIR/functions
exec 4>&1

LANG=C
USE_COMPONENTS=main
KEYRING=""
28
DISABLE_KEYRING=""
29
VARIANT=""
30
ARCH=""
31
HOST_ARCH=""
32
HOST_OS=""
33 34 35
KEEP_DEBOOTSTRAP_DIR=""
USE_DEBIANINSTALLER_INTERACTION=""
SECOND_STAGE_ONLY=""
36
PRINT_DEBS=""
37 38 39 40 41 42 43 44 45 46 47
CHROOTDIR=""
MAKE_TARBALL=""
KEEP_DEBOOTSTRAP_DIR=""
EXTRACTOR_OVERRIDE=""
UNPACK_TARBALL=""
ADDITIONAL=""
EXCLUDE=""
VERBOSE=""
CERTIFICATE=""
CHECKCERTIF=""
PRIVATEKEY=""
48

49
DEF_MIRROR="http://ftp.us.debian.org/debian"
50
DEF_HTTPS_MIRROR="https://mirrors.kernel.org/debian"
51 52 53 54 55 56

export LANG USE_COMPONENTS
umask 022

###########################################################################

57
## phases:
58 59 60 61 62 63
##   finddebs dldebs printdebs first_stage second_stage

RESOLVE_DEPS=true

WHAT_TO_DO="finddebs dldebs first_stage second_stage"
am_doing_phase () {
64 65 66 67 68 69
	# usage:   if am_doing_phase finddebs; then ...; fi
	local x;
	for x in "$@"; do
		if echo " $WHAT_TO_DO " | grep -q " $x "; then return 0; fi
	done
	return 1
70 71 72 73 74 75
}

###########################################################################

usage_err()
{
76 77 78
	info USAGE1 "usage: [OPTION]... <suite> <target> [<mirror> [<script>]]"
	info USAGE2 "Try \`${0##*/} --help' for more information."
	error "$@"
79 80 81 82
}

usage()
{
83
	echo "Usage: ${0##*/} [OPTION]... <suite> <target> [<mirror> [<script>]]"
84
	echo "Bootstrap a Debian base system into a target directory."
85 86
	echo
	cat <<EOF
87
      --help                 display this help and exit
88
      --version              display version information and exit
89 90 91 92 93
      --verbose              don't turn off the output of wget

      --download-only        download packages, but don't perform installation
      --print-debs           print the packages to be installed, and exit

94
      --arch=A               set the architecture to install (use if no dpkg)
95
                               [ --arch=powerpc ]
96 97 98

      --include=A,B,C        adds specified names to the list of base packages
      --exclude=A,B,C        removes specified packages from the list
99
      --components=A,B,C     use packages from the listed components of the
100 101
                             archive
      --variant=X            use variant X of the bootstrap scripts
102
                             (currently supported variants: buildd, fakechroot,
103
                              scratchbox, minbase)
104
      --keyring=K            check Release files against keyring K
105
      --no-check-gpg         avoid checking Release file signatures
106 107
      --no-resolve-deps      don't try to resolve dependencies automatically

108 109 110
      --unpack-tarball=T     acquire .debs from a tarball instead of http
      --make-tarball=T       download .debs and create a tarball (tgz format)
      --second-stage-target=DIR
111 112 113
                             Run second stage in a subdirectory instead of root
                               (can be used to create a foreign chroot)
                               (requires --second-stage)
114 115
      --extractor=TYPE       override automatic .deb extractor selection
                               (supported: $EXTRACTORS_SUPPORTED)
116
      --debian-installer     used for internal purposes by debian-installer
117 118 119
      --private-key=file     read the private key from file
      --certificate=file     use the client certificate stored in file (PEM)
      --no-check-certificate do not check certificate against certificate authorities
120 121 122 123 124
EOF
}

###########################################################################

125
if [ -z "$PKGDETAILS" ]; then
126
	error 1 NO_PKGDETAILS "No pkgdetails available; either install perl, or build pkgdetails.c from the base-installer source package"
127 128 129 130 131
fi

###########################################################################

if [ $# != 0 ] ; then
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    while true ; do
	case "$1" in
	    --help)
		usage
		exit 0
		;;
	    --version)
		echo "debootstrap $VERSION"
		exit 0
		;;
	    --debian-installer)
		if ! (echo -n "" >&3) 2>/dev/null; then
			error 1 ARG_DIBYHAND "If running debootstrap by hand, don't use --debian-installer"
		fi
		USE_DEBIANINSTALLER_INTERACTION=yes
		shift
		;;
	    --foreign)
150 151 152
		if [ "$PRINT_DEBS" != "true" ]; then
			WHAT_TO_DO="finddebs dldebs first_stage"
		fi
153 154 155
		shift
		;;
	    --second-stage)
156
		WHAT_TO_DO="second_stage"
157 158 159
		SECOND_STAGE_ONLY=true
		shift
		;;
160 161 162 163 164 165 166 167 168 169
	    --second-stage-target|--second-stage-target=?*)
		if [ "$SECOND_STAGE_ONLY" != "true" ] ; then
			error 1 STAGE2ONLY "option %s only applies in the second stage" "$1"
		fi
		if [ "$1" = "--second-stage-target" -a -n "$2" ] ; then
			CHROOTDIR="$2"
			shift 2
		elif [ "$1" != "${1#--second-stage-target=}" ]; then
			CHROOTDIR="${1#--second-stage-target=}"
			shift
170
		else
171
			error 1 NEEDARG "option requires an argument: %s" "$1"
172 173 174 175
		fi
		;;
	    --print-debs)
		WHAT_TO_DO="finddebs printdebs kill_target"
176
		PRINT_DEBS=true
177 178 179 180 181 182
		shift
		;;
	    --download-only)
		WHAT_TO_DO="finddebs dldebs"
		shift
		;;
183
	    --make-tarball|--make-tarball=?*)
184
		WHAT_TO_DO="finddebs dldebs maketarball kill_target"
185
		if [ "$1" = "--make-tarball" -a -n "$2" ] ; then
186 187
			MAKE_TARBALL="$2"
			shift 2
188 189
		elif [ "$1" != "${1#--make-tarball=}" ]; then
			MAKE_TARBALL="${1#--make-tarball=}"
190
			shift
191 192 193 194 195
		else
			error 1 NEEDARG "option requires an argument %s" "$1"
		fi
		;;
	    --resolve-deps)
196
		# redundant, but avoids breaking compatibility
197 198 199 200 201 202 203 204 205 206 207
		RESOLVE_DEPS=true
		shift
		;;
	    --no-resolve-deps)
		RESOLVE_DEPS=false
		shift
		;;
	    --keep-debootstrap-dir)
		KEEP_DEBOOTSTRAP_DIR=true
		shift
		;;
208 209
	    --arch|--arch=?*)
		if [ "$1" = "--arch" -a -n "$2" ] ; then
210 211
			ARCH="$2"
			shift 2
212 213 214
                elif [ "$1" != "${1#--arch=}" ]; then
			ARCH="${1#--arch=}"
			shift
215 216 217 218
		else
			error 1 NEEDARG "option requires an argument %s" "$1"
		fi
		;;
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
	    --extractor|--extractor=?*)
		if [ "$1" = "--extractor" -a -n "$2" ] ; then
			EXTRACTOR_OVERRIDE="$2"
			shift 2
		elif [ "$1" != "${1#--extractor=}" ]; then
			EXTRACTOR_OVERRIDE="${1#--extractor=}"
			shift
		else
			error 1 NEEDARG "option requires an argument %s" "$1"
		fi
		if valid_extractor "$EXTRACTOR_OVERRIDE"; then
			if ! type "$EXTRACTOR_OVERRIDE" >/dev/null 2>&1; then
				error 1 MISSINGEXTRACTOR "The selected extractor cannot be found: %s" "$EXTRACTOR_OVERRIDE"
			fi
		else
			error 1 BADEXTRACTOR "%s: unknown extractor" "$EXTRACTOR_OVERRIDE"
		fi
		;;
237 238
	    --unpack-tarball|--unpack-tarball=?*)
		if [ "$1" = "--unpack-tarball" -a -n "$2" ] ; then
239 240
			UNPACK_TARBALL="$2"
			shift 2
241 242 243
		elif [ "$1" != "${1#--unpack-tarball=}" ]; then
			UNPACK_TARBALL="${1#--unpack-tarball=}"
			shift
244 245 246
		else
			error 1 NEEDARG "option requires an argument %s" "$1"
		fi
247 248 249
		if [ ! -f "$UNPACK_TARBALL" ] ; then
			error 1 NOTARBALL "%s: No such file or directory" "$UNPACK_TARBALL"
		fi
250
		;;
251 252
	    --include|--include=?*)
		if [ "$1" = "--include" -a -n "$2" ]; then
253
			ADDITIONAL="$2"
254 255
			shift 2
                elif [ "$1" != "${1#--include=}" ]; then
256
			ADDITIONAL="${1#--include=}"
257 258 259 260
			shift 1
		else
			error 1 NEEDARG "option requires an argument %s" "$1"
		fi
261
		ADDITIONAL="$(echo "$ADDITIONAL" | tr , " ")"
262
		;;
263 264
	    --exclude|--exclude=?*)
		if [ "$1" = "--exclude" -a -n "$2" ]; then
265
			EXCLUDE="$2"
266 267
			shift 2
                elif [ "$1" != "${1#--exclude=}" ]; then
268
			EXCLUDE="${1#--exclude=}"
269 270 271 272
			shift 1
		else
			error 1 NEEDARG "option requires an argument %s" "$1"
		fi
273
		EXCLUDE="$(echo "$EXCLUDE" | tr , " ")"
274 275
		;;
	    --verbose)
276 277
		VERBOSE=true
		export VERBOSE
278 279
		shift 1
		;;
280 281 282 283 284 285 286 287
	    --components|--components=?*)
		if [ "$1" = "--components" -a -n "$2" ]; then
			USE_COMPONENTS="$2"
			shift 2
                elif [ "$1" != "${1#--components=}" ]; then
			USE_COMPONENTS="${1#--components=}"
			shift 1
		else
288 289
			error 1 NEEDARG "option requires an argument %s" "$1"
		fi
290
		USE_COMPONENTS="$(echo "$USE_COMPONENTS" | tr , "|")"
291
		;;
292 293 294 295 296 297 298 299 300 301
	    --variant|--variant=?*)
		if [ "$1" = "--variant" -a -n "$2" ]; then
			VARIANT="$2"
			shift 2
                elif [ "$1" != "${1#--variant=}" ]; then
			VARIANT="${1#--variant=}"
			shift 1
		else
			error 1 NEEDARG "option requires an argument %s" "$1"
		fi
302
		;;
303
	    --keyring|--keyring=?*)
304 305 306
		if ! gpgv --version >/dev/null 2>&1; then
			error 1 NEEDGPGV "gpgv not installed, but required for Release verification"
		fi
307 308 309 310 311 312 313
		if [ "$1" = "--keyring" -a -n "$2" ]; then
			KEYRING="$2"
			shift 2
                elif [ "$1" != "${1#--keyring=}" ]; then
			KEYRING="${1#--keyring=}"
			shift 1
		else
314 315 316
			error 1 NEEDARG "option requires an argument %s" "$1"
		fi
		;;
317 318 319 320
	    --no-check-gpg)
			shift 1
			DISABLE_KEYRING=1
		;;
321 322 323 324 325 326 327 328 329 330 331 332 333
	    --certificate|--certificate=?*)
		if [ "$1" = "--certificate" -a -n "$2" ]; then
			CERTIFICATE="--certificate=$2"
			shift 2
		elif [ "$1" != "${1#--certificate=}" ]; then
			CERTIFICATE="--certificate=${1#--certificate=}" 
			shift 1
		else
		       error 1 NEEDARG "option requires an argument %s" "$1" 
		fi
		;;
	    --private-key|--private-key=?*)
		if [ "$1" = "--private-key" -a -n "$2" ]; then
334
			PRIVATEKEY="--private-key=$2"
335 336
			shift 2
		elif [ "$1" != "${1#--private-key=}" ]; then
337
			PRIVATEKEY="--private-key=${1#--private-key=}" 
338 339 340 341 342 343 344 345 346
			shift 1
		else
		       error 1 NEEDARG "option requires an argument %s" "$1" 
		fi
		;;
	    --no-check-certificate)
		CHECKCERTIF="--no-check-certificate"
		shift
		;;
347 348 349
	    --*)
		error 1 BADARG "unrecognized or invalid option %s" "$1"
		;;
350 351 352 353 354
	    *)
		break
		;;
	esac
    done
355 356 357 358 359
fi

###########################################################################

if [ "$SECOND_STAGE_ONLY" = "true" ]; then
360 361 362 363 364 365
	SUITE=$(cat $DEBOOTSTRAP_DIR/suite)
	ARCH=$(cat $DEBOOTSTRAP_DIR/arch)
	if [ -e $DEBOOTSTRAP_DIR/variant ]; then
		VARIANT=$(cat $DEBOOTSTRAP_DIR/variant)
		SUPPORTED_VARIANTS="$VARIANT"
	fi
366
	if [ -z "$CHROOTDIR" ]; then
367 368 369 370 371
		TARGET=/
	else
		TARGET=$CHROOTDIR
	fi
	SCRIPT=$DEBOOTSTRAP_DIR/suite-script
372
else
373
	if [ -z "$1" ] || [ -z "$2" ]; then
374 375 376 377
		usage_err 1 NEEDSUITETARGET "You must specify a suite and a target."
	fi
	SUITE="$1"
	TARGET="$2"
378
	USER_MIRROR="$3"
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
	TARGET="${TARGET%/}"
	if [ "${TARGET#/}" = "${TARGET}" ]; then
		if [ "${TARGET%/*}" = "$TARGET" ] ; then
			TARGET="$(echo "`pwd`/$TARGET")"
		else
			TARGET="$(cd "${TARGET%/*}"; echo "`pwd`/${TARGET##*/}")"
		fi
	fi

	SCRIPT="$DEBOOTSTRAP_DIR/scripts/$1"
	if [ -n "$VARIANT" ] && [ -e "${SCRIPT}.${VARIANT}" ]; then
		SCRIPT="${SCRIPT}.${VARIANT}"
		SUPPORTED_VARIANTS="$VARIANT"
	fi
	if [ "$4" != "" ]; then
		SCRIPT="$4"
	fi
396 397 398 399
fi

###########################################################################

400 401
if in_path dpkg && \
     dpkg --print-architecture >/dev/null 2>&1; then
402
	HOST_ARCH=`/usr/bin/dpkg --print-architecture`
403
elif in_path udpkg && \
404
     udpkg --print-architecture >/dev/null 2>&1; then
405
	HOST_ARCH=`/usr/bin/udpkg --print-architecture`
406
elif [ -e $DEBOOTSTRAP_DIR/arch ]; then
407
	HOST_ARCH=`cat $DEBOOTSTRAP_DIR/arch`
408 409 410 411
fi
HOST_OS="$HOST_ARCH"
# basic host OS guessing for non-Debian systems
if [ -z "$HOST_OS" ]; then
Joey Hess's avatar
Joey Hess committed
412
	case `uname` in
413 414 415 416 417 418 419 420 421
		Linux)
			HOST_OS=linux
		;;
		GNU/kFreeBSD)
			HOST_OS=kfreebsd
		;;
		GNU)
			HOST_OS=hurd
		;;
422 423 424
		FreeBSD*)
			HOST_OS=freebsd
		;;
425
	esac
426 427
fi

Colin Watson's avatar
Colin Watson committed
428
if [ -z "$ARCH" ]; then
429 430 431
	ARCH=$HOST_ARCH
fi

432 433 434 435 436
if [ -z "$ARCH" ] || [ -z "$HOST_OS" ]; then
        error 1 WHATARCH "Couldn't work out current architecture"

fi

437
if [ "$HOST_OS" = "kfreebsd" ] || [ "$HOST_OS" = "freebsd" ]; then
438
	for module in linprocfs fdescfs tmpfs linsysfs; do
439 440 441 442
		kldstat -m "$module" > /dev/null 2>&1 || warning SANITYCHECK "Probably required module %s is not loaded" "$module"
	done
fi

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
if [ "$TARGET" = "/" ]; then
	CHROOT_CMD=""
elif doing_variant scratchbox; then
	for config in ~/.scratchbox2/*/sb2.config;
	do
		export `grep ^SBOX_TARGET_ROOT= $config`
		if [ "x$SBOX_TARGET_ROOT" = "x$TARGET" ]; then
			SB2_TARGET=$(basename $(dirname $config))
		fi
	done
	[ "x$SB2_TARGET" != "x" ] || error 1 SBOXTARGETREQ "No scratchbox target configured for $TARGET"
	CHROOT_CMD="sb2 -eR -t $SB2_TARGET"
else
	CHROOT_CMD="chroot $TARGET"
fi

Joey Hess's avatar
Joey Hess committed
459 460 461
if [ -z "$SHA_SIZE" ]; then
	SHA_SIZE=256
fi
462
if ! in_path "sha${SHA_SIZE}sum" && ! in_path "sha${SHA_SIZE}"; then
Joey Hess's avatar
Joey Hess committed
463 464 465 466 467
	SHA_SIZE=1
fi
DEBOOTSTRAP_CHECKSUM_FIELD="SHA$SHA_SIZE"

export ARCH SUITE TARGET CHROOT_CMD SHA_SIZE DEBOOTSTRAP_CHECKSUM_FIELD
468 469

if am_doing_phase first_stage second_stage; then
470
	if in_path id && [ `id -u` -ne 0 ]; then
471 472 473 474 475 476
		error 1 NEEDROOT "debootstrap can only run as root"
	fi
	# Ensure that we can create working devices and executables on the target.
	if ! check_sane_mount "$TARGET"; then
		error 1 NOEXEC "Cannot install into target '$TARGET' mounted with noexec or nodev"
	fi
477 478 479
fi

if [ ! -e "$SCRIPT" ]; then
480
	error 1 NOSCRIPT "No such script: %s" "$SCRIPT"
481 482 483 484 485
fi

###########################################################################

if [ "$TARGET" != "" ]; then
486
	mkdir -p "$TARGET/debootstrap"
487 488 489 490 491 492 493 494 495 496 497 498
fi

###########################################################################

# Use of fd's by functions/scripts:
#
#    stdin/stdout/stderr: used normally
#    fd 4: I:/W:/etc information
#    fd 5,6: spare for functions
#    fd 7,8: spare for scripts

if [ "$USE_DEBIANINSTALLER_INTERACTION" = yes ]; then
499 500 501
	#    stdout=stderr: full log of debootstrap run
	#    fd 3: I:/W:/etc information
	exec 4>&3
502
elif am_doing_phase printdebs; then
503 504 505
	#    stderr: I:/W:/etc information
	#    stdout: debs needed
	exec 4>&2
506
else
507 508 509 510 511 512
	#    stderr: used in exceptional circumstances only
	#    stdout: I:/W:/etc information
	#    $TARGET/debootstrap/debootstrap.log: full log of debootstrap run
	exec 4>&1
	exec >>"$TARGET/debootstrap/debootstrap.log"
	exec 2>&1
513 514 515 516 517
fi

###########################################################################

if [ "$UNPACK_TARBALL" ]; then
518 519 520 521 522 523 524 525 526 527
	if [ "${UNPACK_TARBALL#/}" = "$UNPACK_TARBALL" ]; then
		error 1 TARPATH "Tarball must be given a complete path"
	fi
	if [ "${UNPACK_TARBALL%.tar}" != "$UNPACK_TARBALL" ]; then
		(cd "$TARGET" && tar -xf "$UNPACK_TARBALL")
	elif [ "${UNPACK_TARBALL%.tgz}" != "$UNPACK_TARBALL" ]; then
		(cd "$TARGET" && zcat "$UNPACK_TARBALL" | tar -xf -)
	else
		error 1 NOTTAR "Unknown tarball: must be either .tar or .tgz"
	fi
528 529 530 531 532 533
fi

###########################################################################

. "$SCRIPT"

534
if [ "$SECOND_STAGE_ONLY" = "true" ]; then
535
	MIRRORS=null:
536
else
537
	MIRRORS="$DEF_MIRROR"
538 539
	if [ "$USER_MIRROR" != "" ]; then
		MIRRORS="$USER_MIRROR"
540 541
		MIRRORS="${MIRRORS%/}"
	fi
542 543 544 545
fi

export MIRRORS

546 547
ok=false
for v in $SUPPORTED_VARIANTS; do
548
	if doing_variant $v; then ok=true; fi
549 550
done
if ! $ok; then
551
	error 1 UNSUPPVARIANT "unsupported variant"
552 553 554 555 556
fi

###########################################################################

if am_doing_phase finddebs; then
557 558 559 560 561 562 563 564
	if [ "$FINDDEBS_NEEDS_INDICES" = "true" ] || \
	   [ "$RESOLVE_DEPS" = "true" ]; then
		download_indices
		GOT_INDICES=true
	fi

	work_out_debs

565
	base=$(without "$base $ADDITIONAL" "$EXCLUDE")
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594

	if [ "$RESOLVE_DEPS" = true ]; then
		requiredX=$(echo $(echo $required | tr ' ' '\n' | sort | uniq))
		baseX=$(echo $(echo $base | tr ' ' '\n' | sort | uniq))

		baseN=$(without "$baseX" "$requiredX")
		baseU=$(without "$baseX" "$baseN")

		if [ "$baseU" != "" ]; then
			info REDUNDANTBASE "Found packages in base already in required: %s" "$baseU"
		fi

		info RESOLVEREQ "Resolving dependencies of required packages..."
		required=$(resolve_deps $requiredX)
		info RESOLVEBASE "Resolving dependencies of base packages..."
		base=$(resolve_deps $baseX)
		base=$(without "$base" "$required")

		requiredX=$(without "$required" "$requiredX")
		baseX=$(without "$base" "$baseX")
		if [ "$requiredX" != "" ]; then
			info NEWREQUIRED "Found additional required dependencies: %s" "$requiredX"
		fi
		if [ "$baseX" != "" ]; then
			info NEWBASE "Found additional base dependencies: %s" "$baseX"
		fi
	fi

	all_debs="$required $base"
595 596 597
fi

if am_doing_phase printdebs; then
598
	echo "$all_debs"
599 600 601
fi

if am_doing_phase dldebs; then
602 603 604 605
	if [ "$GOT_INDICES" != "true" ]; then
		download_indices
	fi
	download $all_debs
606 607 608
fi

if am_doing_phase maketarball; then
609 610
	(cd $TARGET;
	 tar czf - var/lib/apt var/cache/apt) >$MAKE_TARBALL
611 612 613
fi

if am_doing_phase first_stage; then
614 615
	choose_extractor

616 617 618 619 620 621 622 623
	# first stage sets up the chroot -- no calls should be made to
	# "chroot $TARGET" here; but they should be possible by the time it's
	# finished
	first_stage_install

	if ! am_doing_phase second_stage; then
		cp "$0"				 "$TARGET/debootstrap/debootstrap"
		cp $DEBOOTSTRAP_DIR/functions	 "$TARGET/debootstrap/functions"
624
		cp $DEBOOTSTRAP_DIR/devices.tar.gz	 "$TARGET/debootstrap/devices.tar.gz"
625 626 627 628 629 630 631 632 633 634
		cp $SCRIPT			 "$TARGET/debootstrap/suite-script"
		echo "$ARCH"			>"$TARGET/debootstrap/arch"
		echo "$SUITE"			>"$TARGET/debootstrap/suite"
		[ "" = "$VARIANT" ] ||
		echo "$VARIANT"			>"$TARGET/debootstrap/variant"
		echo "$required"		>"$TARGET/debootstrap/required"
		echo "$base"			>"$TARGET/debootstrap/base"

		chmod 755 "$TARGET/debootstrap/debootstrap"
	fi
635 636 637
fi

if am_doing_phase second_stage; then
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
	if [ "$SECOND_STAGE_ONLY" = true ]; then
		required="$(cat $DEBOOTSTRAP_DIR/required)"
		base="$(cat $DEBOOTSTRAP_DIR/base)"
		all_debs="$required $base"
	fi

	# second stage uses the chroot to clean itself up -- has to be able to
	# work from entirely within the chroot (in case we've booted into it,
	# possibly over NFS eg)

	second_stage_install

	# create sources.list
	# first, kill debootstrap.invalid sources.list
	if [ -e "$TARGET/etc/apt/sources.list" ]; then
		rm -f "$TARGET/etc/apt/sources.list"
	fi
655
	if [ "${MIRRORS#http://}" != "$MIRRORS" ]; then
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
		setup_apt_sources "${MIRRORS%% *}"
		mv_invalid_to "${MIRRORS%% *}"
	else
		setup_apt_sources "$DEF_MIRROR"
		mv_invalid_to "$DEF_MIRROR"
	fi

	if [ -e "$TARGET/debootstrap/debootstrap.log" ]; then
		if [ "$KEEP_DEBOOTSTRAP_DIR" = true ]; then
			cp "$TARGET/debootstrap/debootstrap.log" "$TARGET/var/log/bootstrap.log"
		else
			# debootstrap.log is still open as stdout/stderr and needs
			# to remain so, but after unlinking it some NFS servers
			# implement this by a temporary file in the same directory,
			# which makes it impossible to rmdir that directory.
			# Moving it instead works around the problem.
			mv "$TARGET/debootstrap/debootstrap.log" "$TARGET/var/log/bootstrap.log"
		fi
	fi
	sync

	if [ "$KEEP_DEBOOTSTRAP_DIR" = true ]; then
		if [ -x "$TARGET/debootstrap/debootstrap" ]; then
			chmod 644 "$TARGET/debootstrap/debootstrap"
		fi
	else
		rm -rf "$TARGET/debootstrap"
	fi
684 685 686
fi

if am_doing_phase kill_target; then
687 688 689 690
	if [ "$KEEP_DEBOOTSTRAP_DIR" != true ]; then
		info KILLTARGET "Deleting target directory"
		rm -rf "$TARGET"
	fi
691
fi