forked from unifreq/openwrt_packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
public_funcs
1794 lines (1631 loc) · 60 KB
/
public_funcs
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
# the default zstd compress level is 3
export ZSTD_LEVEL=6
get_random_mac ()
{
# MAC地址第一段可在 02 06 0A 0E 中任选一个
if [ "$SHELL" == "/bin/bash" ];then
#MACADDR=$(printf "%02X:%02X:%02X:%02X:%02X:%02X\n" $[RANDOM%255] $[RANDOM%255] $[RANDOM%255] $[RANDOM%255] $[RANDOM%255] $[RANDOM%255])
MACADDR=$(printf "06:%02X:%02X:%02X:%02X:%02X\n" $[RANDOM%255] $[RANDOM%255] $[RANDOM%255] $[RANDOM%255] $[RANDOM%255])
else
uuid=$(cat /proc/sys/kernel/random/uuid)
mac1="0E"
#mac1=${uuid:24:2}
mac2=${uuid:26:2}
mac3=${uuid:28:2}
mac4=${uuid:30:2}
mac5=${uuid:32:2}
mac6=${uuid:34:2}
MACADDR=$(echo "$mac1:$mac2:$mac3:$mac4:$mac5:$mac6" | tr '[a-z]' '[A-Z]')
fi
}
function check_file() {
if [ ! -f "$1" ];then
echo "文件: [$1] 不存在!"
if [ "$2" == "" ];then
exit 1
else
sleep $2
fi
fi
}
function check_dir() {
if [ ! -d "$1" ];then
echo "目录: [$1] 不存在!"
if [ "$2" == "" ];then
exit 1
else
sleep $2
fi
fi
}
function check_block_dev() {
if [ ! -b "$1" ];then
echo "块设备: [$1] 不存在!"
if [ "$2" == "" ];then
exit 1
else
sleep $2
fi
fi
}
# 此函数原有的用途是检查内核版本是否 >= 5.10, 现改为检查内核的 text_offset 是否为 0x01080000
# text offset:
# 0x01080000 适用晶晨盒子厂商 bootloader 以及 主线 bootloader, 已过时
# 0x00000000 适用主线 bootloader, 但不适用晶晨厂商 bootloader
function get_k510_from_vmlinuz() {
local vmlinuz=$1
local temp_script=$(mktemp)
cat > $temp_script <<EOF
use strict;
my \$filename = \$ARGV[0];
open my \$fh, '<', \$filename or die;
binmode \$fh;
seek \$fh, 0x8, 0;
my \$buf = "";
read \$fh, \$buf, 0x4;
close(\$fh);
my \$str = unpack 'H*', \$buf;
print "\$str\n";
EOF
local result=1
local text_offset=$(perl "$temp_script" "$vmlinuz")
[ "$text_offset" == "00000801" ] && result=0
rm -f $temp_script
echo "$result"
}
function get_k510_from_boot_tgz() {
local boot_tgz=$1
local vmlinuz=$2
local temp_dir=$(mktemp -d)
(
cd $temp_dir
tar xzf "$boot_tgz" "$vmlinuz"
)
local result=$(get_k510_from_vmlinuz "${temp_dir}/${vmlinuz}")
rm -rf $temp_dir
echo "$result"
}
function check_depends() {
if [ $(id -u) -ne 0 ];then
echo "这个脚本需要用root用户来执行,你好象不是root吧?"
exit 1
fi
if mkfs.btrfs -V >/dev/null;then
echo "check mkfs.btrfs ok"
else
echo "mkfs.btrfs 程序不存在,请安装 btrfs-progs"
exit 1
fi
if mkfs.vfat --help 1>/dev/nul 2>&1;then
echo "check mkfs.vfat ok"
else
echo "mkfs.vfat 程序不存在,请安装 dosfstools"
exit 1
fi
if gawk --version >/dev/null;then
echo "check gawk ok"
else
echo "gawk 程序不存在,请安装 gawk"
exit 1
fi
if uuidgen>/dev/null;then
echo "check uuidgen ok"
else
echo "uuidgen 程序不存在,请安装 uuid-runtime"
exit 1
fi
if losetup -V >/dev/null;then
echo "check losetup ok"
else
echo "losetup 程序不存在,请安装 mount"
exit 1
fi
lsblk_version=$(lsblk --version|awk '{print $NF}' 2>/dev/null)
if [ "$lsblk_version" != "" ];then
m_v=$(echo ${lsblk_version} | cut -d '.' -f1)
s_v=$(echo ${lsblk_version} | cut -d '.' -f2)
if [ $m_v -gt 2 ] || [ $m_v -eq 2 ] && [ $s_v -ge 33 ];then
echo "check lsblk ok"
else
echo "lsblk 需要版本 2.33 或更高的版本, 当前的版本是 $lsblk_version !"
exit 1
fi
else
echo "lsblk 程序不存在,请安装 util-linux"
exit 1
fi
if fdisk --version >/dev/null 2>&1;then
echo "check fdisk ok"
else
echo "fdisk 程序不存在,请安装 fdisk"
exit 1
fi
if parted --version >/dev/null 2>&1;then
echo "check parted ok"
else
echo "parted 程序不存在,请安装 parted"
exit 1
fi
# 创建 kvm 虚拟机镜像需要 qemu-img
if [ "$EFI" == "1" ];then
if qemu-img --version >/dev/null 2>&1;then
echo "check qemu-img ok"
else
echo "qemu-img 程序不存在,请安装 qemu-utils"
exit 1
fi
fi
echo
}
function wait_dev() {
while [ ! -b $1 ];do
echo "wait for $1 ..."
sleep 1
done
sleep 1
}
function init_work_env() {
export WORK_DIR="${PWD}/tmp"
[ -d ${WORK_DIR} ] || mkdir -p ${WORK_DIR}
echo "The work directory is : [$WORK_DIR]"
export TEMP_DIR=$(mktemp -p $WORK_DIR)
rm -rf $TEMP_DIR
mkdir -p $TEMP_DIR
echo "The temporary directory is : [$TEMP_DIR]"
if [ "$EFI" == "1" ];then
export TGT_BOOT=${TEMP_DIR}/tgt_efi
echo "The target efi directory is : [$TGT_BOOT]"
else
export TGT_BOOT=${TEMP_DIR}/tgt_boot
echo "The target boot directory is : [$TGT_BOOT]"
fi
export TGT_ROOT=${TEMP_DIR}/tgt_root
mkdir -p $TGT_BOOT $TGT_ROOT
echo "The target rootfs directory is : [$TGT_ROOT]"
export OUTPUT_DIR="${PWD}/output/"
[ -d ${OUTPUT_DIR} ] || mkdir -p ${OUTPUT_DIR}
echo "The output directory is : [$OUTPUT_DIR]"
losetup -D
echo
}
function detach_loopdev() {
cd ${TEMP_DIR}
umount -f * 2>/dev/null
losetup -D
cd ${WORK_DIR}
rm -rf ${TEMP_DIR}
}
function create_image() {
local img_name=$1
local size_mb=$2
if [ "$img_name" == "" ];then
echo "镜像名称为空,请指定一个具体的镜像名称!"
exit 1
fi
if [ "$size_mb" == "" ];then
echo "镜像大小为空,请指定不小于 1 的整数尺寸!"
exit 1
fi
echo "创建空白的目标镜像文件, 大小是 $size_mb MB ... "
dd if=/dev/zero of="$img_name" bs=1M count=$size_mb conv=fsync && sync
if [ $? -ne 0 ];then
echo "空白镜像创建失败!"
exit 1
else
echo "空白镜像创建成功"
fi
echo -n "设置分区循环设备 ... "
losetup -f -P "$img_name"
if [ $? -ne 0 ];then
echo "失败!"
exit 1
else
TGT_DEV=$(losetup | grep "$img_name" | gawk '{print $1}')
export TGT_DEV
echo "成功: [$TGT_DEV]"
fi
echo
}
# 创建分区(允许在空白磁盘里新建分区,也允许在现有分区表中扩充分区)
# 参数1: 块设备名
# 参数2: 分区表类型: msdos|gpt|auto auto表示在现有的磁盘中添加新分区
# 参数3+4+5: 分区1跳过MB 分区1大小MB 分区1类型
# 参数6+7+8: 分区2跳过MB 分区2大小MB 分区2类型
# 参数9+10+11: 分区3跳过MB 分区3大小MB 分区3类型
# 参数12+13+14: 分区4跳过MB 分区4大小MB 分区4类型
# ...
function create_partition() {
local device_name=$1
check_block_dev $device_name
shift
local partition_label=$1
shift
local max_pt_cnt
local cur_pt_cnt=0
case ${partition_label} in
msdos) max_pt_cnt=4
;;
gpt) max_pt_cnt=128
;;
auto) local label=$(parted $device_name print 2>/dev/null | gawk '$NF ~ /&msdos$/ || $NF ~ /^gpt$/ || $NF ~ /^unknown$/ {print $NF}')
case $label in
msdos) partition_label="msdos"
max_pt_cnt=4
cur_pt_cnt=$(parted $device_name print | gawk '$1 ~ /^[0-9]+$/ {print $1}' | tail -n1)
[ "$cur_pt_cnt" == "" ] && cur_pt_cnt=0
;;
gpt) partition_label="gpt"
max_pt_cnt=128
cur_pt_cnt=$(parted $device_name print | gawk '$1 ~ /^[0-9]+$/ {print $1}' | tail -n1)
[ "$cur_pt_cnt" == "" ] && cur_pt_cnt=0
;;
*) # 自动模式下,如果检测到空白磁盘,将会用msdos格式新建分区表
partition_label="msdos"
max_pt_cnt=4
cur_pt_cnt=0
;;
esac
;;
*) echo "未指定分区表类型, 必须为 gpt|msdos|auto"
exit 1
;;
esac
echo "对磁盘设备 [$device_name] 进行分区 ..."
if [ $cur_pt_cnt -eq 0 ];then
echo -n "创建新的分区表 ... "
parted -s ${device_name} mklabel ${partition_label} 2>/dev/null
if [ $? -ne 0 ];then
echo "失败!"
detach_loopdev
exit 1
else
echo "成功"
fi
fi
local start=0
if [ $cur_pt_cnt -gt 0 ];then
start=$(fdisk -l $device_name | tail -n1 | gawk '{ print ($3 + 1) / 512 * 1024 * 1024 }')
fi
i=$((cur_pt_cnt + 1))
while [ $i -le ${max_pt_cnt} ]; do
local skip_mb=$1
shift
local pt_size=$1
shift
local pt_type=$1
shift
local pt_idx=$i
if [ "$skip_mb" == "" ] || [ "$pt_size" == "" ] || [ "$pt_type" == "" ];then
break
fi
local esp_on=0
local lvm_on=0
if [ "$pt_type" == "efi" ];then
pt_type="fat32"
esp_on=1
elif [ "$pt_type" == "lvm" ];then
pt_type="ext2"
lvm_on=1
fi
start=$(( skip_mb * 1024 * 1024 + start ))
local end
if [ "${pt_size}" == "-1" ] || [ "${pt_size}" == "100%" ];then
echo -n "创建第 [$i] 个分区, 分区大小: 所有剩余空间, 分区类型: [$pt_type] ... "
parted -s ${device_name} mkpart primary ${pt_type} ${start}b "100%" 2>/dev/null
if [ $? -ne 0 ];then
echo "失败!"
detach_loopdev
exit 1
else
echo "成功"
break
fi
else
end=$(( pt_size * 1024 * 1024 + start - 1 ))
echo -n "创建第 [$i] 个分区, 分区大小: [$pt_size] MB, 分区类型: [$pt_type] ... "
parted -s ${device_name} mkpart primary ${pt_type} ${start}b ${end}b 2>/dev/null
if [ $? -ne 0 ];then
echo "失败!"
detach_loopdev
exit 1
else
echo "成功"
fi
fi
if [ "$esp_on" == "1" ];then
parted -s ${device_name} set $pt_idx esp on
elif [ "$lvm_on" == "1" ];then
parted -s ${device_name} set $pt_idx lvm on
fi
start=$(( end + 1 ))
i=$((i + 1))
done
echo "[$device_name] 磁盘分区一览:"
echo "==================================================================="
fdisk -l $device_name 2>/dev/null
echo "==================================================================="
echo
}
# 格式化文件系统
# 参数1: 块设备名
# 参数2: 分区1用途: boot=B rootfs=R other=O
# 参数3: 分区1类型: fat16|fat32|ext4|xfs|btrfs|f2fs
# 参数4: 分区1卷标
# 参数5: 分区2用途: boot=B rootfs=R other=O
# 参数6: 分区2类型: fat16|fat32|ext4|xfs|btrfs|f2fs
# 参数7: 分区2卷标
# 参数8: 分区3用途: boot=B rootfs=R other=O
# 参数9: 分区3类型: fat16|fat32|ext4|xfs|btrfs|f2fs
# 参数10: 分区3卷标
# 参数11: 分区4用途: boot=B rootfs=R other=O
# 参数12: 分区4类型: fat16|fat32|ext4|xfs|btrfs|f2fs
# 参数13:分区4卷标
function make_filesystem() {
local device_name=$1
check_block_dev $device_name
shift
echo "==================================================================="
echo "在 [$device_name] 上创建文件系统 ... "
i=1
while [ $i -le 4 ]; do
local fs_use=$1
shift
local fs_type=$1
shift
local fs_label=$1
shift
if [ "$fs_use" == "" ] || [ "$fs_type" == "" ] || [ "$fs_label" == "" ];then
break
fi
local fs_uuid=$(uuidgen)
case $fs_use in
B) if [ "$fs_type" == "ext4" ] || [ "$fs_type" == "xfs" ] || [ "$fs_type" == "btrfs" ] || [ "$fs_type" == "f2fs" ];then
BOOT_UUID=${fs_uuid}
echo "The boot uuid is ${BOOT_UUID}"
export BOOT_UUID
fi
BOOT_LABEL=${fs_label}
echo "The boot label is ${BOOT_LABEL}"
export BOOT_LABEL
;;
R) ROOTFS_UUID=${fs_uuid}
ROOTFS_LABEL=${fs_label}
echo "The rootfs uuid is ${ROOTFS_UUID}"
echo "The rootfs label is ${ROOTFS_LABEL}"
export ROOTFS_UUID ROOTFS_LABEL
;;
O) ;;
*) echo "分区用途: [$fs_use] 无法识别!仅支持 B/R/O 三种!"
detach_loopdev
exit 1
;;
esac
echo "格式化 [${device_name}p${i}], 类型为 [$fs_type], 卷标为 [$fs_label] ... "
wait_dev ${device_name}p${i}
case $fs_type in
fat16) mkfs.vfat -F 16 -n "${fs_label}" ${device_name}p${i}
if [ $? -ne 0 ];then
echo "[${device_name}p${i}] 格式化失败!"
detach_loopdev
exit 1
else
echo "[${device_name}p${i}] 格式化成功"
fi
;;
fat32) mkfs.vfat -F 32 -n "${fs_label}" ${device_name}p${i}
if [ $? -ne 0 ];then
echo "[${device_name}p${i}] 格式化失败!"
detach_loopdev
exit 1
else
echo "[${device_name}p${i}] 格式化成功"
fi
;;
ext4) mkfs.ext4 -U ${fs_uuid} -L "${fs_label}" -b 4k ${device_name}p${i}
if [ $? -ne 0 ];then
echo "[${device_name}p${i}] 格式化失败!"
detach_loopdev
exit 1
else
echo "[${device_name}p${i}] 格式化成功"
fi
;;
xfs) mkfs.xfs -U ${fs_uuid} -L "${fs_label}" ${device_name}p${i}
if [ $? -ne 0 ];then
echo "[${device_name}p${i}] 格式化失败!"
detach_loopdev
exit 1
else
echo "[${device_name}p${i}] 格式化成功"
fi
;;
f2fs) mkfs.f2fs -U ${fs_uuid} -l "${fs_label}" ${device_name}p${i}
if [ $? -ne 0 ];then
echo "[${device_name}p${i}] 格式化失败!"
detach_loopdev
exit 1
else
echo "[${device_name}p${i}] 格式化成功"
fi
;;
btrfs) mkfs.btrfs -U ${fs_uuid} -L "${fs_label}" -m single ${device_name}p${i}
if [ $? -ne 0 ];then
echo "[${device_name}p${i}] 格式化失败!"
detach_loopdev
exit 1
else
echo "[${device_name}p${i}] 格式化成功"
fi
;;
*) echo "文件系统类型 [$fs_type] 不受支持!"
detach_loopdev
exit 1
;;
esac
i=$((i+1))
done
echo "[$device_name] 文件系统已创建."
echo "==================================================================="
echo
}
function mount_fs() {
local device_name=$1
check_block_dev $device_name
local mount_point=$2
check_dir $mount_point
local fs_type=$3
if [ -z "$fs_type" ];then
echo "未指定文件系统类型"
detach_loopdev
exit 1
fi
local mount_options=$4
echo -n "挂载 ${device_name} --> ${mount_point} ... "
if [ "$mount_options" == "" ];then
mount -t "$fs_type" "$device_name" "$mount_point"
else
mount -t "$fs_type" -o "$mount_options" "$device_name" "$mount_point"
fi
if [ $? -ne 0 ];then
echo "失败!"
detach_loopdev
exit 1
else
echo "成功"
fi
}
function extract_rootfs_files() {
echo -n "释放 Openwrt rootfs 压缩包 ... "
(
cd ${TGT_ROOT} && \
tar --exclude="./lib/firmware/*" --exclude="./lib/modules/*" -xzf "$OPWRT_ROOTFS_GZ" && \
mkdir -p .reserved boot rom overlay proc sys run ./etc/modprobe.d ./etc/modules.d.remove && \
sync
)
if [ $? -ne 0 ];then
echo "失败!"
detach_loopdev
exit 1
else
echo "完成"
fi
echo -n "裁减 Rootfs 中用不到的文件 ... "
(
cd ${TGT_ROOT}
rm -rf ./lib/firmware/* ./lib/modules/*
rm -f ./etc/docker-init ./sbin/firstboot ./sbin/jffs2reset ./sbin/jffs2mark
# for collectd
[ -f ./etc/ppp/options-opkg ] && mv ./etc/ppp/options-opkg ./etc/ppp/options
chmod 755 ./etc/init.d/*
rm -f ./etc/rc.d/S80nginx 2>/dev/null
local ps="${PLATFORM}_${SOC}"
case $ps in
amlogic_s905) # s905不支持硬件aes,所以必须删除 ss-rust 程序
[ -f ./usr/bin/sslocal ] && rm -f ./usr/bin/sslocal
[ -f ./usr/bin/ssserver ] && rm -f ./usr/bin/ssserver
;;
esac
)
echo "完成"
echo -n "重新链接 kmod 程序 ... "
( cd $TGT_ROOT/sbin
[ ! -x "kmod" ] && [ -f "${KMOD}" ] && cp "$KMOD" kmod
if [ -x "kmod" ];then
rm -f depmod insmod lsmod modinfo modprobe rmmod
ln -sf kmod depmod
ln -sf kmod insmod
ln -sf kmod lsmod
ln -sf kmod modinfo
ln -sf kmod modprobe
ln -sf kmod rmmod
fi
)
echo "完成"
echo -n "释放 Armbian firmware 压缩包 ... "
( cd ${TGT_ROOT} && tar xJf "$FIRMWARE_TXZ" && sync )
if [ $? -ne 0 ];then
echo "失败!"
detach_loopdev
exit 1
else
echo "完成"
fi
if [ -d "${FMW_HOME}" ];then
echo -n "复制 Added firmware 文件 ... "
( cd ${TGT_ROOT} && cp -a ${FMW_HOME}/* lib/firmware/ && sync )
echo "完成"
fi
echo -n "优化 firmware ... "
(
cd $TGT_ROOT/lib/firmware
mv *.hcd brcm/ 2>/dev/null
cd brcm
# gtking/gtking pro 采用 bcm4356 wifi/bluetooth 模块
get_random_mac
# wifi5 module AP6356S
sed -e "s/macaddr=00:90:4c:1a:10:01/macaddr=${MACADDR}/" "brcmfmac4356-sdio.txt" > "brcmfmac4356-sdio.azw,gtking.txt"
get_random_mac
# wifi6 module AP6275S
sed -e "s/macaddr=00:90:4c:12:d0:01/macaddr=${MACADDR}/" "brcmfmac4375-sdio.txt" > "brcmfmac4375-sdio.azw,gtking.txt"
# Phicomm N1 采用 bcm43455 wifi/bluetooth 模块
get_random_mac
sed -e "s/macaddr=b8:27:eb:74:f2:6c/macaddr=${MACADDR}/" "brcmfmac43455-sdio.txt" > "brcmfmac43455-sdio.phicomm,n1.txt"
# MXQ Pro+ 采用 AP6330(bcm4330) wifi/bluetooth 模块
get_random_mac
sed -e "s/macaddr=00:90:4c:c5:12:38/macaddr=${MACADDR}/" "brcmfmac4330-sdio.txt" > "brcmfmac4330-sdio.crocon,mxq-pro-plus.txt"
# HK1 Box 和 H96 Max X3 采用 bcm54339 wifi/bluetooth 模块
get_random_mac
sed -e "s/macaddr=00:90:4c:c5:12:38/macaddr=${MACADDR}/" "brcmfmac4339-sdio.ZP.txt" > "brcmfmac4339-sdio.amlogic,sm1.txt"
# 旧版ugoos x3 采用 bcm43455 wifi/bluetooth 模块
get_random_mac
sed -e "s/macaddr=b8:27:eb:74:f2:6c/macaddr=${MACADDR}/" "brcmfmac43455-sdio.txt" > "brcmfmac43455-sdio.amlogic,sm1.txt"
# 新版ugoos x3 采用 brm43456
get_random_mac
sed -e "s/macaddr=b8:27:eb:74:f2:6c/macaddr=${MACADDR}/" "brcmfmac43456-sdio.txt" > "brcmfmac43456-sdio.amlogic,sm1.txt"
# V5.1版 x96max plus 采用 AM7256 (brcm4354)
get_random_mac
sed -e "s/macaddr=00:90:4c:00:43:54/macaddr=${MACADDR}/" "brcmfmac4354-sdio.txt" > "brcmfmac4354-sdio.amlogic,sm1.txt"
)
echo "完成"
echo -n "释放 Kernel modules 压缩包 ... "
(
cd ${TGT_ROOT} && \
mkdir -p lib/modules && \
cd lib/modules && \
tar xzf ${MODULES_TGZ} && \
cd ${KERNEL_VERSION}/ && \
find . -name '*.ko' -exec ln -sf {} . \; && \
rm -f "build" "source" "ntfs.ko" && \
sync
)
if [ $? -ne 0 ];then
echo "失败!"
detach_loopdev
exit 1
else
echo "完成"
fi
if [ -n "$MODEL_DB" ] && [ -f "$MODEL_DB" ];then
echo "复制硬件机型数据库 ..."
cp -v "$MODEL_DB" ${TGT_ROOT}/etc/model_database.txt
echo "完成"
fi
}
function extract_amlogic_boot_files() {
echo -n "释放 Kernel zImage、uInitrd 及 dtbs 压缩包 ... "
(
cd ${TGT_BOOT} && \
cp "${BOOTFILES_HOME}"/* . && \
tar xzf "${BOOT_TGZ}" && \
rm -f initrd.img-${KERNEL_VERSION} && \
cp vmlinuz-${KERNEL_VERSION} zImage && \
cp uInitrd-${KERNEL_VERSION} uInitrd && \
cp ${UBOOT_WITHOUT_FIP_HOME}/* . && \
mkdir -p dtb/amlogic && \
cd dtb/amlogic && \
tar xzf "${DTBS_TGZ}" && \
sync
)
if [ $? -ne 0 ];then
echo "失败!"
detach_loopdev
exit 1
fi
(
cd ${TGT_BOOT}
# 5.4内核不再需要u-boot.sd 以及 u-boot.usb
rm -f u-boot.usb u-boot.sd
# 5.10以后的内核,需要增加u-boot重载
# 5.4内核可加可不加
if [ $K510 -eq 1 ];then
check_file ${UBOOT_WITHOUT_FIP}
cp -f ${UBOOT_WITHOUT_FIP} u-boot.ext
if [ $? -ne 0 ];then
echo "失败!"
detach_loopdev
exit 1
fi
fi
)
echo "完成"
}
function extract_rockchip_boot_files() {
echo -n "释放 Kernel zImage、uInitrd 及 dtbs 压缩包 ... "
(
cd ${TGT_BOOT} && \
cp -v "${BOOTFILES_HOME}"/* . && \
tar xzf "${BOOT_TGZ}" && \
rm -f initrd.img-${KERNEL_VERSION} && \
ln -sf vmlinuz-${KERNEL_VERSION} Image && \
ln -sf uInitrd-${KERNEL_VERSION} uInitrd && \
mkdir -p dtb-${KERNEL_VERSION}/rockchip && \
ln -sf dtb-${KERNEL_VERSION} dtb && \
cd dtb/rockchip && \
tar xzf "${DTBS_TGZ}" && \
sync
)
if [ $? -ne 0 ];then
echo "失败!"
detach_loopdev
exit 1
else
echo "完成"
fi
}
function extract_allwinner_boot_files() {
echo -n "释放 Kernel zImage、uInitrd 及 dtbs 压缩包 ... "
(
cd ${TGT_BOOT} && \
cp "${BOOTFILES_HOME}"/* . && \
tar xzf "${BOOT_TGZ}" && \
rm -f initrd.img-${KERNEL_VERSION} && \
cp vmlinuz-${KERNEL_VERSION} zImage && \
cp uInitrd-${KERNEL_VERSION} uInitrd && \
mkdir -p dtb/allwinner && \
cd dtb/allwinner && \
tar xzf "${DTBS_TGZ}" && \
sync
)
if [ $? -ne 0 ];then
echo "失败!"
detach_loopdev
exit 1
else
echo "完成"
fi
}
function extract_qemu-aarch64_boot_files() {
echo -n "释放 Kernel vmlinuz、initrd.img 及 grub2 压缩包 ... "
(
cd ${TGT_ROOT}/boot && \
mkdir -p efi && \
tar xzf "${BOOTFILES_HOME}/grub2.tar.gz" && \
tar xzf "${BOOT_TGZ}" && \
ln -s initrd.img-${KERNEL_VERSION} initrd.img && \
ln -s vmlinuz-${KERNEL_VERSION} vmlinuz && \
rm -f uInitrd* && \
cd ${TGT_BOOT} && \
tar xzf "${BOOTFILES_HOME}/EFI.tar.gz" && \
sync
)
if [ $? -ne 0 ];then
echo "失败!"
detach_loopdev
exit 1
else
echo "完成"
fi
}
# 生成 openwrt 所需的 fstab 文件
function create_fstab_config() {
echo "创建 fstab 配置文件 ... "
(
cd $TGT_ROOT
if [ "$PLATFORM" == "qemu-aarch64" ];then
cat > ./etc/fstab <<EOF
UUID=${ROOTFS_UUID} / btrfs compress=zstd:${ZSTD_LEVEL} 0 1
LABEL=${BOOT_LABEL} /boot/efi vfat noatime,errors=remount-ro 0 2
#tmpfs /tmp tmpfs defaults,nosuid 0 0
EOF
elif [ ! -z "${BOOT_UUID}" ];then
cat > ./etc/fstab <<EOF
UUID=${ROOTFS_UUID} / btrfs compress=zstd:${ZSTD_LEVEL} 0 1
UUID=${BOOT_UUID} /boot ext4 noatime,errors=remount-ro 0 2
#tmpfs /tmp tmpfs defaults,nosuid 0 0
EOF
else
cat > ./etc/fstab <<EOF
UUID=${ROOTFS_UUID} / btrfs compress=zstd:${ZSTD_LEVEL} 0 1
LABEL=${BOOT_LABEL} /boot vfat defaults 0 2
#tmpfs /tmp tmpfs defaults,nosuid 0 0
EOF
fi
cat > ./etc/config/fstab <<EOF
config global
option anon_swap '0'
option auto_swap '0'
option anon_mount '1'
option auto_mount '1'
option delay_root '5'
option check_fs '0'
config mount
option target '/overlay'
option enabled '1'
option enabled_fsck '1'
option uuid '${ROOTFS_UUID}'
option fstype 'btrfs'
option options 'compress=zstd:${ZSTD_LEVEL}'
EOF
if [ "$PLATFORM" == "qemu-aarch64" ];then
cat >> ./etc/config/fstab <<EOF
config mount
option target '/boot/efi'
option enabled '1'
option enabled_fsck '1'
EOF
else
cat >> ./etc/config/fstab <<EOF
config mount
option target '/boot'
option enabled '1'
option enabled_fsck '1'
EOF
fi
if [ "$PLATFORM" == "qemu-aarch64" ];then
cat >> ./etc/config/fstab <<EOF
option label '${BOOT_LABEL}'
option fstype 'vfat'
EOF
elif [ ! -z "${BOOT_UUID}" ];then
cat >> ./etc/config/fstab <<EOF
option uuid '${BOOT_UUID}'
option fstype 'ext4'
EOF
else
cat >> ./etc/config/fstab <<EOF
option label '${BOOT_LABEL}'
option fstype 'vfat'
EOF
fi
)
echo "当前 fstab 内容如下:"
echo "==============================================="
cat $TGT_ROOT/etc/config/fstab
echo "==============================================="
echo
}
# 调整ntfs相关配置
function adjust_ntfs_config() {
echo -n "调整 ntfs 配置 ... "
(
cd $TGT_ROOT/sbin
if ls ../usr/lib/opkg/info/ntfs3* >/dev/null 2>&1;then
rm -f mount.ntfs*
cat > mount.ntfs <<EOF
#!/bin/sh
function check_k515() {
# 判断内核版本是否 >= 5.15
K_VER=\$(uname -r | cut -d '.' -f1)
K_MAJ=\$(uname -r | cut -d '.' -f2)
if [ \$K_VER -eq 5 ];then
if [ \$K_MAJ -ge 15 ];then
K515=1
else
K515=0
fi
elif [ \$K_VER -gt 5 ];then
K515=1
else
K515=0
fi
export K515
}
check_k515
if [ \$K515 -eq 1 ];then
mount -t ntfs3 -o iocharset=utf8,force "\$@"
else
mount -t ntfs3 -o nls=utf8,force "\$@"
fi
EOF
chmod 755 mount.ntfs
echo "发现 ntfs3, 调整完成"
elif ls ../usr/lib/opkg/info/ntfs-3g* >/dev/null 2>&1;then
ln -sf /usr/bin/ntfs-3g mount.ntfs
echo "发现 ntfs-3g, 调整完成"
else
echo "完成"
fi
)
}
# 调整 turboacc 相关配置
function adjust_turboacc_config() {
echo -n "调整 turboacc 配置 ... "
(
cd $TGT_ROOT
if [ -f ./etc/config/turboacc ];then
sed -e "s/option sw_flow '1'/option sw_flow '${SW_FLOWOFFLOAD}'/" -i ./etc/config/turboacc
sed -e "s/option hw_flow '1'/option hw_flow '${HW_FLOWOFFLOAD}'/" -i ./etc/config/turboacc
sed -e "s/option sfe_flow '1'/option sfe_flow '${SFE_FLOW}'/" -i ./etc/config/turboacc
else
cat > ./etc/config/turboacc <<EOF
config turboacc 'config'
option sw_flow '${SW_FLOWOFFLOAD}'
option hw_flow '${HW_FLOWOFFLOAD}'
option sfe_flow '${SFE_FLOW}'
option bbr_cca '0'
option fullcone_nat '1'
option dns_caching '0'
EOF
fi
)
echo "完成"
}
# CPU状态补丁
function patch_admin_status_index_html() {
echo "打补丁:[状态页面相关信息补丁] ... "
(
cd $TGT_ROOT
if [ ! -f usr/bin/perl ];then
echo "未发现 perl 实用程序, 放弃补丁!"
else
if [ -d "${INDEX_PATCH_HOME}" ];then
cp ./usr/lib/lua/luci/view/admin_status/index.htm ./usr/lib/lua/luci/view/admin_status/index.htm.bak
ret=0
for p in `echo ${INDEX_PATCH_HOME}/*.patch`;do
patch -p1 < $p
if [ $? -ne 0 ];then
echo "补丁失败,还原原始文件 ... "
mv ./usr/lib/lua/luci/view/admin_status/index.htm.bak ./usr/lib/lua/luci/view/admin_status/index.htm
ret=1
echo "已还原"
break
fi
done
if [ $ret -eq 0 ];then
echo "补丁成功"
rm -f ./usr/lib/lua/luci/view/admin_status/index.htm.bak
fi
fi
echo "完成"
fi
echo
)
}
# 写入 release 信息
function write_release_info() {
echo "写入 release 信息 ... "
(
cd $TGT_ROOT
# 写入版本信息
cat > ./etc/flippy-openwrt-release <<EOF
PLATFORM=${PLATFORM}
SOC=${SOC}
BOARD=${BOARD}
KERNEL_VERSION=${KERNEL_VERSION}
K510=${K510}
EOF
if [ -f "./etc/uci-defaults/99-default-settings" ] && [ -n "${DISTRIB_REVISION}" ] && [ -n "${DISTRIB_DESCRIPTION}" ];then
sed -e "/exit 0/d" -i ./etc/uci-defaults/99-default-settings
cat >> ./etc/uci-defaults/99-default-settings <<EOF
sed -i '/DISTRIB_REVISION/d' /etc/openwrt_release
echo "DISTRIB_REVISION='${DISTRIB_REVISION}'" >> /etc/openwrt_release
sed -i '/DISTRIB_DESCRIPTION/d' /etc/openwrt_release
echo "DISTRIB_DESCRIPTION='${DISTRIB_DESCRIPTION} '" >> /etc/openwrt_release
exit 0
EOF
fi