-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
openwrt-install-amlogic
executable file
·703 lines (619 loc) · 22.5 KB
/
openwrt-install-amlogic
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
#!/bin/bash
#======================================================================================
# Function: Install openwrt to emmc for Amlogic S9xxx STB
# Copyright (C) 2020-- https://github.com/unifreq/openwrt_packit
# Copyright (C) 2021-- https://github.com/ophub/luci-app-amlogic
#======================================================================================
# The script supports directly setting parameters for installation, skipping interactive selection
# openwrt-install-amlogic ${AUTO_MAINLINE_UBOOT} ${SOC_ID} ${DTB_FILENAME} ${SHARED_FSTYPE}
# E.g: openwrt-install-amlogic yes 1 auto_dtb ext4
# E.g: openwrt-install-amlogic no 99 meson-gxl-s905x-p212.dtb ext4
# Tip: When custom dtb file, set ${SOC_ID} to 99, and parameter ${DTB_FILENAME} must be set
# Tip: ${SHARED_FSTYPE}: Shared partition can be ext4, xfs, btrfs, f2fs
# return 0
# You can also execute the script directly, and interactively select related functions
# E.g: openwrt-install-amlogic
# Receive one-key command related parameters
AUTO_MAINLINE_UBOOT=${1}
ZSTD_LEVEL=6
# For [luci-app-amlogic] input parameter: SOC & DTB
# When there is no input parameter, select manually
SPECIFY_SOC=""
SPECIFY_DTB=""
if [[ -n "$(echo ${2} | sed -n "/^[0-9]\+$/p")" ]]; then
SPECIFY_SOC=${2}
if [[ "${2}" -eq "99" ]]; then
if [[ -n "${3}" ]]; then
SPECIFY_DTB=${3}
else
echo "Please enter the DTB file name!"
exit 1
fi
else
SPECIFY_DTB="auto_dtb"
fi
fi
# shared partition can be ext4, xfs, btrfs, f2fs
SHARED_FSTYPE=${4}
echo "AUTO_MAINLINE_UBOOT: ${AUTO_MAINLINE_UBOOT}"
echo "SPECIFY_SOC: ${SPECIFY_SOC}"
echo "SPECIFY_DTB: ${SPECIFY_DTB}"
echo "SHARED_FSTYPE: ${SHARED_FSTYPE}"
# Current device model
MYDEVICE_NAME=$(cat /proc/device-tree/model | tr -d '\000')
if [[ -z "${MYDEVICE_NAME}" ]]; then
echo "The device name is empty and cannot be recognized."
exit 1
elif [[ ! -f "/etc/flippy-openwrt-release" ]]; then
echo "The [ /etc/flippy-openwrt-release ] file is missing."
exit 1
else
echo -e "Current device: ${MYDEVICE_NAME} [ amlogic ]"
sleep 3
fi
# Find the device name of /
root_devname=$(df / | tail -n1 | awk '{print $1}' | awk -F '\/' '{print substr($3, 1, length($3)-2)}')
if lsblk -l | grep -E "^${root_devname}boot0" >/dev/null; then
echo "you are running in emmc mode, please boot system with usb or tf card!"
exit 1
fi
install_emmc="$(lsblk -l -o NAME | grep -oE '(mmcblk[0-9]?boot0)' | sed "s/boot0//g")"
if [[ "${install_emmc}" == "" ]]; then
echo "No emmc can be found to install the openwrt system!"
exit 1
fi
# EMMC DEVICE NAME
EMMC_NAME="${install_emmc}"
EMMC_DEVPATH="/dev/${EMMC_NAME}"
echo ${EMMC_DEVPATH}
EMMC_SIZE=$(lsblk -l -b -o NAME,SIZE | grep ${EMMC_NAME} | sort | uniq | head -n1 | awk '{print $2}')
echo "${EMMC_NAME} : ${EMMC_SIZE} bytes"
ROOT_NAME=$(lsblk -l -o NAME,MAJ:MIN,MOUNTPOINT | grep -e '/$' | awk '{print $1}')
echo "ROOTFS: ${ROOT_NAME}"
BOOT_NAME=$(lsblk -l -o NAME,MAJ:MIN,MOUNTPOINT | grep -e '/boot$' | awk '{print $1}')
echo "BOOT: ${BOOT_NAME}"
#Choose the type of installation box
DEFAULT_FDTFILE="meson-sm1-x96-max-plus.dtb"
U_BOOT_EXT=0
# box model database
# The field separator is :
# " " or "" or NA or NULL means this field is null
# The fields list:
# 1. id
# 2. model name
# 3. SOC
# 4. FDTFILE
# 5. UBOOT_OVERLOAD
# 6. MAINLINE_UBOOT
# 7. ANDROID_UBOOT
# 8. brief description
#
# allow use external modal database
if [ -f "/etc/model_database.txt" ]; then
model_database=$(cat /etc/model_database.txt)
else
model_database="
# Amlogic GXL Family
11:Phicomm N1:s905d:meson-gxl-s905d-phicomm-n1.dtb:u-boot-n1.bin:NA:/lib/u-boot/u-boot-2015-phicomm-n1.bin:4C@1512Mhz,2GB Mem,1Gb Net,Wifi
12:Phicomm N1 (DMA thresh):s905d:meson-gxl-s905d-phicomm-n1-thresh.dtb:u-boot-n1.bin:NA:/lib/u-boot/u-boot-2015-phicomm-n1.bin:Same as above, when ethmac flow control is off
13:hg680p & b860h:s905x:meson-gxl-s905x-p212.dtb:u-boot-p212.bin:NA:NA:4C@1512Mhz,1Gb Net
14:X96-Mini & TX3-Mini:s905w:meson-gxl-s905w-tx3-mini.dtb:u-boot-s905x-s912.bin:NA:NA:4C@1512Mhz,100M Net
# Amlogic GXM Family
21:Octopus Planet:s912:meson-gxm-octopus-planet.dtb:u-boot-zyxq.bin:NA:NA:4C@1512Mhz+4C@1000Mhz,2GB Mem,1Gb Net
# Amlogic G12A Family
31:X96 Max 4GB:s905x2:meson-g12a-x96-max.dtb:u-boot-x96max.bin:/lib/u-boot/x96max-u-boot.bin.sd.bin:NA:4C@1908Mhz,4GB Mem,1Gb Net
32:X96 Max 2GB:s905x2:meson-g12a-x96-max-rmii.dtb:u-boot-x96max.bin:/lib/u-boot/x96max-u-boot.bin.sd.bin:NA:4C@1908Mhz,2GB Mem,100M Net
# Amlogic G12B Family
41:Beelink GT-King:s922x:meson-g12b-gtking.dtb:u-boot-gtking.bin:/lib/u-boot/gtking-u-boot.bin.sd.bin:NA:2C@1800Mhz(A53)+4C@1908Mhz(A73),4GB Mem,1Gb Net,Wifi
42:Beelink GT-King Pro:s922x:meson-g12b-gtking-pro.dtb:u-boot-gtkingpro.bin:/lib/u-boot/gtkingpro-u-boot.bin.sd.bin:NA:2C@1800Mhz(A53)+4C@1908Mhz(A73),4GB Mem,1Gb Net,Wifi
43:Beelink GT-King Pro H:s922x:meson-g12b-gtking-pro-h.dtb:u-boot-gtkingpro.bin:/lib/u-boot/gtkingpro-u-boot.bin.sd.bin:NA:S922X-H,2C@1800Mhz(A53)+4C@2208Mhz(A73),4GB Mem,1Gb Net,Wifi
44:Beelink GT-King Pro Rev_A:s922x:meson-g12b-gtking-pro-rev_a.dtb:u-boot-gtkingpro-rev-a.bin::NA:2C@1800Mhz(A53)+4C@1908Mhz(A73),4GB Mem,1Gb Net,Wifi
45:Hardkernel ODroid N2:s922x:meson-g12b-odroid-n2.dtb:u-boot-gtkingpro.bin:/lib/u-boot/odroid-n2-u-boot.bin.sd.bin:NA:2C@1800Mhz(A53)+4C@1908Mhz(A73),4GB Mem,1Gb Net
46:UGOOS AM6 Plus:s922x:meson-g12b-ugoos-am6.dtb:u-boot-gtkingpro.bin:/lib/u-boot/gtkingpro-u-boot.bin.sd.bin:NA:2C@1800Mhz(A53)+4C@1908Mhz(A73),4GB Mem,1Gb Net,Wifi
# Amlogic SM1 Family
51:X96 Max+:s905x3:meson-sm1-x96-max-plus.dtb:u-boot-x96maxplus.bin:/lib/u-boot/x96maxplus-u-boot.bin.sd.bin:/lib/u-boot/hk1box-bootloader.img:4C@2100Mhz,4GB Mem,1Gb Net
52:X96 Max+ (OverClock):s905x3:meson-sm1-x96-max-plus-oc.dtb:u-boot-x96maxplus.bin:/lib/u-boot/x96maxplus-u-boot.bin.sd.bin:/lib/u-boot/hk1box-bootloader.img:4C@2208Mhz,4GB Mem,1Gb Net
53:HK1 Box:s905x3:meson-sm1-hk1box-vontar-x3.dtb:u-boot-x96maxplus.bin:/lib/u-boot/hk1box-u-boot.bin.sd.bin:NA:4C@2100Mhz,4GB Mem,1Gb Net,Wifi
54:HK1 Box (OveClock):s905x3:meson-sm1-hk1box-vontar-x3-oc.dtb:u-boot-x96maxplus.bin:/lib/u-boot/hk1box-u-boot.bin.sd.bin:NA:4C@2208Mhz,4GB Mem,1Gb Net,Wifi
55:H96 Max X3:s905x3:meson-sm1-h96-max-x3.dtb:u-boot-x96maxplus.bin:/lib/u-boot/h96maxx3-u-boot.bin.sd.bin:NA:4C@2100Mhz,4GB Mem,1Gb Net,Wifi
56:H96 Max X3:s905x3:meson-sm1-h96-max-x3-oc.dtb:u-boot-x96maxplus.bin:/lib/u-boot/h96maxx3-u-boot.bin.sd.bin:NA:4C@2208Mhz,4GB Mem,1Gb Net,Wifi
57:Ugoos X3:s905x3:meson-sm1-ugoos-x3.dtb:u-boot-ugoos-x3.bin:NA:NA:4C@2100Mhz,2(Cube)/4(Pro,Plus)GB Mem,1Gb Net,Wifi
58:Ugoos X3(OverClock):s905x3:meson-sm1-ugoos-x3-oc.dtb:u-boot-ugoos-x3.bin:NA:NA:4C@2208Mhz,2(Cube)/4(Pro,Plus)GB Mem,1Gb Net,Wifi
# Other
0:Other::NA:NA:NA:NA:Enter the dtb file name of your box
"
fi
function display_database() {
echo "${model_database}" | perl -ne 'chomp;if(m/^#/){s/#+\s+//;print "$_ >>>\n";}elsif($_){@f=split/:/;printf("%4s. %-30s%-8s%-s\n",$f[0],$f[1],$f[2],$f[7])}'
}
function search_model() {
local id=$1
local ret_count=$(echo "${model_database}" | awk -F ':' "\$1~/^$id\$/ {print \$0}" | wc -l)
if [ $ret_count -eq 1 ]; then
echo "${model_database}" | awk -F ':' "\$1~/^$id\$/ {print \$0}"
fi
}
echo "Please select s9xxx box model:"
echo "----------------------------------------------------------------------------------------------------"
display_database
echo "----------------------------------------------------------------------------------------------------"
# For [luci-app-amlogic] input parameter: SOC & DTB
# When there is no input parameter, select manually
if [[ -n "${SPECIFY_SOC}" ]]; then
boxtype=${SPECIFY_SOC}
else
echo -n "Please choose: "
read boxtype
fi
if [ "$boxtype" == "99" ]; then
FDTFILE=${SPECIFY_DTB}
UBOOT_OVERLOAD=""
MAINLINE_UBOOT=""
ANDROID_UBOOT=""
AMLOGIC_SOC=""
else
ret=$(search_model $boxtype)
if [ "$ret" == "" ]; then
echo "Input error, exit!"
exit 1
fi
# 3. soc
# 4. FDTFILE
# 5. UBOOT_OVERLOAD
# 6. MAINLINE_UBOOT
# 7. ANDROID_UBOOT
AMLOGIC_SOC=$(echo "$ret" | awk -F ':' '{print $3}' | perl -pe 's/NA//;s/NULL//;s/\s+//g;')
FDTFILE=$(echo "$ret" | awk -F ':' '{print $4}' | perl -pe 's/NA//;s/NULL//;s/\s+//g;')
UBOOT_OVERLOAD=$(echo "$ret" | awk -F ':' '{print $5}' | perl -pe 's/NA//;s/NULL//;s/\s+//g;')
MAINLINE_UBOOT=$(echo "$ret" | awk -F ':' '{print $6}' | perl -pe 's/NA//;s/NULL//;s/\s+//g;')
ANDROID_UBOOT=$(echo "$ret" | awk -F ':' '{print $7}' | perl -pe 's/NA//;s/NULL//;s/\s+//g;')
fi
if [ "$FDTFILE" == "" ]; then
cat <<EOF
Please enter the .dtb file name of your box, do not include the path.
For example: $DEFAULT_FDTFILE
EOF
echo "Enter the .dtb File name:"
read CUST_FDTFILE
FDTFILE=$CUST_FDTFILE
UBOOT_OVERLOAD=""
MAINLINE_UBOOT=""
ANDROID_UBOOT=""
AMLOGIC_SOC=""
fi
echo -e "FDT Value [ ${FDTFILE} ]"
if [ ! -f "/boot/dtb/amlogic/${FDTFILE}" ]; then
echo "/boot/dtb/amlogic/${FDTFILE} does not exist!"
echo "You can download the .dtb file from [ https://github.com/ophub/amlogic-s9xxx-openwrt/tree/main/amlogic-s9xxx/amlogic-dtb ]"
echo "Copy it to [ /boot/dtb/amlogic/ ]."
echo "Then execute this Install command."
exit 1
fi
#Check if writing to EMMC is supported
MODULES_NOW=$(ls /lib/modules/ 2>/dev/null)
VERSION_NOW=$(echo ${MODULES_NOW} | grep -oE '^[1-9].[0-9]{1,3}' 2>/dev/null)
echo -e "This Kernel [ ${MODULES_NOW} ]"
k510_ver=${VERSION_NOW%%.*}
k510_maj=${VERSION_NOW##*.}
if [ "${k510_ver}" -eq "5" ]; then
if [ "${k510_maj}" -ge "10" ]; then
K510="1"
else
K510="0"
fi
elif [ "${k510_ver}" -gt "5" ]; then
K510="1"
else
K510="0"
fi
# backup old bootloader
if [ ! -f "/root/BackupOldBootloader.img" ]; then
echo "Backup bootloader -> [ BackupOldBootloader.img ] ... "
dd if=/dev/$EMMC_NAME of=/root/BackupOldBootloader.img bs=1M count=4 conv=fsync
echo "Backup bootloader complete."
echo
fi
swapoff -a
# umount all other mount points
MOUNTS=$(lsblk -l -o MOUNTPOINT)
for mnt in $MOUNTS; do
if [ "$mnt" == "MOUNTPOINT" ]; then
continue
fi
if [ "$mnt" == "" ]; then
continue
fi
if [ "$mnt" == "/" ]; then
continue
fi
if [ "$mnt" == "/boot" ]; then
continue
fi
if [ "$mnt" == "/opt" ]; then
continue
fi
if [ "$mnt" == "[SWAP]" ]; then
echo "swapoff -a"
swapoff -a
continue
fi
if echo $mnt | grep $EMMC_NAME; then
echo "umount -f $mnt"
umount -f $mnt
if [ $? -ne 0 ]; then
echo "$mnt Cannot be uninstalled, the installation process is aborted."
exit 1
fi
fi
done
# Delete old partition if exists
p=$(lsblk -l | grep -e "${EMMC_NAME}p" | wc -l)
echo "A total of [ $p ] old partitions on EMMC will be deleted"
>/tmp/fdisk.script
while [ $p -ge 1 ]; do
echo "d" >>/tmp/fdisk.script
if [ $p -gt 1 ]; then
echo "$p" >>/tmp/fdisk.script
fi
p=$((p - 1))
done
# you can change ROOT size(MB) >= 320
ROOT1=960
ROOT2=960
if [[ "${AMLOGIC_SOC}" == "s912" || "${AMLOGIC_SOC}" == "s905d" ]]; then
BOOT=512
BLANK1=68
BLANK2=184
BLANK3=0
BLANK4=0
elif [[ "${AMLOGIC_SOC}" == "s905x" ]]; then
BOOT=160
BLANK1=700
BLANK2=0
BLANK3=0
BLANK4=0
else
BOOT=160
BLANK1=68
BLANK2=0
BLANK3=162
BLANK4=0
fi
DST_TOTAL_MB=$((EMMC_SIZE / 1024 / 1024))
start1=$((BLANK1 * 2048))
end1=$((start1 + (BOOT * 2048) - 1))
start2=$(((BLANK2 * 2048) + end1 + 1))
end2=$((start2 + (ROOT1 * 2048) - 1))
start3=$(((BLANK3 * 2048) + end2 + 1))
end3=$((start3 + (ROOT2 * 2048) - 1))
start4=$(((BLANK4 * 2048) + end3 + 1))
end4=$((DST_TOTAL_MB * 2048 - 1))
cat >>/tmp/fdisk.script <<EOF
n
p
1
$start1
$end1
n
p
2
$start2
$end2
n
p
3
$start3
$end3
n
p
$start4
$end4
t
1
c
t
2
83
t
3
83
t
4
83
w
EOF
fdisk /dev/${EMMC_NAME} </tmp/fdisk.script 2>/dev/null
if [ $? -ne 0 ]; then
echo "The fdisk partition fails, Please try again."
dd if=/root/BackupOldBootloader.img of=/dev/${EMMC_NAME} conf=fsync && sync
dd if=/dev/zero of=/dev/${EMMC_NAME} bs=512 count=1 && sync
exit 1
fi
echo "Partition complete."
# write some zero data to part begin
seek=$((start1 / 2048))
dd if=/dev/zero of=/dev/${EMMC_NAME} bs=1M count=1 seek=$seek conv=fsync
seek=$((start2 / 2048))
dd if=/dev/zero of=/dev/${EMMC_NAME} bs=1M count=1 seek=$seek conv=fsync
seek=$((start3 / 2048))
dd if=/dev/zero of=/dev/${EMMC_NAME} bs=1M count=1 seek=$seek conv=fsync
seek=$((start4 / 2048))
dd if=/dev/zero of=/dev/${EMMC_NAME} bs=1M count=1 seek=$seek conv=fsync
#Mainline U-BOOT detection
FLASH_MAINLINE_UBOOT=0
if [[ -n "${MAINLINE_UBOOT}" && -f "${MAINLINE_UBOOT}" ]]; then
cat <<EOF
----------------------------------------------------------------------------------
Found an available mainline bootloader (Mainline u-boot), you can flash into EMMC.
----------------------------------------------------------------------------------
EOF
while :; do
# For [luci-app-amlogic] input parameter: SOC & DTB
# When there is no input parameter, select manually
if [[ "${AUTO_MAINLINE_UBOOT}" == "yes" ]]; then
if [[ "${K510}" -eq "1" ]]; then
yn="y"
else
yn="n"
fi
elif [[ "${AUTO_MAINLINE_UBOOT}" == "no" ]]; then
yn="n"
else
read -p "Please choose whether to write the mainline bootloader to EMMC? y/n " yn
fi
case $yn in
y | Y)
FLASH_MAINLINE_UBOOT=1
break
;;
n | N)
FLASH_MAINLINE_UBOOT=0
break
;;
esac
done
fi
if [[ "${FLASH_MAINLINE_UBOOT}" -eq "1" ]]; then
echo -e "Write Mainline bootloader: [ ${MAINLINE_UBOOT} ]"
dd if=${MAINLINE_UBOOT} of=/dev/${EMMC_NAME} bs=1 count=444 conv=fsync
dd if=${MAINLINE_UBOOT} of=/dev/${EMMC_NAME} bs=512 skip=1 seek=1 conv=fsync
elif [[ -n "${ANDROID_UBOOT}" && -f "${ANDROID_UBOOT}" ]]; then
echo -e "Write Android bootloader: [ ${ANDROID_UBOOT} ]"
dd if=${ANDROID_UBOOT} of=/dev/${EMMC_NAME} bs=1 count=444 conv=fsync
dd if=${ANDROID_UBOOT} of=/dev/${EMMC_NAME} bs=512 skip=1 seek=1 conv=fsync
else
echo "Did not change the original bootloader."
fi
# fix wifi macaddr
if [ -x /usr/bin/fix_wifi_macaddr.sh ]; then
/usr/bin/fix_wifi_macaddr.sh
fi
# mkfs
echo "Start creating file system ... "
echo "Create a boot file system ... "
echo "format boot partiton..."
mkfs.fat -n EMMC_BOOT -F 32 /dev/${EMMC_NAME}p1
mkdir -p /mnt/${EMMC_NAME}p1
sleep 2
umount -f /mnt/${EMMC_NAME}p1 2>/dev/null
echo "format rootfs1 partiton..."
ROOTFS1_UUID=$(/usr/bin/uuidgen)
mkfs.btrfs -f -U ${ROOTFS1_UUID} -L EMMC_ROOTFS1 -m single /dev/${EMMC_NAME}p2
mkdir -p /mnt/${EMMC_NAME}p2
sleep 2
umount -f /mnt/${EMMC_NAME}p2 2>/dev/null
echo "format rootfs2 partiton..."
ROOTFS2_UUID=$(/usr/bin/uuidgen)
mkfs.btrfs -f -U ${ROOTFS2_UUID} -L EMMC_ROOTFS2 -m single /dev/${EMMC_NAME}p3
mkdir -p /mnt/${EMMC_NAME}p3
sleep 2
umount -f /mnt/${EMMC_NAME}p3 2>/dev/null
# mount and copy
echo "Wait for the boot file system to mount ... "
i=1
max_try=10
while [ $i -le $max_try ]; do
mount -t vfat /dev/${EMMC_NAME}p1 /mnt/${EMMC_NAME}p1 2>/dev/null
sleep 2
mnt=$(lsblk -l -o MOUNTPOINT | grep /mnt/${EMMC_NAME}p1)
if [ "$mnt" == "" ]; then
if [ $i -lt $max_try ]; then
echo "Not mounted successfully, try again ..."
i=$((i + 1))
else
echo "Cannot mount the boot file system, give up!"
exit 1
fi
else
echo "Successfully mounted."
echo "copy boot ..."
cd /mnt/${EMMC_NAME}p1
rm -rf /boot/'System Volume Information/'
(cd /boot && tar cf - .) | tar xf -
sync
echo "Edit uEnv.txt ..."
cat >uEnv.txt <<EOF
LINUX=/zImage
INITRD=/uInitrd
FDT=/dtb/amlogic/${FDTFILE}
APPEND=root=UUID=${ROOTFS1_UUID} rootfstype=btrfs rootflags=compress=zstd:${ZSTD_LEVEL} console=ttyAML0,115200n8 console=tty0 no_console_suspend consoleblank=0 fsck.fix=yes fsck.repair=yes net.ifnames=0 cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory swapaccount=1
EOF
rm -f s905_autoscript* aml_autoscript*
if [ ${K510} -eq 1 ]; then
if [ -f ${UBOOT_OVERLOAD} ]; then
cp -f -v ${UBOOT_OVERLOAD} u-boot.emmc
elif [ -f "u-boot.ext" ]; then
cp -f -v u-boot.ext u-boot.emmc
fi
fi
mv -f boot-emmc.ini boot.ini
mv -f boot-emmc.cmd boot.cmd
mv -f boot-emmc.scr boot.scr
sync
echo "complete."
cd /
umount -f /mnt/${EMMC_NAME}p1
break
fi
done
echo "complete."
echo "Wait for the rootfs file system to mount ... "
i=1
while [ $i -le $max_try ]; do
mount -t btrfs -o compress=zstd:${ZSTD_LEVEL} /dev/${EMMC_NAME}p2 /mnt/${EMMC_NAME}p2 2>/dev/null
sleep 2
mnt=$(lsblk -l -o MOUNTPOINT | grep /mnt/${EMMC_NAME}p2)
if [ "$mnt" == "" ]; then
if [ $i -lt $max_try ]; then
echo "Not mounted successfully, try again ..."
i=$((i + 1))
else
echo "Cannot mount rootfs file system, give up!"
exit 1
fi
else
echo "Successfully mounted"
echo "Create folder ... "
cd /mnt/${EMMC_NAME}p2
btrfs subvolume create etc
mkdir -p bin boot dev lib opt mnt overlay proc rom root run sbin sys tmp usr www .reserved .snapshots
ln -sf lib/ lib64
ln -sf tmp/ var
sync
echo "complete."
COPY_SRC="root etc bin sbin lib opt usr www"
echo "Copy data ... "
for src in $COPY_SRC; do
echo "copy [ $src ] ..."
(cd / && tar cf - $src) | tar xf -
sync
done
echo "Copy complete."
sync
echo "Update the relevant parameters of flippy-openwrt-release"
sed -i '/FDTFILE/d' etc/flippy-openwrt-release 2>/dev/null
echo "FDTFILE='${FDTFILE}'" >>etc/flippy-openwrt-release 2>/dev/null
sed -i '/UBOOT_OVERLOAD/d' etc/flippy-openwrt-release 2>/dev/null
echo "UBOOT_OVERLOAD='${UBOOT_OVERLOAD}'" >>etc/flippy-openwrt-release 2>/dev/null
sed -i '/MAINLINE_UBOOT/d' etc/flippy-openwrt-release 2>/dev/null
echo "MAINLINE_UBOOT='${MAINLINE_UBOOT}'" >>etc/flippy-openwrt-release 2>/dev/null
sed -i '/ANDROID_UBOOT/d' etc/flippy-openwrt-release 2>/dev/null
echo "ANDROID_UBOOT='${ANDROID_UBOOT}'" >>etc/flippy-openwrt-release 2>/dev/null
sed -i '/SOC/d' etc/flippy-openwrt-release 2>/dev/null
echo "SOC='${AMLOGIC_SOC}'" >>etc/flippy-openwrt-release 2>/dev/null
echo "Update complete."
sync
cat >etc/docker/daemon.json <<EOF
{
"bip": "172.31.0.1/24",
"data-root": "/mnt/${EMMC_NAME}p4/docker/",
"log-level": "warn",
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "5"
},
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn",
"https://registry.cn-shanghai.aliyuncs.com",
"https://hub-mirror.c.163.com"
]
}
EOF
# change data_root value in /etc/config/dockerd
if [ -f ./etc/init.d/dockerman ] && [ -f ./etc/config/dockerd ];then
tmp_dockerd=$(mktemp)
perl -npe "s/option data_root '.+'/option data_root '\/mnt\/${EMMC_NAME}p4\/docker\/'/" ./etc/config/dockerd > ${tmp_dockerd} && mv ${tmp_dockerd} ./etc/config/dockerd
unset tmp_dockerd
fi
rm -rf opt/docker && ln -sf /mnt/${EMMC_NAME}p4/docker/ opt/docker >/dev/null
rm -rf usr/bin/AdGuardHome && ln -sf /mnt/${EMMC_NAME}p4/AdGuardHome usr/bin/ >/dev/null
echo "Edit configuration file ..."
#cd /mnt/${EMMC_NAME}p2/usr/bin/
#rm -f openwrt-install-amlogic openwrt-update-amlogic
cd /mnt/${EMMC_NAME}p2/etc/rc.d
ln -sf ../init.d/dockerd S99dockerd
rm -f S??shortcut-fe
if grep "sfe_flow '1'" ../config/turboacc >/dev/null; then
if find ../../lib/modules -name 'shortcut-fe-cm.ko'; then
ln -sf ../init.d/shortcut-fe S99shortcut-fe
fi
fi
cd /mnt/${EMMC_NAME}p2/etc
cat >fstab <<EOF
UUID=${ROOTFS1_UUID} / btrfs compress=zstd:${ZSTD_LEVEL} 0 1
LABEL=EMMC_BOOT /boot vfat defaults 0 2
#tmpfs /tmp tmpfs defaults,nosuid 0 0
EOF
cd /mnt/${EMMC_NAME}p2/etc/config
cat >fstab <<EOF
config global
option anon_swap '0'
option anon_mount '1'
option auto_swap '0'
option auto_mount '1'
option delay_root '5'
option check_fs '0'
config mount
option target '/overlay'
option uuid '${ROOTFS1_UUID}'
option enabled '1'
option enabled_fsck '1'
option fstype 'btrfs'
option options 'compress=zstd:${ZSTD_LEVEL}'
config mount
option target '/boot'
option label 'EMMC_BOOT'
option enabled '1'
option enabled_fsck '1'
option fstype 'vfat'
EOF
echo -n "Create initial etc snapshot -> .snapshots/etc-000"
cd /mnt/${EMMC_NAME}p2 &&
btrfs subvolume snapshot -r etc .snapshots/etc-000
sync
cd /
umount -f /mnt/${EMMC_NAME}p2
break
fi
done
echo "complete."
echo "Create a shared file system."
mkdir -p /mnt/${EMMC_NAME}p4
# When there is no input parameter, select manually
if [[ -n "${SHARED_FSTYPE}" ]]; then
TARGET_SHARED_FSTYPE=${SHARED_FSTYPE}
else
cat <<EOF
---------------------------------------------------------------------------------
Please select the type of shared file system:
1. ext4: [Default options] suitable for general use.
2. btrfs: Which can extend the service life of ssd/mmc.
3. f2fs: Fast reading and writing speed, but the compatibility is slightly poor.
4. xfs: Very good file system, alternative to ext4.
---------------------------------------------------------------------------------
EOF
read TARGET_SHARED_FSTYPE
fi
case $TARGET_SHARED_FSTYPE in
2 | btrfs)
mkfs.btrfs -f -L EMMC_SHARED -m single /dev/${EMMC_NAME}p4 >/dev/null
mount -t btrfs /dev/${EMMC_NAME}p4 /mnt/${EMMC_NAME}p4
;;
3 | f2fs)
mkfs.f2fs -f -l EMMC_SHARED /dev/${EMMC_NAME}p4 >/dev/null
mount -t f2fs /dev/${EMMC_NAME}p4 /mnt/${EMMC_NAME}p4
;;
4 | xfs)
mkfs.xfs -f -L EMMC_SHARED /dev/${EMMC_NAME}p4 >/dev/null
mount -t xfs /dev/${EMMC_NAME}p4 /mnt/${EMMC_NAME}p4
;;
*)
mkfs.ext4 -F -L EMMC_SHARED /dev/${EMMC_NAME}p4 >/dev/null
mount -t ext4 /dev/${EMMC_NAME}p4 /mnt/${EMMC_NAME}p4
;;
esac
mkdir -p /mnt/${EMMC_NAME}p4/docker /mnt/${EMMC_NAME}p4/AdGuardHome/data
sync
echo "Successful installed, please unplug the USB, re-insert the power supply to start the openwrt."
exit 0