Difference between revisions of "Bad Block Howto"

From BITPlan Wiki
Jump to navigation Jump to search
Line 248: Line 248:
  
 
=fixbad script =
 
=fixbad script =
This script has not been used for fixing disks yet. It 's main goal is the automation of the fixing process.
+
This script has only been used for a few disks yet. It 's main goal is the automation of the fixing process.
Do not use this script without checking the steps beforehand - you might fatality damage your disk!
+
Do not use this script without checking the steps manually beforehand - you might fatality damage your disk!
  
 
<source lang='bash'>
 
<source lang='bash'>
Line 263: Line 263:
 
# should commands only be shown?
 
# should commands only be shown?
 
dry=false
 
dry=false
 +
# should write fixes be performed?
 +
fix=false
 
# range of sectors to modify after bad sector
 
# range of sectors to modify after bad sector
 
range=8
 
range=8
Line 308: Line 310:
 
usage() {
 
usage() {
 
   echo "usage: $0 [disk]"
 
   echo "usage: $0 [disk]"
 +
  echo "  [-c|--check]"
 
   echo "  [-d|--dry]"
 
   echo "  [-d|--dry]"
 
   echo "  [-h|--help]"
 
   echo "  [-h|--help]"
Line 314: Line 317:
 
   echo "  [[-r|--range] range]"
 
   echo "  [[-r|--range] range]"
 
   echo "  [[-s|--serial [serial]]"
 
   echo "  [[-s|--serial [serial]]"
 +
  echo "  [-t|--test]"
 +
  echo "  [[-w|--wait [type]]"
 
   echo "  [-v|--verbose]"
 
   echo "  [-v|--verbose]"
 
   echo
 
   echo
 
   echo "  -h|--help: show this usage"
 
   echo "  -h|--help: show this usage"
 +
  echo "  -c|--check: check the disk"
 
   echo "  -d|--dry:  dry run - show commands only"
 
   echo "  -d|--dry:  dry run - show commands only"
 
   echo "  -i|--info: show info about the given disk"
 
   echo "  -i|--info: show info about the given disk"
Line 322: Line 328:
 
   echo "  -r|--range: range of sectors to modify after bad sector"
 
   echo "  -r|--range: range of sectors to modify after bad sector"
 
   echo "  -s|--serial: get serial number of confirm serial number"
 
   echo "  -s|--serial: get serial number of confirm serial number"
 +
  echo "  -t|--test: run test for the given type e.g. selective selftest"
 +
  echo "  -w|--wait: wait for the result of the given testype e.g. selective selftest"
 
   echo "  -v|--verbose: set verbose mode"
 
   echo "  -v|--verbose: set verbose mode"
 
   echo ""
 
   echo ""
Line 336: Line 344:
 
# get a number range from 0 to the given n-1
 
# get a number range from 0 to the given n-1
 
#
 
#
# param $1: n  
+
# params
 +
1: n  
 
function getRange() {
 
function getRange() {
 
   local l_n="$1"
 
   local l_n="$1"
Line 346: Line 355:
 
# read the result of the smartctl test for the given disk
 
# read the result of the smartctl test for the given disk
 
#
 
#
 +
# params
 +
#  1: l_disk: the disk under test e.g. /dev/sdb
 +
#  2: l_type: the type of the test e.g. selective
 
function readResult() {
 
function readResult() {
 
   local l_disk="$1"
 
   local l_disk="$1"
  $sudo smartctl -l selftest $l_disk  | egrep "# [0-9]"
+
  local l_type="$2"
 +
  $sudo smartctl -l $l_type $l_disk  | egrep "^#?[[:space:]]*[0-9]"
 +
}
 +
 
 +
#
 +
# show the Result
 +
#
 +
function showResult() {
 +
  local l_logline="$1"
 +
  local l_logstatus="$2"
 +
  if [ "$verbose" == "true" ]
 +
  then
 +
    echo $l_logstatus:$l_logline 
 +
  else
 +
    echo $l_logline | gawk '
 +
    /#/ {
 +
      print $0; exit
 +
    }
 +
    {
 +
      status=substr($4,1,9)
 +
      progress=$5;
 +
      gsub("\\[","",progress);
 +
      range=$7
 +
      printf("\r%s",progress);
 +
    }'
 +
  fi
 +
}
 +
 
 +
#
 +
# wait for the result of a running selftest
 +
#
 +
# param 1: l_disk: the disk under test e.g. /dev/sdb
 +
# param 2: l_type: the type of the test e.g. selective
 +
# param 3: l_wait: number of seconds to wait
 +
#
 +
function waitForResult() {
 +
  # example
 +
  #=== START OF READ SMART DATA SECTION ===
 +
  #SMART Selective self-test log data structure revision number 1
 +
  #SPAN  MIN_LBA    MAX_LBA  CURRENT_TEST_STATUS
 +
  #        1  7814037167  Self_test_in_progress [90% left] (2564632-2630167)
 +
  local l_disk="$1"
 +
  local l_type="$2"
 +
  local l_wait="$3"
 +
  local l_logline=""
 +
  local l_logstatus=""
 +
  color_msg $blue "Waiting for $l_type test of $l_disk to stop (each dot is $l_wait sec)"
 +
  while [ "$l_logstatus" != "Completed" ]; do
 +
    l_logline=$(readResult "$l_disk" "$l_type"  | egrep "^#?[[:space:]]*1")
 +
    l_logstatus=$(echo $l_logline | gawk ' /Completed/ { print "Completed"; }')
 +
    showResult "$l_logline" "$l_logstatus"
 +
    sleep $l_wait
 +
  done
 
}
 
}
  
Line 396: Line 460:
 
   $sudo hdparm -I $l_disk | egrep "(Serial Number|Model)"
 
   $sudo hdparm -I $l_disk | egrep "(Serial Number|Model)"
 
   fs=$(getPartition $l_disk)
 
   fs=$(getPartition $l_disk)
   color_msg $blue "Partition:        $fs"
+
   if [ "$fs" != "" ]
  blocksize=$(getBlockSize $fs)
+
  then
  color_msg $blue "Blocksize:        $blocksize"
+
    color_msg $blue "Partition:        $fs"
 +
    blocksize=$(getBlockSize $fs)
 +
    color_msg $blue "Blocksize:        $blocksize"
 +
  else
 +
    color_msg $red "couldn't find mounted partition for $l_disk"
 +
  fi
 
}
 
}
  
Line 446: Line 515:
 
  echo hdparm --repair-sector $b1  --yes-i-know-what-i-am-doing  $l_disk
 
  echo hdparm --repair-sector $b1  --yes-i-know-what-i-am-doing  $l_disk
 
     else
 
     else
      color_msg $red "not implemented yet"
+
$sudo hdparm --repair-sector $b1  --yes-i-know-what-i-am-doing  $disk >> /tmp/smart_repaired.log
#hdparm --repair-sector $b1  --yes-i-know-what-i-am-doing  $disk >> /tmp/smart_repaired.log
 
 
     fi
 
     fi
 
done
 
done
Line 459: Line 527:
 
#
 
#
 
checkSoftware() {
 
checkSoftware() {
   for sw in awk debugfs hdparm smartctl python $sudo
+
   for sw in gawk debugfs fdisk hdparm smartctl tune2fs python $sudo
 
   do
 
   do
 
     bin=$(which $sw)
 
     bin=$(which $sw)
Line 472: Line 540:
 
     fi
 
     fi
 
   done
 
   done
 +
}
 +
 +
#
 +
# run a test for the given disk in the given mode
 +
#
 +
# params
 +
#  1: l_disk: the disk under test e.g. /dev/sdb
 +
#  2: l_mode: the mode of the self test e.g. short/long
 +
function runTest() {
 +
  local l_disk="$1"
 +
  local l_mode="$2"
 +
  color_msg $blue  "running $l_mode smartctl test for $l_disk ..."
 +
$sudo smartctl -t $l_mode $l_disk > /tmp/null
 
}
 
}
  
Line 489: Line 570:
 
   if [ $psector -gt 0 ]
 
   if [ $psector -gt 0 ]
 
   then
 
   then
     echo  "running $l_mode smartctl test for $l_disk ..."
+
     runTest $l_disk $l_mode
  $sudo smartctl -t $l_mode $l_disk > /tmp/null
+
  fi
    readResult "$l_disk" | while read line
+
}
    do
