]>
git.datanom.net - omvzfs.git/blob - src/Zpool.php
2 require_once("Vdev.php");
3 require_once("Snapshot.php");
4 require_once("Dataset.php");
5 require_once("Zvol.php");
6 require_once("Exception.php");
9 * Class containing information about the pool
11 * @author Michael Rasmussen
13 * @copyright Michael Rasmussen <mir@datanom.net>
15 class OMVModuleZFSZpool
extends OMVModuleAbstract
16 implements OMVNotifyListener
{
31 * @association OMVModuleZFSVdev to vdevs
40 * @association OMVModuleZFSVdev to spare
49 * @association OMVModuleZFSVdev to log
58 * @association OMVModuleZFSVdev to cache
73 * @var string $mountPoint
81 * @var array $features
88 * Array of OMVModuleZFSSnapshot.
90 * @var array $snapshot
92 * @association OMVModuleZFSSnapshot to snapshot
97 * Array of OMVModuleZFSDataset
99 * @var Dataset $dataset
101 * @association OMVModuleZFSDataset to dataset
106 * Array of OMVModuleZFSZvol
110 * @association OMVModuleZFSZvol to zvol
118 * @param $vdev OMVModuleZFSVdev or array(OMVModuleZFSVdev)
119 * @throws OMVModuleZFSException
122 public function __construct($vdev) {
123 if (is_array($vdev)) {
124 $cmd = $this->getCommandString($vdev);
125 $name = $vdev[0]->getPool();
126 $type = $vdev[0]->getType();
129 $cmd = $this->getCommandString(array($vdev));
130 $name = $vdev->getPool();
131 $type = $vdev->getType();
133 $cmd = "zpool create $name $cmd";
135 OMVUtil
::exec($cmd, $output, $result);
137 throw new OMVModuleZFSException($output);
139 $this->vdevs
= array();
140 $this->spare
= array();
141 $this->log
= array();
142 $this->cache
= array();
143 $this->features
= array();
147 $this->vdevs
= $vdev;
149 array_push ($this->vdevs
, $vdev);
150 $this->size
= $this->getAttribute("size");
151 $this->mountPoint
= $this->getAttribute("mountpoint");
161 public function getName() {
171 public function getVdevs() {
178 * @param array $vdev array of OMVModuleZFSVdev
180 * @throws OMVModuleZFSException
183 public function addVdev(array $vdevs) {
184 $cmd = "zpool add " . $this->name
. " " . $this->getCommandString($vdevs);
185 OMVUtil
::exec($cmd, $output, $result);
187 throw new OMVModuleZFSException($output);
189 $this->vdevs
= array_merge($this->vdevs
, $vdevs);
190 $this->size
= $this->getAttribute("size");
196 * @param OMVModuleZFSVdev $vdev
198 * @throws OMVModuleZFSException
201 public function removeVdev(OMVModuleZFSVdev
$vdev) {
202 throw new OMVModuleZFSException("Cannot remove vdevs from a pool");
208 * @param OMVModuleZFSVdev $cache
210 * @throws OMVModuleZFSException
213 public function addCache(OMVModuleZFSVdev
$cache) {
214 if ($cache->getType() != OMVModuleZFSVdevType
::OMVMODULEZFSPLAIN
)
215 throw new OMVModuleZFSException("Only a plain Vdev can be added as cache");
217 $cmd = "zpool add " . $this->name
. " cache " . $this->getCommandString($vdevs);
218 OMVUtil
::exec($cmd, $output, $result);
220 throw new OMVModuleZFSException($output);
222 $disks = $cache->getDisks();
223 foreach ($disks as $disk) {
224 array_push ($this->cache
, $disk);
231 * @param array $disks
233 * @throws OMVModuleZFSException
236 public function removeCache(array $disks = null) {
238 $disks = $this->cache
;
240 foreach ($disks as $disk)
241 $dist_str .= "$disk ";
243 $cmd = "zpool remove " . $this->name
. " $dist_str";
244 OMVUtil
::exec($cmd, $output, $result);
246 throw new OMVModuleZFSException($output);
248 foreach ($disks as $disk)
249 $this->cache
= $this->removeDisk($this->cache
, $disk);
259 public function getCache() {
266 * @param OMVModuleZFSVdev $log
270 public function addLog(OMVModuleZFSVdev
$log) {
271 if ($log->getType() == OMVModuleZFSVdevType
::OMVMODULEZFSPLAIN ||
272 $log->getType() == OMVModuleZFSVdevType
::OMVMODULEZFSMIRROR
) {
273 $cmd = "zpool add " . $this->name
. " log " . $this->getCommandString($vdevs);
274 OMVUtil
::exec($cmd, $output, $result);
276 throw new OMVModuleZFSException($output);
280 throw new OMVModuleZFSException("Only a plain Vdev or mirror Vdev can be added as log");
289 public function removeLog() {
290 foreach ($this->log
as $vdev) {
291 if ($vdev->getType() == OMVModuleZFSVdevType
::OMVMODULEZFSMIRROR
) {
292 $cmd = "zpool remove " . $this->name
. " mirror-$i";
294 $disks = $vdev->getDisks();
295 foreach ($disks as $disk)
296 $dist_str .= "$disk ";
297 $cmd = "zpool remove " . $this->name
. " $disk_str";
299 OMVUtil
::exec($cmd, $output, $result);
301 throw new OMVModuleZFSException($output);
303 $this->log
= array();
313 public function getLog() {
320 * @param OMVModuleZFSVdev $spares
324 public function addSpare(OMVModuleZFSVdev
$spares) {
325 if ($spares->getType() != OMVModuleZFSVdevType
::OMVMODULEZFSPLAIN
)
326 throw new OMVModuleZFSException("Only a plain Vdev can be added as spares");
328 $cmd = "zpool add " . $this->name
. " spare " . $this->getCommandString($vdevs);
329 OMVUtil
::exec($cmd, $output, $result);
331 throw new OMVModuleZFSException($output);
333 $disks = $spares->getDisks();
334 foreach ($disks as $disk) {
335 array_push ($this->spare
, $disk);
342 * @param array $disks
346 public function removeSpare(array $disks = null) {
348 $disks = $this->spare
;
350 foreach ($disks as $disk)
351 $dist_str .= "$disk ";
353 $cmd = "zpool remove " . $this->name
. " $dist_str";
354 OMVUtil
::exec($cmd, $output, $result);
356 throw new OMVModuleZFSException($output);
358 foreach ($disks as $disk)
359 $this->spare
= $this->removeDisk($this->spare
, $disk);
369 public function getSpares() {
379 public function getSize() {
389 public function getMountPoint() {
390 return $this->mountPoint
;
396 * @param array $features
400 public function setFeatures(array $features) {
401 foreach ($features as $feature => $value) {
402 $cmd = "zpool set $feature=$value " . $this->name
;
403 OMVUtil
::exec($cmd, $output, $result);
405 throw new OMVModuleZFSException($output);
407 $this->features
= $this->getAllAttributes();
411 * We only return array of features for which the user can
414 * @return array of features
417 public function getFeatures() {
420 'recordsize', /* default 131072. 512 <= n^2 <= 131072*/
421 'checksum', /* on | off */
422 'compression', /* off | lzjb | gzip | zle | lz4 */
423 'atime', /* on | off */
424 'aclmode', /* discard | groupmask | passthrough | restricted */
425 'aclinherit', /* discard | noallow | restricted | passthrough | passthrough-x */
426 'casesensitivity', /* sensitive | insensitive | mixed */
427 'primarycache', /* all | none | metadata */
428 'secondarycache', /* all | none | metadata */
429 'logbias', /* latency | throughput */
430 'dedup', /* on | off */
431 'sync' /* standard | always | disabled */
433 if (array_count_values($this->features
) < 1)
434 $this->features
= getAllAttributes();
435 foreach ($this->features
as $attr => $val) {
436 if (in_array($attr, $featureSet))
437 $attrs[$attr] = $val;
449 public function export() {
450 $cmd = "zpool export " . $this->name
;
451 OMVUtil
::exec($cmd, $output, $result);
453 throw new OMVModuleZFSException($output);
459 * @param string $name
463 public function import($name = null) {
465 $cmd = "zpool import $name";
467 $cmd = "zpool import";
468 OMVUtil
::exec($cmd, $output, $result);
470 throw new OMVModuleZFSException($output);
479 public function scrub() {
480 $cmd = "zpool scrub " . $this->name
;
481 OMVUtil
::exec($cmd, $output, $result);
483 throw new OMVModuleZFSException($output);
492 public function status() {
493 $cmd = "zpool status " . $this->name
;
494 OMVUtil
::exec($cmd, $output, $result);
496 throw new OMVModuleZFSException($output);
499 public function bindListeners(OMVNotifyDispatcher
$dispatcher) {
500 // Update service if configuration has been modified
501 $dispatcher->addListener(
503 "org.openmediavault.services.nfs",
504 array($this, "onUpdateNFSService"));
505 $dispatcher->addListener(
507 "org.openmediavault.services.nfs.shares.share",
508 array($this, "onCreateNFSShare"));
509 $dispatcher->addListener(
511 "org.openmediavault.services.nfs.shares.share",
512 array($this, "onDeleteNFSShare"));
513 $dispatcher->addListener(
515 "org.openmediavault.services.nfs.shares.share",
516 array($this, "onUpdateNFSShare"));
521 * org.openmediavault.services.nfs
523 * @param string event
526 public function onUpdateNFSService($args) {
527 $this->debug(sprintf("onUpdateNFSService args=%s", var_export($args, true)));
532 * org.openmediavault.services.nfs.shares.share
534 * @param string event
537 public function onCreateNFSShare($args) {
538 $this->debug(sprintf("onCreateNFSShare args=%s", var_export($args, true)));
543 * org.openmediavault.services.nfs.shares.share
545 * @param string event
548 public function onDeleteNFSShare($args) {
549 $this->debug(sprintf("onDeleteNFSShare args=%s", var_export($args, true)));
554 * org.openmediavault.services.nfs.shares.share
556 * @param string event
559 public function onUpdateNFSShare($args) {
560 $this->debug(sprintf("onUpdateNFSShare args=%s", var_export($args, true)));
564 * Convert array of Vdev to command string
566 * @param array $vdevs
568 * @throws OMVMODULEZFSException
570 private function getCommandString(array $vdevs) {
573 foreach ($vdevs as $vdev) {
574 if (is_object($vdev) == false)
575 throw new OMVMODULEZFSException("Not object of class OMVModuleZFSVdev");
576 if (is_a($vdev, OMVModuleZFSVdev
) == false)
577 throw new OMVMODULEZFSException("Object is not of class OMVModuleZFSVdev");
578 $type = $vdev->getType();
582 case OMVModuleZFSVdevType
::OMVMODULEZFSPLAIN
: break;
583 case OMVModuleZFSVdevType
::OMVMODULEZFSMIRROR
: $command = "mirror"; break;
584 case OMVModuleZFSVdevType
::OMVMODULEZFSRAIDZ1
: $command = "raidz1"; break;
585 case OMVModuleZFSVdevType
::OMVMODULEZFSRAIDZ2
: $command = "raidz2"; break;
586 case OMVModuleZFSVdevType
::OMVMODULEZFSRAIDZ3
: $command = "raidz3"; break;
588 throw new OMVMODULEZFSException("Unknown Vdev type");
590 $disks = $vdev->getDisks();
592 foreach($disks as $disk) {
593 $diskStr .= " $disk";
596 array_push ($adds, $command . $diskStr);
599 return join(" ", $adds);
603 * Get an attribute from pool
605 * @param string $attribute
606 * @return string value
608 private function getAttribute($attribute) {
609 $cmd = "zpool list -H -o $attribute {$this->name}";
610 OMVUtil
::exec($cmd, $output, $result);
612 $cmd = "zfs list -H -o $attribute {$this->name}";
613 OMVUtil
::exec($cmd, $output, $result);
622 * Get all attributes from pool
623 * @return array of attributes
625 private function getAllAttributes() {
627 $cmd = "zfs get -H all {$this->name}";
629 OMVUtil
::exec($cmd, $output, $result);
631 throw new OMVModuleZFSException($output);
632 $res = preg_match_all("/$pool\s+(\w+)\s+([\w\d\.]+).*/", $output, $matches, PREG_SET_ORDER
);
633 if ($res == false ||
$res == 0)
634 throw new OMVModuleZFSException("Error return by zpool get all: $output");
635 foreach ($matches as $match) {
636 $attrs[$match[1]] = $match[2];
643 * Remove a disk from array
645 * @param array $array
646 * @param string $disk
649 private function removeDisk(array $array, $disk) {
650 $new_disks = array();
652 foreach ($array as $item) {
653 if (strcmp($item, $disk) != 0)
654 array_push ($new_disks, $item);
This page took 0.248226 seconds and 6 git commands to generate.