-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshelf-tool
executable file
·140 lines (107 loc) · 3.33 KB
/
shelf-tool
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
#!/usr/bin/php
<?php
//Shelf Tool - CLI tool for managing JBOD disk shelves on Linux / Ubuntu
//Initial version written by David Swinton
//Licensed under GNU GPL v3
//See https://github.com/dswinton/disk-shelf-tool
//Command line arguments
@$action = $argv[1];
@$shelf = $argv[2];
@$command = $argv[3];
class shelfTool {
//Nested array of all shelves and devices
public $o = array();
//Flat array of all devices
public $allDevs = array();
//Constructor -- get the list of all the things
function __construct(){
//Actually list the things
$dir = scandir('/dev/disk/by-path/');
//Build array of shelves and devices
foreach($dir as $d){
$i = explode('-', $d);
if(@count($i) == 7){ //Ignore partitions / . / .. and anything that doesn't look like a shelf
@$this->o[$i[3]][$i[4]] = '/dev/disk/by-path/'.$d;
$allDevs[] = '/dev/disk/by-path/'.$d;
};
};
}
//Output a log line
function log($line){
echo $line."\n";
}
//List shelves and devices in a tree
function list(){
foreach($this->o as $shelfId => $disks){
echo "Shelf ID: \033[0;32m".$shelfId."\033[0m"." (".count($disks)." disks)\n";
foreach($disks as $d){
echo "\033[0;31m".$d."\033[0m"."\n";
};
};
}
//List shelves and devices in a tree - follow symlinks to /dev/sdX
function listSdx(){
foreach($this->o as $shelfId => $disks){
echo "Shelf ID: \033[0;32m".$shelfId."\033[0m"." (".count($disks)." disks)\n";
foreach($disks as $d){
$link = explode('/',readLink($d));
echo "\033[0;31m/dev/".$link[2]."\033[0m"."\n";
};
};
}
//Blink the LED's on all the discovered drives in a shelf
function blink($shelf){
if($shelf){
$this->log("Blinking shelf ".$shelf." (".count($o[$argv[1]])." devices)...");
foreach($this->o[$shelf] as $d){
exec('screen -d -m sas_disk_blink '.$d."\n");
};
}else{
$this->log('Shelf is a required argument');
};
}
//Prints usage information
function usage(){
global $argv;
$this->log('USAGE: '.$argv[0].' <help|list|blink|deps|run> [shelf id] [command]');
$this->log('');
$this->log('help -- displays this message');
$this->log('list -- lists shelves (green) and their disks (red)');
$this->log('list-sdx -- lists shelves (green) and their disks (red) - display /dev/sdX notation rather than /dev/disk/by-path/X');
$this->log('blink <shelf id> -- blinks all disks in a shelf');
$this->log('run <shelf id> <command> -- runs a command on all disks in a shelf interactively, one at a time -- for example, use parted to format or partition disks');
}
//Runs a command on each disk in a shelf
function runCommand($shelf, $command){
if(!$shelf){$this->log('Shelf is required');exit;};
if(!$command){$this->log('Command is required');exit;};
foreach($this->o[$shelf] as $d){
passthru($command. ' '.$d);
};
}
};
$st = new shelfTool();
switch($action){
case "list":
$st->list();
break;
case "list-sdx":
$st->listSdx();
break;
case "blink":
$st->blink($shelf);
break;
case "run":
$st->runCommand($shelf, $command);
break;
case "help":
$st->usage();
break;
case "?":
$st->usage();
break;
default:
$st->usage();
break;
};
?>