summaryrefslogtreecommitdiffstats
path: root/etc/inc/gmirror.inc
blob: 01d21c4e918069cabf2b02b2819184c3a47a06c3 (plain)
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
/*
	gmirror.inc
	Copyright (C) 2009-2014 Jim Pingle
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	1. Redistributions of source code must retain the above copyright notice,
	this list of conditions and the following disclaimer.

	2. Redistributions in binary form must reproduce the above copyright
	notice, this list of conditions and the following disclaimer in the
	documentation and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
	INClUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
*/

global $balance_methods;
$balance_methods = array("load", "prefer", "round-robin", "split");

/* Create a status array for each mirror and its disk components. */
function gmirror_get_status() {
	$status = "";
	exec("/sbin/gmirror status -s", $status);
	$mirrors = array();

	/* Empty output = no mirrors found */
	if (count($status) > 0) {
		/* Loop through gmirror status output. */
		foreach ($status as $line) {
			/* Split the line by whitespace */
			$all = preg_split("/[\s\t]+/", trim($line), 3);
			if (count($all) == 3) {
				/* If there are three items on a line, it is mirror name, status, and component */
				$currentmirror = basename($all[0]);
				$mirrors[$currentmirror]['name'] = basename($all[0]);
				$mirrors[$currentmirror]['status'] = $all[1];
				if (!is_array($mirrors[$currentmirror]['components']))
					$mirrors[$currentmirror]['components'] = array();
				$mirrors[$currentmirror]['components'][] = $all[2];
			}
		}
	}
	/* Return an hash of mirrors and components */	
	return $mirrors;
}

/* Get only status word for a single mirror. */
function gmirror_get_status_single($mirror) {
	$status = "";
	$mirror_status = gmirror_get_status();
	return $mirror_status[$mirror]['status'];
}

/* Generate an HTML formatted status for mirrors and disks in a small format for the widget */
function gmirror_html_status() {
	$mirrors = gmirror_get_status();
	$output = "";
	if (count($mirrors) > 0) {
		$output .= "<tr>\n";
		$output .= "<td width=\"40%\" class=\"vncellt\">Name</td>\n";
		$output .= "<td width=\"40%\" class=\"vncellt\">Status</td>\n";
		$output .= "<td width=\"20%\" class=\"vncellt\">Component</td>\n";
		$output .= "</tr>\n";
		foreach ($mirrors as $mirror => $name) {
			$components = count($name["components"]);
			$output .= "<tr>\n";
			$output .= "<td width=\"40%\" rowspan=\"{$components}\" class=\"listr\">{$name['name']}</td>\n";
			$output .= "<td width=\"40%\" rowspan=\"{$components}\" class=\"listr\">{$name['status']}</td>\n";
			$output .= "<td width=\"20%\" class=\"listr\">{$name['components'][0]}</td>\n";
			$output .= "</tr>\n";
			if (count($name["components"]) > 1) {
				$morecomponents = array_slice($name["components"], 1);
				foreach ($morecomponents as $component) {
					$output .= "<tr>\n";
					$output .= "<td width=\"20%\" class=\"listr\">{$component}</td>\n";
					$output .= "</tr>\n";
				}
			}
		}
	} else {
		$output .= "<tr><td colspan=\"3\" class=\"listr\">No Mirrors Found</td></tr>\n";
	}
	// $output .= "<tr><td colspan=\"3\" class=\"listr\">Updated at " . date("F j, Y, g:i:s a") . "</td></tr>\n";
	return $output;
}

/* List all disks in the system (potential gmirror targets) */
function gmirror_get_disks() {
	$disklist = "";
	/* Get a list of disks in a scriptable way, exclude optical drives */
	exec("/sbin/geom disk status -s | /usr/bin/grep -v '[[:blank:]]*cd[[:digit:]]*' | /usr/bin/awk '{print $1;}'", $disklist);
	return $disklist;
}