+
 
      echo $line | grep "read failure" > /dev/null
+
#
      if [ $? -eq 0 ]
+
# check the lba block
      then
+
#
          index=$(echo $line | cut -f2 -d' ')
+
function lbaCheck() {
          state=$(echo $line | cut -f3-4 -d ' ')
+
  local l_disk="$1"
          progress=$(echo $line | cut -f8 -d ' ')
+
  fs=$(getPartition $l_disk)
          lba=$(echo $line | cut -f10 -d ' ')
+
  blocksize=$(getBlockSize $fs)
          echo $index $state  
+
  startsector=$(getStartSector $l_disk $fs)
          echo "progress:  $progress"
+
  diskserial=$(getSerialNumber $l_disk)
          echo "lba: $lba"
+
  readResult "$l_disk" selftest | while read line
          # calculate the file system block
+
  do
          fsb=$(awk -v L=$lba -v S=$startsector -v B=$blocksize 'BEGIN {printf ("%.0f",((L-S)*512/B))}')
+
    echo $line | grep "read failure" > /dev/null
          echo "file system block: $fsb"
+
    if [ $? -eq 0 ]
           fixBad $l_disk $lba $range
+
    then
      fi
+
      if [ "$verbose" == "true" ]
    done
+
      then
   fi
+
        echo $line
 +
      fi
 +
      index=$(echo $line | cut -f2 -d' ')
 +
      state=$(echo $line | cut -f3-4 -d ' ')
 +
      progress=$(echo $line | cut -f8 -d ' ')
 +
      lba=$(echo $line | cut -f10 -d ' ')
 +
      if [ "$lba" == "" ]
 +
      then
 +
        lba=0
 +
      fi
 +
      if  [ "$lba" -gt 0 ]
 +
      then
 +
        echo $index $state  
 +
        echo "progress:  $progress"
 +
        echo "lba: $lba"
 +
        # calculate the file system block
 +
        fsb=$(gawk -v L=$lba -v S=$startsector -v B=$blocksize 'BEGIN {printf ("%.0f",((L-S)*512/B))}')
 +
        echo "file system block: $fsb"
 +
        if [ "$fix" == "true" ]
 +
        then
 +
          if [ "$serial" != "$diskserial" ]
 +
           then
 +
            color_msg $red "you need to provide the serial number of $l_disk to perform fix operations"
 +
          else
 +
            fixBad $l_disk $lba $range
 +
          fi
 +
        fi
 +
      fi
 +
    fi
 +
  done
 +
}
 +
 
 +
