-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions
More file actions
1574 lines (1401 loc) · 35.8 KB
/
functions
File metadata and controls
1574 lines (1401 loc) · 35.8 KB
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
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
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
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
############################################################### smallutils
smallyes() {
YES="${1-y}"
while echo "$YES" 2>/dev/null ; do : ; done
}
in_path () {
local OLD_IFS="$IFS"
IFS=":"
for dir in $PATH; do
if [ -x "$dir/$1" ]; then
IFS="$OLD_IFS"
return 0
fi
done
IFS="$OLD_IFS"
return 1
}
############################################################### interaction
error () {
# <error code> <name> <string> <args>
local err="$1"
local name="$2"
local fmt="$3"
shift; shift; shift
if [ "$USE_DEBIANINSTALLER_INTERACTION" ]; then
(echo "E: $name"
for x in "$@"; do echo "EA: $x"; done
echo "EF: $fmt") >&4
else
(printf "E: $fmt\n" "$@") >&4
fi
exit $err
}
warning () {
# <name> <string> <args>
local name="$1"
local fmt="$2"
shift; shift
if [ "$USE_DEBIANINSTALLER_INTERACTION" ]; then
(echo "W: $name"
for x in "$@"; do echo "WA: $x"; done
echo "WF: $fmt") >&4
else
printf "W: $fmt\n" "$@" >&4
fi
}
info () {
# <name> <string> <args>
local name="$1"
local fmt="$2"
shift; shift
if [ "$USE_DEBIANINSTALLER_INTERACTION" ]; then
(echo "I: $name"
for x in "$@"; do echo "IA: $x"; done
echo "IF: $fmt") >&4
else
printf "I: $fmt\n" "$@" >&4
fi
}
PROGRESS_NOW=0
PROGRESS_END=0
PROGRESS_NEXT=""
PROGRESS_WHAT=""
progress_next () {
PROGRESS_NEXT="$1"
}
curlprogress () {
[ ! "$VERBOSE" ] && QSWITCH="-s"
local ret=0
if [ "$USE_DEBIANINSTALLER_INTERACTION" ] && [ "$PROGRESS_NEXT" ]; then
curl "$@" 2>&3
ret=$?
else
curl $QSWITCH "$@"
ret=$?
fi
return $ret
}
progress () {
# <now> <end> <name> <string> <args>
local now="$1"
local end="$2"
local name="$3"
local fmt="$4"
shift; shift; shift; shift
if [ "$USE_DEBIANINSTALLER_INTERACTION" ]; then
PROGRESS_NOW="$now"
PROGRESS_END="$end"
PROGRESS_NEXT=""
(echo "P: $now $end $name"
for x in "$@"; do echo "PA: $x"; done
echo "PF: $fmt") >&3
fi
}
dpkg_progress () {
# <now> <end> <name> <desc> UNPACKING|CONFIGURING
local now="$1"
local end="$2"
local name="$3"
local desc="$4"
local action="$5"
local expect=
if [ "$action" = UNPACKING ]; then
expect=half-installed
elif [ "$action" = CONFIGURING ]; then
expect=half-configured
fi
dp () {
now="$(($now + ${1:-1}))"
}
exitcode=0
while read status pkg qstate; do
if [ "$status" = "EXITCODE" ]; then
exitcode="$pkg"
continue
fi
[ "$qstate" = "$expect" ] || continue
case $qstate in
half-installed)
dp; progress "$now" "$end" "$name" "$desc"
info "$action" "Unpacking %s..." "${pkg%:}"
expect=unpacked
;;
unpacked)
expect=half-installed
;;
half-configured)
dp; progress "$now" "$end" "$name" "$desc"
info "$action" "Configuring %s..." "${pkg%:}"
expect=installed
;;
installed)
expect=half-configured
;;
esac
done
return $exitcode
}
############################################################# set variables
default_mirror () {
DEF_MIRROR="$1"
}
FINDDEBS_NEEDS_INDICES=false
finddebs_style () {
case "$1" in
hardcoded)
;;
from-indices)
FINDDEBS_NEEDS_INDICES=true
;;
*)
error 1 BADFINDDEBS "unknown finddebs style"
;;
esac
}
mk_download_dirs () {
if [ $DLDEST = "apt_dest" ]; then
mkdir -p "$TARGET/$APTSTATE/lists/partial"
mkdir -p "$TARGET/var/cache/apt/archives/partial"
fi
}
download_style () {
case "$1" in
apt)
if [ "$2" = "var-state" ]; then
APTSTATE=var/state/apt
else
APTSTATE=var/lib/apt
fi
DLDEST=apt_dest
export APTSTATE DLDEST DEBFOR
;;
*)
error 1 BADDLOAD "unknown download style"
;;
esac
}
keyring () {
KEYRING_WANTED="$1"
if [ -z "$KEYRING" ] && [ -e "$KEYRING_WANTED" ]; then
KEYRING="$KEYRING_WANTED"
fi
}
########################################################## variant handling
doing_variant () {
if [ "$1" = "$VARIANT" ]; then return 0; fi
if [ "$1" = "-" ] && [ "$VARIANT" = "" ]; then return 0; fi
return 1
}
SUPPORTED_VARIANTS="-"
variants () {
SUPPORTED_VARIANTS="$*"
for v in $*; do
if doing_variant "$v"; then return 0; fi
done
error 1 UNSUPPVARIANT "unsupported variant"
}
################################################# work out names for things
mirror_style () {
case "$1" in
release)
DOWNLOAD_INDICES=download_release_indices
DOWNLOAD_DEBS=download_release
;;
main)
DOWNLOAD_INDICES=download_main_indices
DOWNLOAD_DEBS=download_main
;;
*)
error 1 BADMIRROR "unknown mirror style"
;;
esac
export DOWNLOAD_INDICES
export DOWNLOAD_DEBS
}
force_md5 () {
DEBOOTSTRAP_CHECKSUM_FIELD=MD5SUM
export DEBOOTSTRAP_CHECKSUM_FIELD
}
verify_checksum () {
# args: dest checksum size
local expchecksum="$2"
local expsize="$3"
if [ "$DEBOOTSTRAP_CHECKSUM_FIELD" = "MD5SUM" ]; then
if in_path md5sum; then
relchecksum=`md5sum < "$1" | sed 's/ .*$//'`
elif in_path md5; then
relchecksum=`md5 < "$1"`
else
error 1 SIGCHECK "Cannot check md5sum"
fi
else
if in_path "sha${SHA_SIZE}sum"; then
relchecksum=`sha${SHA_SIZE}sum < "$1" | sed 's/ .*$//'`
elif in_path "sha${SHA_SIZE}"; then
relchecksum=`sha${SHA_SIZE} < "$1"`
else
error 1 SIGCHECK "Cannot check sha${SHA_SIZE}sum"
fi
fi
relsize=`wc -c < "$1"`
if [ "$expsize" -ne "$relsize" ] || [ "$expchecksum" != "$relchecksum" ]; then
return 1
fi
return 0
}
get () {
# args: from dest 'nocache'
# args: from dest [checksum size] [alt {checksum size type}]
local displayname
local versionname
if [ "${2%.deb}" != "$2" ]; then
displayname="$(echo "$2" | sed 's,^.*/,,;s,_.*$,,')"
versionname="$(echo "$2" | sed 's,^.*/,,' | cut -d_ -f2)"
else
displayname="$(echo "$1" | sed 's,^.*/,,')"
fi
if [ -e "$2" ]; then
if [ -z "$3" ]; then
return 0
elif [ "$3" = nocache ]; then
rm -f "$2"
else
info VALIDATING "Validating %s %s" "$displayname" "$versionname"
if verify_checksum "$2" "$3" "$4"; then
return 0
else
rm -f "$2"
fi
fi
fi
# Drop 'nocache' option
if [ "$3" = nocache ]; then
set "$1" "$2"
fi
if [ "$#" -gt 5 ]; then
local st=3
if [ "$5" = "-" ]; then st=6; fi
local order="$(a=$st; while [ "$a" -le $# ]; do eval echo \"\${$(($a+1))}\" $a;
a=$(($a + 3)); done | sort -n | sed 's/.* //')"
else
local order=3
fi
for a in $order; do
local checksum="$(eval echo \${$a})"
local siz="$(eval echo \${$(( $a+1 ))})"
local typ="$(eval echo \${$(( $a+2 ))})"
local from
local dest
local iters=0
case "$typ" in
bz2) from="$1.bz2"; dest="$2.bz2" ;;
gz) from="$1.gz"; dest="$2.gz" ;;
*) from="$1"; dest="$2" ;;
esac
if [ "${dest#/}" = "$dest" ]; then
dest="./$dest"
fi
local dest2="$dest"
if [ -d "${dest2%/*}/partial" ]; then
dest2="${dest2%/*}/partial/${dest2##*/}"
fi
while [ "$iters" -lt 10 ]; do
info RETRIEVING "Retrieving %s %s" "$displayname" "$versionname"
if ! just_get "$from" "$dest2"; then continue 2; fi
if [ "$checksum" != "" ]; then
info VALIDATING "Validating %s %s" "$displayname" "$versionname"
if verify_checksum "$dest2" "$checksum" "$siz"; then
checksum=""
fi
fi
if [ -z "$checksum" ]; then
[ "$dest2" = "$dest" ] || mv "$dest2" "$dest"
case "$typ" in
gz) gunzip "$dest" ;;
bz2) bunzip2 "$dest" ;;
esac
return 0
else
rm -f "$dest2"
warning RETRYING "Retrying failed download of %s" "$from"
iters="$(($iters + 1))"
fi
done
warning CORRUPTFILE "%s was corrupt" "$from"
done
return 1
}
just_get () {
# args: from dest
local from="$1"
local dest="$2"
mkdir -p "${dest%/*}"
if [ "${from#null:}" != "$from" ]; then
error 1 NOTPREDL "%s was not pre-downloaded" "${from#null:}"
elif [ "${from#http://}" != "$from" ] || [ "${from#ftp://}" != "$from" ]; then
# http/ftp mirror
if curlprogress -o "$dest" "$from"; then
return 0
else
rm -f "$dest"
return 1
fi
elif [ "${from#https://}" != "$from" ] ; then
# http/ftp mirror
if curlprogress $CHECKCERTIF $CERTIFICATE $PRIVATEKEY -o "$dest" "$from"; then
return 0
else
rm -f "$dest"
return 1
fi
elif [ "${from#file:}" != "$from" ]; then
local base="${from#file:}"
if [ "${base#//}" != "$base" ]; then
base="/${from#file://*/}"
fi
if [ -e "$base" ]; then
cp "$base" "$dest"
return 0
else
return 1
fi
elif [ "${from#ssh:}" != "$from" ]; then
local ssh_dest="$(echo $from | sed -e 's#ssh://##' -e 's#/#:/#')"
if [ -n "$ssh_dest" ]; then
scp "$ssh_dest" "$dest"
return 0
else
return 1
fi
else
error 1 UNKNOWNLOC "unknown location %s" "$from"
fi
}
download () {
mk_download_dirs
"$DOWNLOAD_DEBS" $(echo "$@" | tr ' ' '\n' | sort)
}
download_indices () {
mk_download_dirs
"$DOWNLOAD_INDICES" $(echo "$@" | tr ' ' '\n' | sort)
}
debfor () {
(while read pkg path; do
for p in "$@"; do
[ "$p" = "$pkg" ] || continue;
echo "$path"
done
done <"$TARGET/debootstrap/debpaths"
)
}
apt_dest () {
# args:
# deb package version arch mirror path
# pkg suite component arch mirror path
# rel suite mirror path
case "$1" in
deb)
echo "/var/cache/apt/archives/${2}_${3}_${4}.deb" | sed 's/:/%3a/'
;;
pkg)
local m="$5"
m="debootstrap.invalid"
#if [ "${m#http://}" != "$m" ]; then
# m="${m#http://}"
#elif [ "${m#file://}" != "$m" ]; then
# m="file_localhost_${m#file://*/}"
#elif [ "${m#file:/}" != "$m" ]; then
# m="file_localhost_${m#file:/}"
#fi
printf "%s" "$APTSTATE/lists/"
echo "${m}_$6" | sed 's/\//_/g'
;;
rel)
local m="$3"
m="debootstrap.invalid"
#if [ "${m#http://}" != "$m" ]; then
# m="${m#http://}"
#elif [ "${m#file://}" != "$m" ]; then
# m="file_localhost_${m#file://*/}"
#elif [ "${m#file:/}" != "$m" ]; then
# m="file_localhost_${m#file:/}"
#fi
printf "%s" "$APTSTATE/lists/"
echo "${m}_$4" | sed 's/\//_/g'
;;
esac
}
################################################################## download
get_release_checksum () {
local reldest="$1"
local path="$2"
if [ "$DEBOOTSTRAP_CHECKSUM_FIELD" = MD5SUM ]; then
local match="^[Mm][Dd]5[Ss][Uu][Mm]"
else
local match="^[Ss][Hh][Aa]$SHA_SIZE:"
fi
sed -n "/$match/,/^[^ ]/p" < "$reldest" | \
while read a b c; do
if [ "$c" = "$path" ]; then echo "$a $b"; fi
done | head -n 1
}
extract_release_components () {
local reldest="$1"; shift
TMPCOMPONENTS="$(sed -n 's/Components: *//p' "$reldest")"
for c in $TMPCOMPONENTS ; do
eval "
case \"\$c\" in
$USE_COMPONENTS)
COMPONENTS=\"\$COMPONENTS \$c\"
;;
esac
"
done
COMPONENTS="$(echo $COMPONENTS)"
if [ -z "$COMPONENTS" ]; then
mv "$reldest" "$reldest.malformed"
error 1 INVALIDREL "Invalid Release file, no valid components"
fi
}
download_release_sig () {
local m1="$1"
local reldest="$2"
local relsigdest="$3"
if [ -n "$KEYRING" ] && [ -z "$DISABLE_KEYRING" ]; then
progress 0 100 DOWNRELSIG "Downloading Release file signature"
progress_next 50
get "$m1/dists/$SUITE/Release.gpg" "$relsigdest" nocache ||
error 1 NOGETRELSIG "Failed getting release signature file %s" \
"$m1/dists/$SUITE/Release.gpg"
progress 50 100 DOWNRELSIG "Downloading Release file signature"
info RELEASESIG "Checking Release signature"
# Don't worry about the exit status from gpgv; parsing the output will
# take care of that.
(gpgv --status-fd 1 --keyring "$KEYRING" --ignore-time-conflict \
"$relsigdest" "$reldest" || true) | read_gpg_status
progress 100 100 DOWNRELSIG "Downloading Release file signature"
elif [ -z "$DISABLE_KEYRING" ] && [ -n "$KEYRING_WANTED" ]; then
warning KEYRING "Cannot check Release signature; keyring file not available %s" "$KEYRING_WANTED"
fi
}
download_release_indices () {
local m1="${MIRRORS%% *}"
local reldest="$TARGET/$($DLDEST rel "$SUITE" "$m1" "dists/$SUITE/Release")"
local relsigdest
progress 0 100 DOWNREL "Downloading Release file"
progress_next 100
get "$m1/dists/$SUITE/Release" "$reldest" nocache ||
error 1 NOGETREL "Failed getting release file %s" "$m1/dists/$SUITE/Release"
relsigdest="$TARGET/$($DLDEST rel "$SUITE" "$m1" "dists/$SUITE/Release.gpg")"
progress 100 100 DOWNREL "Downloading Release file"
download_release_sig "$m1" "$reldest" "$relsigdest"
extract_release_components $reldest
local totalpkgs=0
for c in $COMPONENTS; do
local subpath="$c/binary-$ARCH/Packages"
local bz2i="`get_release_checksum "$reldest" "$subpath.bz2"`"
local gzi="`get_release_checksum "$reldest" "$subpath.gz"`"
local normi="`get_release_checksum "$reldest" "$subpath"`"
local i=
if [ "$normi" != "" ]; then
i="$normi"
elif in_path bunzip2 && [ "$bz2i" != "" ]; then
i="$bz2i"
elif in_path gunzip && [ "$gzi" != "" ]; then
i="$gzi"
fi
if [ "$i" != "" ]; then
totalpkgs="$(( $totalpkgs + ${i#* } ))"
else
mv "$reldest" "$reldest.malformed"
error 1 MISSINGRELENTRY "Invalid Release file, no entry for %s" "$subpath"
fi
done
local donepkgs=0
local pkgdest
progress 0 $totalpkgs DOWNPKGS "Downloading Packages files"
for c in $COMPONENTS; do
local subpath="$c/binary-$ARCH/Packages"
local path="dists/$SUITE/$subpath"
local bz2i="`get_release_checksum "$reldest" "$subpath.bz2"`"
local gzi="`get_release_checksum "$reldest" "$subpath.gz"`"
local normi="`get_release_checksum "$reldest" "$subpath"`"
local ext=
local i=
if [ "$normi" != "" ]; then
ext="$ext $normi ."
i="$normi"
fi
if in_path bunzip2 && [ "$bz2i" != "" ]; then
ext="$ext $bz2i bz2"
i="${i:-$bz2i}"
fi
if in_path gunzip && [ "$gzi" != "" ]; then
ext="$ext $gzi gz"
i="${i:-$gzi}"
fi
progress_next "$(($donepkgs + ${i#* }))"
for m in $MIRRORS; do
pkgdest="$TARGET/$($DLDEST pkg "$SUITE" "$c" "$ARCH" "$m" "$path")"
if get "$m/$path" "$pkgdest" $ext; then break; fi
done
if [ ! -f "$pkgdest" ]; then
error 1 COULDNTDL "Couldn't download %s" "$path"
fi
donepkgs="$(($donepkgs + ${i#* }))"
progress $donepkgs $totalpkgs DOWNPKGS "Downloading Packages files"
done
}
get_package_sizes () {
# mirror pkgdest debs..
local m="$1"; shift
local pkgdest="$1"; shift
$PKGDETAILS PKGS "$m" "$pkgdest" "$@" | (
newleft=""
totaldebs=0
countdebs=0
while read p details; do
if [ "$details" = "-" ]; then
newleft="$newleft $p"
else
size="${details##* }";
totaldebs="$(($totaldebs + $size))"
countdebs="$(($countdebs + 1))"
fi
done
echo "$countdebs $totaldebs$newleft"
)
}
# note, leftovers come back on fd5 !!
download_debs () {
local m="$1"
local pkgdest="$2"
shift; shift
$PKGDETAILS PKGS "$m" "$pkgdest" "$@" | (
leftover=""
while read p ver arc mdup fil checksum size; do
if [ "$ver" = "-" ]; then
leftover="$leftover $p"
else
progress_next "$(($dloaddebs + $size))"
local debdest="$($DLDEST deb "$p" "$ver" "$arc" "$m" "$fil")"
if get "$m/$fil" "$TARGET/$debdest" "$checksum" "$size"; then
dloaddebs="$(($dloaddebs + $size))"
echo >>$TARGET/debootstrap/debpaths "$p $debdest"
else
warning COULDNTDL "Couldn't download package %s (ver %s arch %s)" "$p" "$ver" "$arc"
leftover="$leftover $p"
fi
fi
done
echo >&5 ${leftover# }
)
}
download_release () {
local m1="${MIRRORS%% *}"
local numdebs="$#"
local countdebs=0
progress $countdebs $numdebs SIZEDEBS "Finding package sizes"
local totaldebs=0
local leftoverdebs="$*"
for c in $COMPONENTS; do
if [ "$countdebs" -ge "$numdebs" ]; then break; fi
local path="dists/$SUITE/$c/binary-$ARCH/Packages"
local pkgdest="$TARGET/$($DLDEST pkg "$SUITE" "$c" "$ARCH" "$m1" "$path")"
if [ ! -e "$pkgdest" ]; then continue; fi
info CHECKINGSIZES "Checking component %s on %s..." "$c" "$m1"
leftoverdebs="$(get_package_sizes "$m1" "$pkgdest" $leftoverdebs)"
countdebs=$(($countdebs + ${leftoverdebs%% *}))
leftoverdebs=${leftoverdebs#* }
totaldebs=${leftoverdebs%% *}
leftoverdebs=${leftoverdebs#* }
progress $countdebs $numdebs SIZEDEBS "Finding package sizes"
done
if [ "$countdebs" -ne "$numdebs" ]; then
error 1 LEFTOVERDEBS "Couldn't find these debs: %s" "$leftoverdebs"
fi
local dloaddebs=0
progress $dloaddebs $totaldebs DOWNDEBS "Downloading packages"
:>$TARGET/debootstrap/debpaths
pkgs_to_get="$*"
for c in $COMPONENTS; do
local path="dists/$SUITE/$c/binary-$ARCH/Packages"
for m in $MIRRORS; do
local pkgdest="$TARGET/$($DLDEST pkg "$SUITE" "$c" "$ARCH" "$m" "$path")"
if [ ! -e "$pkgdest" ]; then continue; fi
pkgs_to_get="$(download_debs "$m" "$pkgdest" $pkgs_to_get 5>&1 1>&6)"
if [ -z "$pkgs_to_get" ]; then break; fi
done 6>&1
if [ -z "$pkgs_to_get" ]; then break; fi
done
progress $dloaddebs $totaldebs DOWNDEBS "Downloading packages"
if [ "$pkgs_to_get" != "" ]; then
error 1 COULDNTDLPKGS "Couldn't download packages: %s" "$pkgs_to_get"
fi
}
download_main_indices () {
local m1="${MIRRORS%% *}"
local comp="${USE_COMPONENTS}"
progress 0 100 DOWNMAINPKGS "Downloading Packages file"
progress_next 100
if [ -z "$comp" ]; then comp=main; fi
COMPONENTS="$(echo $comp | tr '|' ' ')"
export COMPONENTS
for m in $MIRRORS; do
for c in $COMPONENTS; do
local path="dists/$SUITE/$c/binary-$ARCH/Packages"
local pkgdest="$TARGET/$($DLDEST pkg "$SUITE" "$c" "$ARCH" "$m" "$path")"
if in_path gunzip && get "$m/${path}.gz" "${pkgdest}.gz"; then
rm -f "$pkgdest"
gunzip "$pkgdest.gz"
elif get "$m/$path" "$pkgdest"; then
true
fi
done
done
progress 100 100 DOWNMAINPKGS "Downloading Packages file"
}
download_main () {
local m1="${MIRRORS%% *}"
:>$TARGET/debootstrap/debpaths
for p in "$@"; do
for c in $COMPONENTS; do
local details=""
for m in $MIRRORS; do
local path="dists/$SUITE/$c/binary-$ARCH/Packages"
local pkgdest="$TARGET/$($DLDEST pkg "$SUITE" "$c" "$ARCH" "$m" "$path")"
if [ ! -e "$pkgdest" ]; then continue; fi
details="$($PKGDETAILS PKGS "$m" "$pkgdest" "$p")"
if [ "$details" = "$p -" ]; then
details=""
continue
fi
size="${details##* }"; details="${details% *}"
checksum="${details##* }"; details="${details% *}"
local debdest="$($DLDEST deb $details)"
if get "$m/${details##* }" "$TARGET/$debdest" "$checksum" "$size"; then
echo >>$TARGET/debootstrap/debpaths "$p $debdest"
details="done"
break
fi
done
if [ "$details" != "" ]; then
break
fi
done
if [ "$details" != "done" ]; then
error 1 COULDNTDL "Couldn't download %s" "$p"
fi
done
}
###################################################### deb choosing support
get_debs () {
local field="$1"
shift
local m1 c
for m1 in $MIRRORS; do
for c in $COMPONENTS; do
local path="dists/$SUITE/$c/binary-$ARCH/Packages"
local pkgdest="$TARGET/$($DLDEST pkg "$SUITE" "$c" "$ARCH" "$m1" "$path")"
echo $("$PKGDETAILS" FIELD "$field" "$m1" "$pkgdest" "$@" | sed 's/ .*//')
done
done
}
################################################################ extraction
EXTRACTORS_SUPPORTED="dpkg-deb ar"
# Native dpkg-deb based extractors
extract_dpkg_deb_field () {
local pkg="$1"
local field="$2"
dpkg-deb -f "$pkg" "$field"
}
extract_dpkg_deb_data () {
local pkg="$1"
dpkg-deb --fsys-tarfile "$pkg" | tar -xf -
}
# Raw .deb extractors
extract_ar_deb_field () {
local pkg="$1"
local field="$2"
local tarball=$(ar -t "$pkg" | grep "^control\.tar")
case "$tarball" in
control.tar.gz) cat_cmd=zcat ;;
control.tar.xz) cat_cmd=xzcat ;;
control.tar.zst) cat_cmd=zstdcat ;;
*) error 1 UNKNOWNCONTROLCOMP "Unknown compression type for %s in %s" "$tarball" "$pkg" ;;
esac
if in_path $cat_cmd; then
ar -p "$pkg" "$tarball" | $cat_cmd |
tar -O -xf - control ./control 2>/dev/null |
grep -i "^$field:" | sed -e 's/[^:]*: *//' | head -n 1
else
error 1 UNPACKCMDUNVL "Extracting %s requires the %s command, which is not available" "$pkg" "$cat_cmd"
fi
}
extract_ar_deb_data () {
local pkg="$1"
local tarball=$(ar -t "$pkg" | grep "^data.tar")
case "$tarball" in
data.tar.gz) cat_cmd=zcat ;;
data.tar.bz2) cat_cmd=bzcat ;;
data.tar.xz) cat_cmd=xzcat ;;
data.tar.zst) cat_cmd=zstdcat ;;
*) error 1 UNKNOWNDATACOMP "Unknown compression type for %s in %s" "$tarball" "$pkg" ;;
esac
if type $cat_cmd >/dev/null 2>&1; then
ar -p "$pkg" "$tarball" | $cat_cmd | tar -xf -
else
error 1 UNPACKCMDUNVL "Extracting %s requires the %s command, which is not available" "$pkg" "$cat_cmd"
fi
}
valid_extractor () {
local extractor="$1"
for E in $EXTRACTORS_SUPPORTED; do
if [ "$extractor" = "$E" ]; then
return 0
fi
done
return 1
}
choose_extractor () {
local extractor
if [ -n "$EXTRACTOR_OVERRIDE" ]; then
extractor="$EXTRACTOR_OVERRIDE"
elif type dpkg-deb >/dev/null 2>&1; then
extractor="dpkg-deb"
else
extractor="ar"
fi
info CHOSENEXTRACTOR "Chosen extractor for .deb packages: %s" "$extractor"
case "$extractor" in
dpkg-deb)
extract_deb_field () { extract_dpkg_deb_field "$@"; }
extract_deb_data () { extract_dpkg_deb_data "$@"; }
;;
ar)
extract_deb_field () { extract_ar_deb_field "$@"; }
extract_deb_data () { extract_ar_deb_data "$@"; }
;;
esac
}
extract () { (
cd "$TARGET"
local p=0 cat_cmd
for pkg in $(debfor "$@"); do
p="$(($p + 1))"
progress "$p" "$#" EXTRACTPKGS "Extracting packages"
packagename="$(echo "$pkg" | sed 's,^.*/,,;s,_.*$,,')"
info EXTRACTING "Extracting %s..." "$packagename"
extract_deb_data "./$pkg"
done
); }
in_target_nofail () {
if ! $CHROOT_CMD "$@" 2>/dev/null; then
true
fi
return 0
}
in_target_failmsg () {
local code="$1"
local msg="$2"
local arg="$3"
shift; shift; shift
if ! $CHROOT_CMD "$@"; then
warning "$code" "$msg" "$arg"
# Try to point user at actual failing package.
msg="See %s for details"
if [ -e "$TARGET/debootstrap/debootstrap.log" ]; then
arg="$TARGET/debootstrap/debootstrap.log"
local pkg="$(grep '^dpkg: error processing ' "$TARGET/debootstrap/debootstrap.log" | head -n 1 | cut -d ' ' -f 4)"
if [ -n "$pkg" ]; then
msg="$msg (possibly the package $pkg is at fault)"
fi
else
arg="the log"
fi
warning "$code" "$msg" "$arg"
return 1
fi
return 0
}
in_target () {
in_target_failmsg IN_TARGET_FAIL "Failure trying to run: %s" "$CHROOT_CMD $*" "$@"
}
###################################################### standard setup stuff
conditional_cp () {
if [ ! -e "$2/$1" ]; then
if [ -L "$1" ] && [ -e "$1" ]; then
cat "$1" >"$2/$1"
elif [ -e "$1" ]; then
cp -a "$1" "$2/$1"
fi
fi
}
mv_invalid_to () {
local m="$1"
m="$(echo "${m#http://}" | tr '/' '_' | sed 's/_*//')"
(cd "$TARGET/$APTSTATE/lists"
for a in debootstrap.invalid_*; do
mv "$a" "${m}_${a#*_}"
done
)
}
setup_apt_sources () {
mkdir -p "$TARGET/etc/apt"
for m in "$@"; do
local cs=""
for c in $COMPONENTS; do
local path="dists/$SUITE/$c/binary-$ARCH/Packages"
local pkgdest="$TARGET/$($DLDEST pkg "$SUITE" "$c" "$ARCH" "$m" "$path")"
if [ -e "$pkgdest" ]; then cs="$cs $c"; fi
done
if [ "$cs" != "" ]; then echo "deb $m $SUITE$cs"; fi
done > "$TARGET/etc/apt/sources.list"
}
setup_etc () {
mkdir -p "$TARGET/etc"
conditional_cp /etc/resolv.conf "$TARGET"
conditional_cp /etc/hostname "$TARGET"
if [ "$DLDEST" = apt_dest ] && [ ! -e "$TARGET/etc/apt/sources.list" ]; then
setup_apt_sources "http://debootstrap.invalid/"
fi
}
UMOUNT_DIRS=
umount_exit_function () {
local realdir
for dir in $UMOUNT_DIRS; do
realdir="$(in_target_nofail readlink -f "$dir")"
[ "$realdir" ] || continue
( cd / ; umount "$TARGET/${realdir#/}" ) || true
done
}
umount_on_exit () {
if [ "$UMOUNT_DIRS" ]; then
UMOUNT_DIRS="$UMOUNT_DIRS $1"
else
UMOUNT_DIRS="$1"
on_exit umount_exit_function
fi
}
clear_mtab () {
if [ -f "$TARGET/etc/mtab" ] && [ ! -h "$TARGET/etc/mtab" ]; then
rm -f "$TARGET/etc/mtab"
fi
}
setup_proc () {
case "$HOST_OS" in
*freebsd*)
umount_on_exit /dev
umount_on_exit /proc