1 |
dpavlin |
287 |
#!/usr/bin/perl |
2 |
|
|
|
3 |
|
|
use warnings; |
4 |
|
|
use strict; |
5 |
|
|
|
6 |
|
|
my $drive = $ARGV[0] || die "usage: $0 /dev/sda\n"; |
7 |
|
|
my $delay = 15; |
8 |
|
|
my $rewrite_sectors = 16; |
9 |
|
|
|
10 |
|
|
my $test_started = 0; |
11 |
|
|
|
12 |
|
|
sub smart_test { |
13 |
|
|
my $sector = shift; |
14 |
|
|
my $cmd = "smartctl -t select,$sector-max $drive"; |
15 |
|
|
warn "$cmd\n"; |
16 |
|
|
system $cmd; |
17 |
|
|
$test_started = 1; |
18 |
|
|
} |
19 |
|
|
|
20 |
|
|
sub smart { |
21 |
|
|
open(my $fh, '-|', "smartctl -l selective $drive"); |
22 |
|
|
while(<$fh>) { |
23 |
|
|
chomp; |
24 |
|
|
print "# $_\n"; |
25 |
|
|
if ( m/Completed_read_failure.*\((\d+)-\d+\)/ ) { |
26 |
|
|
my $sector = $1; |
27 |
|
|
print "rewrite sector: $sector\n"; |
28 |
|
|
foreach my $s ( $sector .. $sector + $rewrite_sectors ) { |
29 |
|
|
system "hdparm --write-sector $s --yes-i-know-what-i-am-doing $drive"; |
30 |
|
|
} |
31 |
|
|
smart_test $sector; |
32 |
|
|
return 1; |
33 |
|
|
} elsif ( m/Self_test_in_progress/ ) { |
34 |
|
|
return 1; |
35 |
|
|
} elsif ( m/Not_testing/ ) { |
36 |
|
|
return 1 if $test_started; |
37 |
|
|
smart_test 0; # first-time invocation |
38 |
|
|
} |
39 |
|
|
} |
40 |
|
|
return 0; |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
while ( smart ) { |
44 |
|
|
warn "# sleep $delay s\n"; |
45 |
|
|
sleep $delay; |
46 |
|
|
} |