]>
Commit | Line | Data |
---|---|---|
f891182f | 1 | <?php |
9be18e88 MR |
2 | require_once('openmediavault/object.inc'); |
3 | require_once('openmediavault/module.inc'); | |
b76f4e17 MR |
4 | require_once("Vdev.php"); |
5 | require_once("Snapshot.php"); | |
6 | require_once("Dataset.php"); | |
7 | require_once("Zvol.php"); | |
503ffcc8 MR |
8 | require_once("VdevType.php"); |
9 | require_once("Utils.php"); | |
b76f4e17 | 10 | require_once("Exception.php"); |
f891182f MR |
11 | |
12 | /** | |
b76f4e17 | 13 | * Class containing information about the pool |
f891182f | 14 | * |
b76f4e17 MR |
15 | * @author Michael Rasmussen |
16 | * @version 0.1 | |
17 | * @copyright Michael Rasmussen <mir@datanom.net> | |
f891182f | 18 | */ |
cf4e471d | 19 | class OMVModuleZFSZpool extends OMVModuleAbstract { |
f891182f MR |
20 | // Attributes |
21 | /** | |
b76f4e17 | 22 | * Name of pool |
f891182f MR |
23 | * |
24 | * @var string $name | |
25 | * @access private | |
26 | */ | |
b76f4e17 | 27 | private $name; |
f891182f MR |
28 | |
29 | /** | |
b76f4e17 | 30 | * List of Vdev |
f891182f | 31 | * |
b76f4e17 | 32 | * @var array $vdevs |
f891182f | 33 | * @access private |
e39afba8 | 34 | * @association OMVModuleZFSVdev to vdevs |
f891182f | 35 | */ |
b76f4e17 | 36 | private $vdevs; |
f891182f MR |
37 | |
38 | /** | |
b76f4e17 | 39 | * List of spares |
f891182f | 40 | * |
b76f4e17 | 41 | * @var array $spare |
f891182f | 42 | * @access private |
e39afba8 | 43 | * @association OMVModuleZFSVdev to spare |
f891182f | 44 | */ |
b76f4e17 | 45 | private $spare; |
f891182f MR |
46 | |
47 | /** | |
b76f4e17 | 48 | * List of log |
f891182f | 49 | * |
b76f4e17 | 50 | * @var array $log |
f891182f | 51 | * @access private |
e39afba8 | 52 | * @association OMVModuleZFSVdev to log |
f891182f | 53 | */ |
b76f4e17 | 54 | private $log; |
f891182f MR |
55 | |
56 | /** | |
b76f4e17 | 57 | * List of cache |
f891182f | 58 | * |
b76f4e17 | 59 | * @var array $cache |
f891182f | 60 | * @access private |
e39afba8 | 61 | * @association OMVModuleZFSVdev to cache |
f891182f | 62 | */ |
b76f4e17 | 63 | private $cache; |
f891182f MR |
64 | |
65 | /** | |
b76f4e17 | 66 | * Pool size |
f891182f MR |
67 | * |
68 | * @var int $size | |
69 | * @access private | |
70 | */ | |
b76f4e17 | 71 | private $size; |
f891182f MR |
72 | |
73 | /** | |
b76f4e17 | 74 | * Pool's mountpoint |
f891182f MR |
75 | * |
76 | * @var string $mountPoint | |
77 | * @access private | |
78 | */ | |
b76f4e17 | 79 | private $mountPoint; |
f891182f MR |
80 | |
81 | /** | |
b76f4e17 | 82 | * List of features |
f891182f | 83 | * |
b76f4e17 | 84 | * @var array $features |
f891182f MR |
85 | * @access private |
86 | */ | |
b76f4e17 | 87 | private $features; |
f891182f MR |
88 | |
89 | // Associations | |
90 | /** | |
b76f4e17 | 91 | * Array of OMVModuleZFSSnapshot. |
f891182f | 92 | * |
b76f4e17 | 93 | * @var array $snapshot |
f891182f | 94 | * @access private |
e39afba8 | 95 | * @association OMVModuleZFSSnapshot to snapshot |
f891182f | 96 | */ |
b76f4e17 | 97 | private $snapshot; |
f891182f MR |
98 | |
99 | /** | |
b76f4e17 | 100 | * Array of OMVModuleZFSDataset |
f891182f | 101 | * |
b76f4e17 | 102 | * @var Dataset $dataset |
f891182f | 103 | * @access private |
e39afba8 | 104 | * @association OMVModuleZFSDataset to dataset |
f891182f | 105 | */ |
b76f4e17 | 106 | private $dataset; |
f891182f MR |
107 | |
108 | /** | |
b76f4e17 | 109 | * Array of OMVModuleZFSZvol |
f891182f | 110 | * |
b76f4e17 | 111 | * @var Zvol $zvol |
f891182f | 112 | * @access private |
e39afba8 | 113 | * @association OMVModuleZFSZvol to zvol |
f891182f | 114 | */ |
b76f4e17 | 115 | private $zvol; |
f891182f MR |
116 | |
117 | // Operations | |
b76f4e17 MR |
118 | /** |
119 | * Constructor | |
120 | * | |
e39afba8 | 121 | * @param $vdev OMVModuleZFSVdev or array(OMVModuleZFSVdev) |
b76f4e17 MR |
122 | * @throws OMVModuleZFSException |
123 | */ | |
124 | ||
7d6b772c | 125 | public function __construct($vdev, $opts = "") { |
5e58d5e2 MR |
126 | $create_pool = true; |
127 | ||
b76f4e17 MR |
128 | if (is_array($vdev)) { |
129 | $cmd = $this->getCommandString($vdev); | |
130 | $name = $vdev[0]->getPool(); | |
131 | $type = $vdev[0]->getType(); | |
5e58d5e2 | 132 | } else if ($vdev instanceof OMVModuleZFSVdev) { |
b76f4e17 MR |
133 | $cmd = $this->getCommandString(array($vdev)); |
134 | $name = $vdev->getPool(); | |
135 | $type = $vdev->getType(); | |
5e58d5e2 MR |
136 | } else { |
137 | // Assume we make an instance of an existing pool | |
138 | $create_pool = false; | |
b76f4e17 | 139 | } |
b76f4e17 | 140 | |
5e58d5e2 MR |
141 | $this->vdevs = array(); |
142 | $this->spare = null; | |
143 | $this->log = null; | |
144 | $this->cache = null; | |
145 | $this->features = array(); | |
146 | if ($create_pool) { | |
7d6b772c | 147 | $cmd = "zpool create $opts$name $cmd 2>&1"; |
5e58d5e2 MR |
148 | |
149 | OMVUtil::exec($cmd, $output, $result); | |
150 | if ($result) | |
42856e8b | 151 | throw new OMVModuleZFSException(implode("\n", $output)); |
5e58d5e2 MR |
152 | else { |
153 | $this->name = $name; | |
154 | $this->type = $type; | |
155 | if (is_array($vdev)) | |
156 | $this->vdevs = $vdev; | |
157 | else | |
158 | array_push ($this->vdevs, $vdev); | |
159 | $this->size = $this->getAttribute("size"); | |
160 | $this->mountPoint = $this->getAttribute("mountpoint"); | |
161 | } | |
162 | } else { | |
163 | $this->assemblePool($vdev); | |
b76f4e17 MR |
164 | } |
165 | } | |
166 | ||
f891182f | 167 | /** |
b76f4e17 | 168 | * Get pool name |
f891182f | 169 | * |
b76f4e17 | 170 | * @return string |
f891182f MR |
171 | * @access public |
172 | */ | |
173 | public function getName() { | |
b76f4e17 | 174 | return $this->name; |
f891182f MR |
175 | } |
176 | ||
177 | /** | |
b76f4e17 | 178 | * Get array of Vdev |
f891182f | 179 | * |
b76f4e17 | 180 | * @return array |
f891182f MR |
181 | * @access public |
182 | */ | |
183 | public function getVdevs() { | |
b76f4e17 | 184 | return $this->vdevs; |
f891182f MR |
185 | } |
186 | ||
187 | /** | |
b76f4e17 | 188 | * Add Vdev to pool |
f891182f | 189 | * |
b76f4e17 MR |
190 | * @param array $vdev array of OMVModuleZFSVdev |
191 | * @return void | |
192 | * @throws OMVModuleZFSException | |
f891182f MR |
193 | * @access public |
194 | */ | |
b76f4e17 | 195 | public function addVdev(array $vdevs) { |
e39afba8 | 196 | $cmd = "zpool add " . $this->name . " " . $this->getCommandString($vdevs); |
b76f4e17 MR |
197 | OMVUtil::exec($cmd, $output, $result); |
198 | if ($result) | |
199 | throw new OMVModuleZFSException($output); | |
200 | else | |
201 | $this->vdevs = array_merge($this->vdevs, $vdevs); | |
e39afba8 | 202 | $this->size = $this->getAttribute("size"); |
f891182f MR |
203 | } |
204 | ||
205 | /** | |
206 | * XXX | |
207 | * | |
b76f4e17 MR |
208 | * @param OMVModuleZFSVdev $vdev |
209 | * @return void | |
210 | * @throws OMVModuleZFSException | |
f891182f MR |
211 | * @access public |
212 | */ | |
b76f4e17 MR |
213 | public function removeVdev(OMVModuleZFSVdev $vdev) { |
214 | throw new OMVModuleZFSException("Cannot remove vdevs from a pool"); | |
f891182f MR |
215 | } |
216 | ||
217 | /** | |
218 | * XXX | |
219 | * | |
b76f4e17 MR |
220 | * @param OMVModuleZFSVdev $cache |
221 | * @return void | |
222 | * @throws OMVModuleZFSException | |
f891182f MR |
223 | * @access public |
224 | */ | |
b76f4e17 MR |
225 | public function addCache(OMVModuleZFSVdev $cache) { |
226 | if ($cache->getType() != OMVModuleZFSVdevType::OMVMODULEZFSPLAIN) | |
227 | throw new OMVModuleZFSException("Only a plain Vdev can be added as cache"); | |
228 | ||
e39afba8 | 229 | $cmd = "zpool add " . $this->name . " cache " . $this->getCommandString($vdevs); |
b76f4e17 MR |
230 | OMVUtil::exec($cmd, $output, $result); |
231 | if ($result) | |
232 | throw new OMVModuleZFSException($output); | |
233 | ||
234 | $disks = $cache->getDisks(); | |
235 | foreach ($disks as $disk) { | |
236 | array_push ($this->cache, $disk); | |
237 | } | |
f891182f MR |
238 | } |
239 | ||
240 | /** | |
241 | * XXX | |
242 | * | |
b76f4e17 MR |
243 | * @param array $disks |
244 | * @return void | |
245 | * @throws OMVModuleZFSException | |
f891182f MR |
246 | * @access public |
247 | */ | |
b76f4e17 | 248 | public function removeCache(array $disks = null) { |
b76f4e17 MR |
249 | if (! $disks) |
250 | $disks = $this->cache; | |
251 | ||
e39afba8 MR |
252 | foreach ($disks as $disk) |
253 | $dist_str .= "$disk "; | |
b76f4e17 | 254 | |
e39afba8 MR |
255 | $cmd = "zpool remove " . $this->name . " $dist_str"; |
256 | OMVUtil::exec($cmd, $output, $result); | |
257 | if ($result) | |
258 | throw new OMVModuleZFSException($output); | |
259 | else { | |
260 | foreach ($disks as $disk) | |
261 | $this->cache = $this->removeDisk($this->cache, $disk); | |
b76f4e17 | 262 | } |
f891182f MR |
263 | } |
264 | ||
265 | /** | |
266 | * XXX | |
267 | * | |
b76f4e17 | 268 | * @return Cache |
f891182f MR |
269 | * @access public |
270 | */ | |
271 | public function getCache() { | |
b76f4e17 | 272 | return $this->cache; |
f891182f MR |
273 | } |
274 | ||
275 | /** | |
276 | * XXX | |
277 | * | |
b76f4e17 MR |
278 | * @param OMVModuleZFSVdev $log |
279 | * @return void | |
5e58d5e2 | 280 | * @throws OMVModuleZFSException |
f891182f MR |
281 | * @access public |
282 | */ | |
b76f4e17 | 283 | public function addLog(OMVModuleZFSVdev $log) { |
e39afba8 MR |
284 | if ($log->getType() == OMVModuleZFSVdevType::OMVMODULEZFSPLAIN || |
285 | $log->getType() == OMVModuleZFSVdevType::OMVMODULEZFSMIRROR) { | |
286 | $cmd = "zpool add " . $this->name . " log " . $this->getCommandString($vdevs); | |
287 | OMVUtil::exec($cmd, $output, $result); | |
288 | if ($result) | |
289 | throw new OMVModuleZFSException($output); | |
290 | ||
291 | $this->log = $log; | |
292 | } else | |
293 | throw new OMVModuleZFSException("Only a plain Vdev or mirror Vdev can be added as log"); | |
f891182f MR |
294 | } |
295 | ||
296 | /** | |
297 | * XXX | |
298 | * | |
b76f4e17 | 299 | * @return void |
5e58d5e2 | 300 | * @throws OMVModuleZFSException |
f891182f MR |
301 | * @access public |
302 | */ | |
303 | public function removeLog() { | |
e39afba8 MR |
304 | foreach ($this->log as $vdev) { |
305 | if ($vdev->getType() == OMVModuleZFSVdevType::OMVMODULEZFSMIRROR) { | |
306 | $cmd = "zpool remove " . $this->name . " mirror-$i"; | |
307 | } else { | |
308 | $disks = $vdev->getDisks(); | |
309 | foreach ($disks as $disk) | |
310 | $dist_str .= "$disk "; | |
311 | $cmd = "zpool remove " . $this->name . " $disk_str"; | |
312 | } | |
313 | OMVUtil::exec($cmd, $output, $result); | |
314 | if ($result) | |
315 | throw new OMVModuleZFSException($output); | |
316 | else | |
317 | $this->log = array(); | |
318 | } | |
f891182f MR |
319 | } |
320 | ||
321 | /** | |
322 | * XXX | |
323 | * | |
b76f4e17 | 324 | * @return Log |
f891182f MR |
325 | * @access public |
326 | */ | |
327 | public function getLog() { | |
e39afba8 | 328 | return $this->log; |
f891182f MR |
329 | } |
330 | ||
331 | /** | |
332 | * XXX | |
333 | * | |
e39afba8 | 334 | * @param OMVModuleZFSVdev $spares |
b76f4e17 | 335 | * @return void |
5e58d5e2 | 336 | * @throws OMVModuleZFSException |
f891182f MR |
337 | * @access public |
338 | */ | |
e39afba8 MR |
339 | public function addSpare(OMVModuleZFSVdev $spares) { |
340 | if ($spares->getType() != OMVModuleZFSVdevType::OMVMODULEZFSPLAIN) | |
341 | throw new OMVModuleZFSException("Only a plain Vdev can be added as spares"); | |
342 | ||
343 | $cmd = "zpool add " . $this->name . " spare " . $this->getCommandString($vdevs); | |
344 | OMVUtil::exec($cmd, $output, $result); | |
345 | if ($result) | |
346 | throw new OMVModuleZFSException($output); | |
347 | ||
348 | $disks = $spares->getDisks(); | |
349 | foreach ($disks as $disk) { | |
350 | array_push ($this->spare, $disk); | |
351 | } | |
f891182f MR |
352 | } |
353 | ||
354 | /** | |
355 | * XXX | |
356 | * | |
e39afba8 | 357 | * @param array $disks |
b76f4e17 | 358 | * @return void |
5e58d5e2 | 359 | * @throws OMVModuleZFSException |
f891182f MR |
360 | * @access public |
361 | */ | |
e39afba8 MR |
362 | public function removeSpare(array $disks = null) { |
363 | if (! $disks) | |
364 | $disks = $this->spare; | |
365 | ||
366 | foreach ($disks as $disk) | |
367 | $dist_str .= "$disk "; | |
368 | ||
369 | $cmd = "zpool remove " . $this->name . " $dist_str"; | |
370 | OMVUtil::exec($cmd, $output, $result); | |
371 | if ($result) | |
372 | throw new OMVModuleZFSException($output); | |
373 | else { | |
374 | foreach ($disks as $disk) | |
375 | $this->spare = $this->removeDisk($this->spare, $disk); | |
376 | } | |
f891182f MR |
377 | } |
378 | ||
379 | /** | |
380 | * XXX | |
381 | * | |
b76f4e17 | 382 | * @return list<Disk> |
f891182f MR |
383 | * @access public |
384 | */ | |
385 | public function getSpares() { | |
e39afba8 | 386 | return $this->spare; |
f891182f MR |
387 | } |
388 | ||
389 | /** | |
390 | * XXX | |
391 | * | |
b76f4e17 | 392 | * @return int |
f891182f MR |
393 | * @access public |
394 | */ | |
395 | public function getSize() { | |
e39afba8 | 396 | return $this->size; |
f891182f MR |
397 | } |
398 | ||
399 | /** | |
400 | * XXX | |
401 | * | |
b76f4e17 | 402 | * @return string |
f891182f MR |
403 | * @access public |
404 | */ | |
405 | public function getMountPoint() { | |
e39afba8 | 406 | return $this->mountPoint; |
f891182f MR |
407 | } |
408 | ||
409 | /** | |
410 | * XXX | |
411 | * | |
b76f4e17 MR |
412 | * @param array $features |
413 | * @return void | |
5e58d5e2 | 414 | * @throws OMVModuleZFSException |
f891182f MR |
415 | * @access public |
416 | */ | |
b76f4e17 | 417 | public function setFeatures(array $features) { |
e39afba8 MR |
418 | foreach ($features as $feature => $value) { |
419 | $cmd = "zpool set $feature=$value " . $this->name; | |
420 | OMVUtil::exec($cmd, $output, $result); | |
421 | if ($result) | |
422 | throw new OMVModuleZFSException($output); | |
423 | } | |
424 | $this->features = $this->getAllAttributes(); | |
f891182f MR |
425 | } |
426 | ||
427 | /** | |
e39afba8 MR |
428 | * We only return array of features for which the user can |
429 | * change in GUI. | |
f891182f | 430 | * |
e39afba8 | 431 | * @return array of features |
f891182f MR |
432 | * @access public |
433 | */ | |
503ffcc8 | 434 | public function getFeatures($internal = true) { |
e39afba8 MR |
435 | $attrs = array(); |
436 | $featureSet = array( | |
437 | 'recordsize', /* default 131072. 512 <= n^2 <= 131072*/ | |
438 | 'checksum', /* on | off */ | |
439 | 'compression', /* off | lzjb | gzip | zle | lz4 */ | |
440 | 'atime', /* on | off */ | |
441 | 'aclmode', /* discard | groupmask | passthrough | restricted */ | |
442 | 'aclinherit', /* discard | noallow | restricted | passthrough | passthrough-x */ | |
443 | 'casesensitivity', /* sensitive | insensitive | mixed */ | |
444 | 'primarycache', /* all | none | metadata */ | |
445 | 'secondarycache', /* all | none | metadata */ | |
446 | 'logbias', /* latency | throughput */ | |
447 | 'dedup', /* on | off */ | |
448 | 'sync' /* standard | always | disabled */ | |
449 | ); | |
9be18e88 MR |
450 | if (count($this->features) < 1) |
451 | $this->features = $this->getAllAttributes(); | |
503ffcc8 MR |
452 | if ($internal) { |
453 | foreach ($this->features as $attr => $val) { | |
454 | if (in_array($attr, $featureSet)) | |
455 | $attrs[$attr] = $val['value']; | |
456 | } | |
457 | } else { | |
458 | foreach ($this->features as $attr => $val) { | |
459 | if (in_array($attr, $featureSet)) | |
460 | $attrs[$attr] = $val; | |
461 | } | |
e39afba8 MR |
462 | } |
463 | ||
464 | return $attrs; | |
f891182f MR |
465 | } |
466 | ||
467 | /** | |
468 | * XXX | |
469 | * | |
b76f4e17 | 470 | * @return void |
5e58d5e2 | 471 | * @throws OMVModuleZFSException |
f891182f MR |
472 | * @access public |
473 | */ | |
474 | public function export() { | |
e39afba8 MR |
475 | $cmd = "zpool export " . $this->name; |
476 | OMVUtil::exec($cmd, $output, $result); | |
477 | if ($result) | |
478 | throw new OMVModuleZFSException($output); | |
f891182f MR |
479 | } |
480 | ||
481 | /** | |
482 | * XXX | |
483 | * | |
b76f4e17 MR |
484 | * @param string $name |
485 | * @return void | |
5e58d5e2 | 486 | * @throws OMVModuleZFSException |
f891182f MR |
487 | * @access public |
488 | */ | |
e39afba8 MR |
489 | public function import($name = null) { |
490 | if ($name) | |
491 | $cmd = "zpool import $name"; | |
492 | else | |
493 | $cmd = "zpool import"; | |
494 | OMVUtil::exec($cmd, $output, $result); | |
495 | if ($result) | |
496 | throw new OMVModuleZFSException($output); | |
f891182f MR |
497 | } |
498 | ||
499 | /** | |
500 | * XXX | |
501 | * | |
b76f4e17 | 502 | * @return void |
5e58d5e2 | 503 | * @throws OMVModuleZFSException |
f891182f MR |
504 | * @access public |
505 | */ | |
506 | public function scrub() { | |
e39afba8 MR |
507 | $cmd = "zpool scrub " . $this->name; |
508 | OMVUtil::exec($cmd, $output, $result); | |
509 | if ($result) | |
510 | throw new OMVModuleZFSException($output); | |
f891182f MR |
511 | } |
512 | ||
513 | /** | |
514 | * XXX | |
515 | * | |
b76f4e17 | 516 | * @return string |
5e58d5e2 | 517 | * @throws OMVModuleZFSException |
f891182f MR |
518 | * @access public |
519 | */ | |
520 | public function status() { | |
e39afba8 MR |
521 | $cmd = "zpool status " . $this->name; |
522 | OMVUtil::exec($cmd, $output, $result); | |
523 | if ($result) | |
524 | throw new OMVModuleZFSException($output); | |
f891182f MR |
525 | } |
526 | ||
503ffcc8 MR |
527 | /** |
528 | * Get a single property value associated with the Dataset | |
529 | * | |
530 | * @param string $property Name of the property to fetch | |
531 | * @return array The returned array with the property. The property is an associative array with | |
532 | * two elements, <value> and <source>. | |
533 | * @access public | |
534 | */ | |
535 | public function getProperty($property) { | |
536 | $attrs = $this->getFeatures(false); | |
537 | return $attrs["$property"]; | |
538 | } | |
539 | ||
540 | /** | |
541 | * Get an associative array of all properties associated with the Snapshot | |
542 | * | |
543 | * @return array $properties Each entry is an associative array with two elements | |
544 | * <value> and <source> | |
545 | * @access public | |
546 | */ | |
547 | public function getProperties() { | |
548 | $attrs = $this->getFeatures(false); | |
549 | return $attrs; | |
550 | } | |
551 | ||
552 | /** | |
553 | * Sets a number of Dataset properties. If a property is already set it will be updated with the new value. | |
554 | * | |
555 | * @param array $properties An associative array with properties to set | |
556 | * @return void | |
557 | * @access public | |
558 | */ | |
559 | public function setProperties($properties) { | |
560 | foreach ($properties as $newpropertyk => $newpropertyv) { | |
561 | $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name . " 2>&1"; | |
562 | OMVModuleZFSUtil::exec($cmd,$out,$res); | |
563 | $attr = $this->getAttribute($newpropertyk); | |
564 | $this->features[$newpropertyk] = $attr; | |
565 | } | |
566 | } | |
567 | ||
568 | /** | |
569 | * Destroy the Dataset. | |
570 | * | |
571 | * @return void | |
572 | * @access public | |
573 | */ | |
574 | public function destroy() { | |
575 | $cmd = "zpool destroy " . $this->name . " 2>&1"; | |
576 | $this->exec($cmd,$out,$res); | |
577 | } | |
578 | ||
579 | /** | |
580 | * Clears a previously set proporty and specifies that it should be | |
581 | * inherited from it's parent. | |
582 | * | |
583 | * @param string $property Name of the property to inherit. | |
584 | * @return void | |
585 | * @access public | |
586 | */ | |
587 | public function inherit($property) { | |
588 | $cmd = "zfs inherit " . $property . " " . $this->name . " 2>&1"; | |
589 | $this->exec($cmd,$out,$res); | |
590 | $attr = $this->getAttribute($newpropertyk); | |
591 | $this->features[$newpropertyk] = $attr; | |
592 | } | |
593 | ||
f891182f | 594 | /** |
b76f4e17 | 595 | * Convert array of Vdev to command string |
f891182f | 596 | * |
b76f4e17 MR |
597 | * @param array $vdevs |
598 | * @return string | |
599 | * @throws OMVMODULEZFSException | |
f891182f | 600 | */ |
b76f4e17 MR |
601 | private function getCommandString(array $vdevs) { |
602 | $adds = array(); | |
603 | ||
604 | foreach ($vdevs as $vdev) { | |
e39afba8 MR |
605 | if (is_object($vdev) == false) |
606 | throw new OMVMODULEZFSException("Not object of class OMVModuleZFSVdev"); | |
607 | if (is_a($vdev, OMVModuleZFSVdev) == false) | |
608 | throw new OMVMODULEZFSException("Object is not of class OMVModuleZFSVdev"); | |
b76f4e17 MR |
609 | $type = $vdev->getType(); |
610 | $command = ""; | |
611 | ||
612 | switch ($type) { | |
613 | case OMVModuleZFSVdevType::OMVMODULEZFSPLAIN: break; | |
614 | case OMVModuleZFSVdevType::OMVMODULEZFSMIRROR: $command = "mirror"; break; | |
615 | case OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ1: $command = "raidz1"; break; | |
616 | case OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ2: $command = "raidz2"; break; | |
617 | case OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ3: $command = "raidz3"; break; | |
618 | default: | |
619 | throw new OMVMODULEZFSException("Unknown Vdev type"); | |
620 | } | |
621 | $disks = $vdev->getDisks(); | |
622 | $diskStr = ""; | |
623 | foreach($disks as $disk) { | |
624 | $diskStr .= " $disk"; | |
625 | } | |
626 | ||
627 | array_push ($adds, $command . $diskStr); | |
628 | } | |
629 | ||
da22577f | 630 | return implode(" ", $adds); |
f891182f MR |
631 | } |
632 | ||
633 | /** | |
b76f4e17 | 634 | * Get an attribute from pool |
f891182f | 635 | * |
b76f4e17 MR |
636 | * @param string $attribute |
637 | * @return string value | |
f891182f | 638 | */ |
b76f4e17 MR |
639 | private function getAttribute($attribute) { |
640 | $cmd = "zpool list -H -o $attribute {$this->name}"; | |
641 | OMVUtil::exec($cmd, $output, $result); | |
642 | if ($result) { | |
643 | $cmd = "zfs list -H -o $attribute {$this->name}"; | |
644 | OMVUtil::exec($cmd, $output, $result); | |
645 | if ($result) | |
646 | return null; | |
647 | } | |
648 | ||
649 | return $output; | |
f891182f MR |
650 | } |
651 | ||
e39afba8 MR |
652 | /** |
653 | * Get all attributes from pool | |
654 | * @return array of attributes | |
5e58d5e2 | 655 | * @throws OMVModuleZFSException |
e39afba8 MR |
656 | */ |
657 | private function getAllAttributes() { | |
658 | $attrs = array(); | |
659 | $cmd = "zfs get -H all {$this->name}"; | |
660 | ||
503ffcc8 MR |
661 | try { |
662 | OMVUtil::exec($cmd, $output, $result); | |
663 | } catch (OMVModuleZFSException $e) {} | |
e39afba8 MR |
664 | if ($result) |
665 | throw new OMVModuleZFSException($output); | |
9be18e88 | 666 | $output = implode("\n", $output); |
503ffcc8 | 667 | $res = preg_match_all("/{$this->name}\s+(\w+)\s+([\w\d\.]+)\s+(\w+).*/", $output, $matches, PREG_SET_ORDER); |
e39afba8 MR |
668 | if ($res == false || $res == 0) |
669 | throw new OMVModuleZFSException("Error return by zpool get all: $output"); | |
670 | foreach ($matches as $match) { | |
503ffcc8 | 671 | $attrs[$match[1]] = array('value' => $match[2], 'source' => $match[3]); |
e39afba8 MR |
672 | } |
673 | ||
674 | return $attrs; | |
675 | } | |
676 | ||
503ffcc8 MR |
677 | /** |
678 | * Get all Dataset properties from commandline and update object properties attribute | |
679 | * | |
680 | * @return void | |
681 | * @access private | |
682 | */ | |
683 | private function updateAllProperties() { | |
684 | $this->features = $this->getAllAttributes(); | |
685 | } | |
686 | ||
f891182f | 687 | /** |
b76f4e17 | 688 | * Remove a disk from array |
f891182f | 689 | * |
b76f4e17 MR |
690 | * @param array $array |
691 | * @param string $disk | |
692 | * @return array | |
f891182f | 693 | */ |
b76f4e17 MR |
694 | private function removeDisk(array $array, $disk) { |
695 | $new_disks = array(); | |
696 | ||
697 | foreach ($array as $item) { | |
698 | if (strcmp($item, $disk) != 0) | |
699 | array_push ($new_disks, $item); | |
700 | } | |
701 | ||
702 | return $new_disks; | |
f891182f | 703 | } |
5e58d5e2 MR |
704 | |
705 | /** | |
706 | * Construct existing pool | |
707 | * | |
708 | * @param string $name | |
709 | * @return void | |
710 | * @throws OMVModuleZFSException | |
711 | */ | |
712 | private function assemblePool($name) { | |
9be18e88 | 713 | $cmd = "zpool status -v $name"; |
5e58d5e2 MR |
714 | $types = 'mirror|raidz1|raidz2|raidz3'; |
715 | $dev = null; | |
716 | $type = null; | |
717 | $log = false; | |
718 | $cache = false; | |
719 | $start = true; | |
720 | ||
721 | OMVUtil::exec($cmd, $output, $result); | |
722 | if ($result) | |
723 | throw new OMVModuleZFSException($output); | |
5e58d5e2 MR |
724 | |
725 | $this->name = $name; | |
9be18e88 MR |
726 | foreach($output as $line) { |
727 | if (! strstr($line, PHP_EOL)) | |
728 | $line .= PHP_EOL; | |
5e58d5e2 | 729 | if ($start) { |
9be18e88 MR |
730 | if (preg_match("/^\s*NAME/", $line)) |
731 | $start = false; | |
732 | continue; | |
5e58d5e2 MR |
733 | } else { |
734 | if (preg_match("/^\s*$/", $line)) { | |
735 | if ($dev) { | |
9be18e88 | 736 | $this->output($part, $type, $dev); |
5e58d5e2 MR |
737 | } |
738 | break; | |
739 | } else if (preg_match("/^\s*($name|logs|cache|spares)/", $line, $match)) { | |
740 | if ($dev) { | |
9be18e88 | 741 | $this->output($part, $type, $dev); |
5e58d5e2 MR |
742 | $dev = null; |
743 | $type = null; | |
744 | } | |
745 | $part = $match[1]; | |
746 | } else { | |
747 | switch ($part) { | |
748 | case $name: | |
749 | if (preg_match("/^\s*($types)/", $line, $match)) { | |
750 | /* new vdev */ | |
751 | if ($type) { | |
9be18e88 MR |
752 | $this->output(null, $type, $dev); |
753 | $dev = null; | |
5e58d5e2 MR |
754 | } |
755 | $type = $match[1]; | |
aec28e5f | 756 | } else if (preg_match("/^\s*([\w\d-a-z0-9\:\.\-]+)\s+/", $line, $match)) { |
5e58d5e2 MR |
757 | if ($dev) |
758 | $dev .= " $match[1]"; | |
759 | else | |
760 | $dev = "$match[1]"; | |
761 | } | |
762 | break; | |
763 | case 'logs': | |
9be18e88 | 764 | if (preg_match("/^\s*([\w\d-]+)\s+/", $line, $match)) { |
5e58d5e2 MR |
765 | if ($dev) |
766 | $dev .= " $match[1]"; | |
767 | else | |
768 | $dev = "$match[1]"; | |
769 | } | |
770 | break; | |
771 | case 'cache': | |
772 | case 'spares': | |
9be18e88 | 773 | if (preg_match("/^\s*([\w\d-]+)\s+/", $line, $match)) { |
5e58d5e2 MR |
774 | if ($dev) |
775 | $dev .= " $match[1]"; | |
776 | else | |
777 | $dev = "$match[1]"; | |
778 | } | |
779 | break; | |
780 | default: | |
781 | throw new Exception("$part: Unknown pool part"); | |
782 | } | |
783 | } | |
784 | } | |
785 | } | |
786 | $this->size = $this->getAttribute("size"); | |
787 | $this->mountPoint = $this->getAttribute("mountpoint"); | |
788 | } | |
789 | ||
790 | /** | |
791 | * Create pool config from parsed input | |
792 | * | |
793 | * @param string $part | |
794 | * @param string $type | |
795 | * @param string $dev | |
796 | * @return void | |
797 | * @throws OMVModuleZFSException | |
798 | */ | |
799 | private function output($part, $type, $dev) { | |
800 | $disks = split(" ", $dev); | |
801 | switch ($part) { | |
802 | case 'logs': | |
803 | if ($type && $type != 'mirror') | |
804 | throw new Exception("$type: Logs can only be mirror or plain"); | |
805 | if ($type) | |
806 | $this->log = new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSMIRROR, $disks); | |
807 | else | |
808 | $this->log = new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSPLAIN, $disks); | |
809 | break; | |
810 | case 'cache': | |
811 | if ($type) | |
812 | throw new Exception("$type: cache can only be plain"); | |
813 | $this->cache = new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSPLAIN, $disks); | |
814 | break; | |
815 | case 'spares': | |
816 | if ($type) | |
817 | throw new Exception("$type: spares can only be plain"); | |
818 | $this->spare = new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSPLAIN, $disks); | |
819 | break; | |
820 | default: | |
821 | if ($type) { | |
822 | switch ($type) { | |
823 | case 'mirror': | |
824 | array_push($this->vdevs, new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSMIRROR, $disks)); | |
825 | $this->type = OMVModuleZFSVdevType::OMVMODULEZFSMIRROR; | |
826 | break; | |
827 | case 'raidz1': | |
828 | array_push($this->vdevs, new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ1, $disks)); | |
829 | $this->type = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ1; | |
830 | break; | |
831 | case 'raidz2': | |
832 | array_push($this->vdevs, new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ2, $disks)); | |
833 | $this->type = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ2; | |
834 | break; | |
835 | case 'raidz3': | |
836 | array_push($this->vdevs, new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ3, $disks)); | |
837 | $this->type = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ3; | |
838 | break; | |
839 | } | |
840 | } else { | |
841 | array_push($this->vdevs, new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSPLAIN, $disks)); | |
842 | $this->type = OMVModuleZFSVdevType::OMVMODULEZFSPLAIN; | |
843 | } | |
9be18e88 | 844 | break; |
5e58d5e2 MR |
845 | } |
846 | } | |
847 | ||
f891182f | 848 | } |
f891182f | 849 | ?> |