#
 +
# try Fixing bad sectors
 +
#
 +
function tryFix() {
 +
  local l_disk="$1"
 +
      badsect=$($sudo smartctl -l selective ${baddrive} | gawk '/# 1  Selective offline   Completed: read failure/ {print $10}')
 +
      [ $badsect = "-" ] && exit 0
 +
 
 +
    echo Attempting to fix sector $badsect on $baddrive
 +
    echo hdparm --repair-sector ${badsect} --yes-i-know-what-i-am-doing $baddrive
 
}
 
}
  
Line 521: Line 643:
 
       color_msg $blue "Testing $baddrive from LBA $badsect"
 
       color_msg $blue "Testing $baddrive from LBA $badsect"
 
       $sudo smartctl -t select,${badsect}-max ${baddrive} 2>&1 >> /dev/null
 
       $sudo smartctl -t select,${badsect}-max ${baddrive} 2>&1 >> /dev/null
 
+
       waitForResult $baddrive selective 5
      echo "Waiting for test to stop (each dot is 5 sec)"
+
       tryFix $baddrive
       while [ "$($sudo smartctl -l selective ${baddrive} | awk '/^ *1/{print substr($4,1,9)}')" != "Completed" ]; do
+
       color_msg $blue "running next test"
        echo -n .
 
        sleep 5
 
       done
 
      echo
 
 
 
      badsect=$($sudo smartctl -l selective ${baddrive} | awk '/# 1  Selective offline  Completed: read failure/ {print $10}')
 
       [ $badsect = "-" ] && exit 0
 
 
 
    echo Attempting to fix sector $badsect on $baddrive
 
    echo hdparm --repair-sector ${badsect} --yes-i-know-what-i-am-doing $baddrive
 
    echo running next test
 
 
   done
 
   done
 
}
 
}
 
    
 
    
 
 
# make sure the needed software is available
 
# make sure the needed software is available
 
checkSoftware
 
checkSoftware
Line 570: Line 680:
 
     -l|--loop)
 
     -l|--loop)
 
       checkLoop $disk
 
       checkLoop $disk
 +
      ;;
 +
    -f|--fix)
 +
      fix=true
 
       ;;
 
       ;;
 
     -r|--range)
 
     -r|--range)
Line 589: Line 702:
 
         shift
 
         shift
 
       fi
 
       fi
 +
      ;;
 +
    -t|--test)
 +
      runTest $disk $mode
 
       ;;
 
       ;;
 
     -v|--verbose)
 
     -v|--verbose)
 
       verbose=true
 
       verbose=true
 
       ;;
 
       ;;
 +
    -w|--wait)
 +
      if [ $# -lt 1 ]
 +
      then
 +
        usage
 +
      else
 +
        type=$1
 +
        shift
 +
        waitForResult $disk $type 5
 +
      fi
 +
      ;;
 +
    -x)
 +
      lbaCheck $disk $serial;;
 
     *)
 
     *)
 
       disk=$option
 
       disk=$option
Line 598: Line 726:
 
   esac
 
   esac
 
done
 
done
 
 
</source>
 
</source>

Revision as of 05:45, 9 October 2020

Your hard disk fails on Linux - now what?

