-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathbootstrap.sh
executable file
·701 lines (669 loc) · 18.3 KB
/
bootstrap.sh
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
#!/bin/bash
# 2019'10, [email protected]
#
# Evironment variables
#
# CMVERBOSE : increase verbosity (set any value)
# CMISO : official ISO to use (i.e. CentOS-8.1.1911-x86_64-boot.iso)
# CMOUT : resultig ISO file name (i.e. CentOS-8.1.1911-x86_64-minimal.iso)
# CMETH : dependency resolving method to use (deep or fast)
#
# Default values
#
# default official ISO to use
iso="CentOS-8.1.1911-x86_64-boot.iso"
#
# resulting ISO file name and volume label
# such values will be determined again according to source image during ISO mount
out="CentOS-8.1.1911-x86_64-minimal.iso"
lbl="CentOS-8-1-1911-x86_64"
#
# dependency resolving method
# deep: check dependency of every package one by one
# fast: check core package depedencies only
met="fast"
#
# no need to change further
pw="$(pwd)"
dp="${pw}/image"
md="${pw}/mtemp"
bo="${dp}/BaseOS"
function cmusage() {
echo "Usage: ${0} <run [force] | clean | debug [package [package ..]] | step ..>"
echo
exit 1
}
function cmusagestep() {
echo "Usage: ${0} step .."
echo
echo " Workflow steps:"
echo " isomount"
echo " createtemplate"
echo " scandeps"
echo " createrepo"
echo " createiso"
echo " isounmount"
echo
echo " Some usefull functions:"
echo " rpmname <package> [package ..]"
echo " rpmurl <package> [package ..]"
echo " rpmdownload <package> [package ..]"
echo " fulldeps <package> [package ..]"
echo
exit 1
}
function cmnotcentos() {
echo
echo " ! This script is not suitable to use in this platform"
echo
exit 1
}
function cmcheck() {
if [ "${PIPESTATUS[0]}" != "0" ]; then
exit 1
fi
}
function cmpipe() {
while read line; do
echo " ${1}${line}"
done
}
function cmdot() {
if [ "${CMVERBOSE}" != "" ]; then
cmpipe
else
echo -n " "
while read line; do
echo -n "."
done
echo " done"
fi
}
function cmisounmount() {
if [ -d "${md}" ]; then
echo -n " ~ unmount ISO .."
umount "${md}" 2>/dev/null
rmdir "${md}"
echo " done"
fi
}
function cmisomount() {
if [ ! -e "${iso}" ]; then
echo
echo " ! Reference ISO (${iso}) not found."
echo
echo " You can download CentOS 8 from following resource;"
echo " http://isoredirect.centos.org/centos/8/isos/x86_64/"
echo
echo " If you want to use different minor release, please"
echo " specify it like below;"
echo
echo " CMISO='/path/to/file.iso' ./bootstrap.sh .."
echo
exit 1
fi
cmisounmount
echo " ~ mount ISO "
if [ ! -d "${md}" ]; then
mkdir -p "${md}"
mount -o loop "${iso}" "${md}" 2>&1 | cmpipe
cmcheck
echo " ${md} mounted"
if [ "$(cat "${md}/isolinux/isolinux.cfg" | grep "CentOS Linux 8")" == "" ]; then
cmisounmount
echo
echo " ! Reference ISO should be one of the CentOS 8 distribution."
echo
exit
fi
fi
}
function cmclean() {
cmisounmount
rm -rf "${dp}"
rm -f target_comps.xml "${out}" .[cpmrdtfu]*
}
function cmcreatetemplate() {
if [ ! -d "${md}" ]; then
if [ "${CMSTEP}" != "" ]; then
echo " ! ISO not mounted, please run;"
echo " ${0} step isomount"
echo
fi
return
fi
echo -n " ~ Preparing image template "
echo -n "."
mkdir -p "${dp}"
mkdir -p "${bo}/Packages"
echo -n "."
cp -r "${md}/EFI" "${dp}/"
cmcheck
echo -n "."
cp "templ_discinfo" "${dp}/.discinfo"
cp "templ_media.repo" "${dp}/media.repo"
echo -n "."
cp -r "${md}/isolinux" "${dp}/"
echo -n "."
cp -r "${md}/images" "${dp}/"
cmcheck
rm -f "${dp}/.treeinfo"
touch "${dp}/.treeinfo"
while IFS= read line; do
imgf="$(echo "${line}" | grep "^images/" | awk -F" = " {'print $1'})"
if [ "${imgf}" != "" ]; then
if [ ! -e "${dp}/${imgf}" ]; then
echo
echo
echo " ! Image '${imgf}' not found in base ISO"
echo
exit 1
fi
sum="$(sha256sum "${dp}/${imgf}" | awk {'print $1'})"
echo "${imgf} = sha256:${sum}" >> "${dp}/.treeinfo"
echo -n "."
else
echo "${line}" >> "${dp}/.treeinfo"
fi
done < "templ_treeinfo"
if [ -e "${md}/.treeinfo" ]; then
ts="$(cat ${md}/.treeinfo | grep "timestamp = " | head -1 | awk -F"= " {'print $2'} | tr -d "\n\r")"
if [ "${ts}" != "" ]; then
sed -i "s/\\(timestamp = \\)[0-9]\\+/\\1${ts}/g" "${dp}/.treeinfo"
fi
fi
if [ -e "${md}/.discinfo" ]; then
ts="$(head -1 ${md}/.discinfo | tr -d "\n\r")"
if [ "${ts}" != "" ]; then
sed -i "s/[0-9]\\+\.[0-9]\\+/${ts}/g" "${dp}/.discinfo"
fi
fi
echo " done"
}
function resolvefast() {
# input arguments
# package [package ..]
tf="${CMTEMP}"
vb="${CMVERBOSE}"
if [ "${vb}" != "" ]; then
echo "${@}" | tr " " "\n" | cmpipe " "
fi
repoquery --requires --resolve --recursive "${@}" 2>/dev/null | \
awk -F":" {'print $1'} | \
sed 's/\-[0-9]\+$//g' | \
sort | uniq | \
grep -v "glibc-all-langpacks\|glibc-langpack-[a-z0-9]\+$" \
>> "${tf}"
}
function resolvedeep() {
# input arguments
# package [package ..]
s="${CMSEP}-"
tf="${CMTEMP}"
vb="${CMVERBOSE}"
repoquery --requires --resolve "${@}" 2>/dev/null | \
awk -F":" {'print $1'} | \
sed 's/\-[0-9]\+$//g' | \
sort | uniq | \
while read line; do
if [ "${line}" == "glibc-all-langpacks" -o "$(echo "${line}" | grep "glibc-langpack-[a-z0-9]\+$")" != "" ]; then
if [ "${vb}" != "" ]; then
echo " skip: ${@} ${line}"
fi
continue
fi
if [ "$(cat "${tf}" | grep "^${line}$")" == "" ]; then
echo "${s} ${line}" >> .tree
echo "${line}" >> "${tf}"
if [ "${vb}" != "" ]; then
echo " package: ${@} ${line}"
else
echo -n ","
fi
CMSEP="${s}" resolvedeep "${line}"
fi
done
}
function cmfulldeps() {
# input arguments
# package [package ..]
if [ "${1}" == "" ]; then
echo "Usage: ${0} step fulldeps <package> [package ..]"
echo
exit
fi
rm -f ".pkgs" ".tree"
touch ".pkgs" ".tree"
echo " ~ Resolving dependencies for ${@}"
if [ "${met}" == "deep" ]; then
CMVERBOSE=1 CSEP=" " CMTEMP=".pkgs" resolvedeep "${@}"
else
rm -f .fast
CMVERBOSE=1 CMTEMP=".fast" resolvefast "${@}"
cat .fast | sort | uniq > .pkgs
rm -f .fast
fi
echo " ~ Full dependency list of ${1}"
cat .pkgs | sort | cmpipe " "
}
function cmcreatelist() {
echo -n " ~ Creating package list "
rm -f .core
echo -n "."
cat templ_comps.xml | grep packagereq | awk -F">" {'print $2'} | awk -F"<" {'print $1'} > .core
echo -n "."
cat packages.txt | grep -v "^#" | grep -v "^$" >> .core
echo " done"
tp="$(cat .core | sort | uniq | wc -l)"
echo " ~ Resolving dependencies for ${tp} package(s)"
if [ "${CMVERBOSE}" == "" ]; then
echo -n " "
fi
rm -f .tree .pkgs
touch .pkgs
cat .core | sort | uniq | while read line; do
if [ "${met}" == "deep" ]; then
if [ "${CMVERBOSE}" != "" ]; then
CMTEMP=".pkgs" CMSEP=" " resolvedeep "${line}"
fi
fi
echo "${line}" >> .pkgs
if [ "${CMVERBOSE}" == "" ]; then
echo -n "."
fi
done
if [ "${met}" == "deep" ]; then
if [ "${CMVERBOSE}" == "" ]; then
CMTEMP=".pkgs" CMSEP=" " resolvedeep $(cat .core | sort | uniq | tr "\n" " ")
fi
else
CMTEMP=".pkgs" resolvefast $(cat .core | sort | uniq | tr "\n" " ")
fi
rm -f .core
cat .pkgs | sort | uniq > .pkgf
mv .pkgf .pkgs
if [ "${CMVERBOSE}" == "" ]; then
echo " done"
fi
}
function cmrpmdownload() {
# input arguments
# package [package ..]
if [ "${1}" == "" ]; then
echo "Usage: ${0} rpmdownload <package>"
echo
exit 1
fi
mkdir -p rpms
yumdownloader --urlprotocol http --urls "${@}" 2>/dev/null | \
grep "^http" | \
sort | uniq | \
while read u; do
if [ "${u}" != "" ]; then
f=`echo "${u}" | awk -F"/" {'print $NF'}`
if [ -e "rpms/${f}" ]; then
if [ "$(file "rpms/${f}" | grep "RPM ")" != "" ]; then
echo " - exists (rpms/${f})"
continue
fi
rm -f "rpms/${f}"
fi
echo " ${f} [${u}]"
curl -s "${u}" -o "rpms/${f}"
if [ "${?}" == "0" ]; then
if [ "$(file "rpms/${f}" | grep "RPM ")" == "" ]; then
rm -f "rpms/${f}"
echo " ! failed"
fi
else
rm -f "rpms/${f}"
echo " ! failed"
fi
fi
done
}
function rpmdownload() {
# input arguments
# package [package ..]
if [ "${1}" == "" ]; then
echo " ! Pacakge name required for rpmdownload"
echo
exit 1
fi
ul="${CMURL}"
if [ "${ul}" == "" ]; then
ul="$(yumdownloader --urlprotocol http --urls "${@}" 2>/dev/null | \
grep "^http" | \
sort | uniq)"
fi
mkdir -p rpms
echo "${ul}" | while read u; do
if [ "${u}" != "" ]; then
f=`echo "${u}" | awk -F"/" {'print $NF'}`
if [ -e "rpms/${f}" ]; then
if [ "$(file "rpms/${f}" | grep "RPM ")" != "" ]; then
echo "${f}"
continue
fi
rm -f "rpms/${f}"
fi
if [ -e "cache/${f}" ]; then
cp "cache/${f}" "rpms/"
echo "${f}"
continue
fi
curl -s "${u}" -o "rpms/${f}"
if [ "${?}" == "0" ]; then
if [ "$(file "rpms/${f}" | grep "RPM ")" != "" ]; then
echo "${u}" >> .dlrpm
echo "${f}"
else
rm -f "rpms/${f}"
echo "${u} -> ${f}" > .dler
fi
else
rm -f "rpms/${f}"
echo "${u} -> ${f}" > .dler
fi
fi
done
}
function cmrpmurl() {
# input arguments
# package [package ..]
if [ "${CMSTEP}" != "" -a "${1}" == "" ]; then
echo "Usage: ${0} step rpmurl <package> [package ..]"
echo
exit 1
fi
yumdownloader --urlprotocol http --urls "${@}" | \
grep "^http" | \
sort | uniq > "${pw}/.urls"
}
function cmrpmname() {
# input arguments
# package [package ..]
if [ "${CMSTEP}" != "" -a "${1}" == "" ]; then
echo "Usage: ${0} step rpmname <package> [package ..]"
echo
exit 1
fi
repoquery "${@}" 2>/dev/null | \
sed 's/\-[0-9]\+:/\-/g' | \
awk {'print $1".rpm"'} | \
sort | uniq
}
function cmcollectrpm() {
# input arguments
# package [package ..]
vb="${CMVERBOSE}"
if [ "${CMSTEP}" != "" -a "${1}" == "" ]; then
echo "Usage: ${0} step collectrpm <package> [package ..]"
echo
exit 1
fi
cmrpmurl "${@}"
dl="$(cat "${pw}/.urls")"
rr="$(echo "${dl}" | awk -F"/" {'print $NF'} | sort | uniq)"
if [ "${rr}" != "" ]; then
mkdir -p rpms
echo "${rr}" | while read r; do
if [ -e "rpms/${r}" ]; then
if [ -d "${bo}/Packages" ]; then
cp "rpms/${r}" "${bo}/Packages/"
fi
if [ "${vb}" != "" ]; then
echo " cached: ${r}"
else
echo -n "."
fi
else
pk="$(echo "${r}" | awk -F".el8" {'print $1'} | sed 's/\-[0-9\.\-]\+$//g')"
fu="$(echo "${dl}" | grep "/${r}$")"
if [ "${fu}" == "" ]; then
ir="$(echo "${r}" | sed 's/\.x86_64/\.i686/g')"
fu="$(echo "${dl}" | grep "/${ir}$")"
fi
if [ "$(echo "${fu}" | wc -l)" != "1" ]; then
fu=""
rp="$(echo "${r}" | awk -F".rpm" {'print $1'})"
pk="$(dnf info "${rp}" | grep "^Name" | awk -F": " {'print $2'} | sort | uniq)"
fi
if [ "${vb}" != "" ]; then
echo "downloading: ${pk}, ${r}"
fi
dd="$(CMURL="${fu}" rpmdownload "${pk}")"
if [ "${dd}" == "" ]; then
echo "${pk}:${r}:<none>" >> .miss
if [ "${vb}" != "" ]; then
echo " not found: ${r} (${pk})"
else
echo -n "!"
fi
else
echo "${dd}" | while read d; do
if [ "${d}" != "${r}" ]; then
if [ -d "${bo}/Packages" ]; then
cp "rpms/${d}" "${bo}/Packages/"
fi
if [ "${vb}" != "" ]; then
echo " dowmloaded: ${r} -> ${d}, ${pk}"
else
echo -n ":"
fi
else
if [ -d "${bo}/Packages" ]; then
cp "rpms/${d}" "${bo}/Packages/"
fi
if [ "${vb}" == "" ]; then
echo -n ":"
fi
fi
done
fi
fi
done
else
echo "${@}" >> .rslv
args="${@}"
if [ "${args}" != "" ]; then
echo " unresolved: ${args}"
else
echo -n "!"
fi
fi
}
function cmcollectrpms() {
tp="$(cat .pkgs | sort | uniq | wc -l)"
echo " ~ Searching RPMs for ${tp} package(s)"
if [ "${CMVERBOSE}" == "" ]; then
echo -n " "
fi
rm -f .miss .rslv .dler
mkdir -p rpms
cmcollectrpm $(cat .pkgs | sort | uniq | tr "\n" " ")
if [ "${CMVERBOSE}" == "" ]; then
echo " done"
fi
}
function cmcreaterepo() {
if [ ! -d "${bo}/Packages" ]; then
echo " ! Image temmplate is not ready, please run;"
echo " ${0} step createtemplate"
echo " ${0} step scandeps"
echo
exit 1
fi
tp="$(cat "${pw}/packages.txt" | grep -v "^#" | grep -v "^$" | wc -l)"
echo -n " ~ Creating component list for ${tp} add-on package "
uc="${pw}/target_comps.xml"
rm -f "${uc}"
touch "${uc}"
while IFS= read xl; do
if [ "${xl}" == "" ]; then
cat "${pw}/packages.txt" | grep -v "^#" | grep -v "^$" | while read line; do
echo " <packagereq type=\"default\">${line}</packagereq>" >> "${uc}"
echo -n "."
done
else
echo "${xl}" >> "${uc}"
fi
done < "${pw}/templ_comps.xml"
echo " done"
echo " ~ Creating repodata "
cd "${bo}"
cmcheck
rm -rf repodata
createrepo -g "${uc}" . 2>&1 | cmdot
cmcheck
cd "${pw}"
rm -f "${uc}"
}
function cmcreateiso() {
if [ ! -d "${bo}/repodata" ]; then
echo " ! Repo is not ready, please run;"
echo " ${0} step createrepo"
echo
exit 1
fi
lbl="$(cat "${dp}/isolinux/isolinux.cfg" | grep "LABEL=" | awk -F"LABEL=" {'print $2'} | awk {'print $1'} | grep -v "^$" | head -1 | tr -d "\n\r")"
if [ "${CMOUT}" == "" ]; then
ver="$(cat "${dp}/isolinux/isolinux.cfg" | grep "LABEL=CentOS" | head -1 | awk -F"LABEL=CentOS-" {'print $2'} | awk -F"-x86_64" {'print $1'} | sed 's/\-/\./g')"
if [ "${ver}" == "8.BaseOS" ]; then
ver="8.0.1905"
elif [ "${ver}" == "Stream.8" ]; then
ver="8.0.20191219"
fi
out="CentOS-${ver}-x86_64-minimal.iso"
fi
echo " ~ Creating ISO image"
cd "${dp}"
chmod 664 isolinux/isolinux.bin
rm -f "${pw}/${out}"
mkisofs \
-input-charset utf-8 \
-o "${pw}/${out}" \
-b isolinux/isolinux.bin \
-c isolinux/boot.cat \
-no-emul-boot \
-V "${lbl}" \
-boot-load-size 4 \
-boot-info-table \
-eltorito-alt-boot \
-e images/efiboot.img \
-no-emul-boot \
-R -J -v -T . 2>&1 | cmdot
cmcheck
if [ -e "/usr/bin/isohybrid" ]; then
echo " ~ ISO hybrid"
isohybrid --uefi "${pw}/${out}" | cmdot
cmcheck
fi
if [ -e "/usr/bin/implantisomd5" ]; then
echo " ~ Implant ISO MD5"
implantisomd5 --force --supported-iso "${pw}/${out}" | cmdot
cmcheck
fi
cd "${pw}"
isz="$(du -h "${out}" | awk {'print $1'})"
echo " ~ ISO image ready: ${out} (${isz})"
}
function cmjobsingle() {
# input arguments
# package [package ..]
rm -f .[cpmrdtf]*
touch .pkgs .tree
echo " ~ Creating package list for ${@} "
if [ "${met}" == "deep" ]; then
CMVERBOSE=1 CMTEMP=".pkgs" CMSEP=" " resolvedeep "${@}"
else
rm -f .fast
CMVERBOSE=1 CMTEMP=".fast" resolvefast "${@}"
cat .fast | sort | uniq > .pkgs
fi
echo " ~ Package with dependencies"
cat .pkgs | cmpipe " "
if [ "${met}" == "deep" ]; then
echo " ~ Dependency tree"
cat .tree | cmpipe " "
fi
echo " ~ Searching RPMs"
CMVERBOSE=1 cmcollectrpm $(cat .pkgs | sort | uniq | tr "\n" " ")
}
function cmscandeps() {
cmcreatelist
cmcollectrpms
}
function cmjobfull() {
cmclean
cmisomount
cmcreatetemplate
cmscandeps
cmcreaterepo
cmcreateiso
cmisounmount
}
function cmjobquick() {
if [ "${CMISO}" != "" ]; then
cmisomount
fi
cmcreatetemplate
cmcreaterepo
cmcreateiso
cmisounmount
}
if [ ! -e /etc/centos-release ]; then
cmnotcentos
fi
if [ "$(cat /etc/centos-release | grep "CentOS Linux release 8")" == "" ]; then
cmnotcentos
fi
if [ ! -e "/usr/bin/repoquery" -o ! -e "/usr/bin/createrepo" -o ! -e "/usr/bin/yumdownloader" -o ! -e "/usr/bin/curl" -o ! -e "/usr/bin/mkisofs" ]; then
echo
echo " ! Some additional packages needs to be installed."
echo " Please run following command to have them all:"
echo
echo " yum -y install yum-utils createrepo syslinux genisoimage isomd5sum bzip2 curl file"
echo
exit 1
fi
if [ "${CMISO}" != "" ]; then
iso="${CMISO}"
fi
if [ "${CMOUT}" != "" ]; then
out="${CMOUT}"
fi
if [ "${CMETH}" != "" ]; then
met="${CMETH}"
fi
if [ ! -e "packages.txt" ]; then
touch "packages.txt"
fi
if [ "${1}" == "run" ]; then
shift
if [ "${1}" == "force" ]; then
cmjobfull
elif [ -d "${bo}/Packages" ]; then
cmjobquick
else
cmjobfull
fi
elif [ "${1}" == "clean" ]; then
cmclean
elif [ "${1}" == "debug" ]; then
shift
if [ "${1}" == "" ]; then
cmusage
fi
cmjobsingle "${@}"
elif [ "${1}" == "step" ]; then
shift
if [ "${1}" == "" ]; then
cmusagestep
fi
cmd="cm${1}"
shift
CMVERBOSE=1 CMSTEP=1 ${cmd} "${@}"
else
cmusage
fi