]> git.datanom.net - omvzfs.git/blobdiff - src/Zpool.php
Made it possible to force create Vdevs.
[omvzfs.git] / src / Zpool.php
index f336ee60f84cae93e2efc36a58af8ade22ba52bf..56ec8c64d4426e98d239710d5e3d8b70448c24d5 100644 (file)
@@ -1,8 +1,12 @@
 <?php
+require_once('openmediavault/object.inc');
+require_once('openmediavault/module.inc');
 require_once("Vdev.php");
 require_once("Snapshot.php");
 require_once("Dataset.php");
 require_once("Zvol.php");
+require_once("VdevType.php");
+require_once("Utils.php");
 require_once("Exception.php");
 
 /**
@@ -12,8 +16,7 @@ require_once("Exception.php");
  * @version   0.1
  * @copyright Michael Rasmussen <mir@datanom.net>
  */
-class OMVModuleZFSZpool extends OMVModuleAbstract
-               implements OMVINotifyListener {
+class OMVModuleZFSZpool extends OMVModuleAbstract {
     // Attributes
     /**
      * Name of pool
@@ -119,7 +122,7 @@ class OMVModuleZFSZpool extends OMVModuleAbstract
      * @throws OMVModuleZFSException
         */
 
-       public function __construct($vdev) {
+       public function __construct($vdev, $opts = "") {
                $create_pool = true;
 
                if (is_array($vdev)) {
@@ -141,11 +144,11 @@ class OMVModuleZFSZpool extends OMVModuleAbstract
                $this->cache = null;
                $this->features = array();
                if ($create_pool) {
-                       $cmd = "zpool create $name $cmd";
+                       $cmd = "zpool create $opts$name $cmd 2>&1";
 
                        OMVUtil::exec($cmd, $output, $result);
                        if ($result)
-                               throw new OMVModuleZFSException($output);
+                               throw new OMVModuleZFSException(implode("\n", $output));
                        else {
                                $this->name = $name;
                                $this->type = $type;
@@ -189,11 +192,11 @@ class OMVModuleZFSZpool extends OMVModuleAbstract
      * @throws OMVModuleZFSException
      * @access public
      */
-    public function addVdev(array $vdevs) {
-               $cmd = "zpool add " . $this->name . " " . $this->getCommandString($vdevs);
+    public function addVdev(array $vdevs, $opts= "") {
+               $cmd = "zpool add " . $this->name . " " . $opts . $this->getCommandString($vdevs) . " 2>&1";
                OMVUtil::exec($cmd, $output, $result);
                if ($result)
-                       throw new OMVModuleZFSException($output);
+                       throw new OMVModuleZFSException(implode("\n", $output));
                else
                        $this->vdevs = array_merge($this->vdevs, $vdevs);
                $this->size = $this->getAttribute("size");
@@ -428,7 +431,7 @@ class OMVModuleZFSZpool extends OMVModuleAbstract
      * @return array of features
      * @access public
      */
-    public function getFeatures() {
+    public function getFeatures($internal = true) {
                $attrs = array();
                $featureSet = array(
                        'recordsize', /* default 131072. 512 <= n^2 <=  131072*/
@@ -444,11 +447,18 @@ class OMVModuleZFSZpool extends OMVModuleAbstract
                        'dedup', /* on | off */
                        'sync' /* standard | always | disabled */
                );
-               if (array_count_values($this->features) < 1)
-                       $this->features = getAllAttributes();
-        foreach ($this->features as $attr => $val) {
-                       if (in_array($attr, $featureSet))
-                               $attrs[$attr] = $val;
+               if (count($this->features) < 1)
+                       $this->features = $this->getAllAttributes();
+               if ($internal) {
+                       foreach ($this->features as $attr => $val) {
+                               if (in_array($attr, $featureSet))
+                                       $attrs[$attr] = $val['value'];
+                       }
+               } else {
+                       foreach ($this->features as $attr => $val) {
+                               if (in_array($attr, $featureSet))
+                                       $attrs[$attr] = $val;
+                       }
                }
 
                return $attrs;
@@ -514,68 +524,71 @@ class OMVModuleZFSZpool extends OMVModuleAbstract
                        throw new OMVModuleZFSException($output);
     }
 
-    public function bindListeners(OMVNotifyDispatcher $dispatcher) {
-               // Update service if configuration has been modified
-               $dispatcher->addListener(
-                 OMV_NOTIFY_MODIFY,
-                 "org.openmediavault.services.nfs",
-                 array($this, "onUpdateNFSService"));
-               $dispatcher->addListener(
-                 OMV_NOTIFY_CREATE,
-                 "org.openmediavault.services.nfs.shares.share",
-                 array($this, "onCreateNFSShare"));
-               $dispatcher->addListener(
-                 OMV_NOTIFY_DELETE,
-                 "org.openmediavault.services.nfs.shares.share",
-                 array($this, "onDeleteNFSShare"));
-               $dispatcher->addListener(
-                 OMV_NOTIFY_MODIFY,
-                 "org.openmediavault.services.nfs.shares.share",
-                 array($this, "onUpdateNFSShare"));
-    }
+       /**
+        * Get a single property value associated with the Dataset
+        *
+        * @param string $property Name of the property to fetch
+        * @return array The returned array with the property. The property is an associative array with
+        * two elements, <value> and <source>.
+        * @access public
+        */
+       public function getProperty($property) {
+               $attrs = $this->getFeatures(false);
+               return $attrs["$property"];
+       }
 
        /**
-        * XXX
-        * org.openmediavault.services.nfs
+        * Get an associative array of all properties associated with the Snapshot
         *
-        * @param string event
+        * @return array $properties Each entry is an associative array with two elements
+        * <value> and <source>
         * @access public
         */
-       public function onUpdateNFSService($args) {
-        $this->debug(sprintf("onUpdateNFSService args=%s", var_export($args, true)));
+       public function getProperties() {
+               $attrs = $this->getFeatures(false);
+               return $attrs;
        }
 
        /**
-        * XXX
-        * org.openmediavault.services.nfs.shares.share
+        * Sets a number of Dataset properties. If a property is already set it will be updated with the new value.
         *
-        * @param string event
+        * @param  array $properties An associative array with properties to set
+        * @return void
         * @access public
         */
-       public function onCreateNFSShare($args) {
-        $this->debug(sprintf("onCreateNFSShare args=%s", var_export($args, true)));
+       public function setProperties($properties) {
+               foreach ($properties as $newpropertyk => $newpropertyv) {
+                       $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name . " 2>&1";
+                       OMVModuleZFSUtil::exec($cmd,$out,$res);
+                       $attr = $this->getAttribute($newpropertyk);
+                       $this->features[$newpropertyk] = $attr;
+               }
        }
 
        /**
-        * XXX
-        * org.openmediavault.services.nfs.shares.share
+        * Destroy the Dataset.
         *
-        * @param string event
+        * @return void
         * @access public
         */
-       public function onDeleteNFSShare($args) {
-        $this->debug(sprintf("onDeleteNFSShare args=%s", var_export($args, true)));
+       public function destroy() {
+               $cmd = "zpool destroy " . $this->name . " 2>&1";
+               $this->exec($cmd,$out,$res);
        }
 
        /**
-        * XXX
-        * org.openmediavault.services.nfs.shares.share
+        * Clears a previously set proporty and specifies that it should be
+        * inherited from it's parent.
         *
-        * @param string event
+        * @param string $property Name of the property to inherit.
+        * @return void
         * @access public
         */
-       public function onUpdateNFSShare($args) {
-        $this->debug(sprintf("onUpdateNFSShare args=%s", var_export($args, true)));
+       public function inherit($property) {
+               $cmd = "zfs inherit " . $property . " " . $this->name . " 2>&1";
+               $this->exec($cmd,$out,$res);
+               $attr = $this->getAttribute($newpropertyk);
+               $this->features[$newpropertyk] = $attr;
        }
 
        /**
@@ -614,7 +627,7 @@ class OMVModuleZFSZpool extends OMVModuleAbstract
                        array_push ($adds, $command . $diskStr);
                }
 
-               return join(" ", $adds);
+               return implode(" ", $adds);
        }
 
        /**
@@ -623,7 +636,7 @@ class OMVModuleZFSZpool extends OMVModuleAbstract
         * @param string $attribute
         * @return string value
         */
-       private function getAttribute($attribute) {
+       public function getAttribute($attribute) {
                $cmd = "zpool list -H -o $attribute {$this->name}";
                OMVUtil::exec($cmd, $output, $result);
                if ($result) {
@@ -645,19 +658,32 @@ class OMVModuleZFSZpool extends OMVModuleAbstract
                $attrs = array();
                $cmd = "zfs get -H all {$this->name}";
 
-               OMVUtil::exec($cmd, $output, $result);
+               try {
+                       OMVUtil::exec($cmd, $output, $result);
+               } catch (OMVModuleZFSException $e) {}
                if ($result)
                        throw new OMVModuleZFSException($output);
-               $res = preg_match_all("/$pool\s+(\w+)\s+([\w\d\.]+).*/", $output, $matches, PREG_SET_ORDER);
+               $output = implode("\n", $output);
+               $res = preg_match_all("/{$this->name}\s+(\w+)\s+([\w\d\.]+)\s+(\w+).*/", $output, $matches, PREG_SET_ORDER);
                if ($res == false || $res == 0)
                        throw new OMVModuleZFSException("Error return by zpool get all: $output");
                foreach ($matches as $match) {
-                       $attrs[$match[1]] = $match[2];
+                       $attrs[$match[1]] = array('value' => $match[2], 'source' => $match[3]);
                }
 
                return $attrs;
        }
 
+       /**
+        * Get all Dataset properties from commandline and update object properties attribute
+        *
+        * @return void
+        * @access private
+        */
+       private function updateAllProperties() {
+               $this->features = $this->getAllAttributes();
+       }
+
        /**
         * Remove a disk from array
         *
@@ -684,7 +710,7 @@ class OMVModuleZFSZpool extends OMVModuleAbstract
         * @throws OMVModuleZFSException
         */
        private function assemblePool($name) {
-               $cmd = "zpool list -Hv $name";
+               $cmd = "zpool status -v $name";
                $types = 'mirror|raidz1|raidz2|raidz3';
                $dev = null;
                $type = null;
@@ -695,26 +721,24 @@ class OMVModuleZFSZpool extends OMVModuleAbstract
                OMVUtil::exec($cmd, $output, $result);
                if ($result)
                        throw new OMVModuleZFSException($output);
-               $res = preg_match("/$name\s+([\w\d]+)\s+.*/", $output, $matches);
-               if ($res == false || $res == 0)
-                       throw new OMVModuleZFSException("Error return by zpool list: $output");
 
                $this->name = $name;
-               $lines = split("\n", $output);
-               foreach($lines as $line) {
+               foreach($output as $line) {
+                       if (! strstr($line, PHP_EOL))
+                               $line .= PHP_EOL;
                        if ($start) {
-                               if (preg_match("/^\s*NAME/", $line))
-                                       $start = false;
-                               continue;
+                                       if (preg_match("/^\s*NAME/", $line))
+                                                       $start = false;
+                                       continue;
                        } else {
                                if (preg_match("/^\s*$/", $line)) {
                                        if ($dev) {
-                                               output($part, $type, $dev);
+                                               $this->output($part, $type, $dev);
                                        }
                                        break;
                                } else if (preg_match("/^\s*($name|logs|cache|spares)/", $line, $match)) {
                                        if ($dev) {
-                                               output($part, $type, $dev);
+                                               $this->output($part, $type, $dev);
                                                $dev = null;
                                                $type = null;
                                        }
@@ -725,11 +749,11 @@ class OMVModuleZFSZpool extends OMVModuleAbstract
                                                        if (preg_match("/^\s*($types)/", $line, $match)) {
                                                                /* new vdev */
                                                                if ($type) {
-                                                                               output(null, $type, $dev);
-                                                                               $dev = null;
+                                                                       $this->output(null, $type, $dev);
+                                                                       $dev = null;
                                                                }
                                                                $type = $match[1];
-                                                       } else if (preg_match("/^\s*([\w\d]+)\s+/", $line, $match)) {
+                                                       } else if (preg_match("/^\s*([\w\d-a-z0-9\:\.\-]+)\s+/", $line, $match)) {
                                                                if ($dev)
                                                                        $dev .= " $match[1]";
                                                                else
@@ -737,7 +761,7 @@ class OMVModuleZFSZpool extends OMVModuleAbstract
                                                        }
                                                        break;
                                                case 'logs':
-                                                       if (preg_match("/^\s*([\w\d]+)\s+/", $line, $match)) {
+                                                       if (preg_match("/^\s*([\w\d-]+)\s+/", $line, $match)) {
                                                                if ($dev)
                                                                        $dev .= " $match[1]";
                                                                else
@@ -746,7 +770,7 @@ class OMVModuleZFSZpool extends OMVModuleAbstract
                                                        break;
                                                case 'cache':
                                                case 'spares':
-                                                       if (preg_match("/^\s*([\w\d]+)\s+/", $line, $match)) {
+                                                       if (preg_match("/^\s*([\w\d-]+)\s+/", $line, $match)) {
                                                                if ($dev)
                                                                        $dev .= " $match[1]";
                                                                else
@@ -817,9 +841,9 @@ class OMVModuleZFSZpool extends OMVModuleAbstract
                                        array_push($this->vdevs, new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSPLAIN, $disks));
                                        $this->type = OMVModuleZFSVdevType::OMVMODULEZFSPLAIN;
                                }
+                       break;
                }
        }
 
 }
-
 ?>
This page took 0.048162 seconds and 5 git commands to generate.