I happened to me a few times in the last few weeks. A filesystem check for a hard disk would take for ever - my system won't boot. One of the harddisks was reporting bad block errors. At this time i have four different hard disks that are giving me this kind of trouble. All broken harddisks have been taken out of the system and I am analyzing the problem using a virtual machine and a USB-SATA bridge.

First step: remove harddisk from system and put into SATA USB docking station

Hopefully your disk is not needed to boot or fully operate your system. In that case you might want to boot of a USB stick or other media. In any case my procedure is to remove the disk from the original system and use a different system for analysis. In my case i am using an Ubuntu based virtual machine and connect the drive via a USB-SATA bridge. With USB 2 devices the performance is poor. I am not using my old Logilink QP002 Sata Docking Station anymore for this reason.

The USB 3 device i bought in 2015: 611EMR%2BPqWL._AC_SL1001_.jpg still serves me well.

Recently i added a 2 port docking station: 61ZUg7IV7ZL._AC_SL1300_.jpg

Second step - check problems

Tools needed

  1. A Linux virtual machine
  2. smartctl
  3. hdparam
  4. debugfs
  5. mount

Examples

disk Digda

Where is it mounted?

sudo mount | grep Digda
/dev/sdb1 on /media/wf/Digda type ext2 (rw,nosuid,nodev,relatime,uhelper=udisks2)

get basic info and check

get basic info

  1. using fixbad -i
  2. using smartctl -i
  3. using hdparm -I

see the details below check:

fixbad /dev/sdb -c
checking Current_Pending_Sector count for /dev/sdb partition /dev/sdb1 blocksize 4096 startsector 
197 Current_Pending_Sector  0x0032   252   252   000    Old_age   Always       -       0
Current_Pending_Sector is zero!

So this drive has no bad block issues.


fixbad

The fixbad script tries to summarize the most important information for the bad block fixing needs.

fixbad /dev/sdb -i
Model Family:     SAMSUNG SpinPoint F4 EG (AF)
Device Model:     SAMSUNG HD204UI
Serial Number:    S2H7J9EZC04171
User Capacity:    2,000,398,934,016 bytes [2.00 TB]
Sector Size:      512 bytes logical/physical
Rotation Rate:    5400 rpm
	Model Number:       SAMSUNG HD204UI                         
	Serial Number:      S2H7J9EZC04171      
Partition:        /dev/sdb1
Blocksize:        4096
smartctl
sudo smartctl -i /dev/sdb
smartctl 6.6 2016-05-31 r4324 [x86_64-linux-4.15.0-72-generic] (local build)
Copyright (C) 2002-16, Bruce Allen, Christian Franke, www.smartmontools.org

=== START OF INFORMATION SECTION ===
Model Family:     SAMSUNG SpinPoint F4 EG (AF)
Device Model:     SAMSUNG HD204UI
Serial Number:    S2H7J9EZC04171
LU WWN Device Id: 5 0024e9 203f40fb7
Firmware Version: 1AQ10001
User Capacity:    2,000,398,934,016 bytes [2.00 TB]
Sector Size:      512 bytes logical/physical
Rotation Rate:    5400 rpm
Form Factor:      3.5 inches
Device is:        In smartctl database [for details use: -P show]
ATA Version is:   ATA8-ACS T13/1699-D revision 6
SATA Version is:  SATA 2.6, 3.0 Gb/s
Local Time is:    Mon Oct  5 11:41:44 2020 CEST

==> WARNING: Using smartmontools or hdparm with this
drive may result in data loss due to a firmware bug.
****** THIS DRIVE MAY OR MAY NOT BE AFFECTED! ******
Buggy and fixed firmware report same version number!
See the following web pages for details:
http://knowledge.seagate.com/articles/en_US/FAQ/223571en
http://www.smartmontools.org/wiki/SamsungF4EGBadBlocks

SMART support is: Available - device has SMART capability.
SMART support is: Enabled
hdparm
sudo hdparm -I /dev/sdb

/dev/sdb:

ATA device, with non-removable media
	Model Number:       SAMSUNG HD204UI                         
	Serial Number:      S2H7J9EZC04171      
	Firmware Revision:  1AQ10001
	Transport:          Serial, ATA8-AST, SATA 1.0a, SATA II Extensions, SATA Rev 2.5, SATA Rev 2.6
Standards:
	Used: unknown (minor revision code 0x0028) 
	Supported: 8 7 6 5 
	Likely used: 8
