forked from 007revad/Synology_enable_M2_volume
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syno_enable_m2_volume.sh
596 lines (508 loc) · 18.8 KB
/
syno_enable_m2_volume.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
#!/usr/bin/env bash
#------------------------------------------------------------------------------
# Enables using non-Synology NVMe drives so you can create a storage pool
# and volume on any M.2 drive(s) entirely in DSM Storage Manager.
#
# Github: https://github.com/007revad/Synology_enable_M2_volume
# Script verified at https://www.shellcheck.net/
# Tested on DSM 7.2 beta
#
# To run in a shell (replace /volume1/scripts/ with path to script):
# sudo /volume1/scripts/syno_enable_m2_volume.sh
#------------------------------------------------------------------------------
scriptver="v1.0.7"
script=Synology_enable_M2_volume
repo="007revad/Synology_enable_M2_volume"
# Check BASH variable is is non-empty and posix mode is off, else abort with error.
[ "$BASH" ] && ! shopt -qo posix || {
printf \\a
printf >&2 "This is a bash script, don't run it with sh\n"
exit 1
}
#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}
ding(){
printf \\a
}
usage(){
cat <<EOF
$script $scriptver - by 007revad
Usage: $(basename "$0") [options]
Options:
-c, --check Check value in file and backup file
-r, --restore Restore backup to undo changes
-h, --help Show this help message
-v, --version Show the script version
EOF
}
scriptversion(){
cat <<EOF
$script $scriptver - by 007revad
See https://github.com/$repo
EOF
}
# Save options used
args=("$@")
# Check for flags with getopt
if options="$(getopt -o abcdefghijklmnopqrstuvwxyz0123456789 -a \
-l check,restore,help,version,log,debug -- "$@")"; then
eval set -- "$options"
while true; do
case "${1,,}" in
-c|--check) # Check value in file and backup file
check=yes
;;
-r|--restore) # Restore backup to undo changes
restore=yes
;;
-h|--help) # Show usage options
usage
exit
;;
-v|--version) # Show script version
scriptversion
exit
;;
-l|--log) # Log
#log=yes
;;
-d|--debug) # Show and log debug info
debug=yes
;;
--)
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
# Check script is running as root
if [[ $( whoami ) != "root" ]]; then
ding
echo -e "${Error}ERROR${Off} This script must be run as root or sudo!"
exit 1
fi
# Get DSM major version
dsm=$(get_key_value /etc.defaults/VERSION majorversion)
if [[ $dsm -lt "7" ]]; then
ding
echo "This script only works for DSM 7."
exit 1
fi
# Check bc command exists
if ! which bc >/dev/null ; then
echo -e "${Error}ERROR ${Off} bc command not found!\n"
#echo -e "This script needs the bc command, which is not included in DSM."
echo -e "Please install ${Cyan}SynoCli misc. Tools${Off} from SynoCommunity."
echo -e " 1. Package Center > Settings > Package Sources > Add"
echo -e " 2. Name: ${Cyan}SynoCommunity${Off}"
echo -e " 3. Location: ${Cyan}https://packages.synocommunity.com/${Off}"
echo -e " 4. Click OK and OK again."
echo -e " 5. Click Community on the left."
echo -e " 6. Install ${Cyan}SynoCli misc. Tools${Off}\n"
exit
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=$(get_key_value /etc.defaults/VERSION productversion)
buildphase=$(get_key_value /etc.defaults/VERSION buildphase)
buildnumber=$(get_key_value /etc.defaults/VERSION buildnumber)
smallfixnumber=$(get_key_value /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"
# Show options used
echo "Using options: ${args[*]}"
#------------------------------------------------------------------------------
# Check latest release with GitHub API
get_latest_release() {
# Curl timeout options:
# https://unix.stackexchange.com/questions/94604/does-curl-have-a-timeout
curl --silent -m 10 --connect-timeout 5 \
"https://api.github.com/repos/$1/releases/latest" |
grep '"tag_name":' | # Get tag line
sed -E 's/.*"([^"]+)".*/\1/' # Pluck JSON value
}
tag=$(get_latest_release "$repo")
shorttag="${tag:1}"
#scriptpath=$(dirname -- "$0")
# 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 )
#echo "Script location: $scriptpath" # debug
if ! printf "%s\n%s\n" "$tag" "$scriptver" |
sort --check --version-sort &> /dev/null ; then
echo -e "${Cyan}There is a newer version of this script available.${Off}"
echo -e "Current version: ${scriptver}\nLatest version: $tag"
if [[ -f $scriptpath/$script-$shorttag.tar.gz ]]; then
# They have the latest version tar.gz downloaded but are using older version
echo "https://github.com/$repo/releases/latest"
sleep 10
elif [[ -d $scriptpath/$script-$shorttag ]]; then
# They have the latest version extracted but are using older version
echo "https://github.com/$repo/releases/latest"
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
if cd /tmp; then
url="https://github.com/$repo/archive/refs/tags/$tag.tar.gz"
if ! curl -LJO -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
# Copy new script sh files to script location
if ! cp -p "/tmp/$script-$shorttag/"*.sh "$scriptpath"; then
copyerr=1
echo -e "${Error}ERROR ${Off} Failed to copy"\
"$script-$shorttag .sh file(s) to:\n $scriptpath"
else
# Set permsissions on CHANGES.txt
if ! chmod 744 "$scriptpath/"*.sh ; then
permerr=1
echo -e "${Error}ERROR ${Off} Failed to set permissions on:"
echo "$scriptpath *.sh file(s)"
fi
fi
# Copy new CHANGES.txt file to script location
if ! cp -p "/tmp/$script-$shorttag/CHANGES.txt" "$scriptpath"; then
copyerr=1
echo -e "${Error}ERROR ${Off} Failed to copy"\
"$script-$shorttag/CHANGES.txt to:\n $scriptpath"
else
# Set permsissions on CHANGES.txt
if ! chmod 744 "$scriptpath/CHANGES.txt"; then
permerr=1
echo -e "${Error}ERROR ${Off} Failed to set permissions on:"
echo "$scriptpath/CHANGES.txt"
fi
fi
# Delete downloaded .tar.gz file
if ! rm "/tmp/$script-$shorttag.tar.gz"; then
#delerr=1
echo -e "${Error}ERROR ${Off} Failed to delete"\
"downloaded /tmp/$script-$shorttag.tar.gz!"
fi
# Delete extracted tmp files
if ! rm -r "/tmp/$script-$shorttag"; then
#delerr=1
echo -e "${Error}ERROR ${Off} Failed to delete"\
"downloaded /tmp/$script-$shorttag!"
fi
# Notify of success (if there were no errors)
if [[ $copyerr != 1 ]] && [[ $permerr != 1 ]]; then
echo -e "\n$tag and changes.txt downloaded to:"\
"$scriptpath"
echo -e "${Cyan}Do you want to stop this script"\
"so you can run the new one?${Off} [y/n]"
read -r reply
if [[ ${reply,,} == "y" ]]; then exit; fi
fi
fi
else
echo -e "${Error}ERROR ${Off}"\
"/tmp/$script-$shorttag.tar.gz not found!"
#ls /tmp | grep "$script" # debug
fi
fi
else
echo -e "${Error}ERROR ${Off} Failed to cd to /tmp!"
fi
fi
fi
fi
rebootmsg(){
# Reboot prompt
echo -e "\n${Cyan}The Synology needs to restart.${Off}"
echo -e "Type ${Cyan}yes${Off} to reboot now."
echo -e "Type anything else to quit (if you will restart it yourself)."
read -r -t 10 answer
if [[ ${answer,,} != "yes" ]]; then exit; fi
# Reboot in the background so user can see DSM's "going down" message
reboot &
}
#----------------------------------------------------------
# Check file exists
file="/usr/lib/libhwcontrol.so.1"
if [[ ! -f ${file} ]]; then
ding
echo -e "${Error}ERROR ${Off} File not found!"
exit 1
fi
#----------------------------------------------------------
# Restore from backup file
if [[ $restore == "yes" ]]; then
if [[ -f ${file}.bak ]]; then
# Check if backup size matches file size
filesize=$(wc -c "${file}" | awk '{print $1}')
filebaksize=$(wc -c "${file}.bak" | awk '{print $1}')
if [[ ! $filesize -eq "$filebaksize" ]]; then
echo -e "${Yellow}WARNING Backup file size is different to file!${Off}"
echo "Do you want to restore this backup? [yes/no]:"
read -r answer
if [[ $answer != "yes" ]]; then
exit
fi
fi
# Restore from backup
if cp "$file".bak "$file" ; then
echo "Successfully restored from backup."
rebootmsg
exit
else
ding
echo -e "${Error}ERROR ${Off} Backup failed!"
exit 1
fi
else
ding
echo -e "${Error}ERROR ${Off} Backup file not found!"
exit 1
fi
fi
#----------------------------------------------------------
# Backup file
if [[ ! -f ${file}.bak ]]; then
if cp "$file" "$file".bak ; then
echo "Backup successful."
else
ding
echo -e "${Error}ERROR ${Off} Backup failed!"
exit 1
fi
else
# Check if backup size matches file size
filesize=$(wc -c "${file}" | awk '{print $1}')
filebaksize=$(wc -c "${file}.bak" | awk '{print $1}')
if [[ ! $filesize -eq "$filebaksize" ]]; then
echo -e "${Yellow}WARNING Backup file size is different to file!${Off}"
echo "Maybe you've updated DSM since last running this script?"
echo "Renaming file.bak to file.bak.old"
mv "${file}.bak" "$file".bak.old
if cp "$file" "$file".bak ; then
echo "Backup successful."
else
ding
echo -e "${Error}ERROR ${Off} Backup failed!"
exit 1
fi
else
echo "File already backed up."
fi
fi
#----------------------------------------------------------
# Edit file
findbytes(){
# Get decimal position of matching hex string
match=$(od -v -t x1 "$1" |
sed 's/[^ ]* *//' |
tr '\012' ' ' |
grep -b -i -o "$hexstring" |
#grep -b -i -o "$hexstring ".. |
sed 's/:.*/\/3/' |
bc)
# Convert decimal position of matching hex string to hex
array=("$match")
if [[ ${#array[@]} -gt "1" ]]; then
num="0"
while [[ $num -lt "${#array[@]}" ]]; do
poshex=$(printf "%x" "${array[$num]}")
echo "${array[$num]} = $poshex" # debug
seek="${array[$num]}"
xxd=$(xxd -u -l 12 -s "$seek" "$1")
#echo "$xxd" # debug
printf %s "$xxd" | cut -d" " -f1-7
bytes=$(printf %s "$xxd" | cut -d" " -f6)
#echo "$bytes" # debug
num=$((num +1))
done
elif [[ -n $match ]]; then
poshex=$(printf "%x" "$match")
echo "$match = $poshex" # debug
seek="$match"
xxd=$(xxd -u -l 12 -s "$seek" "$1")
#echo "$xxd" # debug
printf %s "$xxd" | cut -d" " -f1-7
bytes=$(printf %s "$xxd" | cut -d" " -f6)
#echo "$bytes" # debug
else
bytes=""
fi
}
# Check value in file and backup file
if [[ $check == "yes" ]]; then
err=0
# Check value in file
echo -e "\nChecking value in file."
hexstring="80 3E 00 B8 01 00 00 00 90 90 48 8B"
findbytes "$file"
if [[ $bytes == "9090" ]]; then
echo -e "\n${Cyan}File already edited.${Off}"
else
hexstring="80 3E 00 B8 01 00 00 00 75 2. 48 8B"
findbytes "$file"
if [[ $bytes =~ "752"[0-9] ]]; then
echo -e "\n${Cyan}File is unedited.${Off}"
else
echo -e "\n${Red}hex string not found!${Off}"
err=1
fi
fi
# Check value in backup file
if [[ -f ${file}.bak ]]; then
echo -e "\nChecking value in backup file."
hexstring="80 3E 00 B8 01 00 00 00 75 2. 48 8B"
findbytes "${file}.bak"
if [[ $bytes =~ "752"[0-9] ]]; then
echo -e "\n${Cyan}Backup file is unedited.${Off}"
else
hexstring="80 3E 00 B8 01 00 00 00 90 90 48 8B"
findbytes "${file}.bak"
if [[ $bytes == "9090" ]]; then
echo -e "\n${Red}Backup file has been edited!${Off}"
else
echo -e "\n${Red}hex string not found!${Off}"
err=1
fi
fi
else
echo "No backup file found."
fi
exit "$err"
fi
echo -e "\nChecking file."
# Check if the file is already edited
hexstring="80 3E 00 B8 01 00 00 00 90 90 48 8B"
findbytes "$file"
if [[ $bytes == "9090" ]]; then
echo -e "\n${Cyan}File already edited.${Off}"
exit
else
# Check if the file is okay for editing
hexstring="80 3E 00 B8 01 00 00 00 75 2. 48 8B"
findbytes "$file"
if [[ $bytes =~ "752"[0-9] ]]; then
echo -e "\nEditing file."
else
ding
echo -e "\n${Red}hex string not found!${Off}"
exit 1
fi
fi
# Replace bytes in file
posrep=$(printf "%x\n" $((0x${poshex}+8)))
if ! printf %s "${posrep}: 9090" | xxd -r - "$file"; then
ding
echo -e "${Error}ERROR ${Off} Failed to edit file!"
exit 1
fi
#----------------------------------------------------------
# Check if file was successfully edited
echo -e "\nChecking if file was successfully edited."
hexstring="80 3E 00 B8 01 00 00 00 90 90 48 8B"
findbytes "$file"
if [[ $bytes == "9090" ]]; then
echo -e "File successfully edited."
echo -e "\n${Cyan}You can now create your M.2 storage"\
"pool in Storage Manager.${Off}"
else
ding
echo -e "${Error}ERROR ${Off} Failed to edit file!"
exit 1
fi
#--------------------------------------------------------------------
# Enable m2 volume support - DSM 7.1 and later only
# Backup synoinfo.conf if needed
#if [[ $dsm72 == "yes" ]]; then
#if [[ $dsm71 == "yes" ]]; then
synoinfo="/etc.defaults/synoinfo.conf"
if [[ ! -f ${synoinfo}.bak ]]; then
if cp "$synoinfo" "$synoinfo.bak"; then
echo -e "\nBacked up $(basename -- "$synoinfo")" >&2
else
ding
echo -e "\n${Error}ERROR 5${Off} Failed to backup $(basename -- "$synoinfo")!"
exit 1
fi
fi
#fi
# Check if m2 volume support is enabled
#if [[ $dsm72 == "yes" ]]; then
#if [[ $dsm71 == "yes" ]]; then
smp=support_m2_pool
setting="$(get_key_value "$synoinfo" "$smp")"
enabled=""
if [[ ! $setting ]]; then
# Add support_m2_pool="yes"
echo 'support_m2_pool="yes"' >> "$synoinfo"
enabled="yes"
elif [[ $setting == "no" ]]; then
# Change support_m2_pool="no" to "yes"
#sed -i "s/${smp}=\"no\"/${smp}=\"yes\"/" "$synoinfo"
synosetkeyvalue "$synoinfo" "$smp" "yes"
enabled="yes"
elif [[ $setting == "yes" ]]; then
echo -e "\nM.2 volume support already enabled."
fi
# Check if we enabled m2 volume support
setting="$(get_key_value "$synoinfo" "$smp")"
if [[ $enabled == "yes" ]]; then
if [[ $setting == "yes" ]]; then
echo -e "\nEnabled M.2 volume support."
else
echo -e "\n${Error}ERROR${Off} Failed to enable m2 volume support!"
fi
fi
#fi
# Enable creating M.2 storage pool and volume in Storage Manager
# for currently installed NVMe drives
for nvme in /run/synostorage/disks/nvme*; do
echo 1 > /run/synostorage/disks/"$(basename -- "$nvme")"/m2_pool_support
done
#----------------------------------------------------------
# Reboot
rebootmsg
exit