-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblink
239 lines (147 loc) · 4.63 KB
/
blink
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/perl
=head1 NAME
blink - blink disks in a disk enclosure
=head1 SYNOPSIS
B<blink> [-n|--on|-f|--off|-t|--toggle] [I<device>|--all|-d|--all-detected|-s|--all-slots]
B<blink> I<action> I<what>
=head1 DESCRIPTION
B<blink> blinks a device in a harddisk enclosure.
If no I<action> is given the blinking be will toggled.
If no I<device> is given all detected disks in enclosures will be
used.
If no I<action> and no I<device> is given all detected harddisks will
be off and all empty slots or slots with non-detected harddisks in
enclosures will blink. These slots should be safe to remove.
=head1 OPTIONS
=over 9
=item I<action>
What action to do. One of B<-n>, B<--on>, B<-f>, B<--off>, B<-t>,
B<--toggle>. Default is B<--toggle>.
=item I<what>
What slots to perform the action on. One of B<-d>, B<--all>,
B<--all-detected>, B<-s>, B<--all-slots>, I<device>. Default is
B<--all-detected>.
=item I<device>
The disk device to blink. Either as I<sdX> or as I</dev/sdX>.
=item B<--all-detected>
=item B<--all>
=item B<-d>
Select all the detected devices.
=item B<--all-slots>
=item B<-s>
Select all slots in the enclosures.
=item B<--on>
=item B<-n>
Turn the blink on.
=item B<--off>
=item B<-f>
Turn the blink off.
=item B<--toggle>
=item B<-t>
Toggle the blink.
=back
=head1 EXAMPLE: Blink harddisk /dev/sdf
To blink /dev/sdf
B<blink /dev/sdf>
=head1 EXAMPLE: Blink all undetected slots
It will be safe to remove disk from all the blinking slots as the
slots are either empty or not detected.
B<blink>
=head1 EXAMPLE: Turn off blinking of all slots
Turn off the blinking.
B<blink -s -f>
=head1 EXIT STATUS
Always returns true.
=head1 REPORTING BUGS
Contact Ole Tange <[email protected]>.
=head1 AUTHOR
Copyright (C) 2012 Ole Tange <[email protected]>.
=head1 LICENSE
Copyright (C) 2012 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
at your option any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
=head1 DEPENDENCIES
B<blink> uses Perl and GNU Parallel.
=head1 SEE ALSO
B<sg3_utils>(1), B<parallel>(1).
=cut
use Getopt::Long;
GetOptions(
"n|on" => \$::opt_on,
"f|off" => \$::opt_off,
"t|toggle" => \$::opt_toggle,
"d|all|all-detected|alldetected" => \$::opt_alldetected,
"s|all-slots|allslots" => \$::opt_allslots,
);
my @more_args = ();
if(@ARGV) {
for(@ARGV) {
s{/dev/}{}g;
if(/^md/) {
# RAID device. Try to find the matching physical devices
my $md = $_;
for my $line (grep /^$md :/, `cat /proc/mdstat`) {
# md1 : active raid6 sdat[14](S) sdx[13] sds[12](S) sdc[6](S) sdb[0] sdm[9] sdk[7] sdaa[10] sdi[5] sdh[4]
while($line =~ s/(\S+)\[//) {
push @more_args, $1;
}
}
}
}
}
push @ARGV,@more_args;
if(not ($::opt_on or $::opt_off or $::opt_toggle or
$::opt_alldetected or $::opt_allslots or @ARGV)) {
# Default:
# Turn on all
# Turn off all-detected
$locate = "/sys/class/enclosure/*/*/locate";
on($locate);
$locate = "/sys/class/enclosure/*/*/device/enclosure*/locate";
off($locate);
exit;
}
if($::opt_alldetected) {
$locate = "/sys/class/enclosure/*/*/device/enclosure*/locate";
} elsif($::opt_allslots) {
$locate = "/sys/class/enclosure/*/*/locate";
} else {
if($#ARGV == 0) {
$dev = shift;
} else {
local($"=",");
$dev = @ARGV ? "{@ARGV}" : "*";
}
$locate = "/sys/class/enclosure/*/*/device/block/$dev/../../enclosure*/locate";
}
if($::opt_on) {
on($locate);
} elsif($::opt_off) {
off($locate);
} else {
toggle($locate);
}
sub on {
my $locate = shift;
print(q{bash -c 'parallel -j1 echo 1 \\> {} ::: }. $locate."'\n");
system(q{bash -c 'parallel -j1 echo 1 \\> {} ::: }. $locate."'\n");
}
sub off {
my $locate = shift;
print(q{bash -c 'parallel -j1 echo 0 \\> {} ::: }. $locate."'\n");
system(q{bash -c 'parallel -j1 echo 0 \\> {} ::: }. $locate."'\n");
}
sub toggle {
my $locate = shift;
# If the file 'locate' contains 1 it should be put to 0.
print(q{bash -c 'parallel -j1 grep -q 1 {} \; echo \$? \\> {} ::: }. $locate."'\n");
system(q{bash -c 'parallel -j1 grep -q 1 {} \; echo \$? \\> {} ::: }. $locate."'\n");
}