--- recepies/smart/smart-test-relocate.pl 2013/09/09 15:28:00 303 +++ recepies/smart/smart-test-relocate.pl 2013/11/24 21:03:59 304 @@ -9,6 +9,11 @@ my $test_started = 0; +sub write_sector { + my $s = shift; + system "hdparm --write-sector $s --yes-i-know-what-i-am-doing $drive"; +} + sub smart_test { my $sector = shift; my $cmd = "smartctl -t select,$sector-max $drive"; @@ -28,7 +33,7 @@ my $sector = $1; print "rewrite sector: $sector\n"; foreach my $s ( $sector .. $sector + $rewrite_sectors ) { - system "hdparm --write-sector $s --yes-i-know-what-i-am-doing $drive"; + write_sector $s; } smart_test $sector; return 1; @@ -36,13 +41,33 @@ $test_started = 1; return 1; } elsif ( m/Not_testing/ ) { - return 1 if $test_started; + if ( $test_started ) { + smart_last_error(); + return 1 if $test_started; + } smart_test 0; # first-time invocation } } return 0; } +sub smart_last_error { + my $cmd = "smartctl -l selftest $drive"; + warn "$cmd\n"; + open(my $fh, '-|', $cmd); + while(<$fh>) { + chomp; + print "# $_\n"; + if (/^#\s+1.+Completed: read failure\s+\S+\s+\S+\s+(\d+)/) { + write_sector $1; + smart_test $1; + return; + } + } +} + +smart_last_error; + while ( smart ) { warn "sleep $delay s", ( $test_started ? " smart test running..." : "idle" ), "\n"; sleep $delay;