pi-bake 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,38 @@
1
+ ### Copyright (c) 2012 David Love <david@homeunix.org.uk>
2
+ ###
3
+ ### Permission to use, copy, modify, and/or distribute this software for
4
+ ### any purpose with or without fee is hereby granted, provided that the
5
+ ### above copyright notice and this permission notice appear in all copies.
6
+ ###
7
+ ### THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
+ ### WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
+ ### MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10
+ ### ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
+ ### WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12
+ ### ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13
+ ### OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
+ ###
15
+
16
+ ### @author David Love
17
+ ###
18
+ ### Attach an image to the device tree. Creates a device node for an
19
+ ### existing image, allowing the image to be accessed using the standard
20
+ ### file and disk manipulation commands.
21
+ ###
22
+
23
+ # File handling classes
24
+ require "fs/geom"
25
+
26
+ class Bake < Thor
27
+
28
+ desc "unmount", "Unmount the image attached to the standard image directories"
29
+
30
+ def unmount(image_name = "pi.img")
31
+ say("Unmounting current image", :blue)
32
+
33
+ unless Geom.umount_image_dirs then
34
+ say("ERROR: Unable to unmount the image directories", :red)
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,156 @@
1
+ ### Copyright (c) 2012 David Love <david@homeunix.org.uk>
2
+ ###
3
+ ### Permission to use, copy, modify, and/or distribute this software for
4
+ ### any purpose with or without fee is hereby granted, provided that the
5
+ ### above copyright notice and this permission notice appear in all copies.
6
+ ###
7
+ ### THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
+ ### WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
+ ### MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10
+ ### ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
+ ### WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12
+ ### ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13
+ ### OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
+ ###
15
+
16
+ ### @author David Love
17
+ ###
18
+ ### Attach an image to the device tree. Creates a device node for an
19
+ ### existing image, allowing the image to be accessed using the standard
20
+ ### file and disk manipulation commands.
21
+ ###
22
+
23
+ # File handling classes
24
+ require "fileutils"
25
+ require "fs/geom"
26
+
27
+ class Bake < Thor
28
+
29
+ desc "update", "Update the boot directory with the latest firmware and kernel"
30
+
31
+ def update(image_name = "pi.img")
32
+ ##
33
+ ## Prepare for the update
34
+ ##
35
+
36
+ say("Attaching image #{image_name}", :blue)
37
+
38
+ device_name = Geom.attach_image(image_name)
39
+
40
+ unless device_name.nil? then
41
+ say("...Image attached to #{device_name}", :blue)
42
+ else
43
+ say("ERROR: Could not attach #{image_name}", :red)
44
+ end
45
+
46
+ say("Mounting image #{image_name}...", :blue)
47
+
48
+ if Geom.mount_image(device_name) then
49
+ say("...Image mounted under /mnt", :blue)
50
+ else
51
+ say("ERROR: Could not mount #{image_name}", :blue)
52
+
53
+ # try to unmount and detach the failed image
54
+ unless Geom.umount_image_dirs then
55
+ say("ERROR: Unable to unmount all the image directories", :red)
56
+ end
57
+ unless Geom.detach_image(device_name) then
58
+ say("ERROR: Failed to detach device #{device_name}", :red)
59
+ end
60
+
61
+ exit
62
+ end
63
+
64
+ ##
65
+ ## Fetch the latest version of the firmware
66
+ ##
67
+
68
+ abort = false
69
+
70
+ # Fetch/update the upstream firmware
71
+ if not File.exists?("firmware") then
72
+ # Create the firmware directory, and clone the distributions
73
+ # if this hasn't been done before
74
+ say("Fetching upstream firmware...", :blue)
75
+
76
+ unless system("git clone https://github.com/raspberrypi/firmware.git firmware") then
77
+ say("ERROR: Failed to update firmware. Aborting.", :red)
78
+ abort = true
79
+ end
80
+ else
81
+ # Use git to get the latest version of the firmware
82
+ say("Updating upstream firmware...", :blue)
83
+
84
+ unless system("cd firmware; git pull") then
85
+ say("ERROR: Failed to update firmware. Aborting.", :red)
86
+ abort = false
87
+ end
88
+ end
89
+
90
+ # Copy the system binaries to the boot directory
91
+ unless abort then
92
+ say("Updating firmware and kernel...", :blue)
93
+
94
+ # Call the system directly, so that we can use sudo (i.e. don't use the native
95
+ # ruby calls)
96
+ unless system("sudo cp firmware/boot/bootcode.bin /mnt/pi-boot/bootcode.bin") then
97
+ abort = true;
98
+ end
99
+
100
+ unless system("sudo cp firmware/boot/loader.bin /mnt/pi-boot/loader.bin") then
101
+ abort = true;
102
+ end
103
+
104
+ unless system("sudo cp firmware/boot/arm224_start.elf /mnt/pi-boot/start.elf") then
105
+ abort = true;
106
+ end
107
+
108
+ unless system("sudo cp firmware/boot/kernel.img /mnt/pi-boot/kernel.img") then
109
+ abort = true;
110
+ end
111
+
112
+ say("...Done", :blue)
113
+ end
114
+
115
+ #cmdline.txt: Parameters passed to the kernel on boot
116
+
117
+ ##
118
+ ## Bootstrap the Debian (Raspbian) environment
119
+ ##
120
+
121
+ say("Bootstrapping the Debian (Raspbian) environment", :blue)
122
+
123
+ # Download the modified bootstrap scripts
124
+ #unless system("wget debian.tgz") then
125
+ # say("ERROR: Cannot find the bootstrap scripts", :red)
126
+ # abort = true
127
+ #end
128
+
129
+ # Now try to do the actual bootstrap
130
+ unless abort then
131
+ unless system("sudo ./lib/deb/debootstrap --foreign --arch armhf wheezy /mnt/pi-system1 http://archive.raspbian.org/raspbian") then
132
+ say("ERROR: Cannot find the bootstrap the Raspbian root", :red)
133
+ abort = true
134
+ end
135
+ end
136
+
137
+ ##
138
+ ## Cleanup
139
+ ##
140
+
141
+ # try to unmount and detach the image
142
+ if Geom.umount_image_dirs then
143
+ say("Unmounted staging directories", :blue)
144
+ else
145
+ say("ERROR: Unable to unmount all the image directories", :red)
146
+ end
147
+
148
+ if Geom.detach_image(device_name) then
149
+ say("Detached image", :blue)
150
+ else
151
+ say("ERROR: Failed to detach device #{device_name}", :red)
152
+ end
153
+
154
+ end
155
+
156
+ end
@@ -0,0 +1,682 @@
1
+ #!/bin/sh -e
2
+
3
+ VERSION='@VERSION@'
4
+
5
+ unset TMP TEMP TMPDIR || true
6
+
7
+ # might not be exported if we're running from init=/bin/sh or similar
8
+ export PATH
9
+
10
+ ###########################################################################
11
+
12
+ DEBOOTSTRAP_DIR=./lib/deb
13
+ DEVICES_TARGZ=$DEBOOTSTRAP_DIR/devices.tar.gz
14
+
15
+ . $DEBOOTSTRAP_DIR/functions
16
+ exec 4>&1
17
+
18
+ LANG=C
19
+ USE_COMPONENTS=main
20
+ KEYRING=""
21
+ DISABLE_KEYRING=""
22
+ VARIANT=""
23
+ ARCH=""
24
+ HOST_ARCH=""
25
+ HOST_OS=""
26
+ KEEP_DEBOOTSTRAP_DIR=""
27
+ USE_DEBIANINSTALLER_INTERACTION=""
28
+ SECOND_STAGE_ONLY=""
29
+ PRINT_DEBS=""
30
+ CHROOTDIR=""
31
+ MAKE_TARBALL=""
32
+ KEEP_DEBOOTSTRAP_DIR=""
33
+ EXTRACTOR_OVERRIDE=""
34
+ UNPACK_TARBALL=""
35
+ ADDITIONAL=""
36
+ EXCLUDE=""
37
+ VERBOSE=""
38
+ CERTIFICATE=""
39
+ CHECKCERTIF=""
40
+ PRIVATEKEY=""
41
+
42
+ DEF_MIRROR="http://ftp.us.debian.org/debian"
43
+
44
+ export LANG USE_COMPONENTS
45
+ umask 022
46
+
47
+ ###########################################################################
48
+
49
+ ## phases:
50
+ ## finddebs dldebs printdebs first_stage second_stage
51
+
52
+ RESOLVE_DEPS=true
53
+
54
+ WHAT_TO_DO="finddebs dldebs first_stage second_stage"
55
+ am_doing_phase () {
56
+ # usage: if am_doing_phase finddebs; then ...; fi
57
+ local x;
58
+ for x in "$@"; do
59
+ if echo " $WHAT_TO_DO " | grep -q " $x "; then return 0; fi
60
+ done
61
+ return 1
62
+ }
63
+
64
+ ###########################################################################
65
+
66
+ usage_err()
67
+ {
68
+ info USAGE1 "usage: [OPTION]... <suite> <target> [<mirror> [<script>]]"
69
+ info USAGE2 "Try \`${0##*/} --help' for more information."
70
+ error "$@"
71
+ }
72
+
73
+ usage()
74
+ {
75
+ echo "Usage: ${0##*/} [OPTION]... <suite> <target> [<mirror> [<script>]]"
76
+ echo "Bootstrap a Debian base system into a target directory."
77
+ echo
78
+ cat <<EOF
79
+ --help display this help and exit
80
+ --version display version information and exit
81
+ --verbose don't turn off the output of wget
82
+
83
+ --download-only download packages, but don't perform installation
84
+ --print-debs print the packages to be installed, and exit
85
+
86
+ --arch=A set the architecture to install (use if no dpkg)
87
+ [ --arch=powerpc ]
88
+
89
+ --include=A,B,C adds specified names to the list of base packages
90
+ --exclude=A,B,C removes specified packages from the list
91
+ --components=A,B,C use packages from the listed components of the
92
+ archive
93
+ --variant=X use variant X of the bootstrap scripts
94
+ (currently supported variants: buildd, fakechroot,
95
+ scratchbox, minbase)
96
+ --keyring=K check Release files against keyring K
97
+ --no-check-gpg avoid checking Release file signatures
98
+ --no-resolve-deps don't try to resolve dependencies automatically
99
+
100
+ --unpack-tarball=T acquire .debs from a tarball instead of http
101
+ --make-tarball=T download .debs and create a tarball (tgz format)
102
+ --second-stage-target=DIR
103
+ Run second stage in a subdirectory instead of root
104
+ (can be used to create a foreign chroot)
105
+ (requires --second-stage)
106
+ --extractor=TYPE override automatic .deb extractor selection
107
+ (supported: $EXTRACTORS_SUPPORTED)
108
+ --debian-installer used for internal purposes by debian-installer
109
+ --private-key=file read the private key from file
110
+ --certificate=file use the client certificate stored in file (PEM)
111
+ --no-check-certificate do not check certificate against certificate authorities
112
+ EOF
113
+ }
114
+
115
+ ###########################################################################
116
+
117
+ if [ -z "$PKGDETAILS" ]; then
118
+ error 1 NO_PKGDETAILS "No pkgdetails available; either install perl, or build pkgdetails.c from source"
119
+ fi
120
+
121
+ ###########################################################################
122
+
123
+ if [ $# != 0 ] ; then
124
+ while true ; do
125
+ case "$1" in
126
+ --help)
127
+ usage
128
+ exit 0
129
+ ;;
130
+ --version)
131
+ echo "debootstrap $VERSION"
132
+ exit 0
133
+ ;;
134
+ --debian-installer)
135
+ if ! (echo -n "" >&3) 2>/dev/null; then
136
+ error 1 ARG_DIBYHAND "If running debootstrap by hand, don't use --debian-installer"
137
+ fi
138
+ USE_DEBIANINSTALLER_INTERACTION=yes
139
+ shift
140
+ ;;
141
+ --foreign)
142
+ if [ "$PRINT_DEBS" != "true" ]; then
143
+ WHAT_TO_DO="finddebs dldebs first_stage"
144
+ fi
145
+ shift
146
+ ;;
147
+ --second-stage)
148
+ WHAT_TO_DO="second_stage"
149
+ SECOND_STAGE_ONLY=true
150
+ shift
151
+ ;;
152
+ --second-stage-target|--second-stage-target=?*)
153
+ if [ "$SECOND_STAGE_ONLY" != "true" ] ; then
154
+ error 1 STAGE2ONLY "option %s only applies in the second stage" "$1"
155
+ fi
156
+ if [ "$1" = "--second-stage-target" -a -n "$2" ] ; then
157
+ CHROOTDIR="$2"
158
+ shift 2
159
+ elif [ "$1" != "${1#--second-stage-target=}" ]; then
160
+ CHROOTDIR="${1#--second-stage-target=}"
161
+ shift
162
+ else
163
+ error 1 NEEDARG "option requires an argument: %s" "$1"
164
+ fi
165
+ ;;
166
+ --print-debs)
167
+ WHAT_TO_DO="finddebs printdebs kill_target"
168
+ PRINT_DEBS=true
169
+ shift
170
+ ;;
171
+ --download-only)
172
+ WHAT_TO_DO="finddebs dldebs"
173
+ shift
174
+ ;;
175
+ --make-tarball|--make-tarball=?*)
176
+ WHAT_TO_DO="finddebs dldebs maketarball kill_target"
177
+ if [ "$1" = "--make-tarball" -a -n "$2" ] ; then
178
+ MAKE_TARBALL="$2"
179
+ shift 2
180
+ elif [ "$1" != "${1#--make-tarball=}" ]; then
181
+ MAKE_TARBALL="${1#--make-tarball=}"
182
+ shift
183
+ else
184
+ error 1 NEEDARG "option requires an argument %s" "$1"
185
+ fi
186
+ ;;
187
+ --resolve-deps)
188
+ # redundant, but avoids breaking compatibility
189
+ RESOLVE_DEPS=true
190
+ shift
191
+ ;;
192
+ --no-resolve-deps)
193
+ RESOLVE_DEPS=false
194
+ shift
195
+ ;;
196
+ --keep-debootstrap-dir)
197
+ KEEP_DEBOOTSTRAP_DIR=true
198
+ shift
199
+ ;;
200
+ --arch|--arch=?*)
201
+ if [ "$1" = "--arch" -a -n "$2" ] ; then
202
+ ARCH="$2"
203
+ shift 2
204
+ elif [ "$1" != "${1#--arch=}" ]; then
205
+ ARCH="${1#--arch=}"
206
+ shift
207
+ else
208
+ error 1 NEEDARG "option requires an argument %s" "$1"
209
+ fi
210
+ ;;
211
+ --extractor|--extractor=?*)
212
+ if [ "$1" = "--extractor" -a -n "$2" ] ; then
213
+ EXTRACTOR_OVERRIDE="$2"
214
+ shift 2
215
+ elif [ "$1" != "${1#--extractor=}" ]; then
216
+ EXTRACTOR_OVERRIDE="${1#--extractor=}"
217
+ shift
218
+ else
219
+ error 1 NEEDARG "option requires an argument %s" "$1"
220
+ fi
221
+ if valid_extractor "$EXTRACTOR_OVERRIDE"; then
222
+ if ! type "$EXTRACTOR_OVERRIDE" >/dev/null 2>&1; then
223
+ error 1 MISSINGEXTRACTOR "The selected extractor cannot be found: %s" "$EXTRACTOR_OVERRIDE"
224
+ fi
225
+ else
226
+ error 1 BADEXTRACTOR "%s: unknown extractor" "$EXTRACTOR_OVERRIDE"
227
+ fi
228
+ ;;
229
+ --unpack-tarball|--unpack-tarball=?*)
230
+ if [ "$1" = "--unpack-tarball" -a -n "$2" ] ; then
231
+ UNPACK_TARBALL="$2"
232
+ shift 2
233
+ elif [ "$1" != "${1#--unpack-tarball=}" ]; then
234
+ UNPACK_TARBALL="${1#--unpack-tarball=}"
235
+ shift
236
+ else
237
+ error 1 NEEDARG "option requires an argument %s" "$1"
238
+ fi
239
+ if [ ! -f "$UNPACK_TARBALL" ] ; then
240
+ error 1 NOTARBALL "%s: No such file or directory" "$UNPACK_TARBALL"
241
+ fi
242
+ ;;
243
+ --include|--include=?*)
244
+ if [ "$1" = "--include" -a -n "$2" ]; then
245
+ ADDITIONAL="$2"
246
+ shift 2
247
+ elif [ "$1" != "${1#--include=}" ]; then
248
+ ADDITIONAL="${1#--include=}"
249
+ shift 1
250
+ else
251
+ error 1 NEEDARG "option requires an argument %s" "$1"
252
+ fi
253
+ ADDITIONAL="$(echo "$ADDITIONAL" | tr , " ")"
254
+ ;;
255
+ --exclude|--exclude=?*)
256
+ if [ "$1" = "--exclude" -a -n "$2" ]; then
257
+ EXCLUDE="$2"
258
+ shift 2
259
+ elif [ "$1" != "${1#--exclude=}" ]; then
260
+ EXCLUDE="${1#--exclude=}"
261
+ shift 1
262
+ else
263
+ error 1 NEEDARG "option requires an argument %s" "$1"
264
+ fi
265
+ EXCLUDE="$(echo "$EXCLUDE" | tr , " ")"
266
+ ;;
267
+ --verbose)
268
+ VERBOSE=true
269
+ export VERBOSE
270
+ shift 1
271
+ ;;
272
+ --components|--components=?*)
273
+ if [ "$1" = "--components" -a -n "$2" ]; then
274
+ USE_COMPONENTS="$2"
275
+ shift 2
276
+ elif [ "$1" != "${1#--components=}" ]; then
277
+ USE_COMPONENTS="${1#--components=}"
278
+ shift 1
279
+ else
280
+ error 1 NEEDARG "option requires an argument %s" "$1"
281
+ fi
282
+ USE_COMPONENTS="$(echo "$USE_COMPONENTS" | tr , "|")"
283
+ ;;
284
+ --variant|--variant=?*)
285
+ if [ "$1" = "--variant" -a -n "$2" ]; then
286
+ VARIANT="$2"
287
+ shift 2
288
+ elif [ "$1" != "${1#--variant=}" ]; then
289
+ VARIANT="${1#--variant=}"
290
+ shift 1
291
+ else
292
+ error 1 NEEDARG "option requires an argument %s" "$1"
293
+ fi
294
+ ;;
295
+ --keyring|--keyring=?*)
296
+ if ! gpgv --version >/dev/null 2>&1; then
297
+ error 1 NEEDGPGV "gpgv not installed, but required for Release verification"
298
+ fi
299
+ if [ "$1" = "--keyring" -a -n "$2" ]; then
300
+ KEYRING="$2"
301
+ shift 2
302
+ elif [ "$1" != "${1#--keyring=}" ]; then
303
+ KEYRING="${1#--keyring=}"
304
+ shift 1
305
+ else
306
+ error 1 NEEDARG "option requires an argument %s" "$1"
307
+ fi
308
+ ;;
309
+ --no-check-gpg)
310
+ shift 1
311
+ DISABLE_KEYRING=1
312
+ ;;
313
+ --certificate|--certificate=?*)
314
+ if [ "$1" = "--certificate" -a -n "$2" ]; then
315
+ CERTIFICATE="--certificate=$2"
316
+ shift 2
317
+ elif [ "$1" != "${1#--certificate=}" ]; then
318
+ CERTIFICATE="--certificate=${1#--certificate=}"
319
+ shift 1
320
+ else
321
+ error 1 NEEDARG "option requires an argument %s" "$1"
322
+ fi
323
+ ;;
324
+ --private-key|--private-key=?*)
325
+ if [ "$1" = "--private-key" -a -n "$2" ]; then
326
+ PRIVATEKEY="--private-key=$2"
327
+ shift 2
328
+ elif [ "$1" != "${1#--private-key=}" ]; then
329
+ PRIVATEKEY="--private-key=${1#--private-key=}"
330
+ shift 1
331
+ else
332
+ error 1 NEEDARG "option requires an argument %s" "$1"
333
+ fi
334
+ ;;
335
+ --no-check-certificate)
336
+ CHECKCERTIF="--no-check-certificate"
337
+ shift
338
+ ;;
339
+ --*)
340
+ error 1 BADARG "unrecognized or invalid option %s" "$1"
341
+ ;;
342
+ *)
343
+ break
344
+ ;;
345
+ esac
346
+ done
347
+ fi
348
+
349
+ ###########################################################################
350
+
351
+ if [ "$SECOND_STAGE_ONLY" = "true" ]; then
352
+ SUITE=$(cat $DEBOOTSTRAP_DIR/suite)
353
+ ARCH=$(cat $DEBOOTSTRAP_DIR/arch)
354
+ if [ -e $DEBOOTSTRAP_DIR/variant ]; then
355
+ VARIANT=$(cat $DEBOOTSTRAP_DIR/variant)
356
+ SUPPORTED_VARIANTS="$VARIANT"
357
+ fi
358
+ if [ -z "$CHROOTDIR" ]; then
359
+ TARGET=/
360
+ else
361
+ TARGET=$CHROOTDIR
362
+ fi
363
+ SCRIPT=$DEBOOTSTRAP_DIR/suite-script
364
+ else
365
+ if [ -z "$1" ] || [ -z "$2" ]; then
366
+ usage_err 1 NEEDSUITETARGET "You must specify a suite and a target."
367
+ fi
368
+ SUITE="$1"
369
+ TARGET="$2"
370
+ TARGET="${TARGET%/}"
371
+ if [ "${TARGET#/}" = "${TARGET}" ]; then
372
+ if [ "${TARGET%/*}" = "$TARGET" ] ; then
373
+ TARGET="$(echo "`pwd`/$TARGET")"
374
+ else
375
+ TARGET="$(cd "${TARGET%/*}"; echo "`pwd`/${TARGET##*/}")"
376
+ fi
377
+ fi
378
+
379
+ SCRIPT="$DEBOOTSTRAP_DIR/scripts/$1"
380
+ if [ -n "$VARIANT" ] && [ -e "${SCRIPT}.${VARIANT}" ]; then
381
+ SCRIPT="${SCRIPT}.${VARIANT}"
382
+ SUPPORTED_VARIANTS="$VARIANT"
383
+ fi
384
+ if [ "$4" != "" ]; then
385
+ SCRIPT="$4"
386
+ fi
387
+ fi
388
+
389
+ ###########################################################################
390
+
391
+ if in_path dpkg && \
392
+ dpkg --print-architecture >/dev/null 2>&1; then
393
+ HOST_ARCH=`/usr/bin/dpkg --print-architecture`
394
+ elif in_path udpkg && \
395
+ udpkg --print-architecture >/dev/null 2>&1; then
396
+ HOST_ARCH=`/usr/bin/udpkg --print-architecture`
397
+ elif [ -e $DEBOOTSTRAP_DIR/arch ]; then
398
+ HOST_ARCH=`cat $DEBOOTSTRAP_DIR/arch`
399
+ fi
400
+ HOST_OS="$HOST_ARCH"
401
+ # basic host OS guessing for non-Debian systems
402
+ if [ -z "$HOST_OS" ]; then
403
+ case `uname` in
404
+ Linux)
405
+ HOST_OS=linux
406
+ ;;
407
+ GNU/kFreeBSD)
408
+ HOST_OS=kfreebsd
409
+ ;;
410
+ GNU)
411
+ HOST_OS=hurd
412
+ ;;
413
+ FreeBSD*)
414
+ HOST_OS=freebsd
415
+ ;;
416
+ esac
417
+ fi
418
+
419
+ if [ -z "$ARCH" ]; then
420
+ ARCH=$HOST_ARCH
421
+ fi
422
+
423
+ if [ -z "$ARCH" ] || [ -z "$HOST_OS" ]; then
424
+ error 1 WHATARCH "Couldn't work out current architecture"
425
+
426
+ fi
427
+
428
+ if [ "$HOST_OS" = "kfreebsd" ] || [ "$HOST_OS" = "freebsd" ]; then
429
+ for module in "linprocfs fdescfs tmpfs linsysfs"; do
430
+ kldstat -m "$module" > /dev/null 2>&1 || warning SANITYCHECK "Probably required module %s is not loaded" "$module"
431
+ done
432
+ fi
433
+
434
+ if [ "$TARGET" = "/" ]; then
435
+ CHROOT_CMD=""
436
+ elif doing_variant scratchbox; then
437
+ for config in ~/.scratchbox2/*/sb2.config;
438
+ do
439
+ export `grep ^SBOX_TARGET_ROOT= $config`
440
+ if [ "x$SBOX_TARGET_ROOT" = "x$TARGET" ]; then
441
+ SB2_TARGET=$(basename $(dirname $config))
442
+ fi
443
+ done
444
+ [ "x$SB2_TARGET" != "x" ] || error 1 SBOXTARGETREQ "No scratchbox target configured for $TARGET"
445
+ CHROOT_CMD="sb2 -eR -t $SB2_TARGET"
446
+ else
447
+ CHROOT_CMD="chroot $TARGET"
448
+ fi
449
+
450
+ if [ -z "$SHA_SIZE" ]; then
451
+ SHA_SIZE=256
452
+ fi
453
+ if ! in_path "sha${SHA_SIZE}sum" && ! in_path "sha${SHA_SIZE}"; then
454
+ SHA_SIZE=1
455
+ fi
456
+ DEBOOTSTRAP_CHECKSUM_FIELD="SHA$SHA_SIZE"
457
+
458
+ export ARCH SUITE TARGET CHROOT_CMD SHA_SIZE DEBOOTSTRAP_CHECKSUM_FIELD
459
+
460
+ if am_doing_phase first_stage second_stage; then
461
+ if in_path id && [ `id -u` -ne 0 ]; then
462
+ error 1 NEEDROOT "debootstrap can only run as root"
463
+ fi
464
+ # Ensure that we can create working devices and executables on the target.
465
+ if ! check_sane_mount "$TARGET"; then
466
+ error 1 NOEXEC "Cannot install into target '$TARGET' mounted with noexec or nodev"
467
+ fi
468
+ fi
469
+
470
+ if [ ! -e "$SCRIPT" ]; then
471
+ error 1 NOSCRIPT "No such script: %s" "$SCRIPT"
472
+ fi
473
+
474
+ ###########################################################################
475
+
476
+ if [ "$TARGET" != "" ]; then
477
+ mkdir -p "$TARGET/debootstrap"
478
+ fi
479
+
480
+ ###########################################################################
481
+
482
+ # Use of fd's by functions/scripts:
483
+ #
484
+ # stdin/stdout/stderr: used normally
485
+ # fd 4: I:/W:/etc information
486
+ # fd 5,6: spare for functions
487
+ # fd 7,8: spare for scripts
488
+
489
+ if [ "$USE_DEBIANINSTALLER_INTERACTION" = yes ]; then
490
+ # stdout=stderr: full log of debootstrap run
491
+ # fd 3: I:/W:/etc information
492
+ exec 4>&3
493
+ elif am_doing_phase printdebs; then
494
+ # stderr: I:/W:/etc information
495
+ # stdout: debs needed
496
+ exec 4>&2
497
+ else
498
+ # stderr: used in exceptional circumstances only
499
+ # stdout: I:/W:/etc information
500
+ # $TARGET/debootstrap/debootstrap.log: full log of debootstrap run
501
+ exec 4>&1
502
+ exec >>"$TARGET/debootstrap/debootstrap.log"
503
+ exec 2>&1
504
+ fi
505
+
506
+ ###########################################################################
507
+
508
+ if [ "$UNPACK_TARBALL" ]; then
509
+ if [ "${UNPACK_TARBALL#/}" = "$UNPACK_TARBALL" ]; then
510
+ error 1 TARPATH "Tarball must be given a complete path"
511
+ fi
512
+ if [ "${UNPACK_TARBALL%.tar}" != "$UNPACK_TARBALL" ]; then
513
+ (cd "$TARGET" && tar -xf "$UNPACK_TARBALL")
514
+ elif [ "${UNPACK_TARBALL%.tgz}" != "$UNPACK_TARBALL" ]; then
515
+ (cd "$TARGET" && zcat "$UNPACK_TARBALL" | tar -xf -)
516
+ else
517
+ error 1 NOTTAR "Unknown tarball: must be either .tar or .tgz"
518
+ fi
519
+ fi
520
+
521
+ ###########################################################################
522
+
523
+ . "$SCRIPT"
524
+
525
+ if [ "$SECOND_STAGE_ONLY" = "true" ]; then
526
+ MIRRORS=null:
527
+ else
528
+ MIRRORS="$DEF_MIRROR"
529
+ if [ "$3" != "" ]; then
530
+ MIRRORS="$3"
531
+ MIRRORS="${MIRRORS%/}"
532
+ fi
533
+ fi
534
+
535
+ export MIRRORS
536
+
537
+ ok=false
538
+ for v in $SUPPORTED_VARIANTS; do
539
+ if doing_variant $v; then ok=true; fi
540
+ done
541
+ if ! $ok; then
542
+ error 1 UNSUPPVARIANT "unsupported variant"
543
+ fi
544
+
545
+ ###########################################################################
546
+
547
+ if am_doing_phase finddebs; then
548
+ if [ "$FINDDEBS_NEEDS_INDICES" = "true" ] || \
549
+ [ "$RESOLVE_DEPS" = "true" ]; then
550
+ download_indices
551
+ GOT_INDICES=true
552
+ fi
553
+
554
+ work_out_debs
555
+
556
+ base=$(without "$base $ADDITIONAL" "$EXCLUDE")
557
+
558
+ if [ "$RESOLVE_DEPS" = true ]; then
559
+ requiredX=$(echo $(echo $required | tr ' ' '\n' | sort | uniq))
560
+ baseX=$(echo $(echo $base | tr ' ' '\n' | sort | uniq))
561
+
562
+ baseN=$(without "$baseX" "$requiredX")
563
+ baseU=$(without "$baseX" "$baseN")
564
+
565
+ if [ "$baseU" != "" ]; then
566
+ info REDUNDANTBASE "Found packages in base already in required: %s" "$baseU"
567
+ fi
568
+
569
+ info RESOLVEREQ "Resolving dependencies of required packages..."
570
+ required=$(resolve_deps $requiredX)
571
+ info RESOLVEBASE "Resolving dependencies of base packages..."
572
+ base=$(resolve_deps $baseX)
573
+ base=$(without "$base" "$required")
574
+
575
+ requiredX=$(without "$required" "$requiredX")
576
+ baseX=$(without "$base" "$baseX")
577
+ if [ "$requiredX" != "" ]; then
578
+ info NEWREQUIRED "Found additional required dependencies: %s" "$requiredX"
579
+ fi
580
+ if [ "$baseX" != "" ]; then
581
+ info NEWBASE "Found additional base dependencies: %s" "$baseX"
582
+ fi
583
+ fi
584
+
585
+ all_debs="$required $base"
586
+ fi
587
+
588
+ if am_doing_phase printdebs; then
589
+ echo "$all_debs"
590
+ fi
591
+
592
+ if am_doing_phase dldebs; then
593
+ if [ "$GOT_INDICES" != "true" ]; then
594
+ download_indices
595
+ fi
596
+ download $all_debs
597
+ fi
598
+
599
+ if am_doing_phase maketarball; then
600
+ (cd $TARGET;
601
+ tar czf - var/lib/apt var/cache/apt) >$MAKE_TARBALL
602
+ fi
603
+
604
+ if am_doing_phase first_stage; then
605
+ choose_extractor
606
+
607
+ # first stage sets up the chroot -- no calls should be made to
608
+ # "chroot $TARGET" here; but they should be possible by the time it's
609
+ # finished
610
+ first_stage_install
611
+
612
+ if ! am_doing_phase second_stage; then
613
+ cp "$0" "$TARGET/debootstrap/debootstrap"
614
+ cp $DEBOOTSTRAP_DIR/functions "$TARGET/debootstrap/functions"
615
+ cp $DEBOOTSTRAP_DIR/devices.tar.gz "$TARGET/debootstrap/devices.tar.gz"
616
+ cp $SCRIPT "$TARGET/debootstrap/suite-script"
617
+ echo "$ARCH" >"$TARGET/debootstrap/arch"
618
+ echo "$SUITE" >"$TARGET/debootstrap/suite"
619
+ [ "" = "$VARIANT" ] ||
620
+ echo "$VARIANT" >"$TARGET/debootstrap/variant"
621
+ echo "$required" >"$TARGET/debootstrap/required"
622
+ echo "$base" >"$TARGET/debootstrap/base"
623
+
624
+ chmod 755 "$TARGET/debootstrap/debootstrap"
625
+ fi
626
+ fi
627
+
628
+ if am_doing_phase second_stage; then
629
+ if [ "$SECOND_STAGE_ONLY" = true ]; then
630
+ required="$(cat $DEBOOTSTRAP_DIR/required)"
631
+ base="$(cat $DEBOOTSTRAP_DIR/base)"
632
+ all_debs="$required $base"
633
+ fi
634
+
635
+ # second stage uses the chroot to clean itself up -- has to be able to
636
+ # work from entirely within the chroot (in case we've booted into it,
637
+ # possibly over NFS eg)
638
+
639
+ second_stage_install
640
+
641
+ # create sources.list
642
+ # first, kill debootstrap.invalid sources.list
643
+ if [ -e "$TARGET/etc/apt/sources.list" ]; then
644
+ rm -f "$TARGET/etc/apt/sources.list"
645
+ fi
646
+ if [ "${MIRRORS#http*://}" != "$MIRRORS" ]; then
647
+ setup_apt_sources "${MIRRORS%% *}"
648
+ mv_invalid_to "${MIRRORS%% *}"
649
+ else
650
+ setup_apt_sources "$DEF_MIRROR"
651
+ mv_invalid_to "$DEF_MIRROR"
652
+ fi
653
+
654
+ if [ -e "$TARGET/debootstrap/debootstrap.log" ]; then
655
+ if [ "$KEEP_DEBOOTSTRAP_DIR" = true ]; then
656
+ cp "$TARGET/debootstrap/debootstrap.log" "$TARGET/var/log/bootstrap.log"
657
+ else
658
+ # debootstrap.log is still open as stdout/stderr and needs
659
+ # to remain so, but after unlinking it some NFS servers
660
+ # implement this by a temporary file in the same directory,
661
+ # which makes it impossible to rmdir that directory.
662
+ # Moving it instead works around the problem.
663
+ mv "$TARGET/debootstrap/debootstrap.log" "$TARGET/var/log/bootstrap.log"
664
+ fi
665
+ fi
666
+ sync
667
+
668
+ if [ "$KEEP_DEBOOTSTRAP_DIR" = true ]; then
669
+ if [ -x "$TARGET/debootstrap/debootstrap" ]; then
670
+ chmod 644 "$TARGET/debootstrap/debootstrap"
671
+ fi
672
+ else
673
+ rm -rf "$TARGET/debootstrap"
674
+ fi
675
+ fi
676
+
677
+ if am_doing_phase kill_target; then
678
+ if [ "$KEEP_DEBOOTSTRAP_DIR" != true ]; then
679
+ info KILLTARGET "Deleting target directory"
680
+ rm -rf "$TARGET"
681
+ fi
682
+ fi