forked from 007revad/Synology_HDD_db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syno_hdd_db.sh
2249 lines (1969 loc) · 80 KB
/
syno_hdd_db.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
863
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
#!/usr/bin/env bash
# shellcheck disable=SC1083,SC2054,SC2121,SC2207
#--------------------------------------------------------------------------------------------------
# Github: https://github.com/007revad/Synology_HDD_db
# Script verified at https://www.shellcheck.net/
#
# To run in task manager as root (manually or scheduled):
# /volume1/scripts/syno_hdd_db.sh # replace /volume1/scripts/ with path to script
#
# To run in a shell (replace /volume1/scripts/ with path to script):
# sudo -i /volume1/scripts/syno_hdd_db.sh
# or
# sudo -i /volume1/scripts/syno_hdd_db.sh -showedits
# or
# sudo -i /volume1/scripts/syno_hdd_db.sh -force -showedits
#--------------------------------------------------------------------------------------------------
# https://smarthdd.com/database/
# RECENT CHANGES
# Make DSM read md0 and md1 from SSD drive(s) if internal SSD and HDD are installed.
# https://github.com/007revad/Synology_HDD_db/issues/318
# https://www.techspark.de/speed-up-synology-dsm-with-hdd-ssd/
# https://raid.wiki.kernel.org/index.php/Write-mostly
# TODO
# Enable SMART Attributes button on Storage Manager
# disabled:e.healthInfoDisabled
# enabled:e.healthInfoDisabled
# /var/packages/StorageManager/target/ui/storage_panel.js
scriptver="v3.5.102"
script=Synology_HDD_db
repo="007revad/Synology_HDD_db"
scriptname=syno_hdd_db
# Check BASH variable is bash
if [ ! "$(basename "$BASH")" = bash ]; then
echo "This is a bash script. Do not run it with $(basename "$BASH")"
printf \\a
exit 1
fi
# Check script is running on a Synology NAS
if ! /usr/bin/uname -a | grep -q -i synology; then
echo "This script is NOT running on a Synology NAS!"
echo "Copy the script to a folder on the Synology"
echo "and run it from there."
exit 1
fi
ding(){
printf \\a
}
usage(){
cat <<EOF
$script $scriptver - by 007revad
Usage: $(basename "$0") [options]
Options:
-s, --showedits Show edits made to <model>_host db and db.new file(s)
-n, --noupdate Prevent DSM updating the compatible drive databases
-r, --ram Disable memory compatibility checking (DSM 7.x only)
and sets max memory to the amount of installed memory
-f, --force Force DSM to not check drive compatibility
Do not use this option unless absolutely needed
-i, --incompatible Change incompatible drives to supported
Do not use this option unless absolutely needed
-w, --wdda Disable WD Device Analytics to prevent DSM showing
a false warning for WD drives that are 3 years old
DSM 7.2.1 already has WDDA disabled
-p, --pcie Enable creating volumes on M2 in unknown PCIe adaptor
-e, --email Disable colored text in output scheduler emails
-S, --ssd=DRIVE Enable write_mostly on internal HDDs so DSM primary
reads from internal SSDs or your specified drives
-S automatically sets internal SSDs as DSM preferred
--ssd=DRIVE requires the fast drive(s) as argument,
or restore as the argument to reset drives to default
--ssd=sata1 or --ssd=sata1,sata2 or --ssd=sda etc
--ssd=restore
--restore Undo all changes made by the script (except -S --ssd)
To restore all changes including write_mostly use
--restore --ssd=restore
--autoupdate=AGE Auto update script (useful when script is scheduled)
AGE is how many days old a release must be before
auto-updating. AGE must be a number: 0 or greater
-h, --help Show this help message
-v, --version Show the script version
EOF
exit 0
}
scriptversion(){
cat <<EOF
$script $scriptver - by 007revad
See https://github.com/$repo
EOF
exit 0
}
# Save options used
args=("$@")
# Check for flags with getopt
if options="$(getopt -o Sabcdefghijklmnopqrstuvwxyz0123456789 -l \
ssd:,restore,showedits,noupdate,nodbupdate,m2,force,incompatible,ram,pcie,wdda,email,autoupdate:,help,version,debug \
-- "$@")"; then
eval set -- "$options"
while true; do
case "$1" in
-d|--debug) # Show and log debug info
debug=yes
;;
-e|--email) # Disable colour text in task scheduler emails
color=no
;;
--restore) # Restore changes from backups
restore=yes
if $(echo "${args[@]}" | grep -q -- '--ssd=restore'); then
ssd_restore=yes
fi
#break
;;
-s|--showedits) # Show edits done to host db file
showedits=yes
;;
-S) # Enable writemostly for md0 and md1
ssd=yes
;;
--ssd) # Enable writemostly for md0 and md1
ssd=yes
if [[ ${2,,} == "restore" ]]; then
ssd_restore=yes
else
IFS=$','
for d in $2; do ssds_writemostly =("${d,,}"); done
unset IFS
fi
shift
;;
-n|--nodbupdate|--noupdate) # Disable disk compatibility db updates
nodbupdate=yes
;;
-m|--m2) # Don't add M.2 drives to db files
m2=no
;;
-f|--force) # Disable "support_disk_compatibility"
force=yes
;;
-i|--incompatible) # Change incompatible drives to supported
incompatible=yes
;;
-r|--ram) # Disable "support_memory_compatibility"
ram=yes
;;
-w|--wdda) # Disable "support_wdda"
wdda=no
;;
-p|--pcie) # Enable creating volumes on M2 in unknown PCIe adaptor
forcepci=yes
;;
--autoupdate) # Auto update script
autoupdate=yes
if [[ $2 =~ ^[0-9] $ ]]; then
delay="$2"
shift
else
delay="0"
fi
;;
-h|--help) # Show usage options
usage
;;
-v|--version) # Show script version
scriptversion
;;
--)
shift
break
;;
*) # Show usage options
echo -e "Invalid option '$1'\n"
usage "$1"
;;
esac
shift
done
else
echo
usage
fi
if [[ $debug == "yes" ]]; then
set -x
export PS4='`[[ $? == 0 ]] || echo "\e[1;31;40m($?)\e[m\n "`:.$LINENO:'
fi
# Shell Colors
if [[ $color != "no" ]]; then
#Black='\e[0;30m' # ${Black}
Red='\e[0;31m' # ${Red}
#Green='\e[0;32m' # ${Green}
Yellow='\e[0;33m' # ${Yellow}
#Blue='\e[0;34m' # ${Blue}
#Purple='\e[0;35m' # ${Purple}
Cyan='\e[0;36m' # ${Cyan}
#White='\e[0;37m' # ${White}
Error='\e[41m' # ${Error}
Off='\e[0m' # ${Off}
else
echo "" # For task scheduler email readability
fi
# Check script is running as root
if [[ $( whoami ) != "root" ]]; then
ding
echo -e "${Error}ERROR${Off} This script must be run as sudo or root!"
exit 1
fi
# Get DSM major version
dsm=$(/usr/syno/bin/synogetkeyvalue /etc.defaults/VERSION majorversion)
if [[ $dsm -gt "6" ]]; then
version="_v$dsm"
fi
# Get Synology model
model=$(cat /proc/sys/kernel/syno_hw_version)
modelname="$model"
# Show script version
#echo -e "$script $scriptver\ngithub.com/$repo\n"
echo "$script $scriptver"
# Get DSM full version
productversion=$(/usr/syno/bin/synogetkeyvalue /etc.defaults/VERSION productversion)
buildphase=$(/usr/syno/bin/synogetkeyvalue /etc.defaults/VERSION buildphase)
buildnumber=$(/usr/syno/bin/synogetkeyvalue /etc.defaults/VERSION buildnumber)
smallfixnumber=$(/usr/syno/bin/synogetkeyvalue /etc.defaults/VERSION smallfixnumber)
# Show DSM full version and model
if [[ $buildphase == GM ]]; then buildphase=""; fi
if [[ $smallfixnumber -gt "0" ]]; then smallfix="-$smallfixnumber"; fi
echo "$model DSM $productversion-$buildnumber$smallfix $buildphase"
# Convert model to lower case
model=${model,,}
# Check for dodgy characters after model number
if [[ $model =~ 'pv10-j'$ ]]; then # GitHub issue #10
modelname=${modelname%??????} # replace last 6 chars with
model=${model%??????} # replace last 6 chars with
echo -e "\nUsing model: $model"
elif [[ $model =~ '-j'$ ]]; then # GitHub issue #2
modelname=${modelname%??} # remove last 2 chars
model=${model%??} # remove last 2 chars
echo -e "\nUsing model: $model"
fi
# Get StorageManager version
storagemgrver=$(/usr/syno/bin/synopkg version StorageManager)
# Show StorageManager version
if [[ $storagemgrver ]]; then echo -e "StorageManager $storagemgrver\n"; fi
# Show host drive db version
if [[ -f "/var/lib/disk-compatibility/${model}_host_v7.version" ]]; then
echo -n "${model}_host_v7 version "
cat "/var/lib/disk-compatibility/${model}_host_v7.version"
echo -e "\n"
fi
if [[ -f "/var/lib/disk-compatibility/${model}_host.version" ]]; then
echo -n "${model}_host version "
cat "/var/lib/disk-compatibility/${model}_host.version"
echo -e "\n"
fi
# Show options used
if [[ ${#args[@]} -gt "0" ]]; then
echo "Using options: ${args[*]}"
fi
#echo "" # To keep output readable
# shellcheck disable=SC2317 # Don't warn about unreachable commands in this function
pause(){
# When debugging insert pause command where needed
read -s -r -n 1 -p "Press any key to continue..."
read -r -t 0.1 -s -e -- # Silently consume all input
stty echo echok # Ensure read didn't disable echoing user input
echo -e "\n"
}
#------------------------------------------------------------------------------
# Check latest release with GitHub API
syslog_set(){
if [[ ${1,,} == "info" ]] || [[ ${1,,} == "warn" ]] || [[ ${1,,} == "err" ]]; then
if [[ $autoupdate == "yes" ]]; then
# Add entry to Synology system log
/usr/syno/bin/synologset1 sys "$1" 0x11100000 "$2"
fi
fi
}
# Get latest release info
# Curl timeout options:
# https://unix.stackexchange.com/questions/94604/does-curl-have-a-timeout
release=$(curl --silent -m 10 --connect-timeout 5 \
"https://api.github.com/repos/$repo/releases/latest")
# Release version
tag=$(echo "$release" | grep '"tag_name":' | sed -E 's/.*"([^"] )".*/\1/')
shorttag="${tag:1}"
# Release published date
published=$(echo "$release" | grep '"published_at":' | sed -E 's/.*"([^"] )".*/\1/')
published="${published:0:10}"
published=$(date -d "$published" ' %s')
# Today's date
now=$(date ' %s')
# Days since release published
age=$(((now - published)/(60*60*24)))
# Get script location
# https://stackoverflow.com/questions/59895/
source=${BASH_SOURCE[0]}
while [ -L "$source" ]; do # Resolve $source until the file is no longer a symlink
scriptpath=$( cd -P "$( dirname "$source" )" >/dev/null 2>&1 && pwd )
source=$(readlink "$source")
# If $source was a relative symlink, we need to resolve it
# relative to the path where the symlink file was located
[[ $source != /* ]] && source=$scriptpath/$source
done
scriptpath=$( cd -P "$( dirname "$source" )" >/dev/null 2>&1 && pwd )
scriptfile=$( basename -- "$source" )
echo "Running from: ${scriptpath}/$scriptfile"
#echo "Script location: $scriptpath" # debug
#echo "Source: $source" # debug
#echo "Script filename: $scriptfile" # debug
#echo "tag: $tag" # debug
#echo "scriptver: $scriptver" # debug
# Warn if script located on M.2 drive
scriptvol=$(echo "$scriptpath" | cut -d"/" -f2)
vg=$(lvdisplay | grep /volume_"${scriptvol#volume}" | cut -d"/" -f3)
md=$(pvdisplay | grep -B 1 -E '[ ]'"$vg" | grep /dev/ | cut -d"/" -f3)
# shellcheck disable=SC2002 # Don't warn about "Useless cat"
if cat /proc/mdstat | grep "$md" | grep -q nvme; then
echo -e "\n${Yellow}WARNING${Off} Don't store this script on an NVMe volume!"
fi
cleanup_tmp(){
cleanup_err=
# Delete downloaded .tar.gz file
if [[ -f "/tmp/$script-$shorttag.tar.gz" ]]; then
if ! rm "/tmp/$script-$shorttag.tar.gz"; then
echo -e "${Error}ERROR${Off} Failed to delete"\
"downloaded /tmp/$script-$shorttag.tar.gz!" >&2
cleanup_err=1
fi
fi
# Delete extracted tmp files
if [[ -d "/tmp/$script-$shorttag" ]]; then
if ! rm -r "/tmp/$script-$shorttag"; then
echo -e "${Error}ERROR${Off} Failed to delete"\
"downloaded /tmp/$script-$shorttag!" >&2
cleanup_err=1
fi
fi
# Add warning to DSM log
if [[ $cleanup_err ]]; then
syslog_set warn "$script update failed to delete tmp files"
fi
}
if ! printf "%s\n%s\n" "$tag" "$scriptver" |
sort --check=quiet --version-sort >/dev/null ; then
echo -e "\n${Cyan}There is a newer version of this script available.${Off}"
echo -e "Current version: ${scriptver}\nLatest version: $tag"
scriptdl="$scriptpath/$script-$shorttag"
if [[ -f ${scriptdl}.tar.gz ]] || [[ -f ${scriptdl}.zip ]]; then
# They have the latest version tar.gz downloaded but are using older version
echo "You have the latest version downloaded but are using an older version"
sleep 10
elif [[ -d $scriptdl ]]; then
# They have the latest version extracted but are using older version
echo "You have the latest version extracted but are using an older version"
sleep 10
else
if [[ $autoupdate == "yes" ]]; then
if [[ $age -gt "$delay" ]] || [[ $age -eq "$delay" ]]; then
echo "Downloading $tag"
reply=y
else
echo "Skipping as $tag is less than $delay days old."
fi
else
echo -e "${Cyan}Do you want to download $tag now?${Off} [y/n]"
read -r -t 30 reply
fi
if [[ ${reply,,} == "y" ]]; then
# Delete previously downloaded .tar.gz file and extracted tmp files
cleanup_tmp
if cd /tmp; then
url="https://github.com/$repo/archive/refs/tags/$tag.tar.gz"
if ! curl -JLO -m 30 --connect-timeout 5 "$url"; then
echo -e "${Error}ERROR${Off} Failed to download"\
"$script-$shorttag.tar.gz!"
syslog_set warn "$script $tag failed to download"
else
if [[ -f /tmp/$script-$shorttag.tar.gz ]]; then
# Extract tar file to /tmp/<script-name>
if ! tar -xf "/tmp/$script-$shorttag.tar.gz" -C "/tmp"; then
echo -e "${Error}ERROR${Off} Failed to"\
"extract $script-$shorttag.tar.gz!"
syslog_set warn "$script failed to extract $script-$shorttag.tar.gz!"
else
# Set script sh files as executable
if ! chmod a x "/tmp/$script-$shorttag/"*.sh ; then
permerr=1
echo -e "${Error}ERROR${Off} Failed to set executable permissions"
syslog_set warn "$script failed to set permissions on $tag"
fi
# Copy new script sh file to script location
if ! cp -p "/tmp/$script-$shorttag/${scriptname}.sh" "${scriptpath}/${scriptfile}";
then
copyerr=1
echo -e "${Error}ERROR${Off} Failed to copy"\
"$script-$shorttag sh file(s) to:\n $scriptpath/${scriptfile}"
syslog_set warn "$script failed to copy $tag to script location"
fi
# Copy new syno_hdd_vendor_ids.txt file
vidstxt="syno_hdd_vendor_ids.txt"
if [[ $scriptpath =~ /volume* ]]; then
if [[ ! -f "$scriptpath/$vidstxt" ]]; then # Don't overwrite file
# Copy new syno_hdd_vendor_ids.txt file to script location
if ! cp -p "/tmp/$script-$shorttag/$vidstxt" "$scriptpath"; then
if [[ $autoupdate != "yes" ]]; then copyerr=1; fi
echo -e "${Error}ERROR${Off} Failed to copy"\
"$script-$shorttag/$vidstxt to:\n $scriptpath"
else
# Set permissions on syno_hdd_vendor_ids.txt
if ! chmod 755 "$scriptpath/$vidstxt"; then
if [[ $autoupdate != "yes" ]]; then permerr=1; fi
echo -e "${Error}ERROR${Off} Failed to set permissions on:"
echo "$scriptpath/$vidstxt"
fi
vids_txt=", syno_hdd_vendor_ids.txt"
fi
fi
fi
# Copy new CHANGES.txt file to script location (if script on a volume)
if [[ $scriptpath =~ /volume* ]]; then
# Set permissions on CHANGES.txt
if ! chmod 664 "/tmp/$script-$shorttag/CHANGES.txt"; then
permerr=1
echo -e "${Error}ERROR${Off} Failed to set permissions on:"
echo "$scriptpath/CHANGES.txt"
fi
# Copy new CHANGES.txt file to script location
if ! cp -p "/tmp/$script-$shorttag/CHANGES.txt"\
"${scriptpath}/${scriptname}_CHANGES.txt";
then
if [[ $autoupdate != "yes" ]]; then copyerr=1; fi
echo -e "${Error}ERROR${Off} Failed to copy"\
"$script-$shorttag/CHANGES.txt to:\n $scriptpath"
else
changestxt=" and changes.txt"
fi
fi
# Delete downloaded tmp files
cleanup_tmp
# Notify of success (if there were no errors)
if [[ $copyerr != 1 ]] && [[ $permerr != 1 ]]; then
echo -e "\n$tag ${scriptfile}$vids_txt$changestxt downloaded to: ${scriptpath}\n"
syslog_set info "$script successfully updated to $tag"
# Reload script
printf -- '-%.0s' {1..79}; echo # print 79 -
exec "${scriptpath}/$scriptfile" "${args[@]}"
else
syslog_set warn "$script update to $tag had errors"
fi
fi
else
echo -e "${Error}ERROR${Off}"\
"/tmp/$script-$shorttag.tar.gz not found!"
#ls /tmp | grep "$script" # debug
syslog_set warn "/tmp/$script-$shorttag.tar.gz not found"
fi
fi
cd "$scriptpath" || echo -e "${Error}ERROR${Off} Failed to cd to script location!"
else
echo -e "${Error}ERROR${Off} Failed to cd to /tmp!"
syslog_set warn "$script update failed to cd to /tmp"
fi
fi
fi
fi
#------------------------------------------------------------------------------
# Set file variables
if [[ -f /etc.defaults/model.dtb ]]; then # Is device tree model
# Get syn_hw_revision, r1 or r2 etc (or just a linefeed if not a revision)
hwrevision=$(cat /proc/sys/kernel/syno_hw_revision)
# If syno_hw_revision is r1 or r2 it's a real Synology,
# and I need to edit model_rN.dtb instead of model.dtb
if [[ $hwrevision =~ r[0-9] ]]; then
#echo "hwrevision: $hwrevision" # debug
hwrev="_$hwrevision"
fi
dtb_file="/etc.defaults/model${hwrev}.dtb"
dtb2_file="/etc/model${hwrev}.dtb"
#dts_file="/etc.defaults/model${hwrev}.dts"
dts_file="/tmp/model${hwrev}.dts"
fi
adapter_cards="/usr/syno/etc.defaults/adapter_cards.conf"
adapter_cards2="/usr/syno/etc/adapter_cards.conf"
dbpath=/var/lib/disk-compatibility/
synoinfo="/etc.defaults/synoinfo.conf"
if [[ $buildnumber -gt 64570 ]]; then
# DSM 7.2.1 and later
#strgmgr="/var/packages/StorageManager/target/ui/storage_panel.js"
strgmgr="/usr/local/packages/@appstore/StorageManager/ui/storage_panel.js"
elif [[ $buildnumber -ge 64561 ]]; then
# DSM 7.2
strgmgr="/usr/syno/synoman/webman/modules/StorageManager/storage_panel.js"
fi
vidfile="/usr/syno/etc.defaults/pci_vendor_ids.conf"
vidfile2="/usr/syno/etc/pci_vendor_ids.conf"
set_writemostly(){
# $1 is writemostly or -writemostly
# $2 is sata1 or sas1 or sda etc
local model
# Show drive model
model="$(cat /sys/block/"${2}"/device/model | xargs)"
echo -e "${Yellow}$model${Off}"
if [[ ${1::2} == "sd" ]]; then
# sda etc
# md0 DSM system partition
echo "$1" > /sys/block/md0/md/dev-"${2}"1/state
# Show setting
echo -n " $2 DSM partition: "
cat /sys/block/md0/md/dev-"${2}"1/state
# md1 DSM swap partition
echo "$1" > /sys/block/md1/md/dev-"${2}"2/state
# Show setting
echo -n " $2 Swap partition: "
cat /sys/block/md1/md/dev-"${2}"2/state
else
# sata1 or sas1 etc
# md0 DSM system partition
echo "$1" > /sys/block/md0/md/dev-"${2}"p1/state
# Show setting
echo -n " $2 DSM partition: "
cat /sys/block/md0/md/dev-"${2}"p1/state
# md1 DSM swap partition
echo "$1" > /sys/block/md1/md/dev-"${2}"p2/state
# Show setting
echo -n " $2 Swap partition: "
cat /sys/block/md1/md/dev-"${2}"p2/state
fi
}
#------------------------------------------------------------------------------
# Restore changes from backups
if [[ $restore == "yes" ]]; then
dbbaklist=($(find $dbpath -maxdepth 1 \( -name "*.db.new.bak" -o -name "*.db.bak" \)))
# Sort array
IFS=$'\n'
dbbakfiles=($(sort <<<"${dbbaklist[*]}"))
unset IFS
echo ""
if [[ ${#dbbakfiles[@]} -gt "0" ]] || [[ -f ${synoinfo}.bak ]] ||\
[[ -f ${dtb_file}.bak ]] || [[ -f ${adapter_cards}.bak ]] ; then
# Restore synoinfo.conf from backup
if [[ -f ${synoinfo}.bak ]]; then
keyvalues=("support_disk_compatibility" "support_memory_compatibility")
keyvalues =("mem_max_mb" "supportnvme" "support_m2_pool" "support_wdda")
for v in "${!keyvalues[@]}"; do
defaultval="$(/usr/syno/bin/synogetkeyvalue ${synoinfo}.bak "${keyvalues[v]}")"
currentval="$(/usr/syno/bin/synogetkeyvalue ${synoinfo} "${keyvalues[v]}")"
if [[ $currentval != "$defaultval" ]]; then
if /usr/syno/bin/synosetkeyvalue "$synoinfo" "${keyvalues[v]}" "$defaultval";
then
echo "Restored ${keyvalues[v]} = $defaultval"
fi
fi
done
fi
# Delete "drive_db_test_url=127.0.0.1" line (and line break) from synoinfo.conf
sed -i "/drive_db_test_url=*/d" "$synoinfo"
sed -i "/drive_db_test_url=*/d" /etc/synoinfo.conf
# Restore adapter_cards.conf from backup
# /usr/syno/etc.defaults/adapter_cards.conf
if [[ -f ${adapter_cards}.bak ]]; then
if cp -p "${adapter_cards}.bak" "${adapter_cards}"; then
echo "Restored ${adapter_cards}"
else
restoreerr=1
echo -e "${Error}ERROR${Off} Failed to restore ${adapter_cards}!\n"
fi
# /usr/syno/etc/adapter_cards.conf
if cp -p "${adapter_cards}.bak" "${adapter_cards2}"; then
echo -e "Restored ${adapter_cards2}"
else
restoreerr=1
echo -e "${Error}ERROR${Off} Failed to restore ${adapter_cards2}!\n"
fi
# Make sure they don't lose E10M20-T1 network connection
modelrplowercase=${modelname//RP/rp}
/usr/syno/bin/set_section_key_value ${adapter_cards} E10M20-T1_sup_nic "$modelrplowercase"
/usr/syno/bin/set_section_key_value ${adapter_cards2} E10M20-T1_sup_nic "$modelrplowercase"
fi
# Restore model.dtb from backup
if [[ -f ${dtb_file}.bak ]]; then
# /etc.default/model.dtb
if cp -p "${dtb_file}.bak" "${dtb_file}"; then
echo "Restored ${dtb_file}"
else
restoreerr=1
echo -e "${Error}ERROR${Off} Failed to restore ${dtb_file}!\n"
fi
# Restore /etc/model.dtb from /etc.default/model.dtb
if cp -p "${dtb_file}.bak" "${dtb2_file}"; then
echo -e "Restored ${dtb2_file}"
else
restoreerr=1
echo -e "${Error}ERROR${Off} Failed to restore ${dtb2_file}!\n"
fi
fi
# Restore storage_panel.js from backup
if [[ $buildnumber -gt 64570 ]]; then
# DSM 7.2.1 and later
strgmgrver="$(/usr/syno/bin/synopkg version StorageManager)"
elif [[ $buildnumber -ge 64561 ]]; then
# DSM 7.2
strgmgrver="${buildnumber}${smallfixnumber}"
fi
if [[ -f "${strgmgr}.$strgmgrver" ]]; then
if cp -p "${strgmgr}.$strgmgrver" "$strgmgr"; then
echo "Restored $(basename -- "$strgmgr")"
else
restoreerr=1
echo -e "${Error}ERROR${Off} Failed to restore $(basename -- "$strgmgr")!\n"
fi
else
echo "No backup of $(basename -- "$strgmgr") found."
fi
echo ""
# Restore .db files from backups
for f in "${!dbbakfiles[@]}"; do
replaceme="${dbbakfiles[f]%.bak}" # Remove .bak
if cp -p "${dbbakfiles[f]}" "$replaceme"; then
echo "Restored $(basename -- "$replaceme")"
else
restoreerr=1
echo -e "${Error}ERROR${Off} Failed to restore $(basename -- "$replaceme")!\n"
fi
done
# Delete any .dbr and .db.newr files left by previous script versions
for f in "${dbpath}"*dbr; do
if [[ -f $f ]]; then
rm "$f" >/dev/null
fi
done
for f in "${dbpath}"*db.newr; do
if [[ -f $f ]]; then
rm "$f" >/dev/null
fi
done
# Update .db files from Synology
/usr/syno/bin/syno_disk_db_update --update
# Enable SynoMemCheck.service if disabled
memcheck="/usr/lib/systemd/system/SynoMemCheck.service"
if [[ $(/usr/syno/bin/synogetkeyvalue "$memcheck" ExecStart) == "/bin/true" ]]; then
/usr/syno/bin/synosetkeyvalue "$memcheck" ExecStart /usr/syno/bin/syno_mem_check
fi
if [[ -z $restoreerr ]]; then
echo -e "\nRestore successful."
fi
# Restore writemostly if set
if [[ $ssd_restore == "yes" ]]; then
# Get array of internal drives
readarray -t internal_drives < <(synodisk --enum -t internal | grep 'Disk path' | cut -d"/" -f3)
# Restore all internal drives to just in_sync
echo -e "\nRestoring internal drive's state"
for idrive in "${internal_drives[@]}"; do
md0="/sys/block/md0/md/dev-"
md1="/sys/block/md1/md/dev-"
if [[ ${idrive::2} == "sd" ]]; then
# sda etc
# Check DSM system and swap partitions
if grep -q "write_mostly" "${md0}$idrive"1/state ||\
grep -q "write_mostly" "${md1}$idrive"2/state; then
set_writemostly -writemostly "$idrive"
fi
else
# sata1 or sas1 etc
# Check DSM system and swap partitions
if grep -q "write_mostly" "${md0}$idrive"p1/state ||\
grep -q "write_mostly" "${md1}$idrive"p2/state; then
set_writemostly -writemostly "$idrive"
fi
fi
done
fi
else
echo "Nothing to restore."
fi
exit
fi
#------------------------------------------------------------------------------
# Get list of installed SATA, SAS and M.2 NVMe/SATA drives,
# PCIe M.2 cards and connected Expansion Units.
vendor_from_id(){
# Vendor ids missing in /usr/syno/etc.defaults/pci_vendor_ids.conf
# $1 is vendor id
# https://devicehunt.com/all-pci-vendors
# https://pci-ids.ucw.cz/
vendor=""
case "${1,,}" in
0x10ec) vendor=TEAMGROUP ;;
0x025e) vendor=Solidigm ;;
0x1458) vendor=Gigabyte ;;
0x1462) vendor=MSI ;;
0x196e) vendor=PNY ;;
0x1987) vendor=Phison ;;
0x1b1c) vendor=Corsair ;;
0x1c5c) vendor="SK Hynix" ;;
0x1cc4) vendor=UMIS ;;
0x1cfa) vendor=Corsair ;; # Memory only?
0x1d97) vendor=SPCC/Lexar ;; # 2 brands with same vid
0x1dbe) vendor=ADATA ;;
0x1e0f) vendor=KIOXIA ;;
0x1e49) vendor=ZHITAI ;;
0x1e4b) vendor=HS/MAXIO ;; # 2 brands with same vid
0x1f40) vendor=Netac ;;
0x1bdc) vendor=Apacer;;
0x0ed1) vendor=aigo ;;
0x05dc) vendor=Lexar ;;
0x1d79) vendor=Transcend;;
*)
# Get vendor from syno_hdd_vendor_ids.txt
vidlist="$scriptpath/syno_hdd_vendor_ids.txt"
if [[ -r "$vidlist" ]]; then
val=$(/usr/syno/bin/synogetkeyvalue "$vidlist" "$1")
if [[ -n "$val" ]]; then
vendor="$val"
else
echo -e "\n${Yellow}WARNING${Off} No vendor found for vid $1" >&2
echo -e "You can add ${Cyan}$1${Off} and your drive's vendor to: " >&2
echo "$vidlist" >&2
fi
else
echo -e "\n${Error}ERROR{OFF} $vidlist not found!" >&2
fi
;;
esac
}
set_vendor(){
# Add missing vendors to /usr/syno/etc.defaults/pci_vendor_ids.conf
if [[ $vendor ]]; then
# DS1817 , DS1517 , RS1219 , RS818 don't have pci_vendor_ids.conf
if [[ "$vidfile" ]]; then
if ! grep -q "$vid" "$vidfile"; then
/usr/syno/bin/synosetkeyvalue "$vidfile" "${vid,,}" "$vendor"
val=$(/usr/syno/bin/synogetkeyvalue "$vidfile" "${vid,,}")
if [[ $val == "${vendor}" ]]; then
echo -e "\nAdded $vendor to pci_vendor_ids" >&2
else
echo -e "\nFailed to add $vendor to pci_vendor_ids!" >&2
fi
fi
if ! grep -q "$vid" "$vidfile2"; then
/usr/syno/bin/synosetkeyvalue "$vidfile2" "${vid,,}" "$vendor"
fi
# Add leading 0 to short vid (change 0x5dc to 0x05dc)
if [[ ${#vid} -eq "5" ]]; then
vid="0x0${vid: -3}"
fi
if ! grep -q "$vid" "$vidfile"; then
/usr/syno/bin/synosetkeyvalue "$vidfile" "${vid,,}" "$vendor"
fi
if ! grep -q "$vid" "$vidfile2"; then
/usr/syno/bin/synosetkeyvalue "$vidfile2" "${vid,,}" "$vendor"
fi
fi
fi
}
get_vid(){
# $1 is /dev/nvme0n1 etc
if [[ $1 ]]; then
vid=$(nvme id-ctrl "$1" | grep -E ^vid | awk '{print $NF}')
if [[ $vid ]]; then
val=$(/usr/syno/bin/synogetkeyvalue "$vidfile" "${vid,,}")
if [[ -z $val ]]; then
vendor_from_id "$vid" && set_vendor
fi
fi
fi
}
fixdrivemodel(){
# Remove " 00Y" from end of Samsung/Lenovo SSDs # Github issue #13
if [[ $1 =~ MZ.*' 00Y' ]]; then
hdmodel=$(printf "%s" "$1" | sed 's/ 00Y.*//')
fi
# Brands that return "BRAND <model>" and need "BRAND " removed.
if [[ $1 =~ ^[A-Za-z]{3,7}' '.* ]]; then
# See Smartmontools database in /var/lib/smartmontools/drivedb.db
hdmodel=${hdmodel#"WDC "} # Remove "WDC " from start of model name
hdmodel=${hdmodel#"HGST "} # Remove "HGST " from start of model name
hdmodel=${hdmodel#"TOSHIBA "} # Remove "TOSHIBA " from start of model name
# Old drive brands
hdmodel=${hdmodel#"Hitachi "} # Remove "Hitachi " from start of model name
hdmodel=${hdmodel#"SAMSUNG "} # Remove "SAMSUNG " from start of model name
hdmodel=${hdmodel#"FUJISTU "} # Remove "FUJISTU " from start of model name
elif [[ $1 =~ ^'APPLE HDD '.* ]]; then
# Old drive brands
hdmodel=${hdmodel#"APPLE HDD "} # Remove "APPLE HDD " from start of model name
fi
}
get_size_gb(){
# $1 is /sys/block/sata1 or /sys/block/nvme0n1 etc
local float
local int
float=$(synodisk --info /dev/"$(basename -- "$1")" | grep 'Total capacity' | awk '{print $4 * 1.0737}')
int="${float%.*}"
echo "$int"
}
getdriveinfo(){
# $1 is /sys/block/sata1 etc
# Skip USB drives
usb=$(grep "$(basename -- "$1")" /proc/mounts | grep "[Uu][Ss][Bb]" | cut -d" " -f1-2)
if [[ ! $usb ]]; then
# Get drive model
hdmodel=$(cat "$1/device/model")
hdmodel=$(printf "%s" "$hdmodel" | xargs) # trim leading and trailing white space
# Fix dodgy model numbers
fixdrivemodel "$hdmodel"
# Get drive firmware version
#fwrev=$(cat "$1/device/rev")
#fwrev=$(printf "%s" "$fwrev" | xargs) # trim leading and trailing white space
device=/dev/"$(basename -- "$1")"
#fwrev=$(/usr/syno/bin/syno_hdd_util --ssd_detect | grep "$device " | awk '{print $2}') # GitHub issue #86, 87
# Account for SSD drives with spaces in their model name/number
fwrev=$(/usr/syno/bin/syno_hdd_util --ssd_detect | grep "$device " | awk '{print $(NF-3)}') # GitHub issue #86, 87
# Get M.2 SATA SSD firmware version
if [[ -z $fwrev ]]; then
dev=/dev/"$(basename -- "$1")"
fwrev=$(smartctl -a -d sat -T permissive "$dev" | grep -i firmware | awk '{print $NF}')
fi
# Get drive GB size
size_gb=$(get_size_gb "$1")
if [[ $hdmodel ]] && [[ $fwrev ]]; then
if /usr/syno/bin/synodisk --enum -t cache | grep -q /dev/"$(basename -- "$1")"; then
# Is SATA M.2 SSD
nvmelist =("${hdmodel},${fwrev},${size_gb}")
else
hdlist =("${hdmodel},${fwrev},${size_gb}")
fi
drivelist =("${hdmodel}")
fi
fi
}
getm2info(){
# $1 is /sys/block/nvme0n1 etc
nvmemodel=$(cat "$1/device/model")
nvmemodel=$(printf "%s" "$nvmemodel" | xargs) # trim leading and trailing white space
if [[ $2 == "nvme" ]]; then
nvmefw=$(cat "$1/device/firmware_rev")
elif [[ $2 == "nvc" ]]; then
nvmefw=$(cat "$1/device/rev")
fi
nvmefw=$(printf "%s" "$nvmefw" | xargs) # trim leading and trailing white space
# Get drive GB size
size_gb=$(get_size_gb "$1")
if [[ $nvmemodel ]] && [[ $nvmefw ]]; then
nvmelist =("${nvmemodel},${nvmefw},${size_gb}")
drivelist =("${nvmemodel}")
fi
}
getcardmodel(){
# Get M.2 card model (if M.2 drives found)
# $1 is /dev/nvme0n1 etc
if [[ ${#nvmelist[@]} -gt "0" ]]; then
cardmodel=$(/usr/syno/bin/synodisk --m2-card-model-get "$1")
if [[ $cardmodel =~ M2D[0-9][0-9] ]]; then
# M2 adaptor card
if [[ -f "${model}_${cardmodel,,}${version}.db" ]]; then
m2carddblist =("${model}_${cardmodel,,}${version}.db") # M.2 card's db file
fi
if [[ -f "${model}_${cardmodel,,}.db" ]]; then
m2carddblist =("${model}_${cardmodel,,}.db") # M.2 card's db file
fi
m2cardlist =("$cardmodel") # M.2 card
elif [[ $cardmodel =~ E[0-9][0-9] M. ]]; then
# Ethernet M2 adaptor card
if [[ -f "${model}_${cardmodel,,}${version}.db" ]]; then
m2carddblist =("${model}_${cardmodel,,}${version}.db") # M.2 card's db file
fi
if [[ -f "${model}_${cardmodel,,}.db" ]]; then
m2carddblist =("${model}_${cardmodel,,}.db") # M.2 card's db file
fi
m2cardlist =("$cardmodel") # M.2 card
fi
fi
}
m2_pool_support(){
# M.2 drives in M2 adaptor card do not officially support storage pools
if [[ -f /run/synostorage/disks/"$(basename -- "$1")"/m2_pool_support ]]; then # GitHub issue #86, 87
echo -n 1 > /run/synostorage/disks/"$(basename -- "$1")"/m2_pool_support