9 |
|
|
10 |
my $test_started = 0; |
my $test_started = 0; |
11 |
|
|
12 |
|
sub write_sector { |
13 |
|
my $s = shift; |
14 |
|
system "hdparm --write-sector $s --yes-i-know-what-i-am-doing $drive"; |
15 |
|
} |
16 |
|
|
17 |
sub smart_test { |
sub smart_test { |
18 |
my $sector = shift; |
my $sector = shift; |
19 |
my $cmd = "smartctl -t select,$sector-max $drive"; |
my $cmd = "smartctl -t select,$sector-max $drive"; |
33 |
my $sector = $1; |
my $sector = $1; |
34 |
print "rewrite sector: $sector\n"; |
print "rewrite sector: $sector\n"; |
35 |
foreach my $s ( $sector .. $sector + $rewrite_sectors ) { |
foreach my $s ( $sector .. $sector + $rewrite_sectors ) { |
36 |
system "hdparm --write-sector $s --yes-i-know-what-i-am-doing $drive"; |
write_sector $s; |
37 |
} |
} |
38 |
smart_test $sector; |
smart_test $sector; |
39 |
return 1; |
return 1; |
41 |
$test_started = 1; |
$test_started = 1; |
42 |
return 1; |
return 1; |
43 |
} elsif ( m/Not_testing/ ) { |
} elsif ( m/Not_testing/ ) { |
44 |
return 1 if $test_started; |
if ( $test_started ) { |
45 |
|
smart_last_error(); |
46 |
|
return 1 if $test_started; |
47 |
|
} |
48 |
smart_test 0; # first-time invocation |
smart_test 0; # first-time invocation |
49 |
} |
} |
50 |
} |
} |
51 |
return 0; |
return 0; |
52 |
} |
} |
53 |
|
|
54 |
|
sub smart_last_error { |
55 |
|
my $cmd = "smartctl -l selftest $drive"; |
56 |
|
warn "$cmd\n"; |
57 |
|
open(my $fh, '-|', $cmd); |
58 |
|
while(<$fh>) { |
59 |
|
chomp; |
60 |
|
print "# $_\n"; |
61 |
|
if (/^#\s+1.+Completed: read failure\s+\S+\s+\S+\s+(\d+)/) { |
62 |
|
write_sector $1; |
63 |
|
smart_test $1; |
64 |
|
return; |
65 |
|
} |
66 |
|
} |
67 |
|
} |
68 |
|
|
69 |
|
smart_last_error; |
70 |
|
|
71 |
while ( smart ) { |
while ( smart ) { |
72 |
warn "sleep $delay s", ( $test_started ? " smart test running..." : "idle" ), "\n"; |
warn "sleep $delay s", ( $test_started ? " smart test running..." : "idle" ), "\n"; |
73 |
sleep $delay; |
sleep $delay; |