/* List all potential gmirror consumers */
function gmirror_get_unused_consumers() {
	$consumerlist = "";
	$disklist = gmirror_get_disks();
	/* Get a list of consumers, exclude existing mirrors and diskid entries */
	exec("/sbin/geom part status -s | /usr/bin/egrep -v '(mirror|diskid)' | /usr/bin/awk '{print $1, $3;}'", $consumerlist);
	$all_consumers = array();
	foreach ($consumerlist as $cl) {
		$parts = explode(" ", $cl);
		foreach ($parts as $part)
			$all_consumers[] = $part;
	}
	foreach ($disklist as $d) {
		if (!is_consumer_used($d) && !in_array($d, $all_consumers)) {
			$all_consumers[] = $d;
		}
	}
	return $all_consumers;
}

/* List all existing geom mirrors */
function gmirror_get_mirrors() {
	$mirrorlist = "";
	exec("/sbin/gmirror list | /usr/bin/grep '^Geom name:' | /usr/bin/awk '{print $3;}'", $mirrorlist);
	return $mirrorlist;
}


/* List all consumers for a given mirror */
function gmirror_get_consumers_in_mirror($mirror) {
	if (!is_valid_mirror($mirror))
		return array();

	$consumers = array();
	exec("/sbin/gmirror status -s " . escapeshellarg($mirror) . " | /usr/bin/awk '{print $3;}'", $consumers);
	return $consumers;
}

/* Test if a given consumer is a member of an existing mirror */
function is_consumer_in_mirror($consumer, $mirror) {
	if (!is_valid_consumer($consumer) || !is_valid_mirror($mirror))
		return false;

	$mirrorconsumers = gmirror_get_consumers_in_mirror($mirror);
	return in_array(basename($consumer), $mirrorconsumers);
}

/* Test if a mirror exists */
function is_valid_mirror($mirror) {
	$mirrors = gmirror_get_mirrors();
	return in_array($mirror, $mirrors);
}

/* Test if a disk is valid/exists */
function is_valid_disk($disk) {
	$adisks = gmirror_get_disks();
	return in_array(basename($disk), $adisks);
}

/* Test if a consumer is valid and in use in a mirror */
function is_consumer_used($consumer) {
	$found = false;
	$mirrors = gmirror_get_mirrors();
	foreach ($mirrors as $mirror) {
		$consumers = gmirror_get_consumers_in_mirror($mirror);
		if (in_array($consumer, $consumers))
			return true;
	}
	return false;
}

/* Test if a consumer is valid and not in use */
function is_consumer_unused($consumer) {
	$consumers = gmirror_get_unused_consumers();
	return in_array($consumer, $consumers);
}

/* Test if a consumer is valid (either a disk or partition) */
function is_valid_consumer($consumer) {
	return (is_consumer_unused($consumer) || is_consumer_used($consumer));
}

/* Remove all disconnected drives from a mirror */
function gmirror_forget_disconnected($mirror) {
	if (!is_valid_mirror($mirror))
		return false;
	return mwexec("/sbin/gmirror forget " . escapeshellarg($mirror));
}

/* Insert another consumer into a mirror */
function gmirror_insert_consumer($mirror, $consumer) {
	if (!is_valid_mirror($mirror) || !is_valid_consumer($consumer))
		return false;
	return mwexec("/sbin/gmirror insert " . escapeshellarg($mirror) . " " . escapeshellarg($consumer));
}

/* Remove consumer from a mirror and clear its metadata */
function gmirror_remove_consumer($mirror, $consumer) {
	if (!is_valid_mirror($mirror) || !is_valid_consumer($consumer))
		return false;
	return mwexec("/sbin/gmirror remove " . escapeshellarg($mirror) . " " . escapeshellarg($consumer));
}

/* Wipe geom info from drive (if mirror is not running) */
function gmirror_clear_consumer($consumer) {
	if (!is_valid_consumer($consumer))
		return false;
	return mwexec("/sbin/gmirror clear " . escapeshellarg($consumer));
}

/* Find the balance method used by a given mirror */
function gmirror_get_mirror_balance($mirror) {
	if (!is_valid_mirror($mirror))
		return false;
	$balancemethod = "";
	exec("/sbin/gmirror list " . escapeshellarg($mirror) . " | /usr/bin/grep '^Balance:' | /usr/bin/awk '{print $2;}'", $balancemethod);
	return $balancemethod[0];
}

/* Change balance algorithm of the mirror */
function gmirror_configure_balance($mirror, $balancemethod) {
	global $balance_methods;
	if (!is_valid_mirror($mirror) || !in_array($balancemethod, $balance_methods))
		return false;
	return mwexec("/sbin/gmirror configure -b " . escapeshellarg($balancemethod) . " " . escapeshellarg($mirror));
}

