-
Notifications
You must be signed in to change notification settings - Fork 0
/
iso-helper.sh
executable file
·1155 lines (998 loc) · 35.4 KB
/
iso-helper.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
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
#!/bin/bash
# shellcheck disable=SC2034
# @Author: Wang Hong
# @Date: 2022-10-22 12:38:37
# @Last Modified by: Wang Hong
# @Last Modified time: 2024-09-03 10:19:43
# set -e
Version=1.5.8
ExecDir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
WorkDir=$(pwd)
LiveCDRoot=${WorkDir}
RootDir=${WorkDir}/squashfs-root
SquashfsFile=${WorkDir}/filesystem.squashfs
FileSystemSize=${WorkDir}/filesystem.size
FileSystemManifest=${WorkDir}/filesystem.manifest
ISOExcludeList=(
squashfs-root
squashfs-root.bk
squashfs-root.bak
squashfs-root.back
squashfs-root.backup
squashfs-root.old
filesystem.squashfs.bk
filesystem.squashfs.bak
filesystem.squashfs.back
filesystem.squashfs.backup
filesystem.squashfs.old
initrd.dir
initrd.tmp
initrd.lz.bk
initrd.lz.bak
initrd.lz.back
initrd.lz.backup
initrd.lz.old
initrd.img.bk
initrd.img.bak
initrd.img.back
initrd.img.backup
initrd.img.old
)
# Echo Color Settings
C_CLR="\\e[0m"
C_H="\\e[1m"
C_R="\\e[31m"
C_G="\\e[32m"
C_B="\\e[36m"
C_Y="\\e[33m"
C_HR="${C_H}${C_R}"
C_HG="${C_H}${C_G}"
C_HB="${C_H}${C_B}"
C_HY="${C_H}${C_Y}"
C_OK="${C_G}OK${C_CLR}"
C_FL="${C_R}FAILED${C_CLR}"
C_WARN="${C_Y}WARNING${C_CLR}"
C_ERROR="${C_R}ERROR${C_CLR}"
# Cmd Hook Methods:
# hook_mkdir() {
# echo "$@"
# /usr/bin/mkdir "$@"
# }
# alias mkdir='hook_mkdir'
# Usage: Caller <Prefix> <Desc> <Cmd>
Caller() {
Usage="Caller <Prefix> <Desc> <Cmd>"
if [ $# -lt 2 ] || [ -z "$1" ] || [ -z "$2" ]; then
echo "${Usage}"
return 1
fi
local Prefix=$1
local Desc=$2
shift 2
local Cmd=$*
local ReturnCode=0
local LogFile=/tmp/.caller.log
echo -en "${C_HB}${Prefix}${C_CLR}: ${Desc}"
if eval "${Cmd}" > "${LogFile}" 2>&1; then
ReturnCode=$?
echo -e "[${C_OK}]"
else
ReturnCode=$?
echo -e "[${C_FL}]"
cat "${LogFile}"
fi
rm -f "${LogFile}"
return ${ReturnCode}
}
CheckPrivilege() {
if [ ${UID} -ne 0 ]; then
echo -e "Please run this script with ${C_HY}root${C_CLR} privileges."
return 1
else
return 0
fi
}
CheckQemuBinfmtSupport() {
if ! ls "/usr/share/binfmts/qemu-*" > /dev/null 2>&1; then
echo -e "Please install [${C_R}qemu-user${C_CLR} or ${C_R}qemu-user-static${C_CLR}] first"
return 1
fi
}
CheckBuildEnvironment() {
Utils=(blkid lsblk losetup parted mkfs.ext4 mkfs.fat mksquashfs findmnt)
for Util in "${Utils[@]}"; do
if ! which "${Util}" >/dev/null 2>&1; then
echo -e "Please install [${C_R}${Util}${C_CLR}] first"
return 1
fi
done
# CheckQemuBinfmtSupport
return 0
}
GenUUID() {
local UUID=4c0efd70-04f4-45c4-88f3-fa943249da15
if which uuid >/dev/null 2>&1; then
UUID=$(uuid)
else
UUID=$(cat /proc/sys/kernel/random/uuid)
fi
[ -n "${UUID}" ] || UUID=4c0efd70-04f4-45c4-88f3-fa943249da15
echo "${UUID}"
}
# Usage: IsTargetMounted <Target>
IsTargetMounted() {
local Usage="Usage: IsTargetMounted <Target>"
if [ $# -ne 1 ] || [ -z "$1" ]; then
echo -e "${Usage}"
return 1
fi
local Target=$1
if [ -d "${Target}" ]; then
mountpoint -q "${Target}"
elif [ -f "${Target}" ] || [ -L "${Target}" ]; then
/bin/grep -q "${Target}" "/proc/mounts"
else
return 1
fi
}
# Usage: GetTargetMountPoint <Target>
GetTargetMountPoint() {
local Usage="Usage: GetTargetMountPoint <Target>"
if [ $# -ne 1 ] || [ -z "$1" ]; then
echo -e "${Usage}"
return 1
fi
local Target=$1
local MountedDir
if [ -e "${Target}" ]; then
echo "Target:[${Target}] Not Exist!"
return 1
fi
IsTargetMounted "${Target}" || return 1
if ! MountedDir=$(lsblk -n -o MOUNTPOINT "${Target}"); then
return 1
fi
[ -n "${MountedDir}" ] || return 1
echo "${MountedDir}"
}
# Usage: Mount [-c <RootDir>] [-t <Type> | -b] <Source> <Target>
Mount() {
local Usage="Usage: Mount [-c <RootDir>] [-t <Type> | -b] <Source> <Target>"
local Prefix=""
local Options=""
local RootDir=""
while [ $# -ne 0 ]
do
case $1 in
-c|--chroot)
RootDir=$2
Prefix="${Prefix:+${Prefix} }chroot ${RootDir}"
shift 2
;;
-t|--types)
local Type=$2
Options="${Options:+${Options} }--types ${Type}"
shift 2
;;
-b|--bind)
Options="${Options:+${Options} }--bind"
shift
;;
-r|-ro|--readonly)
Options="${Options:+${Options} }--options ro"
shift
;;
*)
if [ $# -ne 2 ]; then
echo -e "${Usage}"
return 1
fi
local Source=$1
local Target=$2
shift 2
;;
esac
done
if [ -z "${Source}" ] || [ -z "${Target}" ]; then
echo -e "${Usage}"
return 1
fi
if eval "${Prefix}" mountpoint -q "${Target}"; then
return 0
fi
# When ${Target} dir is a symbolic link and point to ${Source} dir, skip this mount.
if [ -L "${Target}" ] && [ "$(realpath "${Target}")" = "${Source}" ]; then
return 0
fi
local Desc Cmd
Desc="${Options:+[${C_G}${Options}${C_CLR}] }${C_Y}${Source##*${WorkDir}/}${C_CLR} --> ${C_B}${Target##*${WorkDir}/}${C_CLR} ... "
Cmd="${Prefix} mount ${Options} \"${Source}\" \"${Target}\""
Caller "MOUNT" "${Desc}" "${Cmd}"
}
# Usage: ReleaseRes <RootDir>
ReleaseRes() {
local Usage="Usage: ReleaseRes <RootDir>"
if [ $# -ne 1 ] || [ -z "$1" ]; then
echo -e "${Usage}"
return 1
fi
local RootDir=$1
local ResUsers
# Stop All Process first!!! TODO: Test Code!
pushd "${RootDir}/.." > /dev/null || exit $?
read -ra ResUsers <<< "$(fuser -a "$(basename "${RootDir}")" 2>/dev/null | grep "$(basename "${RootDir}")" | awk -F':' '{print $2}')"
for ResUser in "${ResUsers[@]}";
do
local PID=${ResUser:0:-1}
pgrep "${PID}" > /dev/null && kill -9 "${PID}"
done
popd >/dev/null || exit $?
}
# Usage: UnMount [-c <RootDir>] <Directory>
UnMount() {
local Usage="Usage: UnMount [-c <RootDir>] <Directory>"
local Prefix=""
local RootDir=""
local Directory=""
while [ $# -ne 0 ]
do
case $1 in
-c|--chroot)
RootDir=$2
Prefix="${Prefix:+${Prefix} }chroot \"${RootDir}\""
shift 2
;;
*)
if [ $# -ne 1 ]; then
echo -e "${Usage}"
return 1
fi
Directory=$1
shift
;;
esac
done
if [ -z "${Directory}" ]; then
echo -e "${Usage}"
return 1
fi
local Desc Cmd
if eval "${Prefix}" umount --help | grep -iq "recursive"; then
if eval "${Prefix}" mountpoint -q "${Directory}"; then
Desc="[${C_G}Recursive${C_CLR}] ${C_Y}${Directory##*${WorkDir}/}${C_CLR} ... "
Cmd="${Prefix} umount -R \"${Directory}\""
if ! Caller "UMOUNT" "${Desc}" "${Cmd}"; then
Desc="[${C_Y}Retry${C_CLR}] ${Desc}"
Cmd="${Prefix} umount -Rl \"${Directory}\""
Caller "UMOUNT" "${Desc}" "${Cmd}"
fi
fi
else
dirlist=$(eval "${Prefix}" cat /proc/mounts | grep "${Directory}")
[ -n "${dirlist}" ] && return 0
for dir in ${dirlist}
do
if eval "${Prefix}" mountpoint -q "${dir}"; then
Desc="${C_Y}${dir##*${WorkDir}/}${C_CLR} ... "
Cmd="${Prefix} umount \"${dir}\""
if ! Caller "UMOUNT" "${Desc}" "${Cmd}"; then
Desc="[${C_Y}Retry${C_CLR}] ${Desc}"
Cmd="${Prefix} umount -l \"${Directory}\""
Caller "UMOUNT" "${Desc}" "${Cmd}"
fi
fi
done
fi
return 0
}
# Usage: MountCache <RootDir> <CacheDir>
MountCache() {
local Usage="Usage: MountCache <RootDir> <CacheDir>"
if [ $# -ne 2 ] || [ -z "$1" ] || [ -z "$2" ]; then
echo -e "${Usage}"
return 1
fi
local RootDir=$1
local CacheDir=$2
local RootAptCache=${RootDir}/var/cache/apt
local RootAptLists=${RootDir}/var/lib/apt/lists
local CacheAptCache=${CacheDir}/aptcache
local CacheAptLists=${CacheDir}/aptlists
mkdir -p "${CacheAptCache}" "${CacheAptLists}" "${RootAptCache}" "${RootAptLists}" || return 1
Mount --bind "${CacheAptCache}" "${RootAptCache}" || return 1
Mount --bind "${CacheAptLists}" "${RootAptLists}" || return 1
return 0
}
# Usage: UnMountCache <RootDir>
UnMountCache() {
local Usage="Usage: UnMountCache <RootDir>"
if [ $# -ne 1 ] || [ -z "$1" ]; then
echo -e "${Usage}"
return 1
fi
local RootDir=$1
local RootAptCache=${RootDir}/var/cache/apt
local RootAptLists=${RootDir}/var/lib/apt/lists
for dir in ${RootAptCache} ${RootAptLists}
do
UnMount "${dir}" || return 1
done
return 0
}
# Usage: MountSystemEntries <RootDir>
MountSystemEntries() {
local Usage="Usage: MountSystemEntries <RootDir>"
if [ $# -ne 1 ] || [ -z "$1" ]; then
echo -e "${Usage}"
return 1
fi
local RootDir=$1
mkdir -p "${RootDir}/proc" "${RootDir}/sys" "${RootDir}/dev" "${RootDir}/run" "${RootDir}/tmp" || return 1
if [ -x "${RootDir}/bin/mount" ]; then
Mount --chroot "${RootDir}" --types proc proc-chroot "/proc"
Mount --chroot "${RootDir}" --types sysfs sysfs-chroot "/sys"
Mount --chroot "${RootDir}" --types devtmpfs udev-chroot "/dev"
[ -d "${RootDir}/dev/pts" ] || mkdir -p "${RootDir}/dev/pts"
Mount --chroot "${RootDir}" --types devpts devpts-chroot "/dev/pts"
Mount --bind /run "${RootDir}/run"
Mount --bind /tmp "${RootDir}/tmp"
# Bind rootfs of host os to chroot environment
if which findmnt > /dev/null 2>&1; then
mkdir -p "${RootDir}/host" || return 1
findmnt -es --real -n -t "ext2,ext3,ext4,vfat,ntfs,xfs,btrfs" | awk '{print $1}' | while read -r MountPoint
do
Mount --readonly --bind "${MountPoint}" "${RootDir}/host${MountPoint%/}"
done
fi
else
echo -e "MOUNT: ${C_WARN} Please unpack rootfs package first."
return 99
fi
return 0
}
# Usage: UnMountSystemEntries <RootDir>
UnMountSystemEntries() {
local Usage="Usage: UnMountSystemEntries <RootDir>"
if [ $# -ne 1 ] || [ -z "$1" ]; then
echo -e "${Usage}"
return 1
fi
local RootDir=$1
for dir in sys dev/pts dev proc
do
UnMount --chroot "${RootDir}" "/${dir}" || return 1
done
for dir in host tmp run
do
UnMount "${RootDir}/${dir}" || return 1
done
rm -rf "${RootDir}/host"
return 0
}
# Usage: MountUserEntries <RootDir> <ExtPackageDir>
MountUserEntries() {
local Usage="Usage: MountUserEntries <RootDir> <ExtPackageDir>"
if [ $# -ne 1 ] || [ -z "$1" ]; then
echo -e "${Usage}"
return 1
fi
local RootDir=$1
local ExtPackageDir=$2
local UserDir=${RootDir}/data
for dir in home root var/log
do
mkdir -p "${RootDir}/${dir}" "${UserDir}/${dir}" || return 1
Mount --bind "${UserDir}/${dir}" "${RootDir}/${dir}" || return 1
done
# Mount ExtraPackage to rootfs/media
mkdir -p "${RootDir}/media/PackagesExtra" "${ExtPackageDir}"
Mount --bind "${ExtPackageDir}" "${RootDir}/media/PackagesExtra"
return 0
}
# Usage: UnMountUserEntries <RootDir>
UnMountUserEntries() {
local Usage="Usage: UnMountUserEntries <RootDir>"
if [ $# -ne 1 ] || [ -z "$1" ]; then
echo -e "${Usage}"
return 1
fi
local RootDir=$1
# Mount ExtraPackage to rootfs/media
UnMount "${RootDir}/media/PackagesExtra"
rm -rf "${RootDir}/media/PackagesExtra"
for dir in home root var/log
do
UnMount "${RootDir}/${dir}" || return 1
done
return 0
}
# Usage: MkSquashfs <Squashfs File> <RootDir>
MkSquashfs() {
local Usage="Usage: MkSquashfs <Squashfs File> <RootDir>"
if [ $# -ne 2 ] || [ -z "$1" ] || [ -z "$2" ]; then
echo -e "${Usage}"
return 1
fi
local Squashfs=$1
local RootDir=$2
if [ ! -d "${RootDir}" ]; then
echo "Cannot find Rootfs dir."
return 1
fi
local SQUASHFSARGS=''
SQUASHFSARGS="${SQUASHFSARGS:+${SQUASHFSARGS} }-b 1M"
# SQUASHFSARGS="${SQUASHFSARGS:+${SQUASHFSARGS} }-comp xz"
# SQUASHFSARGS="${SQUASHFSARGS:+${SQUASHFSARGS} }-processors 4"
local Desc Cmd
if [ -d "${RootDir}" ]; then
Desc="Removing exist squashfs file [${C_H}${Squashfs##*${WorkDir}/}${C_CLR}] ... "
Cmd="rm -f \"${Squashfs}\""
Caller "REMOVE" "${Desc}" "${Cmd}"
fi
Desc="${C_B}${RootDir##*${WorkDir}/}${C_CLR} --> ${C_H}${Squashfs##*${WorkDir}/}${C_CLR} ... "
Cmd="mksquashfs \"${RootDir}\" \"${Squashfs}\" ${SQUASHFSARGS}"
Caller "MKSQUASH" "${Desc}" "${Cmd}"
}
# Usage: UnSquashfs <Squashfs File>
UnSquashfs() {
local Usage="Usage: UnSquashfs <Squashfs File>"
if [ $# -ne 1 ] || [ -z "$1" ]; then
echo -e "${Usage}"
return 1
fi
local Squashfs=$1
local RootDir=squashfs-root
local Desc Cmd
if [ -d "${RootDir}" ]; then
Desc="Removing exist RootDir [${C_B}${RootDir##*${WorkDir}/}${C_CLR}] ... "
Cmd="rm -rf \"${RootDir}\""
Caller "REMOVE" "${Desc}" "${Cmd}"
fi
Desc="${C_H}${Squashfs##*${WorkDir}/}${C_CLR} --> ${C_B}${RootDir##*${WorkDir}/}${C_CLR} ... "
Cmd="unsquashfs \"${Squashfs}\""
Caller "UNSQUASH" "${Desc}" "${Cmd}"
}
# Usage: GenFileSystemSize <FileSystem Size File> <RootDir>
GenFileSystemSize() {
local Usage="Usage: GenFileSystemSize <FileSystem Size File> <RootDir>"
if [ $# -ne 2 ] || [ -z "$1" ] || [ -z "$2" ]; then
echo -e "${Usage}"
return 1
fi
local FileSystemSize=$1
local RootDir=$2
if [ ! -d "${RootDir}" ]; then
echo -e "Cannot find Rootfs dir."
return 1
fi
[ -f "${FileSystemSize}" ] && rm -f "${FileSystemSize}"
local Desc Cmd
Desc="Calculating ${C_B}${RootDir##*${WorkDir}/}${C_CLR} Size ... "
Cmd="du -sx --block-size=1 \"${RootDir}\" | cut -f1 > \"${FileSystemSize}\""
Caller "GEN SIZE" "${Desc}" "${Cmd}"
}
# Usage: GenFileSystemManifest <FileSystem Manifest File> <RootDir>
GenFileSystemManifest() {
local Usage="Usage: GenFileSystemManifest <FileSystem Manifest File> <RootDir>"
if [ $# -ne 2 ] || [ -z "$1" ] || [ -z "$2" ]; then
echo -e "${Usage}"
return 1
fi
local FileSystemManifest=$1
local RootDir=$2
if [ ! -d "${RootDir}" ]; then
echo -e "Cannot find Rootfs dir."
return 1
fi
[ -f "${FileSystemManifest}" ] && rm -f "${FileSystemManifest}"
local Desc Cmd
Desc="Calculating ${C_B}${RootDir##*${WorkDir}/}${C_CLR} Manifest ... "
Cmd="chroot \"${RootDir}\" dpkg-query -W > \"${FileSystemManifest}\""
Caller "GEN MANIFEST" "${Desc}" "${Cmd}"
}
# Usage: GenSums <Sum Type: md5 | sha256> <Live CD Root Dir>
GenSums() {
local Usage="Usage: GenSums <Sum Type: md5 | sha256> <Live CD Root Dir>"
if [ $# -ne 2 ] || [ -z "$1" ] || [ -z "$2" ]; then
echo -e "${Usage}"
return 1
fi
local SumType=$1
local LiveCDRoot=$2
local ReturnCode=0
local SUM_TOOL=''
local SUM_FILE=''
if [ -z "${LiveCDRoot}" ]; then
echo -e "Live CD root [${LiveCDRoot}] is empty!"
echo -e "${Usage}"
return 1
fi
if [ ! -d "${LiveCDRoot}" ]; then
echo -e "Live CD root [${LiveCDRoot}] is not exist or a directory!"
return 1
fi
# Push ISO Root to build
pushd "${LiveCDRoot}" >/dev/null || exit $?
local Exclude=''
Exclude="${Exclude:+${Exclude}|}isolinux/boot.cat"
Exclude="${Exclude:+${Exclude}|}$(basename "${RootDir}")"
for Ex in "${ISOExcludeList[@]}";
do
Exclude="${Exclude:+${Exclude}|}${Ex}"
done
local Desc Cmd
case $SumType in
md5|md5sum)
SUM_TYPE_STR="MD5SUM"
SUM_TOOL=md5sum
SUM_FILE="md5sum.txt"
;;
sha256|sha256sum)
SUM_TYPE_STR="SHA256SUM"
SUM_TOOL=sha256sum
SUM_FILE="SHA256SUMS"
;;
*)
;;
esac
[ -f "${SUM_FILE}" ] && rm -f "${SUM_FILE}"
Desc="Calculating ${C_B}$(basename "$(pwd)")${C_CLR} ${SUM_TYPE_STR} ... "
Cmd="find . -type f -print0 | grep -vzE "\"${Exclude}\"" | xargs -0 \"${SUM_TOOL}\" | tee \"${SUM_FILE}\""
if ! Caller "GEN ${SUM_TYPE_STR}" "${Desc}" "${Cmd}"; then
ReturnCode=1
fi
popd >/dev/null || return $?
return ${ReturnCode}
}
# Usage: PrepareExcludes <Backup|Restore> <ISO Root Dir> <UUID> <Exclude List>
PrepareExcludes() {
local Usage="Usage: PrepareExcludes <Backup|Restore> <ISO Root Dir> <UUID> <Exclude List>"
if [ $# -lt 3 ] || [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
echo -e "${Usage}"
return 1
fi
local Method=$1
local ISORootDir=$2
local UUID=$3
local ExcludeList
read -ra ExcludeList <<< "$@"
if [ ! -d "${ISORootDir}" ]; then
echo -e "ISO Root Dir [${ISORootDir}] is not exist or a directory!"
return 1
fi
local ISORootDirAbs
local ISORootParentDirAbs
local BackupDirAbs
ISORootDirAbs=$(cd "${ISORootDir}" && pwd)
ISORootParentDirAbs=$(dirname "${ISORootDirAbs}")
BackupDirAbs=${ISORootParentDirAbs}/.${UUID}
local ItemAbs Item ItemDirAbs ItemDir TargetDir
case ${Method} in
Backup)
for Exclude in "${ExcludeList[@]}";
do
find "${ISORootDirAbs}" -name "${Exclude}" | while read -r ItemAbs;
do
Item=${ItemAbs#*"${ISORootDirAbs}"/}
ItemDirAbs=$(dirname "${ItemAbs}")
ItemDir=${ItemDirAbs#*"${ISORootDirAbs}"/}
TargetDir=${BackupDirAbs}/${ItemDir}
echo -e "Found Exclude File: [${C_B}${Item}${C_CLR}], Backup it to [${C_HY}${UUID}${C_CLR}]"
mkdir -p "${TargetDir}"
mv -f "${ItemAbs}" "${TargetDir}"
done
done
;;
Restore)
if [ -d "${BackupDirAbs}" ]; then
for Exclude in "${ExcludeList[@]}";
do
find "${BackupDirAbs}" -name "${Exclude}" | while read -r ItemAbs;
do
Item=${ItemAbs#*"${BackupDirAbs}"/}
ItemDirAbs=$(dirname "${ItemAbs}")
ItemDir=${ItemDirAbs#*"${BackupDirAbs}"/}
TargetDir=${ISORootDirAbs}/${ItemDir}
echo -e "Found Exclude File Backup: [${C_HY}${UUID}${C_CLR}]: [${C_B}${Item}${C_CLR}]"
mkdir -p "${TargetDir}"
mv -f "${ItemAbs}" "${TargetDir}"
done
done
rm -rf "${BackupDirAbs}"
fi
;;
*)
echo -e "${Usage}"
return 1
;;
esac
}
# Usage: GetDebFileInfo <Section Title> <Live CD Root>
function GetDebFileInfo() {
local Usage="Usage: GetDebFileInfo <Section Title> <Live CD Root>"
if [ $# -ne 2 ] || [ -z "$1" ] || [ -z "$2" ]; then
echo -e "${Usage}"
return 1
fi
local SectionTitle=$1
local LiveCDRoot=$2
echo "##### ${SectionTitle} #####"
if [ -d "${LiveCDRoot}" ]; then
find "${LiveCDRoot}" -type f -name "*.deb" | while read -r line; do
echo "##### ${SectionTitle}.$(basename "${line}") #####"
dpkg-deb --info "${line}" | grep -E "Package:|Version:|Architecture:|Description:"
done
fi
}
# Usage: GenerateISOInfo <ISO File>
function GenerateISOInfo() {
local Usage="Usage: GenerateISOInfo <ISO File>"
if [ $# -ne 1 ] || [ -z "$1" ]; then
echo -e "${Usage}"
return 1
fi
local ISOFile=$1
local ISOBaseName=${ISOFile%.iso}
local ISOInfoFile=${ISOBaseName}.info
local ISOMountDir=${ISOBaseName}
local FSMountDir=${ISOBaseName}.filesystem.squashfs
local User
if [ -n "${SUDO_USER}" ]; then
User=${SUDO_USER}
else
User=${USER}
fi
rm -f "${ISOInfoFile}"
if [ -f "${ISOFile}" ]; then
echo "##### ${ISOFile} #####" >> "${ISOInfoFile}"
if test -d "${FSMountDir}"; then
mountpoint -q "${FSMountDir}" && umount "${FSMountDir}"
fi
if test -d "${ISOMountDir}"; then
mountpoint -q "${ISOMountDir}" && umount "${ISOMountDir}"
fi
mkdir -p "${ISOMountDir}" "${FSMountDir}"
mount -o loop,ro "${ISOFile}" "${ISOMountDir}"
mount "${ISOMountDir}/casper/filesystem.squashfs" "${FSMountDir}"
{
echo "##### cdrom.casper.filesystem.manifest #####"
cat "${ISOMountDir}/casper/filesystem.manifest"
echo "##### cdrom.casper.filesystem.manifest-remove #####"
cat "${ISOMountDir}/casper/filesystem.manifest-remove"
GetDebFileInfo "cdrom.casper.filesystem.squashfs.opt.third" "${FSMountDir}/opt/third"
GetDebFileInfo "cdrom.casper.filesystem.squashfs.opt.kscset" "${FSMountDir}/opt/kscset"
GetDebFileInfo "cdrom.third-party" "${ISOMountDir}/third-party"
GetDebFileInfo "cdrom.citrix" "${ISOMountDir}/citrix"
GetDebFileInfo "cdrom.sdm" "${ISOMountDir}/sdm"
GetDebFileInfo "cdrom.ecp" "${ISOMountDir}/ecp"
GetDebFileInfo "cdrom.ecp" "${ISOMountDir}/ECIP_ALL_1101"
GetDebFileInfo "cdrom.mail" "${ISOMountDir}/mail"
GetDebFileInfo "cdrom.rhtx" "${ISOMountDir}/rhtx"
GetDebFileInfo "cdrom.sogou" "${ISOMountDir}/sogou"
GetDebFileInfo "cdrom.kernel" "${ISOMountDir}/kernel"
echo "##### cdrom.kylin-post-actions #####"
cat "${ISOMountDir}/.kylin-post-actions"
} | sed -e "s/${ISOMountDir}\//iso\//g" -e "s/${ISOMountDir}\./iso\./g" >> "${ISOInfoFile}"
umount "${FSMountDir}"
umount "${ISOMountDir}"
rm -rf "${ISOMountDir}" "${FSMountDir}"
chown "${User}":"${User}" "${ISOInfoFile}"
fi
}
# Usage: MakeISO <ISO File> <ISO Label> <Live CD Root>
MakeISO() {
local Usage="Usage: MakeISO <ISO File> <ISO Label> <Live CD Root>"
if [ $# -ne 3 ] || [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
echo -e "${Usage}"
return 1
fi
local ISOFile=$1
local ISOLabel=$2
local LiveCDRoot=$3
local ISOLogFile=${ISOFile}.log
local ReturnCode=0
local User
if [ -n "${SUDO_USER}" ]; then
User=${SUDO_USER}
else
User=${USER}
fi
if [ -z "${LiveCDRoot}" ]; then
echo -e "Live CD root [${LiveCDRoot}] is empty!"
echo -e "${Usage}"
return 1
fi
if [ ! -d "${LiveCDRoot}" ]; then
echo -e "Live CD root [${LiveCDRoot}] is not exist or a directory!"
return 1
fi
# Push ISO Root to build
if [ "x${LiveCDRoot}" != "x." ]; then
ISOFile=$(pwd)/${ISOFile}
ISOLogFile=${ISOFile}.log
pushd "${LiveCDRoot}" >/dev/null || exit $?
LiveCDRoot=.
fi
local ISOARGS=''
# ISOARGS="${ISOARGS:+${ISOARGS} }-check-oldnames"
ISOARGS="${ISOARGS:+${ISOARGS} }-sysid 'LINUX'"
ISOARGS="${ISOARGS:+${ISOARGS} }-volid \"${ISOLabel}\""
ISOARGS="${ISOARGS:+${ISOARGS} }-joliet"
ISOARGS="${ISOARGS:+${ISOARGS} }-joliet-long"
ISOARGS="${ISOARGS:+${ISOARGS} }-full-iso9660-filenames"
# ISOARGS="${ISOARGS:+${ISOARGS} }-max-iso9660-filenames"
ISOARGS="${ISOARGS:+${ISOARGS} }-untranslated-filenames"
# ISOARGS="${ISOARGS:+${ISOARGS} }-no-iso-translate"
ISOARGS="${ISOARGS:+${ISOARGS} }-input-charset utf-8"
ISOARGS="${ISOARGS:+${ISOARGS} }-cache-inodes"
ISOARGS="${ISOARGS:+${ISOARGS} }-allow-multidot"
ISOARGS="${ISOARGS:+${ISOARGS} }-rational-rock"
ISOARGS="${ISOARGS:+${ISOARGS} }-translation-table"
ISOARGS="${ISOARGS:+${ISOARGS} }-udf"
# ISOARGS="${ISOARGS:+${ISOARGS} }-allow-limited-size"
if [ -f "${LiveCDRoot}/isolinux/isolinux.bin" ]; then
ISOARGS="${ISOARGS:+${ISOARGS} }-no-emul-boot"
ISOARGS="${ISOARGS:+${ISOARGS} }-boot-load-size 4"
ISOARGS="${ISOARGS:+${ISOARGS} }-boot-info-table"
ISOARGS="${ISOARGS:+${ISOARGS} }-eltorito-boot isolinux/isolinux.bin"
ISOARGS="${ISOARGS:+${ISOARGS} }-eltorito-catalog isolinux/boot.cat"
fi
if [ -f "${LiveCDRoot}/boot/grub/efi.img" ]; then
ISOARGS="${ISOARGS:+${ISOARGS} }-eltorito-alt-boot"
ISOARGS="${ISOARGS:+${ISOARGS} }-no-emul-boot"
ISOARGS="${ISOARGS:+${ISOARGS} }-efi-boot boot/grub/efi.img"
fi
# ISOARGS="${ISOARGS:+${ISOARGS} }-no-bak"
ISOARGS="${ISOARGS:+${ISOARGS} }-log-file ${ISOLogFile}.tmp"
# ISOARGS="${ISOARGS:+${ISOARGS} }-quiet"
ISOARGS="${ISOARGS:+${ISOARGS} }-verbose"
# ISOARGS="${ISOARGS:+${ISOARGS} }-debug"
# Backup Exclude files
local UUID
UUID=$(GenUUID)
PrepareExcludes Backup "${LiveCDRoot}" "${UUID}" "${ISOExcludeList[@]}"
echo "ISO Build Command:" > "${ISOLogFile}"
# eval echo "genisoimage ${ISOARGS} -output \"${ISOFile}\" \"${LiveCDRoot}\" >> ${ISOLogFile}"
# echo "" >> "${ISOLogFile}"
local Desc Cmd
if [ -f "${ISOFile}" ]; then
Desc="Removing exist ISO file [${C_B}$(basename "${ISOFile}")${C_CLR}] ... "
Cmd="rm -f \"${ISOFile}\""
Caller "REMOVE" "${Desc}" "${Cmd}"
fi
if [ -f "${ISOLogFile}" ]; then
Desc="Removing exist ISO log file [${C_B}$(basename "${ISOLogFile}")${C_CLR}] ... "
Cmd="rm -f \"${ISOLogFile}\""
Caller "REMOVE" "${Desc}" "${Cmd}"
fi
Desc="${C_B}$(basename "$(pwd)")${C_CLR} --> ${C_H}$(basename "${ISOFile}")${C_CLR} ... "
Cmd="genisoimage ${ISOARGS} -output \"${ISOFile}\" \"${LiveCDRoot}\"; cat \"${ISOLogFile}.tmp\" >> \"${ISOLogFile}\"; rm -f \"${ISOLogFile}.tmp\""
if ! Caller "MAKEISO" "${Desc}" "${Cmd}"; then
ReturnCode=1
cat "${ISOLogFile}"
fi
if [ -f "${LiveCDRoot}/isolinux/isolinux.bin" ]; then
local ISOHYBRID=""
ISOHYBRID="${ISOHYBRID:+${ISOHYBRID} }--uefi"
ISOHYBRID="${ISOHYBRID:+${ISOHYBRID} }--partok"
ISOHYBRID="${ISOHYBRID:+${ISOHYBRID} }--verbose"
Desc="${C_B}$(basename "${ISOFile}")${C_CLR} ... "
Cmd="isohybrid ${ISOHYBRID} \"${ISOFile}\""
if ! Caller "ISOHYBRID" "${Desc}" "${Cmd}"; then
ReturnCode=1
fi
fi
chown "${User}":"${User}" "${ISOFile}"
# Calc ISO Sum
local ISOFileName ISOFileDir ISOSumFile
ISOFileName=$(basename "${ISOFile}")
ISOFileDir=$(dirname "${ISOFile}")
pushd "${ISOFileDir}" > /dev/null || exit $?
ISOSumFile=${ISOFileName}.md5sum
Desc="Calculating ${C_H}${ISOFileName}${C_CLR} MD5SUM ... "
Cmd="md5sum \"${ISOFileName}\" > \"${ISOSumFile}\""
if ! Caller "GEN ISO MD5SUM" "${Desc}" "${Cmd}"; then
ReturnCode=1
fi
chown "${User}":"${User}" "${ISOSumFile}"
ISOSumFile=${ISOFileName}.sha256sum
Desc="Calculating ${C_H}${ISOFileName}${C_CLR} SHA256SUM ... "
Cmd="sha256sum \"${ISOFileName}\" > \"${ISOSumFile}\""
if ! Caller "GEN ISO SHA256SUM" "${Desc}" "${Cmd}"; then
ReturnCode=1
fi
chown "${User}":"${User}" "${ISOSumFile}"
popd > /dev/null || exit $?
# Restore Exclude files
PrepareExcludes Restore "${LiveCDRoot}" "${UUID}" "${ISOExcludeList[@]}"
if pushd >/dev/null 2>&1; then
popd >/dev/null || return $?
fi
return ${ReturnCode}
}
# Usage: UnpackISO <ISO File> <Target Dir>
UnpackISO() {
local Usage="Usage: UnpackISO <ISO File> <Target Dir>"
if [ $# -ne 2 ] || [ -z "$1" ] || [ -z "$2" ]; then
echo -e "${Usage}"
return 1
fi
local ISOFile=$1
local LiveCDRoot=$2
local ISOMount=.${ISOFile%.*}
if [ ! -f "${ISOFile}" ]; then
echo -e "ISO file [${ISOFile}] is not exist or a directory!"
return 1
fi
if [ -d "${LiveCDRoot}" ]; then
local Size
Size=$(du -sb "${LiveCDRoot}" | awk '{print $1}')
if [ "${Size}" -gt 4096 ]; then
echo -e "Target dir [${C_B}${LiveCDRoot}${C_CLR}] is exist and not empty, please remove it manual."
return 1
fi
else
mkdir -p "${LiveCDRoot}"
fi
mkdir -p "${ISOMount}"
local Desc Cmd
Desc="${C_H}${ISOFile}${C_CLR} --> ${C_Y}${ISOMount}${C_CLR} ... "
Cmd="mount -r \"${ISOFile}\" \"${ISOMount}\""
if ! Caller "MOUNTISO" "${Desc}" "${Cmd}"; then
return $?
fi
Desc="Copying files ${C_Y}${ISOMount}${C_CLR} --> ${C_B}${LiveCDRoot}${C_CLR} ... "
Cmd="rsync -aq --exclude='*.TBL' --exclude='*.tbl' \"${ISOMount}/\" \"${LiveCDRoot}\""
if ! Caller "RSYNC" "${Desc}" "${Cmd}"; then
return $?
fi
Desc="${C_Y}${ISOMount}${C_CLR} ... "
Cmd="umount \"${ISOMount}\""
if ! Caller "UMOUNTISO" "${Desc}" "${Cmd}"; then
return $?
fi