Determine the right TYPE between scanned and auto

This commit is contained in:
Jeremy Gardais 2020-02-20 14:36:02 +01:00
parent 3b016ae797
commit fb1094308d
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
1 changed files with 41 additions and 12 deletions

View File

@ -3,7 +3,7 @@
# NOTE: Must be run as root, so you probably need to setup sudo for this.
# Vars {{{
debug="1"
debug="0"
## Colors {{{
c_redb='\033[1;31m'
@ -29,9 +29,11 @@ is_disk_support_smart() {
if test -f "${_smarctl_support_result}"; then rm -f "${_smarctl_support_result}"; fi
## Get only "support" lines from disk's informations
## Grep only "support" lines from disk's informations
smartctl -d "${_type}" -i -- "${_disk}" | grep -E "^SMART support is:" -- > "${_smarctl_support_result}"
## TODO: Test if grep successfully got something
## Parse all "support" lines
while IFS= read -r _LINE; do
if ! printf -- '%s' "${_LINE}" | grep -q -E -- "(Enabled|Available)"
@ -49,6 +51,31 @@ is_disk_support_smart() {
rm -f -- "${_smarctl_support_result}"
}
## }}}
## Test the type of disk with smartctl {{{
choose_correct_type() {
_disk="${1}"
_scanned_type="${2}"
_default_type="auto"
TYPE=""
SMART_SUPPORT_MSG=""
for test_type in "${_default_type}" "${_scanned_type}"; do
is_disk_support_smart "${_disk}" "${test_type}"
## If no message, the type is correct
if [ -z "${smart_support_msg}" ]; then
TYPE="${test_type}"
SMART_SUPPORT_MSG=""
return
else
SMART_SUPPORT_MSG="${smart_support_msg}"
fi
done
}
## }}}
if test -f /tmp/dres; then rm -f /tmp/dres; fi
if test -f /tmp/dscan; then rm -f /tmp/dscan; fi
@ -56,23 +83,25 @@ if test -f /tmp/dscan; then rm -f /tmp/dscan; fi
# Get the list of all available devices
smartctl --scan > /tmp/dscan
# TODO: Test if the file is not empty
while IFS= read -r LINE; do
## Get device and type
## Get device path
DISK=$(echo "${LINE}" | cut -d" " -f1)
TYPE=$(echo "${LINE}" | cut -d" " -f3)
## Try to determine the best type
SCANNED_TYPE=$(echo "${LINE}" | cut -d" " -f3)
choose_correct_type "${DISK}" "${SCANNED_TYPE}"
is_disk_support_smart "${DISK}" "${TYPE}"
## If SMART is supported on the disk
if [ -z "${smart_support_msg}" ]; then
## If no correct type was found for this device
if [ -z "${TYPE}" ]; then
[ "${debug}" -eq "0" ] && printf "${c_magentab}%-6b${c_reset}\n" "DEBUG: SMART is not fully supported."
DRES=$(printf '%s' "${SMART_SUPPORT_MSG}")
DCODE="2"
else
[ "${debug}" -eq "0" ] && printf "${c_magentab}%-6b${c_reset}\n" "DEBUG: SMART seems fully supported, proceed normally."
### Get SMART Health Status and return code
DRES=$(/usr/sbin/smartctl -H -d "${TYPE}" -n standby "${DISK}")
DCODE=$?
else
[ "${debug}" -eq "0" ] && printf "${c_magentab}%-6b${c_reset}\n" "DEBUG: SMART is not fully supported, get ."
DRES=$(printf '%s' "${smart_support_msg}")
DCODE="2"
fi
DSTBY=$(( DCODE & 2 ))