Configuration:
	Logical		max	current
	cylinders	16383	16383
	heads		16	16
	sectors/track	63	63
	--
	CHS current addressable sectors:    16514064
	LBA    user addressable sectors:   268435455
	LBA48  user addressable sectors:  3907029168
	Logical  Sector size:                   512 bytes
	Physical Sector size:                   512 bytes
	device size with M = 1024*1024:     1907729 MBytes
	device size with M = 1000*1000:     2000398 MBytes (2000 GB)
	cache/buffer size  = unknown
	Form Factor: 3.5 inch
	Nominal Media Rotation Rate: 5400
Capabilities:
	LBA, IORDY(can be disabled)
	Queue depth: 32
	Standby timer values: spec'd by Standard, no device specific minimum
	R/W multiple sector transfer: Max = 16	Current = ?
	Advanced power management level: disabled
	Recommended acoustic management value: 254, current value: 0
	DMA: mdma0 mdma1 *mdma2 udma0 udma1 udma2 udma3 udma4 udma5 udma6 
	     Cycle time: min=120ns recommended=120ns
	PIO: pio0 pio1 pio2 pio3 pio4 
	     Cycle time: no flow control=120ns  IORDY flow control=120ns
Commands/features:
	Enabled	Supported:
	   *	SMART feature set
	    	Security Mode feature set
	   *	Power Management feature set
	   *	Write cache
	   *	Look-ahead
	   *	Host Protected Area feature set
	   *	WRITE_BUFFER command
	   *	READ_BUFFER command
	   *	NOP cmd
	   *	DOWNLOAD_MICROCODE
	    	Advanced Power Management feature set
	    	Power-Up In Standby feature set
	   *	SET_FEATURES required to spinup after power up
	    	SET_MAX security extension
	    	Automatic Acoustic Management feature set
	   *	48-bit Address feature set
	   *	Device Configuration Overlay feature set
	   *	Mandatory FLUSH_CACHE
	   *	FLUSH_CACHE_EXT
	   *	SMART error logging
	   *	SMART self-test
	   *	General Purpose Logging feature set
	   *	64-bit World wide name
	   *	WRITE_UNCORRECTABLE_EXT command
	   *	{READ,WRITE}_DMA_EXT_GPL commands
	   *	Segmented DOWNLOAD_MICROCODE
	   *	Gen1 signaling speed (1.5Gb/s)
	   *	Gen2 signaling speed (3.0Gb/s)
	   *	Native Command Queueing (NCQ)
	   *	Host-initiated interface power management
	   *	Phy event counters
	   *	NCQ priority information
	    	DMA Setup Auto-Activate optimization
	    	Device-initiated interface power management
	   *	Software settings preservation
	   *	SMART Command Transport (SCT) feature set
	   *	SCT Read/Write Long (AC1), obsolete
	   *	SCT Write Same (AC2)
	   *	SCT Error Recovery Control (AC3)
	   *	SCT Features Control (AC4)
	   *	SCT Data Tables (AC5)
Security: 
	Master password revision code = 65534
		supported
	not	enabled
	not	locked
	not	frozen
	not	expired: security count
		supported: enhanced erase
	344min for SECURITY ERASE UNIT. 344min for ENHANCED SECURITY ERASE UNIT.
Logical Unit WWN Device Identifier: 50024e9203f40fb7
	NAA		: 5
	IEEE OUI	: 0024e9
	Unique ID	: 203f40fb7
Checksum: correct

disk wendi

Where is it mounted?

sudo mount | grep wendi
/dev/sdb1 on /media/wf/wendi type ext3 (rw,nosuid,nodev,relatime,data=ordered,uhelper=udisks2)

get basic info and check

fixbad /dev/sdb -i -c
Model Family:     SAMSUNG SpinPoint F4 EG (AF)
Device Model:     SAMSUNG HD204UI
Serial Number:    S2H7J9EZB05266
User Capacity:    2,000,398,934,016 bytes [2.00 TB]
Sector Size:      512 bytes logical/physical
Rotation Rate:    5400 rpm
	Model Number:       SAMSUNG HD204UI                         
	Serial Number:      S2H7J9EZB05266      
Partition:        /dev/sdb1
Blocksize:        4096
checking Current_Pending_Sector count for /dev/sdb partition /dev/sdb1 blocksize 4096 startsector 
197 Current_Pending_Sector  0x0032   252   252   000    Old_age   Always       -       0
Current_Pending_Sector is zero!

disk riako

 highlight='1'>
fixbad /dev/sdb -i -c
Model Family:     Western Digital Green
Device Model:     WDC WD40EZRX-00SPEB0
Serial Number:    WD-WCC4E3LNJA7X
User Capacity:    4,000,787,030,016 bytes [4.00 TB]
Sector Sizes:     512 bytes logical, 4096 bytes physical
Rotation Rate:    5400 rpm
	Model Number:       WDC WD40EZRX-00SPEB0                    
	Serial Number:      WD-WCC4E3LNJA7X
