+
+ /**
+ * Construct existing pool
+ *
+ * @param string $name
+ * @return void
+ * @throws OMVModuleZFSException
+ */
+ private function assemblePool($name) {
+ $cmd = "zpool status -v $name";
+ $types = 'mirror|raidz1|raidz2|raidz3';
+ $dev = null;
+ $type = null;
+ $log = false;
+ $cache = false;
+ $start = true;
+
+ OMVUtil::exec($cmd, $output, $result);
+ if ($result)
+ throw new OMVModuleZFSException($output);
+
+ $this->name = $name;
+ foreach($output as $line) {
+ if (! strstr($line, PHP_EOL))
+ $line .= PHP_EOL;
+ if ($start) {
+ if (preg_match("/^\s*NAME/", $line))
+ $start = false;
+ continue;
+ } else {
+ if (preg_match("/^\s*$/", $line)) {
+ if ($dev) {
+ $this->output($part, $type, $dev);
+ }
+ break;
+ } else if (preg_match("/^\s*($name|logs|cache|spares)/", $line, $match)) {
+ if ($dev) {
+ $this->output($part, $type, $dev);
+ $dev = null;
+ $type = null;
+ }
+ $part = $match[1];
+ } else {
+ switch ($part) {
+ case $name:
+ if (preg_match("/^\s*($types)/", $line, $match)) {
+ /* new vdev */
+ if ($type) {
+ $this->output(null, $type, $dev);
+ $dev = null;
+ }
+ $type = $match[1];
+ } else if (preg_match("/^\s*([\w\d-]+)\s+/", $line, $match)) {
+ if ($dev)
+ $dev .= " $match[1]";
+ else
+ $dev = "$match[1]";
+ }
+ break;
+ case 'logs':
+ if (preg_match("/^\s*([\w\d-]+)\s+/", $line, $match)) {
+ if ($dev)
+ $dev .= " $match[1]";
+ else
+ $dev = "$match[1]";
+ }
+ break;
+ case 'cache':
+ case 'spares':
+ if (preg_match("/^\s*([\w\d-]+)\s+/", $line, $match)) {
+ if ($dev)
+ $dev .= " $match[1]";
+ else
+ $dev = "$match[1]";
+ }
+ break;
+ default:
+ throw new Exception("$part: Unknown pool part");
+ }
+ }
+ }
+ }
+ $this->size = $this->getAttribute("size");
+ $this->mountPoint = $this->getAttribute("mountpoint");
+ }
+
+ /**
+ * Create pool config from parsed input
+ *
+ * @param string $part
+ * @param string $type
+ * @param string $dev
+ * @return void
+ * @throws OMVModuleZFSException
+ */
+ private function output($part, $type, $dev) {
+ $disks = split(" ", $dev);
+ switch ($part) {
+ case 'logs':
+ if ($type && $type != 'mirror')
+ throw new Exception("$type: Logs can only be mirror or plain");
+ if ($type)
+ $this->log = new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSMIRROR, $disks);
+ else
+ $this->log = new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSPLAIN, $disks);
+ break;
+ case 'cache':
+ if ($type)
+ throw new Exception("$type: cache can only be plain");
+ $this->cache = new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSPLAIN, $disks);
+ break;
+ case 'spares':
+ if ($type)
+ throw new Exception("$type: spares can only be plain");
+ $this->spare = new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSPLAIN, $disks);
+ break;
+ default:
+ if ($type) {
+ switch ($type) {
+ case 'mirror':
+ array_push($this->vdevs, new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSMIRROR, $disks));
+ $this->type = OMVModuleZFSVdevType::OMVMODULEZFSMIRROR;
+ break;
+ case 'raidz1':
+ array_push($this->vdevs, new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ1, $disks));
+ $this->type = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ1;
+ break;
+ case 'raidz2':
+ array_push($this->vdevs, new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ2, $disks));
+ $this->type = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ2;
+ break;
+ case 'raidz3':
+ array_push($this->vdevs, new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ3, $disks));
+ $this->type = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ3;
+ break;
+ }
+ } else {
+ array_push($this->vdevs, new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSPLAIN, $disks));
+ $this->type = OMVModuleZFSVdevType::OMVMODULEZFSPLAIN;
+ }
+ break;
+ }
+ }
+