diff --git a/CHANGES.txt b/CHANGES.txt index de26f75..52ac6e6 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,3 +1,14 @@ +v2.2.42 +- Fixed bug where expansion units ending in RP or II were not detected. +- Added a --restore option to undo all changes made by the script. +- Now looks for and edits both v7 and non-v7 db files to solve issue #11 for RS '21 models running DSM 6.2.4. This will also ensure the script still works if: + - Synology append different numbers to the db file names in DSM 8 etc. + - The detected NAS model name does not match the .db files' model name. +- Now backs up the .db.new files (as well as the .db files). +- Now shows max memory in GB instead of MB. +- Now shows status of "Support disk compatibility" setting even if it wasn't changed. +- Now shows status of "Support memory compatibility" setting even if it wasn't changed. + v2.1.38 - Improved shell output when editing max memory setting. - Changed method of checking if drive is a USB drive to prevent ignoring internal drives on RS models. diff --git a/syno_hdd_db.sh b/syno_hdd_db.sh index 8197c5a..cf0b986 100644 --- a/syno_hdd_db.sh +++ b/syno_hdd_db.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# shellcheck disable=SC1083,SC2054,SC2121 +# shellcheck disable=SC1083,SC2054,SC2121,SC2207 #-------------------------------------------------------------------------------------------------- # Github: https://github.com/007revad/Synology_HDD_db # Script verified at https://www.shellcheck.net/ @@ -30,6 +30,23 @@ # It's also parsed and checked and probably in some cases it could be more critical to patch that one instead. # DONE +# Fixed bug where expansion units ending in RP or II were not detected. +# +# Added a --restore option to undo all changes. +# +# Now looks for and edits both v7 and non-v7 db files to solve issue #11 for RS '21 models running DSM 6.2.4. +# This will also ensure the script still works if: +# Synology append different numbers to the db file names in DSM 8 etc. +# The detected NAS model name does not match the .db files' model name. +# +# Now backs up the .db.new files (as well as the .db files). +# +# Now shows max memory in GB instead of MB. +# +# Now shows status of "Support disk compatibility" setting even if it wasn't changed. +# +# Now shows status of "Support memory compatibility" setting even if it wasn't changed. +# # Improved shell output when editing max memory setting. # # Changed method of checking if drive is a USB drive to prevent ignoring internal drives on RS models. @@ -121,7 +138,7 @@ # Optionally disable "support_disk_compatibility". -scriptver="v2.1.38" +scriptver="v2.2.42" script=Synology_HDD_db repo="007revad/Synology_HDD_db" @@ -171,19 +188,23 @@ EOF # Save options used -args="$@" +args="$*" # Check for flags with getopt if options="$(getopt -o abcdefghijklmnopqrstuvwxyz0123456789 -a \ - -l showedits,noupdate,nodbupdate,m2,force,ram,help,version,debug -- "$@")"; then + -l restore,showedits,noupdate,nodbupdate,m2,force,ram,help,version,debug -- "$@")"; then eval set -- "$options" while true; do case "${1,,}" in + --restore) # Restore changes from backups + restore=yes + break + ;; -s|--showedits) # Show edits done to host db file showedits=yes ;; - -n|--nodbupdate|--noupdate) # Disable disk compatibility db updates + -n|--nodbupdate|--noupdate) # Disable disk compatibility db updates nodbupdate=yes ;; -m|--m2) # Don't add M.2 drives to db files @@ -371,14 +392,12 @@ if ! printf "%s\n%s\n" "$tag" "$scriptver" | # 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 @@ -407,6 +426,49 @@ if ! printf "%s\n%s\n" "$tag" "$scriptver" | fi +#------------------------------------------------------------------------------ +# Restore changes from backups + +dbpath=/var/lib/disk-compatibility/ +synoinfo="/etc.defaults/synoinfo.conf" + +if [[ $restore == "yes" ]]; then + dbbakfiles=($(find $dbpath -maxdepth 1 \( -name "*.db.new.bak" -o -name "*.db.bak" \))) + echo + + if [[ ${#dbbakfiles[@]} -gt "0" ]] || [[ -f ${synoinfo}.bak ]]; then + + # Restore .db files from backups + for f in "${!dbbakfiles[@]}"; do + deleteme="${dbbakfiles[f]%.bak}" # Remove .bak + if mv "${dbbakfiles[f]}" "$deleteme"; then + echo "Restored $(basename -- "$deleteme")" + else + restoreerr=1 + echo -e "${Error}ERROR${Off} Failed to restore $(basename -- "$deleteme")!\n" + fi + done + + # Restore synoinfo.conf from backup + if [[ -f ${synoinfo}.bak ]]; then + if mv "${synoinfo}.bak" "${synoinfo}"; then + echo "Restored $(basename -- "$synoinfo")" + else + restoreerr=1 + echo -e "${Error}ERROR${Off} Failed to restore synoinfo.conf!\n" + fi + fi + + if [[ -z $restoreerr ]]; then + echo -e "\nRestore successful." + 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. @@ -437,7 +499,7 @@ getdriveinfo() { # $1 is /sys/block/sata1 etc # Skip USB drives - usb=$(grep $(basename -- "$1") /proc/mounts | grep usb | cut -d" " -f1-2) + usb=$(grep "$(basename -- "$1")" /proc/mounts | grep usb | cut -d" " -f1-2) if [[ ! $usb ]]; then # Get drive model and firmware version @@ -479,12 +541,22 @@ getcardmodel() { cardmodel=$(synodisk --m2-card-model-get "$1") if [[ $cardmodel =~ M2D[0-9][0-9] ]]; then # M2 adaptor card - m2carddblist+=("${model}_${cardmodel,,}${version}.db") # M.2 card's db file - m2cardlist+=("$cardmodel") # M.2 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 - m2carddblist+=("${model}_${cardmodel,,}${version}.db") # M.2 card's db file - m2cardlist+=("$cardmodel") # M.2 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 } @@ -513,12 +585,11 @@ for d in /sys/block/*; do getcardmodel "/dev/$(basename -- "${d}")" # Enable creating M.2 storage pool and volume in Storage Manager - echo 1 > /run/synostorage/disks/$(basename -- "$d")/m2_pool_support + echo 1 > /run/synostorage/disks/"$(basename -- "$d")"/m2_pool_support fi fi ;; nvc*) # M.2 SATA drives (in PCIe card only?) - #if [[ $d =~ nvc[0-9][0-9]?p[0-9][0-9]?$ ]]; then if [[ $d =~ nvc[0-9][0-9]?$ ]]; then if [[ $m2 != "no" ]]; then getm2info "$d" "nvc" @@ -526,7 +597,7 @@ for d in /sys/block/*; do getcardmodel "/dev/$(basename -- "${d}")" # Enable creating M.2 storage pool and volume in Storage Manager - echo 1 > /run/synostorage/disks/$(basename -- "$d")/m2_pool_support + echo 1 > /run/synostorage/disks/"$(basename -- "$d")"/m2_pool_support fi fi ;; @@ -615,9 +686,8 @@ fi # Get list of connected expansion units (aka eunit/ebox) path="/var/log/diskprediction" # shellcheck disable=SC2012 -file=$(ls $path | tail -n1) -# shellcheck disable=SC2207 -eunitlist=($(grep -Eow "([FRD]XD?[0-9]{3,4})(RP|II|sas){0,2}" "$path/$file" | uniq)) +file=$(ls $path | tail -n1) +eunitlist=($(grep -Eowi "([FRD]XD?[0-9]{3,4})(rp|ii|sas){0,2}" "$path/$file" | uniq)) # Sort eunitlist array into new eunits array to remove duplicates if [[ ${#eunitlist[@]} -gt "0" ]]; then @@ -644,16 +714,27 @@ fi #------------------------------------------------------------------------------ # Check databases and add our drives if needed -db1="/var/lib/disk-compatibility/${model}_host${version}.db" -db2="/var/lib/disk-compatibility/${model}_host${version}.db.new" -dbpath="/var/lib/disk-compatibility/" +# Host db files +db1list=($(find "$dbpath" -maxdepth 1 -name "*_host*.db")) +db2list=($(find "$dbpath" -maxdepth 1 -name "*_host*.db.new")) -synoinfo="/etc.defaults/synoinfo.conf" +# Expansion Unit db files +for i in "${!eunits[@]}"; do + eunitdb1list=($(find "$dbpath" -maxdepth 1 -name "${eunits[i],,}*.db")) + eunitdb2list=($(find "$dbpath" -maxdepth 1 -name "${eunits[i],,}*.db.new")) +done +# M.2 Card db files +for i in "${!m2cards[@]}"; do + m2carddb1list=($(find "$dbpath" -maxdepth 1 -name "*_${m2cards[i],,}*.db")) + m2carddb2list=($(find "$dbpath" -maxdepth 1 -name "*_${m2cards[i],,}*.db.new")) +done -if [[ ! -f "$db1" ]]; then echo -e "${Error}ERROR 3${Off} $db1 not found!" && exit 3; fi -#if [[ ! -f "$db2" ]]; then echo -e "${Error}ERROR 4${Off} $db2 not found!" && exit 4; fi -# new installs don't have a .db.new file + +if [[ ${#db1list[@]} -eq "0" ]]; then + echo -e "${Error}ERROR 4${Off} Host db file not found!" && exit 4 +fi +# Don't check .db.new as new installs don't have a .db.new file getdbtype(){ @@ -689,7 +770,18 @@ backupdb() { # Backup host database file if needed -backupdb "$db1" || exit 5 +for i in "${!db1list[@]}"; do + backupdb "${db1list[i]}" ||{ + echo -e "${Error}ERROR 5${Off} Failed to backup $(basename -- "${db1list[i]}")!" + exit 5 + } +done +for i in "${!db2list[@]}"; do + backupdb "${db2list[i]}" ||{ + echo -e "${Error}ERROR 5${Off} Failed to backup $(basename -- "${db2list[i]}")!" + exit 5 # maybe don't exit for .db.new file + } +done #------------------------------------------------------------------------------ @@ -697,9 +789,9 @@ backupdb "$db1" || exit 5 editcount(){ # Count drives added to host db files - if [[ $1 == "$db1" ]]; then + if [[ $1 =~ .*\.db$ ]]; then db1Edits=$((db1Edits +1)) - elif [[ $1 == "$db2" ]]; then + elif [[ $1 =~ .*\.db.new ]]; then db2Edits=$((db2Edits +1)) fi } @@ -764,17 +856,17 @@ updatedb() { if grep '"disk_compatbility_info":{}' "$2" >/dev/null; then # Replace "disk_compatbility_info":{} with "disk_compatbility_info":{"WD40PURX-64GVNY0":{"80.00A80":{ ... }}},"default":{ ... }}}} - echo "Edit empty db file" + #echo "Edit empty db file:" # debug editdb7 "empty" "$2" elif grep '"'"$hdmodel"'":' "$2" >/dev/null; then # Replace "WD40PURX-64GVNY0":{ with "WD40PURX-64GVNY0":{"80.00A80":{ ... }}}, - echo "Insert firmware version" + #echo "Insert firmware version:" # debug editdb7 "insert" "$2" else # Add "WD40PURX-64GVNY0":{"80.00A80":{ ... }}},"default":{ ... }}} - echo "Append drive and firmware" + #echo "Append drive and firmware:" # debug editdb7 "append" "$2" fi @@ -786,23 +878,10 @@ updatedb() { string="{\"model\":\"${hdmodel}\",\"firmware\":\"\",\"rec_intvl\":\[1\]}," # {"success":1,"list":[ startstring="{\"success\":1,\"list\":\[" - - #echo "$startstring" >&2 # debug - #echo "$string" >&2 # debug - #echo >&2 # debug - # example: # {"success":1,"list":[{"model":"WD60EFRX-68MYMN1","firmware":"82.00A82","rec_intvl":[1]}, if sed -ir "s/$startstring/$startstring$string/" "$2"; then echo -e "Added ${Yellow}$hdmodel${Off} to ${Cyan}$(basename -- "$2")${Off}" - - # Count drives added to host db files - if [[ $2 == "$db1" ]]; then - db1Edits=$((db1Edits +1)) - elif [[ $2 == "$db2" ]]; then - db2Edits=$((db2Edits +1)) - fi - else echo -e "\n${Error}ERROR 8${Off} Failed to update $(basename -- "$2")${Off}" >&2 exit 8 @@ -814,23 +893,22 @@ updatedb() { # HDDs and SATA SSDs num="0" while [[ $num -lt "${#hdds[@]}" ]]; do - updatedb "${hdds[$num]}" "$db1" - if [[ -f "$db2" ]]; then - updatedb "${hdds[$num]}" "$db2" - fi + for i in "${!db1list[@]}"; do + updatedb "${hdds[$num]}" "${db1list[i]}" + done + for i in "${!db2list[@]}"; do + updatedb "${hdds[$num]}" "${db2list[i]}" + done #------------------------------------------------ # Expansion Units - num2="0" - while [[ $num2 -lt "${#eunits[@]}" ]]; do - eudb="${dbpath}${eunits[$num2],,}${version}.db" - if [[ -f "$eudb" ]]; then - backupdb "$eudb" &&\ - updatedb "${hdds[$num]}" "$eudb" - else - echo -e "${Error}ERROR 11${Off} $eudb not found!" - fi - num2=$((num2 +1)) + for i in "${!eunitdb1list[@]}"; do + backupdb "${eunitdb1list[i]}" &&\ + updatedb "${hdds[$num]}" "${eunitdb1list[i]}" + done + for i in "${!eunitdb2list[@]}"; do + backupdb "${eunitdb2list[i]}" &&\ + updatedb "${hdds[$num]}" "${eunitdb2list[i]}" done #------------------------------------------------ @@ -840,22 +918,22 @@ done # M.2 NVMe/SATA drives num="0" while [[ $num -lt "${#nvmes[@]}" ]]; do - updatedb "${nvmes[$num]}" "$db1" - if [[ -f "$db2" ]]; then - updatedb "${nvmes[$num]}" "$db2" - fi + for i in "${!db1list[@]}"; do + updatedb "${nvmes[$num]}" "${db1list[i]}" + done + for i in "${!db2list[@]}"; do + updatedb "${nvmes[$num]}" "${db2list[i]}" + done #------------------------------------------------ # M.2 adaptor cards - num2="0" - while [[ $num2 -lt "${#m2carddbs[@]}" ]]; do - if [[ -f "${dbpath}${m2carddbs[$num2]}" ]]; then - backupdb "${dbpath}${m2carddbs[$num2]}" &&\ - updatedb "${nvmes[$num]}" "${dbpath}${m2carddbs[$num2]}" - else - echo -e "${Error}ERROR 10${Off} ${m2carddbs[$num2]} not found!" - fi - num2=$((num2 +1)) + for i in "${!m2carddb1list[@]}"; do + backupdb "${m2carddb1list[i]}" &&\ + updatedb "${nvmes[$num]}" "${m2carddb1list[i]}" + done + for i in "${!m2carddb2list[@]}"; do + backupdb "${m2carddb2list[i]}" &&\ + updatedb "${nvmes[$num]}" "${m2carddb2list[i]}" done #------------------------------------------------ @@ -875,22 +953,24 @@ setting="$(get_key_value $synoinfo $sdc)" if [[ $force == "yes" ]]; then if [[ $setting == "yes" ]]; then # Disable support_disk_compatibility - #sed -i "s/${sdc}=\"yes\"/${sdc}=\"no\"/" "$synoinfo" synosetkeyvalue "$synoinfo" "$sdc" "no" setting="$(get_key_value "$synoinfo" $sdc)" if [[ $setting == "no" ]]; then echo -e "\nDisabled support disk compatibility." fi + elif [[ $setting == "no" ]]; then + echo -e "\nSupport disk compatibility already disabled." fi else if [[ $setting == "no" ]]; then # Enable support_disk_compatibility - #sed -i "s/${sdc}=\"no\"/${sdc}=\"yes\"/" "$synoinfo" synosetkeyvalue "$synoinfo" "$sdc" "yes" setting="$(get_key_value "$synoinfo" $sdc)" if [[ $setting == "yes" ]]; then echo -e "\nRe-enabled support disk compatibility." fi + elif [[ $setting == "yes" ]]; then + echo -e "\nSupport disk compatibility already enabled." fi fi @@ -901,26 +981,28 @@ setting="$(get_key_value $synoinfo $smc)" if [[ $ram == "yes" ]]; then if [[ $setting == "yes" ]]; then # Disable support_memory_compatibility - #sed -i "s/${smc}=\"yes\"/${smc}=\"no\"/" "$synoinfo" synosetkeyvalue "$synoinfo" "$smc" "no" setting="$(get_key_value "$synoinfo" $smc)" if [[ $setting == "no" ]]; then echo -e "\nDisabled support memory compatibility." fi + elif [[ $setting == "no" ]]; then + echo -e "\nSupport memory compatibility already disabled." fi else if [[ $setting == "no" ]]; then # Enable support_memory_compatibility - #sed -i "s/${smc}=\"no\"/${smc}=\"yes\"/" "$synoinfo" synosetkeyvalue "$synoinfo" "$smc" "yes" setting="$(get_key_value "$synoinfo" $smc)" if [[ $setting == "yes" ]]; then echo -e "\nRe-enabled support memory compatibility." fi + elif [[ $setting == "yes" ]]; then + echo -e "\nSupport memory compatibility already enabled." fi fi -# Optioanlly set mem_max_mb to the amount of installed memory +# Optionally set mem_max_mb to the amount of installed memory if [[ $ram == "yes" ]]; then # Get total amount of installed memory IFS=$'\n' read -r -d '' -a array < <(dmidecode -t memory | grep -i 'size') @@ -948,7 +1030,9 @@ if [[ $ram == "yes" ]]; then echo -e "\n${Error}ERROR${Off} Failed to change max memory!" fi elif [[ $setting == "$ramtotal" ]]; then - echo -e "\nMax memory already set to $ramtotal MB." + #echo -e "\nMax memory already set to $ramtotal MB." + ramgb=$((ramtotal / 1024)) + echo -e "\nMax memory already set to $ramgb GB." fi fi @@ -966,7 +1050,6 @@ if [[ $m2 != "no" ]]; then 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 @@ -997,7 +1080,6 @@ if [[ $nodbupdate == "yes" ]]; then disabled="yes" elif [[ $url != "127.0.0.1" ]]; then # Edit drive_db_test_url= - #sed -i "s/drive_db_test_url=.*/drive_db_test_url=\"127.0.0.1\"/" "$synoinfo" >/dev/null synosetkeyvalue "$synoinfo" "$dtu" "127.0.0.1" disabled="yes" fi @@ -1018,7 +1100,6 @@ else #if [[ $url == "127.0.0.1" ]]; then if [[ $url ]]; then # Delete "drive_db_test_url=127.0.0.1" line (inc. line break) - #sed -i "/drive_db_test_url=\"127.0.0.1\"/d" "/etc.defaults/synoinfo.conf" sed -i "/drive_db_test_url=*/d" "/etc.defaults/synoinfo.conf" # Check if we re-enabled drive db auto updates @@ -1039,37 +1120,30 @@ fi # Show the changes if [[ ${showedits,,} == "yes" ]]; then - getdbtype "$db1" - if [[ $dbtype -gt "6" ]]; then - # Show 11 lines after hdmodel line - lines=11 - elif [[ $dbtype -eq "6" ]]; then - # Show 2 lines after hdmodel line - lines=2 - fi + if [[ ${#db1list[@]} -gt "0" ]]; then + getdbtype "${db1list[0]}" + if [[ $dbtype -gt "6" ]]; then + # Show 11 lines after hdmodel line + lines=11 + elif [[ $dbtype -eq "6" ]]; then + # Show 2 lines after hdmodel line + lines=2 + fi - # HDDs/SSDs - if [[ ${#hdds[@]} -gt "0" ]]; then - num="0" - while [[ $num -lt "${#hdds[@]}" ]]; do - hdmodel=$(printf "%s" "${hdds[$num]}" | cut -d"," -f 1) + # HDDs/SSDs + for i in "${!hdds[@]}"; do + hdmodel=$(printf "%s" "${hdds[i]}" | cut -d"," -f 1) echo - jq . "$db1" | grep -A "$lines" "$hdmodel" - num=$((num +1)) + jq . "${db1list[0]}" | grep -A "$lines" "$hdmodel" done - fi - # NVMe drives - if [[ ${#nvmes[@]} -gt "0" ]]; then - num="0" - while [[ $num -lt "${#nvmes[@]}" ]]; do - nvmemodel=$(printf "%s" "${nvmes[$num]}" | cut -d"," -f 1) + # NVMe drives + for i in "${!nvmes[@]}"; do + hdmodel=$(printf "%s" "${nvmes[i]}" | cut -d"," -f 1) echo - jq . "$db1" | grep -A "$lines" "$nvmemodel" - num=$((num +1)) + jq . "${db1list[0]}" | grep -A "$lines" "$hdmodel" done fi - fi @@ -1081,6 +1155,7 @@ if [[ -f /usr/syno/sbin/synostgdisk ]]; then # DSM 6.2.3 does not have synostgd echo -e "\nDSM successfully checked disk compatibility." else # Ignore DSM 6.2.4 as it returns 255 for "synostgdisk --check-all-disks-compatibility" + # and DSM 6.2.3 and lower have no synostgdisk command if [[ $dsm -gt "6" ]]; then echo -e "\nDSM ${Red}failed${Off} to check disk compatibility with exit code $status" echo -e "\nYou may need to ${Cyan}reboot the Synology${Off} to see the changes."