Partition:        /dev/sdb1
Blocksize:        4096
checking Current_Pending_Sector count for /dev/sdb partition /dev/sdb1 blocksize 4096 startsector 2048
197 Current_Pending_Sector  0x0032   200   200   000    Old_age   Always       -       30
Current_Pending_Sector is not zero but 30
running short smartctl test for /dev/sdb ...

Links

fixbad script

This script has only been used for a few disks yet. It 's main goal is the automation of the fixing process. Do not use this script without checking the steps manually beforehand - you might fatality damage your disk!

#!/bin/bash 
# see http://wiki.bitplan.com/index.php/Bad_Block_Howto
# see https://github.com/hradec/fix_smart_last_bad_sector/blob/master/fix_smart_last_bad_sector.sh
# see https://www.thomas-krenn.com/de/wiki/Analyse_einer_fehlerhaften_Festplatte_mit_smartctl
# WF 2020-10-04 
disk=/dev/sdb
mode=short
# verbose
verbose=false
# should commands only be shown?
dry=false
# should write fixes be performed?
fix=false
# range of sectors to modify after bad sector
range=8
# set to sudo if sudo is needed
sudo=sudo
# serial number
serial="-?-"

#ansi colors
#http://www.csc.uvic.ca/~sae/seng265/fall04/tips/s265s047-tips/bash-using-colors.html
blue='\033[0;34m'  
red='\033[0;31m'  
green='\033[0;32m' # '\e[1;32m' is too bright for white bg.
endColor='\033[0m'

#
# a colored message 
#   params:
#     1: l_color - the color of the message
#     2: l_msg - the message to display
#
color_msg() {
  local l_color="$1"
  local l_msg="$2"
  echo -e "${l_color}$l_msg${endColor}"
}

#
# error
#
#   show an error message and exit
#
#   params:
#     1: l_msg - the message to display
error() {
  local l_msg="$1"
  # use ansi red for error
  color_msg $red "Error: $l_msg" 1>&2
  exit 1
}

#
# show the usage
#
usage() {
  echo "usage: $0 [disk]"
  echo "   [-c|--check]"
  echo "   [-d|--dry]"
  echo "   [-h|--help]"
  echo "   [-i|--info]"
  echo "   [[-m|--mode] mode]"
  echo "   [[-r|--range] range]"
  echo "   [[-s|--serial [serial]]"
  echo "   [-t|--test]" 
  echo "   [[-w|--wait [type]]"
  echo "   [-v|--verbose]"
  echo
  echo "  -h|--help: show this usage"
  echo "  -c|--check: check the disk"
  echo "  -d|--dry:  dry run - show commands only"
  echo "  -i|--info: show info about the given disk"
  echo "  -m|--mode: set mode: default=short"
  echo "  -r|--range: range of sectors to modify after bad sector"
  echo "  -s|--serial: get serial number of confirm serial number"
  echo "  -t|--test: run test for the given type e.g. selective selftest"
  echo "  -w|--wait: wait for the result of the given testype e.g. selective selftest"
  echo "  -v|--verbose: set verbose mode"
  echo ""
  echo "example:"
  echo "   $0 /dev/sdb -i"
  echo ""
  echo "for any write operation you need to confirm the serial number"
  echo "to get serial number: "
  echo "   $0 disk -s "
  exit 1
}

#
# get a number range from 0 to the given n-1
#
# params 
#   1: n 
function getRange() {
  local l_n="$1"
  range=$(python -c "for i in range($l_n): print i,")
  echo $range
}

#
# read the result of the smartctl test for the given disk
#
# params
#   1: l_disk: the disk under test e.g. /dev/sdb
#   2: l_type: the type of the test e.g. selective
function readResult() {
   local l_disk="$1"
   local l_type="$2"
 	 $sudo smartctl -l $l_type $l_disk  | egrep "^#?[[:space:]]*[0-9]"
}

#
# show the Result
#
function showResult() {
  local l_logline="$1"
  local l_logstatus="$2"
  if [ "$verbose" == "true" ]
  then
    echo $l_logstatus:$l_logline  
  else
    echo $l_logline | gawk '
    /#/ {
      print $0; exit
    }
    { 
       status=substr($4,1,9)
       progress=$5;
       gsub("\\[","",progress);
       range=$7 
       printf("\r%s",progress);
     }'
  fi
}

