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