/* Force a mirror member to rebuild */
function gmirror_force_rebuild($mirror, $consumer) {
	if (!is_valid_mirror($mirror) || !is_valid_consumer($consumer))
		return false;
	return mwexec("/sbin/gmirror rebuild " . escapeshellarg($mirror) . " " . escapeshellarg($consumer));
}

/* Show all metadata on the physical consumer */
function gmirror_get_consumer_metadata($consumer) {
	if (!is_valid_consumer($consumer))
		return array();
	$output = "";
	exec("/sbin/gmirror dump " . escapeshellarg($consumer), $output);
	return array_map('trim', $output);
}

/* Test if a consumer has metadata, indicating it is a member of a mirror (active or inactive) */
function gmirror_consumer_has_metadata($consumer) {
	return (count(gmirror_get_consumer_metadata($consumer)) > 0);
}

/* Find the mirror to which this consumer belongs */
function gmirror_get_consumer_metadata_mirror($consumer) {
	if (!is_valid_consumer($consumer))
		return array();
	$metadata = gmirror_get_consumer_metadata($consumer);
	foreach ($metadata as $line) {
		if (substr($line, 0, 5) == "name:") {
			list ($key, $value) = explode(":", $line, 2);
			return trim($value);
		}
	}
}

/* Deactivate consumer, removing it from service in the mirror, but leave metadata intact */
function gmirror_deactivate_consumer($mirror, $consumer) {
	if (!is_valid_mirror($mirror) || !is_valid_consumer($consumer))
		return false;
	return mwexec("/sbin/gmirror deactivate " . escapeshellarg($mirror) . " " . escapeshellarg($consumer));
}

/* Reactivate a deactivated consumer */
function gmirror_activate_consumer($mirror, $consumer) {
	if (!is_valid_mirror($mirror) || !is_valid_consumer($consumer))
		return false;
	return mwexec("/sbin/gmirror activate " . escapeshellarg($mirror) . " " . escapeshellarg($consumer));
}

/* Find the size of the given mirror */
function gmirror_get_mirror_size($mirror) {
	if (!is_valid_mirror($mirror))
		return false;
	$mirrorsize = "";
	exec("/sbin/gmirror list " . escapeshellarg($mirror) . " | /usr/bin/grep 'Mediasize:' | /usr/bin/head -n 1 | /usr/bin/awk '{print $2;}'", $mirrorsize);
	return $mirrorsize[0];
}

/* Return a list of all potential consumers on a disk with sizes. The geom part
	list output is a little odd, we can't get the output for just the disk, if the disk contains
	slices those get output also. */
function gmirror_get_all_unused_consumer_sizes_on_disk($disk) {
	if (!is_valid_disk($disk) || !is_consumer_unused($disk))
		return array();
	$output = "";
	exec("/sbin/geom part list " . escapeshellarg($disk) . " | /usr/bin/egrep '(Name:|Mediasize:)' | /usr/bin/cut -c4- | /usr/bin/sed -l -e 'N;s/\\nMediasize://;P;D;' | /usr/bin/cut -c7-", $output);
	if (empty($output)) {
		exec("/sbin/geom disk list " . escapeshellarg($disk) . " | /usr/bin/egrep '(Name:|Mediasize:)' | /usr/bin/cut -c4- | /usr/bin/sed -l -e 'N;s/\\nMediasize://;P;D;' | /usr/bin/cut -c7-", $output);
	}
	$disk_contents = array();
	foreach ($output as $line) {
		list($name, $size, $humansize) = explode(" ", $line, 3);
		$consumer = array();
		$consumer['name'] = $name;
		$consumer['size'] = $size;
		$consumer['humansize'] = $humansize;
		$disk_contents[] = $consumer;
	}
	return $disk_contents;
}

/* Get only the size for one specific potential consumer. */
function gmirror_get_unused_consumer_size($consumer) {
	$consumersizes = gmirror_get_all_unused_consumer_sizes_on_disk($consumer);
	foreach ($consumersizes as $csize) {
		if ($csize['name'] == $consumer)
			return $csize['size'];
	}
	return -1;
}
?>
OpenPOWER on IntegriCloud