#
# wait for the result of a running selftest
#
# param 1: l_disk: the disk under test e.g. /dev/sdb
# param 2: l_type: the type of the test e.g. selective
# param 3: l_wait: number of seconds to wait 
#
function waitForResult() {
   # example
   #=== START OF READ SMART DATA SECTION ===
   #SMART Selective self-test log data structure revision number 1
   #SPAN  MIN_LBA     MAX_LBA  CURRENT_TEST_STATUS
   #         1  7814037167  Self_test_in_progress [90% left] (2564632-2630167)
   local l_disk="$1"
   local l_type="$2"
   local l_wait="$3"
   local l_logline=""
   local l_logstatus=""
   color_msg $blue "Waiting for $l_type test of $l_disk to stop (each dot is $l_wait sec)"
   while [ "$l_logstatus" != "Completed" ]; do
     l_logline=$(readResult "$l_disk" "$l_type"  | egrep "^#?[[:space:]]*1")
     l_logstatus=$(echo $l_logline | gawk ' /Completed/ { print "Completed"; }')
     showResult "$l_logline" "$l_logstatus"
     sleep $l_wait 
   done
}

#
# get the serial number of the device
#
function getSerialNumber() {
  local l_disk="$1"
  serial=$($sudo smartctl -i  $l_disk  | grep "Serial Number" | cut -f 2 -d':')
  echo $serial
}

#
# get the blocksize of the given file system
#
function getBlockSize() {
  local l_fs="$1"
  blocksize=$($sudo tune2fs -l $l_fs | grep "Block size:" | cut -f2 -d':')
  echo $blocksize
}

#
# get the partition for the given disk
#
function getPartition() {
  local l_disk="$1"
  fs=$(mount | grep $l_disk | cut -f1 -d' ')
  echo $fs
}

#
# get the start sector for the given disk
#
function getStartSector() {
  local l_disk="$1"
  local l_fs="$2"
  startsector=$($sudo fdisk -l $l_disk | grep $l_fs | cut -f4 -d' ')
  echo $startsector
}

#
# get Info about the given disk
#
function getInfo() {
  local l_disk="$1"
  $sudo smartctl -i $l_disk | egrep "(Model|Serial|Rotation|Sector|Capacity)"
  $sudo hdparm -I $l_disk | egrep "(Serial Number|Model)"
  fs=$(getPartition $l_disk)
  if [ "$fs" != "" ]
  then
    color_msg $blue "Partition:        $fs"
    blocksize=$(getBlockSize $fs)
    color_msg $blue "Blocksize:        $blocksize"
  else
    color_msg $red "couldn't find mounted partition for $l_disk"
  fi
}

#
# geh the current pending sector for the given disk
#
function getCurrentPendingSector() {
   local l_disk="$1"
   # if msg is empty don't show message but only return the current pending sector count
   local l_msg="$2"
   psectorline=$($sudo smartctl -A $l_disk | grep Current_Pending_Sector)
   psector=0
   if [ $? -eq 0 ]
   then
     if [ "$l_msg" != "" ]; then color_msg $green "$psectorline"; fi
     psector=$(echo $psectorline | cut -f 10 -d ' ')
     if  [ $psector -gt 0 ]
     then
        if [ "$l_msg" != "" ]; then color_msg $red "Current_Pending_Sector is not zero but $psector"; fi
     else
        if [ "$l_msg" != "" ]; then color_msg $green "Current_Pending_Sector is zero!"; fi
     fi
   else
     if [ "$l_msg" != "" ]; then color_msg $red "smartctl -A did not output Current_Pending_Sector"; fi
     psector=-1
   fi
   if [ "$l_msg" == "" ]; then echo $psector; fi
}

#
# fix the given bad sector on the given disk with the given range of sectors to fix
#
# param 1: disk e.g. /dev/sdb1
# param 2: defect sector to repair
# param 3: range - range of sectors to repair e.g. 8
# 
fixBad() {
  local l_disk="$1"
  local l_sector="$2"
  local l_range="$3"
  color_msg $blue "repairing sector $l_sector to $l_sector+$l_range on $l_disk ..."
  r=$(getRange $l_range)
	for i in $r ; do
		let b1=$l_sector+$i
    if [ "$dry" == "true" ]
    then
		  echo hdparm --repair-sector $b1  --yes-i-know-what-i-am-doing  $l_disk
    else
			$sudo hdparm --repair-sector $b1  --yes-i-know-what-i-am-doing  $disk >> /tmp/smart_repaired.log
    fi
	done
	#tail -n 60 /tmp/smart_repaired.log | grep writing | tail -n 20
	#grep '#' /tmp/smart | head -5
	#hdparm -I $disk > /tmp/hdparm
}

#
# check the needed software
#
checkSoftware() {
  for sw in gawk debugfs fdisk hdparm smartctl tune2fs python $sudo
  do
    bin=$(which $sw)
    if [ $? -eq 0 ]
    then
      if [ "$verbose" == "true" ]
      then
        color_msg $green "will use $bin as $sw"
      fi
    else
      error "$0 needs $sw to work please install it"
    fi
  done
}

