forked from pocopico/tinycore-redpill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rploader.sh
3854 lines (3023 loc) · 146 KB
/
rploader.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
#
# Author :
# Date : 230607
# Version : 0.9.4.9
#
#
# User Variables :
rploaderver="0.9.4.9"
build="main"
redpillmake="prod"
rploaderfile="https://raw.githubusercontent.com/pocopico/tinycore-redpill/$build/rploader.sh"
rploaderrepo="https://github.com/pocopico/tinycore-redpill/raw/$build/"
rploadergit="https://github.com/pocopico/tinycore-redpill.git"
redpillextension="https://github.com/pocopico/rp-ext/raw/main/redpill${redpillmake}/rpext-index.json"
modextention="https://github.com/pocopico/rp-ext/raw/main/rpext-index.json"
modalias4="https://raw.githubusercontent.com/pocopico/tinycore-redpill/$build/modules.alias.4.json.gz"
modalias3="https://raw.githubusercontent.com/pocopico/tinycore-redpill/$build/modules.alias.3.json.gz"
dtcbin="https://raw.githubusercontent.com/pocopico/tinycore-redpill/$build/tools/dtc"
dtsfiles="https://raw.githubusercontent.com/pocopico/tinycore-redpill/$build"
timezone="UTC"
ntpserver="pool.ntp.org"
userconfigfile="/home/tc/user_config.json"
CUSTOMCONFIG="/home/tc/custom_config.json"
HOMEPATH="/home/tc"
fullupdatefiles="${CUSTOMCONFIG} custom_config.json custom_config_jun.json global_config.json modules.alias.3.json.gz modules.alias.4.json.gz rpext-index.json user_config.json rploader.sh"
# END Do not modify after this line
######################################################################################################
# extract nano LD_LIBRARY_PATH=/home/tc/archive/lib /home/tc/archive/synoarchive.nano -xvf ../synology_geminilake_dva1622.pat
function history() {
cat <<EOF
--------------------------------------------------------------------------------------
0.7.0.0 Added build for version greater than 42218
0.7.0.1 Added required extension parsing adding and downloading
0.7.0.2 Added usb patch in patchdtc
0.7.0.3 Added portnumber on patchdtc
0.7.0.4 Make sure that local cache folder is created early in the process
0.7.0.5 Enabled interactive
0.7.0.6 Added save/restore session functions
0.7.0.7 Added a check date function
0.7.0.8 Added the ability to use local dtb file
0.7.0.9 Added flyride satamap review
0.7.1.0 Added the history, version and enhanced patchdtc function
0.7.1.1 Added a syntaxcheck function
0.7.1.2 Added sync time with NTP server : pool.ntp.org (Set timezone and ntpserver variables accordingly )
0.7.1.3 Added the option to create JUN mod loader (By Jumkey)
0.7.1.4 Added the use of the additional custom_config_jun.json for JUN mod loader creation
0.7.1.5 Updated satamap function to support higher the 9 port counts per HBA.
0.7.1.6 Updated satamap function to fix the broken q35 KVM controller, and to stop scanning for CD-ROM's
0.7.1.7 Updated serialgen function to include the option for using the realmac
0.7.1.8 Updated satamap function to fine tune SATA port identification and identify SATABOOT
0.7.1.9 Updated patchdtc function to fix wrong port identification for VMware hosted systems
0.8.0.0 Stable version. All new features will be moved to develop repo
0.9.0.0 Development version. Moving all new features to development build
0.9.0.1 Updated postupdate to facilitate update to update2
0.9.0.2 Added system monitor function
0.9.0.3 Updated satamap to support DUMMY PORT detection
0.9.0.4 More satamap fixes
0.9.0.5 Added the option to get grub variables into user_config.json
0.9.0.6 Experimental DVA1622 (geminilake) addition
0.9.0.7 Experimental DVA1622 serialgen
0.9.0.8 Experimental DVA1622 increase disk count to 16
0.9.0.9 Fixed missing bspatch
0.9.1.0 Added dtc depth patch
0.9.1.1 Default action for DTB system is to use the dtbpatch by fbelavenuto
0.9.1.2 Fixed a jq issue in listextension
0.9.1.3 Fixed bsdiff not found issue
0.9.1.4 Fixed overlaping downloadextractor processes
0.9.1.5 Enhanced postupdate process to update user_config.json to new format
0.9.1.6 Fixed compressed non-compressed RAMDISK issue
0.9.1.7 Enhanced build process to update user_config.json during build process
0.9.1.8 Enhanced build process to create friend files
0.9.1.9 Further enhanced build process
0.9.2.0 Introducing TCRP Friend
0.9.2.1 If TCRP Friend is used then default option will be TCRP Friend
0.9.2.2 Upgrade your system by adding TCRP Friend with command bringfriend
0.9.2.3 Adding experimental DS2422+ support
0.9.2.4 Added the redpillmake variable to select between prod and dev modules
0.9.2.5 Adding experimental RS4021xs+ support
0.9.2.6 Added the downloadupgradepat action **experimental
0.9.2.7 Added setting the static network configuration for TCRP Friend
0.9.2.8 Changed all curl calls to use the --insecure flag to avoid expired certificate issues
0.9.2.9 Added the smallfixnumber key in user_config.json and changed the platform ids to model ids
0.9.3.0 Changed set root entry to search for FS UUID
0.9.4.0 Added experimental DS923+ model, added new extensions handler functions
0.9.4.1 Fixed missing serian and mac if user has not taken that into account.
0.9.4.2 Added serial numbers prefixes for DS923+ and DS1522+
0.9.4.3 Added serial numbers prefixes for SA6400, added class 104 to listpci function
0.9.4.4 Added cmdmonitor function
0.9.4.5 Changed the way the system is updated to use the new build method
0.9.4.6 Updated configuration files, minor fixes, changed from 64551 to 64561
0.9.4.7 Added missing configs, Added support for RS3413xs+
0.9.4.8 Updated httpconf to support php uploader
0.9.4.9 Included DS1019+ DS1520+ DS1621xs+ DS723+
--------------------------------------------------------------------------------------
EOF
}
function cmdmonitor() {
echo "TCRP Command Monitoring daemon"
touch cmdout
while true; do
cmd="$(cat $HOME/cmdout)"
if [ -z $cmd ]; then
nocommand="yes"
else
echo "Command to execute : $cmd" >>$HOME/cmdout.log
$cmd | tee -a $HOME/cmdout.log
>$HOME/cmdout
fi
done
}
function setnetwork() {
if [ -f /opt/eth*.sh ] && [ "$(grep dhcp /opt/eth*.sh | wc -l)" -eq 0 ]; then
ipset="static"
ipgw="$(route | grep default | head -1 | awk '{print $2}')"
ipprefix="$(grep ifconfig /opt/eth*.sh | head -1 | awk '{print "ipcalc -p " $3 " " $5 }' | sh - | awk -F= '{print $2}')"
myip="$(grep ifconfig /opt/eth*.sh | head -1 | awk '{print $3 }')"
ipaddr="${myip}/${ipprefix}"
ipgw="$(grep route /opt/eth*.sh | head -1 | awk '{print $5 }')"
ipdns="$(grep nameserver /opt/eth*.sh | head -1 | awk '{print $3 }')"
ipproxy="$(env | grep -i http | awk -F= '{print $2}' | uniq)"
for field in ipset ipaddr ipgw ipdns ipproxy; do
jsonfile=$(jq ".ipsettings+={\"$field\":\"${!field}\"}" $userconfigfile)
echo $jsonfile | jq . >$userconfigfile
done
fi
}
function httpconf() {
tce-load -iw php-8.0-cgi 2>&1 >/dev/null
tce-load -iw lighttpd 2>&1 >/dev/null
sudo cp /home/tc/include/php.ini /usr/local/etc/php/
cat >/home/tc/lighttpd.conf <<EOF
server.document-root = "/home/tc/html"
server.modules = ( "mod_cgi" , "mod_alias" )
server.errorlog = "/home/tc/html/error.log"
server.pid-file = "/home/tc/html/lighttpd.pid"
server.username = "tc"
server.groupname = "staff"
server.port = 80
alias.url = ( "/rploader/" => "/home/tc/html/" )
cgi.assign = ( ".sh" => "/usr/local/bin/bash", ".php" => "/usr/local/bin/php-cgi" )
index-file.names = ( "index.sh", "index.html","index.htm" )
EOF
ps -ef | grep -i light | grep -v grep | awk '{print $1}' | xargs kill -9
sudo lighttpd -f /home/tc/lighttpd.conf
[ $(sudo netstat -an 2>/dev/null | grep LISTEN | grep ":80" 2>/dev/null | wc -l) -eq 1 ] && echo "Server started succesfully"
# Add entry to bootlocal and backup
# cat /opt/bootlocal.sh
}
## New ext manager
function extadd() {
extvars $1 $2
[ ! -d payload ] && mkdir payload
cd payload
[ ! -f platform ] && echo "$platform" >platform
[ -f extensions ] && [ $(grep $extid extensions | wc -l) -gt 0 ] && echo "Extension $extid has been already added" && return
echo -n "Adding $extid"
[ ! -d $extid ] && mkdir $extid
echo "$ext" >$extid/rpext-index.json
echo "$extid" >>extensions
echo " -> Done"
}
function extremove() {
extvars $1 $2
[ ! -d payload ] && mkdir payload
cd payload
[ -f extensions ] && [ $(grep $extid extensions | wc -l) -gt 0 ] && echo "Extension $extid will be removed"
echo "Removing $extid payload" && rm -rf $extid
sed -i "/$extid/d" extensions
sed -i "/$extid/d" on_boot.sh
sed -i "/$extid/d" on_os_load.sh
}
function extvars() {
ext="$(curl --silent --location $1)"
platform="$2"
[ $(echo $ext | grep 404 | wc -l) -eq 1 ] && echo "Extension not found" && exit 1
if [ -f platform ] && [ ! "$(cat platform)" == "$platform" ]; then
echo "Payload already has extensions for $(cat platform), using platform $(cat platform)"
platform="$(cat platform)"
extcontents="$(echo $ext | jq -r -e ".releases .$platform")"
else
extcontents="$(echo $ext | jq -r -e ".releases .$2")"
fi
extid="$(echo $ext | jq -r -e .id)"
extrelease="$(curl --silent --location $extcontents)"
[ $(echo $extrelease | jq . | wc -l) -eq 0 ] && echo "Extension does not contain information about platform $2" && exit 1
payload="$(echo $extrelease | jq -r -e ".files[]")"
}
function processexts() {
cd payload
for ext in $(cat extensions); do
extvars "file://$PWD/$ext/rpext-index.json" "$(cat platform)"
echo "Downloading extension $extid payload for platform $platform"
files="$(echo $extrelease | jq -r -e '.files[] .name')"
#echo "Found files : $files"
for file in $files; do
name=$(echo $extrelease | jq -r -e ".files[] | select(.name | contains(\"$file\")) .name")
download=$(echo $extrelease | jq -r -e ".files[] | select(.name | contains(\"$file\")) .url")
modules="$(echo $extrelease | jq -r -e '.kmods')"
echo " Downloading : $name "
cd $extid && curl --silent --location $download -O && cd ..
packed=$(echo $extrelease | jq -r -e ".files[] | select(.name | contains(\"$file\")) .packed")
if [ "$packed" == "true" ]; then
echo "File $name , is packed, extracting"
if [ -f $extid/$name ] && [ $(echo $modules | grep ko | wc -l) -gt 0 ]; then
[ ! -f "mods_load.sh" ] && touch mods_load.sh && echo "#!/usr/bin/env sh" >>mods_load.sh
hasmodules=$(tar --wildcards *.ko -tvf $extid/$name | wc -l)
echo "File contains $hasmodules modules, copying to modules folder"
[ ! -d modules ] && mkdir modules
tar xf $extid/$name -C modules && rm $extid/$name
for mod in $(echo "$modules" | grep ko | sed -e "s/\"//g" | sed -e "s/://g" | sed -e "s/,//g" | awk '{print $1}'); do
if [ $(grep $mod mods_load.sh | wc -l) -eq 0 ]; then
modname=$(basename $mod .ko)
echo "adding module $modname"
echo "echo -n \":: Loading module $modname ... \" && /sbin/insmod modules/$mod && [ $(lsmod | grep -i $modname | wc -l) -gt 0 ] && echo \"[ OK ]\" || echo \"[ FAIL ]\"" >>mods_load.sh
fi
done
else
tar xfz $extid/$name -C $extid && rm $extid/$name
fi
fi
done
touch on_boot.sh && touch on_os_load.sh
onboot="$(echo $extrelease | jq -r -e '.scripts .on_boot')"
onosload="$(echo $extrelease | jq -r -e '.scripts .on_os_load')"
if [ $(echo $onboot | wc -l) -gt 0 ] && [ $(grep $extid on_boot.sh | wc -l) -eq 0 ] && [ "$onboot" != "null" ]; then
echo "Adding boot script"
echo "cd $extid && ./$onboot && cd .." >>on_boot.sh
fi
if [ $(echo $onosload | wc -l) -gt 0 ] && [ $(grep $extid on_os_load.sh | wc -l) -eq 0 ] && [ "$onosload" != "null" ]; then
echo "Adding os load script"
echo "echo -n \":: Executing $extid os load scripts ... \" && cd $extid && ./$onosload && cd .. " >>on_os_load.sh
fi
done
chmod 777 *.sh */*.sh
}
function createcustominitfile() {
echo "Creating custom initrd structure"
mkdir -p customtemp && cd customtemp
mkdir -p usr/lib/modules/
mkdir -p usr/sbin/
#### CREATE modprobe file
cat <<EOF >usr/sbin/modprobe
#!/usr/bin/sh
for arg in "\$@"
do
if [ "\$arg" = "elevator-iosched" ]; then
/sbin/insmod /usr/lib/modules/rp.ko
rm /usr/lib/modules/rp.ko
rm /sbin/modprobe
exit 0
fi
done
exit 1
EOF
chmod 777 usr/sbin/modprobe
echo "getredpillmodule and place it under usr/lib/modules/"
mkdir -p exts && cp -arfp /home/tc/payload/* exts/
#### CREATE exec.sh
cat <<EOF >exts/exec.sh
#!/usr/bin/env sh
cd "$(dirname "\${0}")" || exit 1 # get to the script directory realiably in POSIX
PLATFORM_ID="$SYNOMODEL"
EXTENSION_IDS="dtbpatch pocopico.mptspi pocopico.vmxnet3 redpill-boot-wait redpill-misc "
_load_kmods(){
echo ":: Loading custom modules... [ OK ]"
./mods_load.sh
}
_run_scripts(){
case \$1 in
on_boot)
echo "Executing Junior scripts"
./on_boot.sh
;;
on_os_load)
echo "Executing OS load scripts"
./on_os_load.sh
;;
esac
}
cd /exts
case \$1 in
load_kmods)
_load_kmods
;;
on_boot_scripts)
_run_scripts 'on_boot'
;;
on_os_load_scripts)
_run_scripts 'on_os_load'
;;
*)
if [ \$# -lt 1 ]; then
echo "Usage: \$0 ACTION_NAME <...args>"
else
echo "Invalid ACTION_NAME=\${1}"
fi
exit 1
;;
esac
EOF
chmod 777 exts/exec.sh
echo "Creating custom.gz file and placing it in place"
sudo find . | sudo cpio -o -H newc -R root:root >../custom.gz
cd ..
echo "Cleaning up ..." && rm -rf customtemp
}
### End of new ext manager
function getgrubconf() {
tcrpdisk="$(mount | grep -i optional | grep cde | awk -F / '{print $3}' | uniq | cut -c 1-3)"
grubdisk="${tcrpdisk}1"
echo "Mounting bootloader disk to get grub contents"
sudo mount /dev/$grubdisk
if [ $(df | grep -i $grubdisk | wc -l) -gt 0 ]; then
echo -n "Mounted succesfully : $(df -h | grep $grubdisk)"
[ -f /mnt/$grubdisk/boot/grub/grub.cfg ] && [ $(cat /mnt/$grubdisk/boot/grub/grub.cfg | wc -l) -gt 0 ] && echo " -> Grub cfg is accessible and readable"
else
echo "Couldnt mount device : $grubdisk "
exit 99
fi
echo "Getting known loader grub variables"
grep pid /mnt/$grubdisk/boot/grub/grub.cfg >/tmp/grub.vars
while IFS=" " read -r -a line; do
printf "%s\n" "${line[@]}"
done </tmp/grub.vars | egrep -i "sataportmap|sn|pid|vid|mac|hddhotplug|diskidxmap|netif_num" | sort | uniq >/tmp/known.vars
if [ -f /tmp/known.vars ]; then
echo "Sourcing vars, found in grub : "
. /tmp/known.vars
rows="%-15s| %-15s | %-10s | %-10s | %-10s | %-15s | %-15s %c\n"
printf "$rows" Serial Mac Netif_num PID VID SataPortMap DiskIdxMap
printf "$rows" $sn $mac1 $netif_num $pid $vid $SataPortMap $DiskIdxMap
echo "Checking user config against grub vars"
for var in pid vid sn mac1 SataPortMap DiskIdxMap; do
if [ $(jq -r .extra_cmdline.$var user_config.json) == "${!var}" ]; then
echo "Grub var $var = ${!var} Matches your user_config.json"
else
echo "Grub var $var = ${!var} does not match your user_config.json variable which is set to : $(jq -r .extra_cmdline.$var user_config.json) "
echo "Should we populate user_config.json with these variables ? [Yy/Nn] "
read answer
if [ -n "$answer" ] && [ "$answer" = "Y" ] || [ "$answer" = "y" ]; then
json="$(jq --arg newvar "${!var}" '.extra_cmdline.'$var'= $newvar' user_config.json)" && echo -E "${json}" | jq . >user_config.json
else
echo "OK, you can edit yourself later"
fi
fi
done
else
echo "Could not read variables"
fi
}
function monitor() {
loaderdisk="$(mount | grep -i optional | grep cde | awk -F / '{print $3}' | uniq | cut -c 1-3)"
mount /dev/${loaderdisk}1
mount /dev/${loaderdisk}2
NETGW="$(route | grep -i def | awk '{print $2}')"
ping -c 5 $NEWGW >/dev/null &
[ ! -d /lib64 ] && sudo ln -s /lib /lib64
sudo chown -R tc:staff /home/tc >/dev/null 2>&1
sudo chown -R tc:staff /opt >/dev/null 2>&1
while [ -z "$GATEWAY_INTERFACE" ]; do
clear
echo -e "-------------------------------System Information----------------------------"
echo -e "Hostname:\t\t"$(hostname) "uptime:\t\t\t"$(uptime | awk '{print $3,$4}' | sed 's/,//')
echo -e "Manufacturer:\t\t"$(cat /sys/class/dmi/id/chassis_vendor) "Product Name:\t\t"$(cat /sys/class/dmi/id/product_name)
echo -e "Version:\t\t"$(cat /sys/class/dmi/id/product_version)
echo -e "Serial Number:\t\t"$(sudo cat /sys/class/dmi/id/product_serial)
echo -e "Machine Type:\t\t"$(
vserver=$(lscpu | grep Hypervisor | wc -l)
if [ $vserver -gt 0 ]; then echo "VM"; else echo "Physical"; fi
) "Operating System:\t"$(grep PRETTY_NAME /etc/os-release | awk -F \= '{print $2}')
echo -e "Kernel:\t\t\t"$(uname -r)
echo -e ""$(lscpu | head -1)"\t" "Processor Name:\t\t"$(awk -F':' '/^model name/ {print $2}' /proc/cpuinfo | uniq | sed -e 's/^[ \t]*//')
echo -e "Active Users:\t\t"$(who -u | cut -d ' ' -f1 | grep -v USER | xargs -n1)
echo -e "System Main IP:\t\t"$(ifconfig | grep inet | awk '{print $2}' | awk -F \: '{print $2}')
[ $(ps -ef | grep -i sshd | wc -l) -gt 0 ] && echo -e "SSHD connections ready" || echo -e "SSHD connections not ready"
echo -e "-------------------------------Loader boot entries------------------------------"
grep -i menuentry /mnt/${loaderdisk}1/boot/grub/grub.cfg | awk -F \' '{print $2}'
echo -e "-------------------------------CPU/Memory Usage------------------------------"
echo -e "Memory Usage:\t"$(free | awk '/Mem/{printf("%.2f%"), $3/$2*100}')
echo -e "Swap Usage:\t"$(free | awk '/Swap/{printf("%.2f%"), $3/$2*100}')
echo -e "CPU Usage:\t"$(cat /proc/stat | awk '/cpu/{printf("%.2f%\n"), ($2+$4)*100/($2+$4+$5)}' | awk '{print $0}' | head -1)
echo -e "-------------------------------Disk Usage >80%-------------------------------"
df -Ph | grep -v loop
[ $(lscpu | grep Hypervisor | wc -l) -gt 0 ] && echo "$(hostname) is a VM"
echo "Press ctrl-c to exti"
sleep 10
done
}
function syntaxcheck() {
if [ $# -lt 2 ] && [ "$1" == "download" ] || [ "$1" == "build" ] || [ "$1" == "ext" ] || [ "$1" == "restoresession" ] || [ "$1" == "listmods" ] || [ "$1" == "serialgen" ] || [ "$1" == "patchdtc" ] || [ "$1" == "postupdate" ]; then
echo "Error : Number of arguments : $#, options $@ "
case $1 in
download)
echo "Syntax error, You have to specify one of the existing platforms" && getPlatforms
;;
build)
echo "Syntax error, You have to specify one of the existing platforms" && getPlatforms
;;
ext)
echo "Syntax error, You have to specify one of the existing platforms, the action and the extension URL"
echo "example:"
echo "rploader.sh ext apollolake-7.0.1-42218 add https://raw.githubusercontent.com/pocopico/rp-ext/master/e1000/rpext-index.json"
echo "or for auto detect use"
echo "rploader.sh ext apollolake-7.0.1-42218 auto"
;;
restoresession)
echo "Syntax error, You have to specify one of the existing platforms" && getPlatforms
;;
listmods)
echo "Syntax error, You have to specify one of the existing platforms" && getPlatforms
;;
serialgen)
echo "Syntax error, You have to specify one of the existing models"
echo "DS3615xs DS3617xs DS916+ DS918+ DS920+ DS3622xs+ FS6400 DVA3219 DVA3221 DS1621+ DVA1622 DS2422+ RS4021xs+ DS923+ DS1522+ SA6400 FS2500 RS3413xs+ DS1019+ DS1520+ DS1621xs+ DS723+"
;;
patchdtc)
echo "Syntax error, You have to specify one of the existing platforms" && getPlatforms
;;
postupdate)
echo "Syntax error, You have to specify one of the existing platforms" && getPlatforms
;;
*)
echo "Syntax error, not valid arguments or not enough options"
showhelp
;;
esac
exit 99
else
return
fi
}
function version() {
shift 1
echo $rploaderver
[ "$1" == "history" ] && history
}
function savesession() {
lastsessiondir="/mnt/${tcrppart}/lastsession"
echo -n "Saving user session for future use. "
[ ! -d ${lastsessiondir} ] && sudo mkdir ${lastsessiondir}
echo -n "Saving current extensions "
cat /home/tc/redpill-load/custom/extensions/*/*json | jq '.url' >${lastsessiondir}/extensions.list
[ -f ${lastsessiondir}/extensions.list ] && echo " -> OK !"
echo -n "Saving current user_config.json "
cp /home/tc/user_config.json ${lastsessiondir}/user_config.json
[ -f ${lastsessiondir}/user_config.json ] && echo " -> OK !"
}
function restoresession() {
lastsessiondir="/mnt/${tcrppart}/lastsession"
if [ -d $lastsessiondir ]; then
echo -n "Found last user session : , restore session ? [yY/nN] : "
read answer
if [ "$answer" == "y" ] || [ "$answer" == "Y" ]; then
if [ -d $lastsessiondir ] && [ -f ${lastsessiondir}/extensions.list ]; then
for extension in $(cat ${lastsessiondir}/extensions.list); do
echo "Adding extension ${extension} "
cd /home/tc/redpill-load/ && ./ext-manager.sh add "$(echo $extension | sed -s 's/"//g' | sed -s 's/,//g')"
done
fi
if [ -d $lastsessiondir ] && [ -f ${lastsessiondir}/user_config.json ]; then
echo "Copying last user_config.json"
cp ${lastsessiondir}/user_config.json /home/tc
fi
fi
else
echo "OK, we will not restore last session"
fi
}
function downloadextractor() {
loaderdisk="$(mount | grep -i optional | grep cde | awk -F / '{print $3}' | uniq | cut -c 1-3)"
tcrppart="$(mount | grep -i optional | grep cde | awk -F / '{print $3}' | uniq | cut -c 1-3)3"
local_cache="/mnt/${tcrppart}/auxfiles"
temp_folder="/tmp/synoesp"
if [ -d ${local_cache/extractor /} ] && [ -f ${local_cache}/extractor/scemd ]; then
echo "Found extractor locally cached"
echo "Copying required libraries to local lib directory"
sudo cp /mnt/${tcrppart}/auxfiles/extractor/lib* /lib/
echo "Linking lib to lib64"
[ ! -h /lib64 ] && sudo ln -s /lib /lib64
echo "Copying executable"
sudo cp /mnt/${tcrppart}/auxfiles/extractor/scemd /bin/syno_extract_system_patch
echo "Removing temp folder /tmp/synoesp"
rm -rf $temp_folder
echo "Checking if tool is accessible"
/bin/syno_extract_system_patch 2>&1 >/dev/null
if [ $? -eq 255 ]; then echo "Executed succesfully"; else echo "Cound not execute"; fi
else
echo "Getting required extraction tool"
echo "------------------------------------------------------------------"
echo "Checking tinycore cache folder"
[ -d $local_cache ] && echo "Found tinycore cache folder, linking to home/tc/custom-module" && [ ! -h /home/tc/custom-module ] && sudo ln -s $local_cache /home/tc/custom-module
echo "Creating temp folder /tmp/synoesp"
mkdir ${temp_folder}
if [ -d /home/tc/custom-module ] && [ -f /home/tc/custom-module/*42218*.pat ]; then
patfile=$(ls /home/tc/custom-module/*42218*.pat | head -1)
echo "Found custom pat file ${patfile}"
echo "Processing old pat file to extract required files for extraction"
tar -C${temp_folder} -xf /${patfile} rd.gz
else
curl --insecure --location https://global.download.synology.com/download/DSM/release/7.0.1/42218/DSM_DS3622xs%2B_42218.pat --output /home/tc/oldpat.tar.gz
[ -f /home/tc/oldpat.tar.gz ] && tar -C${temp_folder} -xf /home/tc/oldpat.tar.gz rd.gz
fi
echo "Entering synoesp"
cd ${temp_folder}
xz -dc <rd.gz >rd 2>/dev/null || echo "extract rd.gz"
echo "finish"
cpio -idm <rd 2>&1 || echo "extract rd"
mkdir extract
mkdir -p /mnt/${tcrppart}/auxfiles && cd /mnt/${tcrppart}/auxfiles
echo "Copying required files to local cache folder for future use"
mkdir -p /mnt/${tcrppart}/auxfiles/extractor
for file in usr/lib/libcurl.so.4 usr/lib/libmbedcrypto.so.5 usr/lib/libmbedtls.so.13 usr/lib/libmbedx509.so.1 usr/lib/libmsgpackc.so.2 usr/lib/libsodium.so usr/lib/libsynocodesign-ng-virtual-junior-wins.so.7 usr/syno/bin/scemd; do
echo "Copying $file to /mnt/${tcrppart}/auxfiles"
cp $file /mnt/${tcrppart}/auxfiles/extractor
done
echo "Copying required libraries to local lib directory"
sudo cp /mnt/${tcrppart}/auxfiles/extractor/lib* /lib/
echo "Linking lib to lib64"
[ ! -h /lib64 ] && sudo ln -s /lib /lib64
echo "Copying executable"
sudo cp /mnt/${tcrppart}/auxfiles/extractor/scemd /bin/syno_extract_system_patch
echo "Removing temp folder /tmp/synoesp"
rm -rf $temp_folder
echo "Checking if tools is accessible"
/bin/syno_extract_system_patch
if [ $? -eq 255 ]; then echo "Executed succesfully"; else echo "Cound not execute"; fi
fi
}
function processpat() {
loaderdisk="$(mount | grep -i optional | grep cde | awk -F / '{print $3}' | uniq | cut -c 1-3)"
tcrppart="$(mount | grep -i optional | grep cde | awk -F / '{print $3}' | uniq | cut -c 1-3)3"
local_cache="/mnt/${tcrppart}/auxfiles"
temp_pat_folder="/tmp/pat"
setplatform
if [ ! -d "${temp_pat_folder}" ]; then
echo "Creating temp folder ${temp_pat_folder} "
mkdir ${temp_pat_folder} && cd ${temp_pat_folder}
fi
echo "Checking for cached pat file"
[ -d $local_cache ] && echo "Found tinycore cache folder, linking to home/tc/custom-module" && [ ! -h /home/tc/custom-module ] && sudo ln -s $local_cache /home/tc/custom-module
if [ -d ${local_cache} ] && [ -f ${local_cache}/*${SYNOMODEL}*.pat ] || [ -f ${local_cache}/*${MODEL}*${TARGET_REVISION}*.pat ]; then
[ -f /home/tc/custom-module/*${SYNOMODEL}*.pat ] && patfile=$(ls /home/tc/custom-module/*${SYNOMODEL}*.pat | head -1)
[ -f ${local_cache}/*${MODEL}*${TARGET_REVISION}*.pat ] && patfile=$(ls /home/tc/custom-module/*${MODEL}*${TARGET_REVISION}*.pat | head -1)
echo "Found locally cached pat file ${patfile}"
testarchive "${patfile}"
if [ ${isencrypted} = "no" ]; then
echo "File ${patfile} is already unencrypted"
echo "Copying file to /home/tc/redpill-load/cache folder"
cp ${patfile} /home/tc/redpill-load/cache/
elif [ ${isencrypted} = "yes" ]; then
[ -f /home/tc/redpill-load/cache/${SYNOMODEL}.pat ] && testarchive /home/tc/redpill-load/cache/${SYNOMODEL}.pat
if [ -f /home/tc/redpill-load/cache/${SYNOMODEL}.pat ] && [ ${isencrypted} = "no" ]; then
echo "Unecrypted file is already cached in : /home/tc/redpill-load/cache/${SYNOMODEL}.pat"
patfile="/home/tc/redpill-load/cache/${SYNOMODEL}.pat"
elif [ -f /bin/syno_extract_system_patch ]; then
echo "Extracting encrypted pat file : ${patfile} to ${temp_pat_folder}"
sudo /bin/syno_extract_system_patch ${patfile} ${temp_pat_folder} || echo "extract latest pat"
echo "Creating unecrypted pat file ${SYNOMODEL}.pat to /home/tc/redpill-load/cache folder "
mkdir -p /home/tc/redpill-load/cache/
cd ${temp_pat_folder} && tar -czf /home/tc/redpill-load/cache/${SYNOMODEL}.pat *
patfile="/home/tc/redpill-load/cache/${SYNOMODEL}.pat"
else
echo "Extracting encrypted pat file : ${patfile} to ${temp_pat_folder}"
sudo LD_LIBRARY_PATH=/home/tc/custom-module/patch-extractor/lib/ /home/tc/custom-module/patch-extractor/synoarchive.system -xvf ${patfile} -C ${temp_pat_folder}
echo "Creating unecrypted pat file ${SYNOMODEL}.pat to /home/tc/redpill-load/cache folder "
mkdir -p /home/tc/redpill-load/cache/
cd ${temp_pat_folder} && tar -czf /home/tc/redpill-load/cache/${SYNOMODEL}.pat *
patfile="/home/tc/redpill-load/cache/${SYNOMODEL}.pat"
fi
else
echo "Something went wrong, please check cache files"
exit 99
fi
tar xvf /home/tc/redpill-load/cache/${SYNOMODEL}.pat VERSION && . ./VERSION && rm -f ./VERSION
os_sha256=$(sha256sum ${patfile} | awk '{print $1}')
echo "Pat file sha256sum is : $os_sha256"
echo -n "Checking config file existence -> "
if [ -f "/home/tc/redpill-load/config/$MODEL/${major}.${minor}.${micro}-${buildnumber}/config.json" ]; then
echo "OK"
configfile="/home/tc/redpill-load/config/$MODEL/${major}.${minor}.${micro}-${buildnumber}/config.json"
else
echo "Config file /home/tc/redpill-load/config/$MODEL/${major}.${minor}.${micro}-${buildnumber}/config.json not found, please use the proper repo, clean and download again"
exit 99
fi
echo -n "Editing config file -> "
sed -i "/\"os\": {/!b;n;n;n;c\"sha256\": \"$os_sha256\"" ${configfile}
echo -n "Verifying config file -> "
verifyid="$(cat ${configfile} | jq -r -e '.os .sha256')"
if [ "$os_sha256" == "$verifyid" ]; then
echo "OK ! "
else
echo "config file, os sha256 verify FAILED, check ${configfile} "
exit 99
fi
echo "Clearing temp folders"
sudo rm -rf ${temp_pat_folder}
return
else
echo "Could not find pat file locally cached"
configdir="/home/tc/redpill-load/config/${MODEL}/${TARGET_VERSION}-${TARGET_REVISION}"
configfile="${configdir}/config.json"
pat_url=$(cat ${configfile} | jq '.os .pat_url' | sed -s 's/"//g')
echo -e "Configdir : $configdir \nConfigfile: $configfile \nPat URL : $pat_url"
echo "Downloading pat file from URL : ${pat_url} "
if [ $(df -h /${local_cache} | grep mnt | awk '{print $4}' | sed -e 's/M//g' -e 's/G//g' | cut -c 1-3) -le 370 ]; then
echo "No adequate space on ${local_cache} to download file into cache folder, clean up the space and restart"
exit 99
fi
[ -n $pat_url ] && curl --insecure --location ${pat_url} -o "/${local_cache}/${SYNOMODEL}.pat"
patfile="/${local_cache}/${SYNOMODEL}.pat"
if [ -f ${patfile} ]; then
testarchive ${patfile}
else
echo "Failed to download PAT file $patfile from ${pat_url} "
exit 99
fi
if [ "${isencrypted}" = "yes" ]; then
echo "File ${patfile}, has been cached but its encrypted, re-running decrypting process"
processpat
else
return
fi
fi
}
function testarchive() {
archive="$1"
archiveheader="$(od -bc ${archive} | head -1 | awk '{print $3}')"
case ${archiveheader} in
105)
echo "${archive}, is a Tar file"
isencrypted="no"
return 0
;;
255)
echo "File ${archive}, is encrypted"
isencrypted="yes"
return 1
;;
213)
echo "File ${archive}, is a compressed tar"
isencrypted="no"
;;
*)
echo "Could not determine if file ${archive} is encrypted or not, maybe corrupted"
ls -ltr ${archive}
echo ${archiveheader}
exit 99
;;
esac
}
function addrequiredexts() {
echo "Processing add_extensions entries found on ${CUSTOMCONFIG} file : ${EXTENSIONS}"
for extension in ${EXTENSIONS_SOURCE_URL}; do
echo "Adding extension ${extension} "
cd /home/tc/redpill-load/ && ./ext-manager.sh add "$(echo $extension | sed -s 's/"//g' | sed -s 's/,//g')"
done
for extension in ${EXTENSIONS}; do
echo "Updating extension : ${extension} contents for model : ${SYNOMODEL} "
cd /home/tc/redpill-load/ && ./ext-manager.sh _update_platform_exts ${SYNOMODEL} ${extension}
done
if [ ${TARGET_PLATFORM} = "geminilake" ] || [ ${TARGET_PLATFORM} = "v1000" ] || [ ${TARGET_PLATFORM} = "dva1622" ] || [ ${TARGET_PLATFORM} = "ds2422p" ] || [ ${TARGET_PLATFORM} = "rs4021xsp" ]; then
#patchdtc
echo "Patch dtc is superseded by fbelavenuto dtbpatch"
fi
}
function installapache() {
echo "Installing apache2 and php module"
tce-load -iw apache2.4.tcz
tce-load -iw apache2.4-doc.tcz
tce-load -iw php-8.0-mod.tcz
tce-load -iw libnghttp2.tcz
#cd /usr/local/
#sudo tar xvf /home/tc/tcrphtml/tc.apache.tar.gz etc/httpd/
#apachectl start
}
function updateuserconfig() {
echo "Checking user config for general block"
generalblock="$(jq -r -e '.general' $userconfigfile)"
if [ "$generalblock" = "null" ] || [ -n "$generalblock" ]; then
echo "Result=${generalblock}, File does not contain general block, adding block"
for field in model version smallfixnumber redpillmake zimghash rdhash usb_line sata_line; do
jsonfile=$(jq ".general+={\"$field\":\"\"}" $userconfigfile)
echo $jsonfile | jq . >$userconfigfile
done
fi
}
function updateuserconfigfield() {
block="$1"
field="$2"
value="$3"
if [ -n "$1 " ] && [ -n "$2" ]; then
jsonfile=$(jq ".$block+={\"$field\":\"$value\"}" $userconfigfile)
echo $jsonfile | jq . >$userconfigfile
else
echo "No values to update specified"
fi
}
function removefriend() {
clear
loaderdisk="$(mount | grep -i optional | grep cde | awk -F / '{print $3}' | uniq | cut -c 1-3)"
echo "------------------------------------------------------------------------------------------------------------"
echo "You are not satisfied with TCRP friend."
echo "Understandable, but you will not be able to perform automatic patching after updates."
echo "you can still though use the postupdate process instead or just set the default option to SATA or USB as usual"
echo "------------------------------------------------------------------------------------------------------------"
echo -n "Do you still want to remove TCRP Friend, please answer [Yy/Nn]"
read answer
if [ "${answer}" = "Y" ] || [ "${answer}" = "y" ]; then
mount /dev/${loaderdisk}1 2>/dev/null
mount /dev/${loaderdisk}2 2>/dev/null
mount /dev/${loaderdisk}3 2>/dev/null
echo "Removing TCRP Friend from ${loaderdisk}3 "
[ -f /mnt/${loaderdisk}3/initrd-friend ] && sudo rm -rf /mnt/${loaderdisk}3/initrd-friend
[ -f /mnt/${loaderdisk}3/bzImage-friend ] && sudo rm -rf /mnt/${loaderdisk}3/bzImage-friend
echo "Removing initrd-dsm and zimage-dsm from ${loaderdisk}3 "
[ ! "$(sha256sum /mnt/${loaderdisk}3/initrd-dsm | awk '{print $2}')" = "$(sha256sum /mnt/${loaderdisk}1/rd.gz | awk '{print $2}')" ] && cp /mnt/${loaderdisk}3/initrd-dsm /mnt/${loaderdisk}1/rd.gz
[ -f /mnt/${loaderdisk}3/initrd-dsm ] && sudo rm -rf /mnt/${loaderdisk}3/initrd-dsm
[ ! "$(sha256sum /mnt/${loaderdisk}3/zImage-dsm | awk '{print $2}')" = "$(sha256sum /mnt/${loaderdisk}1/zImage | awk '{print $2}')" ] && cp /mnt/${loaderdisk}3/zImage-dsm /mnt/${loaderdisk}1/zImage
[ -f /mnt/${loaderdisk}3/zimage-dsm ] && sudo rm -rf /mnt/${loaderdisk}3/zimage-dsm
echo "Removing TCRP Friend Grub entry "
[ $(grep -i "Tiny Core Friend" /mnt/${loaderdisk}1/boot/grub/grub.cfg | wc -l) -eq 1 ] && sed -i "/Tiny Core Friend/,+9d" /mnt/${loaderdisk}1/boot/grub/grub.cfg
if [ "$MACHINE" = "VIRTUAL" ]; then
echo "Setting default boot entry to SATA"
cd /home/tc/redpill-load/ && sudo sed -i "/set default=\"*\"/cset default=\"1\"" /mnt/${loaderdisk}1/boot/grub/grub.cfg
else
echo "Setting default boot entry to USB"
cd /home/tc/redpill-load/ && sudo sed -i "/set default=\"*\"/cset default=\"0\"" /mnt/${loaderdisk}1/boot/grub/grub.cfg
fi
else
echo "OK ! Wise choice !!! "
fi
}
function bringfriend() {
clear
loaderdisk="$(mount | grep -i optional | grep cde | awk -F / '{print $3}' | uniq | cut -c 1-3)"
mount /dev/${loaderdisk}1 2>/dev/null
mount /dev/${loaderdisk}2 2>/dev/null
mount /dev/${loaderdisk}3 2>/dev/null
if [ -f /mnt/${loaderdisk}3/lastsession/user_config.json ]; then
cp /mnt/${loaderdisk}3/lastsession/user_config.json /home/tc/user_config.json
getgrubconf
else
getgrubconf
fi
if [ -f /mnt/${loaderdisk}3/bzImage-friend ] && [ -f /mnt/${loaderdisk}3/initrd-friend ] && [ -f /mnt/${loaderdisk}3/zImage-dsm ] && [ -f /mnt/${loaderdisk}3/initrd-dsm ] && [ -f /mnt/${loaderdisk}3/user_config.json ] && [ $(grep -i "Tiny Core Friend" /mnt/${loaderdisk}1/boot/grub/grub.cfg | wc -l) -eq 1 ]; then
echo "Your TCRP friend seems in place, do you want to re-run the process ?"
read answer
if [ "${answer}" = "Y" ] || [ "${answer}" = "y" ]; then
echo "OK re-running the TCRP Friend bring over process"
else
echo "Wise choice"
exit 0
fi
fi
echo "You are upgrading your system with TCRP friend."
echo "Your system will still be able to boot using the USB/SATA options."
echo "After bringing over TCRP Friend, The default boot option will be set TCRP Friend."
echo "You will still have the option to move to SATA/USB but for automatic patching after an update,"
echo "please leave the default to TCRR Friend"
echo -n "If you agree with the above, please answer [Yy/Nn]"
read answer
if [ "${answer}" = "Y" ] || [ "${answer}" = "y" ]; then
if [ ! -f /mnt/${loaderdisk}3/initrd-friend ] || [ ! -f /mnt/${loaderdisk}3/bzImage-friend ]; then
[ ! -f /home/tc/friend/initrd-friend ] && [ ! -f /home/tc/friend/bzImage-friend ] && bringoverfriend
if [ -f /home/tc/friend/initrd-friend ] && [ -f /home/tc/friend/bzImage-friend ]; then
cp /home/tc/friend/initrd-friend /mnt/${loaderdisk}3/
cp /home/tc/friend/bzImage-friend /mnt/${loaderdisk}3/
fi