-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpandable-raid.php
More file actions
executable file
·261 lines (210 loc) · 7.39 KB
/
expandable-raid.php
File metadata and controls
executable file
·261 lines (210 loc) · 7.39 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/php
<?
/* vim set noexpandtab */
/**
*
* expandable-raid.php
*
* Use Linux RAID and LVM together to add disks to your RAID array without stopping it
*
* https://github.com/stevecoug/expandable-raid
*
* Copyright (c) 2014 Steve Meyers
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/.
*
*/
$mode = false;
$partitions = [];
$volgroup = false;
$raiddev = false;
$chunk = 64;
$level = 5;
$layout = false;
$sync = true;
$dryrun = false;
try {
for ($i = 1; $i < $argc; $i++) {
switch ($argv[$i]) {
case "-c":
case "--create":
$mode = "create";
break;
case "-x":
case "--extend":
$mode = "extend";
break;
case "--remove":
$mode = "remove";
break;
case "-v":
case "--vg":
case "--volgroup":
$volgroup = $argv[++$i];
break;
case "-l":
case "--level":
$level = intval($argv[++$i]);
if (!in_array($level, [ 0, 1, 5, 10 ])) throw new Exception("Invalid RAID level: $level");
break;
case "-r":
case "--raid":
$raiddev = $argv[++$i];
if ($raiddev[0] !== "/") $raiddev = "/dev/$raiddev";
if (!preg_match('|^/dev/md[0-9]+$|', $raiddev)) throw new Exception("Invalid RAID device: $raiddev");
break;
case "-p":
case "--partition":
case "--partitions":
$partitions = [];
$tmp = explode(",", $argv[++$i]);
foreach ($tmp as $part) {
if ($part[0] !== "/") $part = "/dev/$part";
$partitions[] = $part;
}
break;
case "--chunk":
$chunk = intval($argv[++$i]);
if ($chunk < 1 || $chunk > 1024) throw new Exception("Chunk size must be between 1-1024");
break;
case "--layout":
$layout = $argv[++$i];
break;
case "--no-sync":
if (in_array($level, [ 0, 1, 6, 10 ])) {
$sync = false;
} else {
throw new Exception("--no-sync is only valid with RAID levels 0, 1, 6, and 10");
}
break;
case "--dry-run":
$dryrun = true;
break;
default:
throw new Exception("Invalid argument: ".$argv[$i]);
break;
}
}
if ($mode === false) throw new Exception("You must specify either --create or --extend");
if ($volgroup === false) throw new Exception("Volume group must be specified");
if (count($partitions) == 0 && in_array($mode, [ "create", "extend" ])) throw new Exception("Partitions must be specified");
if ($mode == "extend") {
if ($raiddev === false) throw new Exception("RAID device must be specified");
}
} catch (Exception $e) {
printf("ERROR: %s\n", $e->getMessage());
echo "\n";
echo "Usage:\n";
echo " expandable-raid.php --create [--level RAIDLEVEL] [--chunk CHUNKKB] [--layout LAYOUT] [--no-sync] --vg VOLGROUP --partitions PART1,PART2,PART3\n";
echo " expandable-raid.php --extend --vg VOLGROUP --raid RAIDDEV --partition PART1\n";
echo " expandable-raid.php --remove --vg VOLGROUP --raid RAIDDEV\n";
echo "\n";
exit(1);
}
function run_command($cmd, $err_ok = false) {
echo " + $cmd\n";
if ($GLOBALS['dryrun']) return true;
system($cmd, $retval);
if ($retval > 0) {
if ($err_ok) return false;
throw new Exception("ERROR: command did not complete successfully!");
}
return true;
}
// Some common escaping...
$sh_volgroup = escapeshellarg($volgroup);
try {
if ($mode === "create") {
//************************** BEGIN CREATE MODE PREPARATION ****************************
// Make sure any partitions being used in a volume group are moved off
foreach ($partitions as $part) {
$sh_part = escapeshellarg($part);
if (run_command("pvdisplay $sh_part >/dev/null 2>/dev/null", true)) {
echo "Removing $part from volume group...\n";
run_command("pvmove --autobackup y $sh_part", true);
run_command("vgreduce --autobackup y $sh_volgroup $sh_part");
run_command("pvremove $sh_part");
}
}
for ($i = 0; file_exists("/dev/md$i"); $i++);
$raiddev = "/dev/md$i";
//************************** END CREATE MODE PREPARATION ****************************
} else if ($mode === "extend" || $mode === "remove") {
//************************** BEGIN EXTEND MODE PREPARATION (OR REMOVE MODE) ****************************
$old_partitions = false;
$dev = substr($raiddev, 5);
echo "Determining current partitions in $raiddev...\n";
foreach (file("/proc/mdstat") as $line) {
if (!preg_match("/^$dev : active raid([0-9]+) (.*)/", $line, $regs)) continue;
$level = intval($regs[1]);
if (!in_array($level, [ 0, 1, 5, 10 ])) {
throw new Exception("ERROR: Unknown RAID level ($level)");
}
if (!preg_match_all('/([a-z]+[0-9]+)\[[0-9]+\]/', $regs[2], $matches)) {
throw new Exception("ERROR: Could not get partition information from $regs[2]");
}
$old_partitions = $matches[1];
}
if ($old_partitions === false) {
throw new Exception("ERROR: Could not get partition information for $raiddev");
}
// Get the chunk size for the existing RAID device
$md = substr($raiddev, 5);
if (file_exists("/sys/block/$md/md/chunk_size")) {
$chunk = intval(file_get_contents("/sys/block/$md/md/chunk_size")) / 1024;
}
// Get the layout of the existing RAID device
if (in_array($level, [ 5, 10 ])) {
$output = shell_exec("mdadm --detail $raiddev");
if (!preg_match('/Layout : (.*)$/m', $output, $regs)) throw new Exception("Could not determine layout of $raiddev");
$layout = $regs[1];
if ($level == 10) {
list($layout_type, $layout_num) = explode("=", $layout);
$layout_num = intval($layout_num);
if ($layout_num < 1 || !in_array($layout_type, [ "near", "far", "offset" ])) {
throw new Exception("$raiddev has an unknown layout: $layout");
}
$layout = $layout_type[0] . $layout_num;
} else if ($level == 5) {
if (!in_array($layout, [ "left-asymmetric", "left-symmetric", "right-asymmetric", "right-symmetric" ])) {
throw new Exception("$raiddev has an unknown layout: $layout");
}
}
}
echo "Removing RAID device: $raiddev\n";
run_command("pvmove --autobackup y $raiddev", true);
run_command("vgreduce --autobackup y $sh_volgroup $raiddev");
run_command("pvremove $raiddev");
echo "Stopping RAID device $raiddev\n";
run_command("mdadm --stop $raiddev");
echo "Zeroing superblocks for old RAID drives\n";
foreach ($old_partitions as $part) {
$part = "/dev/$part";
echo " + $part\n";
run_command("mdadm --zero-superblock $part");
$partitions[] = $part;
}
//************************** END EXTEND MODE PREPARATION ****************************
}
if ($mode === "create" || $mode === "extend") {
//************************** BEGIN RAID DEVICE CREATION ****************************
echo "Creating RAID device: $raiddev\n";
$num_parts = count($partitions);
$sh_partitions = "";
foreach ($partitions as $part) {
$sh_partitions .= escapeshellarg($part) . " ";
}
$other_options = "";
if ($layout !== false) $other_options .= " --layout=".escapeshellarg($layout);
if ($sync) $other_options .= " --assume-clean";
run_command("mdadm --create --verbose $raiddev --level=$level --chunk=$chunk $other_options --raid-devices=$num_parts $sh_partitions");
run_command("pvcreate $raiddev");
run_command("vgextend $sh_volgroup $raiddev");
//************************** END RAID DEVICE CREATION ****************************
}
} catch (Exception $e) {
printf("ERROR: %s\n", $e->getMessage());
exit(2);
}