#
# run a test for the given disk in the given mode
# 
# params
#   1: l_disk: the disk under test e.g. /dev/sdb
#   2: l_mode: the mode of the self test e.g. short/long 
function runTest() {
   local l_disk="$1"
   local l_mode="$2"
   color_msg $blue  "running $l_mode smartctl test for $l_disk ..."
	 $sudo smartctl -t $l_mode $l_disk > /tmp/null
}

#
# check the given disk in the given mode
#
function checkDisk() {
   local l_disk="$1"
   local l_mode="$2"
   local l_serial="$3"
   fs=$(getPartition $l_disk)
   blocksize=$(getBlockSize $fs)
   startsector=$(getStartSector $l_disk $fs)
   color_msg $blue "checking Current_Pending_Sector count for $l_disk partition $fs blocksize $blocksize startsector $startsector"
   getCurrentPendingSector "$l_disk" show
   psector=$(getCurrentPendingSector "$l_disk")
   if [ $psector -gt 0 ]
   then
     runTest $l_disk $l_mode
   fi
}

#
# check the lba block
#
function lbaCheck() {
  local l_disk="$1"
  fs=$(getPartition $l_disk)
  blocksize=$(getBlockSize $fs)
  startsector=$(getStartSector $l_disk $fs)
  diskserial=$(getSerialNumber $l_disk)
  readResult "$l_disk" selftest | while read line
  do
    echo $line | grep "read failure" > /dev/null
    if [ $? -eq 0 ]
    then
      if [ "$verbose" == "true" ]
      then
        echo $line
      fi
      index=$(echo $line | cut -f2 -d' ')
      state=$(echo $line | cut -f3-4 -d ' ')
      progress=$(echo $line | cut -f8 -d ' ')
      lba=$(echo $line | cut -f10 -d ' ')
      if [ "$lba" == "" ]
      then 
        lba=0
      fi
      if  [ "$lba" -gt 0 ]
      then
        echo $index $state 
        echo "progress:  $progress"
        echo "lba: $lba"
        # calculate the file system block
        fsb=$(gawk -v L=$lba -v S=$startsector -v B=$blocksize 'BEGIN {printf ("%.0f",((L-S)*512/B))}')
        echo "file system block: $fsb"
        if [ "$fix" == "true" ]
        then
          if [ "$serial" != "$diskserial" ]
          then
            color_msg $red "you need to provide the serial number of $l_disk to perform fix operations"
          else
            fixBad $l_disk $lba $range
          fi
        fi
      fi
    fi
  done
}

#
# try Fixing bad sectors
#
function tryFix() {
   local l_disk="$1"
      badsect=$($sudo smartctl -l selective ${baddrive} | gawk '/# 1  Selective offline   Completed: read failure/ {print $10}')
      [ $badsect = "-" ] && exit 0

    echo Attempting to fix sector $badsect on $baddrive
    echo hdparm --repair-sector ${badsect} --yes-i-know-what-i-am-doing $baddrive
}

#
# start a check loop on the given drive
#
function checkLoop() {
   local baddrive="$1"
   badsect=1
   while true; do
      color_msg $blue "Testing $baddrive from LBA $badsect"
      $sudo smartctl -t select,${badsect}-max ${baddrive} 2>&1 >> /dev/null
      waitForResult $baddrive selective 5
      tryFix $baddrive
      color_msg $blue "running next test" 
  done
}
  
# make sure the needed software is available
checkSoftware
# commandline option
while [  "$1" != ""  ]
do
  option=$1
  shift
  case $option in
    -h|--help)
       usage
       ;;
    -i|--info)
      getInfo $disk
      ;;
    -m|--mode)
      if [ $# -lt 1 ]
      then
        usage
      else
        mode=$1
        shift
      fi
      ;;
    -c|--check)
      checkDisk $disk $mode $serial
      ;;
    -d|--dry)
      dry=true
      ;;
    -l|--loop)
      checkLoop $disk
      ;;
    -f|--fix)
      fix=true
      ;;
    -r|--range)
      if [ $# -lt 1 ]
      then
        usage
      else
        range=$1
        shift
      fi
      ;;
    -s|--serial)
      if [ $# -lt 1 ]
      then
        getSerialNumber $disk
        exit 1
      else
        serial=$1
        shift
      fi
      ;;
    -t|--test)
      runTest $disk $mode
      ;;
    -v|--verbose)
      verbose=true
      ;;
    -w|--wait)
      if [ $# -lt 1 ]
      then
        usage
      else
        type=$1
        shift
        waitForResult $disk $type 5
      fi
      ;;
    -x)
      lbaCheck $disk $serial;;
    *)
      disk=$option
      ;;
  esac
done