-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
syno_shr_switch.sh
528 lines (448 loc) · 17.3 KB
/
syno_shr_switch.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
#!/usr/bin/env bash
#------------------------------------------------------------------------------
# Switch between SHR and RAID Group for models that have SHR & SHR-2 disabled
# Enable RAID-F1 on non-business models that don't have RAID-F1 enabled
#
# Github: https://github.com/007revad/Synology_SHR_switch
# Script verified at https://www.shellcheck.net/
#
# To run in a shell (replace /volume1/scripts/ with path to script):
# sudo /volume1/scripts/syno_shr_switch.sh
#------------------------------------------------------------------------------
scriptver="v2.0.13"
script=Synology_SHR_switch
repo="007revad/Synology_SHR_switch"
scriptname=syno_shr_switch
# 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
#echo -e "bash version: $(bash --version | head -1 | cut -d' ' -f4)\n" # debug
# Shell Colors
#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}
usage(){
cat <<EOF
$script $scriptver - by 007revad
Usage: $(basename "$0") [options]
Options:
-c, --check Check the currently set RAID type
-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 abcdefghijklmnopqrstuvwxyz0123456789 -l \
check,help,version,log,debug -- "$@")"; then
eval set -- "$options"
while true; do
case "${1,,}" in
-h|--help) # Show usage options
usage
;;
-v|--version) # Show script version
scriptversion
;;
-l|--log) # Log
log=yes
;;
-d|--debug) # Show and log debug info
debug=yes
;;
-c|--check) # Show current raid type
check=yes
break
;;
--)
shift
break
;;
*) # Show usage options
echo -e "Invalid option '$1'\n"
usage "$1"
;;
esac
shift
done
else
usage
fi
if [[ $debug == "yes" ]]; then
set -x
export PS4='`[[ $? == 0 ]] || echo "\e[1;31;40m($?)\e[m\n "`:.$LINENO:'
fi
# Check script is running as root
if [[ $( whoami ) != "root" ]]; then
echo -e "${Error}ERROR${Off} This script must be run as root or sudo!"
exit 1
fi
# Show script version
#echo -e "$script $scriptver\ngithub.com/$repo\n"
echo "$script $scriptver"
# Get NAS model
model=$(cat /proc/sys/kernel/syno_hw_version)
# 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 -e "$model DSM $productversion-$buildnumber$smallfix $buildphase\n"
#------------------------------------------------------------------------------
# Check latest release with GitHub API
# 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}"
# 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
cleanup_tmp(){
# 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
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
fi
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
echo -e "${Cyan}Do you want to download $tag now?${Off} [y/n]"
read -r -t 30 reply
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!"
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!"
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"
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}"
fi
# Copy new CHANGES.txt file to script location (if script on a volume)
if [[ $scriptpath =~ /volume* ]]; then
# Set permsissions 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
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}$changestxt downloaded to: ${scriptpath}\n"
# Reload script
printf -- '-%.0s' {1..79}; echo # print 79 -
exec "${scriptpath}/$scriptfile" "${args[@]}"
fi
fi
else
echo -e "${Error}ERROR${Off}"\
"/tmp/$script-$shorttag.tar.gz not found!"
#ls /tmp | grep "$script" # debug
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!"
fi
fi
fi
fi
synoinfo="/etc.defaults/synoinfo.conf"
#----------------------------------------------------------
# Check currently enabled RAID type
# Enable SHR
# support_syno_hybrid_raid="yes"
# supportraidgroup="no"
#
# Enable RAID Group
# support_syno_hybrid_raid="no"
# supportraidgroup="yes"
# Set short variables
sshr=support_syno_hybrid_raid
srg=supportraidgroup
srf1=support_diffraid
# Check current setting
echo ""
checkcurrent(){
settingshr="$(/usr/syno/bin/synogetkeyvalue $synoinfo ${sshr})"
settingraidgrp="$(/usr/syno/bin/synogetkeyvalue $synoinfo ${srg})"
if [[ $settingshr == "yes" ]] && [[ $settingraidgrp != "yes" ]]; then
echo -e "${Cyan}SHR${Off} ${1}enabled.\n" >&2
enabled="shr"
elif [[ $settingshr != "yes" ]] && [[ $settingraidgrp == "yes" ]]; then
echo -e "${Cyan}RAID Groups${Off} ${1}enabled.\n" >&2
enabled="raidgrp"
fi
settingraidf1="$(/usr/syno/bin/synogetkeyvalue $synoinfo ${srf1})"
if [[ $settingraidf1 == "yes" ]]; then
echo -e "${Cyan}RAID-F1${Off} ${1}enabled.\n" >&2
enabledf1="raidf1"
else
echo -e "${Cyan}RAID-F1${Off} ${1}disabled.\n" >&2
fi
}
#checkcurrent "is currently "
checkcurrent "is "
if [[ $check == "yes" ]]; then
exit
fi
#--------------------------------------------------------------------
# Select RAID type
PS3="Select the RAID type to enable: "
if [[ $enabled == "shr" ]]; then
#options=("RAID Groups" "Quit")
if [[ $enabledf1 == "raidf1" ]]; then
options=("RAID Groups" "Restore" "Quit")
else
options=("RAID Groups" "RAID-F1" "Restore" "Quit")
fi
elif [[ $enabled == "raidgrp" ]]; then
#options=("SHR" "Quit")
if [[ $enabledf1 == "raidf1" ]]; then
options=("SHR" "Restore" "Quit")
else
options=("SHR" "RAID-F1" "Restore" "Quit")
fi
else
#options=("SHR" "RAID Groups" "Restore" "Quit")
if [[ $enabledf1 == "raidf1" ]]; then
options=("SHR" "RAID Groups" "Quit")
else
options=("SHR" "RAID Groups" "RAID-F1" "Quit")
fi
fi
select raid in "${options[@]}"; do
case "$raid" in
SHR)
shr="yes"
echo -e "You selected ${Cyan}SHR${Off}"
if [[ $enabled == "shr" ]]; then
echo -e "${Cyan}SHR${Off} is already enabled."
else
break
fi
;;
"RAID Group")
raidgrp="yes"
echo -e "You selected ${Cyan}RAID Group${Off}"
if [[ $enabled == "raidgrp" ]]; then
echo -e "${Cyan}RAID Groups${Off} is already enabled."
else
break
fi
break
;;
"RAID-F1")
raidf1="yes"
echo -e "You selected ${Cyan}RAID-F1${Off}"
if [[ $enabledf1 == "raidf1" ]]; then
echo -e "${Cyan}RAID-F1${Off} is already enabled."
else
break
fi
break
;;
Restore)
restore="yes"
echo -e "You selected ${Cyan}Restore${Off}"
break
;;
Quit)
echo -e "You selected ${Cyan}Quit${Off}"
exit
;;
*)
echo -e "${Red}Invalid answer!${Off} Try again."
;;
esac
done
#----------------------------------------------------------
# Restore from backup synoinfo.conf
if [[ $restore == "yes" ]]; then
if [[ -f ${synoinfo}.bak ]]; then
# Restore from backup
#if cp -p "$synoinfo".bak "$synoinfo" ; then
# echo -e "\nSuccessfully restored from backup.\n"
# checkcurrent "is now "
# exit
#else
# echo -e "\n${Error}ERROR${Off} Restore from backup failed!"
# exit 1
#fi
# Get default key values from backup
defaultsrg=$(/usr/syno/bin/synogetkeyvalue "$synoinfo".bak "$srg")
defaultsshr=$(/usr/syno/bin/synogetkeyvalue "$synoinfo".bak "$sshr")
defaultsrf1=$(/usr/syno/bin/synogetkeyvalue "$synoinfo".bak "$srf1")
# Set key values to defaults
if [[ $defaultsrg ]]; then
#echo -e "\nset support_raid_group $defaultsrg" # debug
/usr/syno/bin/synosetkeyvalue "$synoinfo" "$srg" "$defaultsrg"
else
#echo -e "\nset support_raid_group no" # debug
/usr/syno/bin/synosetkeyvalue "$synoinfo" "$srg" "no"
fi
if [[ $defaultsshr ]]; then
#echo "set support_syno_hybrid_raid $defaultsshr" # debug
/usr/syno/bin/synosetkeyvalue "$synoinfo" "$sshr" "$defaultsshr"
else
#echo "set support_syno_hybrid_raid no" # debug
/usr/syno/bin/synosetkeyvalue "$synoinfo" "$sshr" "no"
fi
echo
if [[ $defaultsrf1 ]]; then
#echo -e "\nset support_raid_group $defaultsrf1" # debug
/usr/syno/bin/synosetkeyvalue "$synoinfo" "$srf1" "$defaultsrf1"
else
#echo -e "\nset support_diffraid no" # debug
/usr/syno/bin/synosetkeyvalue "$synoinfo" "$srf1" "no"
fi
checkcurrent "is "
else
echo -e "\n${Error}ERROR${Off} Backup synoinfo.conf not found!"
exit 1
fi
exit
fi
#----------------------------------------------------------
# Backup synoinfo.conf
if [[ ! -f ${synoinfo}.bak ]]; then
if cp -p "$synoinfo" "$synoinfo".bak ; then
echo -e "\nsynoinfo.conf backed up."
else
echo -e "\n${Error}ERROR${Off} synoinfo.conf backup failed!"
exit 1
fi
else
echo -e "\nsynoinfo.conf already backed up."
fi
#----------------------------------------------------------
# Edit synoinfo.conf
# Enable RAID Group
if [[ $raidgrp == "yes" ]]; then
/usr/syno/bin/synosetkeyvalue "$synoinfo" "$srg" yes
/usr/syno/bin/synosetkeyvalue "$synoinfo" "$sshr" no
# Check if we enabled RAID Group
settingshr="$(/usr/syno/bin/synogetkeyvalue $synoinfo ${sshr})"
settingraidgrp="$(/usr/syno/bin/synogetkeyvalue $synoinfo ${srg})"
if [[ $settingshr != "yes" ]] && [[ $settingraidgrp == "yes" ]]; then
echo -e "\n${Cyan}RAID Group${Off} has been enabled.\n"
else
echo -e "\n${Error}ERROR${Off} Failed to enable RAID Group!"
fi
fi
# Enable SHR
if [[ $shr == "yes" ]]; then
/usr/syno/bin/synosetkeyvalue "$synoinfo" "$srg" no
/usr/syno/bin/synosetkeyvalue "$synoinfo" "$sshr" yes
# Check if we enabled SHR
settingshr="$(/usr/syno/bin/synogetkeyvalue $synoinfo ${sshr})"
settingraidgrp="$(/usr/syno/bin/synogetkeyvalue $synoinfo ${srg})"
if [[ $settingshr == "yes" ]] && [[ $settingraidgrp != "yes" ]]; then
echo -e "\n${Cyan}SHR${Off} has been enabled.\n"
else
echo -e "\n${Error}ERROR${Off} Failed to enable SHR!"
fi
fi
# Enable RAID-F1
if [[ $raidf1 == "yes" ]]; then
/usr/syno/bin/synosetkeyvalue "$synoinfo" "$srf1" yes
# Check if we enabled RAID-F1
settingraidf1="$(/usr/syno/bin/synogetkeyvalue $synoinfo ${srf1})"
if [[ $settingraidf1 == "yes" ]]; then
echo -e "\n${Cyan}RAID-F1${Off} has been enabled.\n"
else
echo -e "\n${Error}ERROR${Off} Failed to enable RAID-F1!"
fi
fi
exit