]> git.datanom.net - omvzfs.git/commitdiff
Refactored to mimic zpool create
authorMichael Rasmussen <mir@datanom.net>
Fri, 28 Feb 2014 00:52:48 +0000 (01:52 +0100)
committerMichael Rasmussen <mir@datanom.net>
Fri, 28 Feb 2014 00:52:48 +0000 (01:52 +0100)
class-diagram.dia
src/Dataset.php
src/Exception.php [new file with mode: 0644]
src/Snapshot.php
src/Vdev.php
src/VdevType.php [new file with mode: 0644]
src/Zpool.php
src/Zvol.php

index c46a19b5231e8f727d78d4a3621bb410efafbad6..7c3b469c1a268f32c8ea0b410a1b117192443ae7 100644 (file)
Binary files a/class-diagram.dia and b/class-diagram.dia differ
index 210fc6ea2f54e57fcb82d2d458f881115fedf661..8cf2802e48921892bac95f63140cdc21ba6b2007 100644 (file)
@@ -7,7 +7,7 @@
  * @version   XXX
  * @copyright XXX
  */
-class Dataset {
+class OMVModuleZFSDataset {
     // Attributes
     /**
      * XXX
diff --git a/src/Exception.php b/src/Exception.php
new file mode 100644 (file)
index 0000000..9195025
--- /dev/null
@@ -0,0 +1,26 @@
+<?php
+
+/**
+ * OMVModuleZFSException class
+ *
+ * @author    Michael Rasmussen
+ * @version   0.1
+ * @copyright Michael Rasmussen <mir@datanom.net>
+ * @abstract
+ */
+class OMVModuleZFSException extends Exception {
+
+    /**
+     * XXX
+     *
+     * @param  Disk $disk XXX
+     * @return void XXX
+     * @access public
+     */
+    public function __construct(string $message = "", int $code = 0, Exception $previous = NULL) {
+        parent::__construct(string $message = "", int $code = 0, Exception $previous = NULL);
+    }
+
+}
+
+?>
index 33a469a9e3092bf3f94a1743ee1531c19af6bf0a..c47bf6c0b891f7c6273ce19d1a1939e8e1fa035a 100644 (file)
@@ -7,7 +7,7 @@
  * @version   XXX
  * @copyright XXX
  */
-class Snapshot {
+class OMVModuleZFSSnapshot {
     // Attributes
     /**
      * XXX
index e740edf7843529b39a11e08507998e225e0f6898..d6c0047fe87da15227ce156c23df4c931898f63e 100644 (file)
 <?php
+require_once("Exception.php");
+require_once("VdevType.php");
+require_once("openmediavault/util.inc");
 
 /**
- * XXX detailed description
+ * Contains a Vdev
  *
- * @author    XXX
- * @version   XXX
- * @copyright XXX
+ * @author    Michael Rasmussen
+ * @version   0.1
+ * @copyright Michael Rasmussen <mir@datanom.net>
  */
-class Vdev {
+class OMVModuleZFSVdev {
     // Attributes
     /**
-     * XXX
+     * Array holding disks
      *
-     * @var    list<Disk> $disks
+     * @var    array $disks
      * @access private
      */
-    protected $_disks;
+    private $disks;
+
+    /**
+     * Name of pool
+     *
+     * @var    string $pool pool name
+     * @access private
+     */
+    private $pool;
+
+    /**
+     * Array holding disks
+     *
+     * @var    OMVModuleZFSVdevType $type Vdev type
+     * @access private
+     */
+    private $type;
 
     // Associations
     // Operations
+       /**
+        * Constructor
+        *
+        * @param $pool pool this mirror belongs to
+        */
+
+       public function __construct($pool, OMVModuleZFSVdevType $type, array $disks) {
+               switch ($type) {
+                       case OMVModuleZFSVdevType::OMVMODULEZFSPLAIN:
+                               break;
+                       case OMVModuleZFSVdevType::OMVMODULEZFSMIRROR:
+                               if (count($disks) < 2)
+                                       throw new OMVModuleZFSException("A mirror must contain at least 2 disks");
+                               break;
+                       case OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ1:
+                               if (count($disks) < 3)
+                                       throw new OMVModuleZFSException("A Raidz1 must contain at least 3 disks");
+                               break;
+                       case OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ2:
+                               if (count($disks) < 4)
+                                       throw new OMVModuleZFSException("A Raidz2 must contain at least 4 disks");
+                               break;
+                       case OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ3:
+                               if (count($disks) < 5)
+                                       throw new OMVModuleZFSException("A Raidz3 must contain at least 5 disks");
+                               break;
+               }
+               $this->pool = $pool;
+               $this->disks = $disks;
+               $this->type = $type;
+       }
+
+       /**
+        * Helper function to execute an external program.
+        * @param command The command that will be executed.
+        * @param output If the output argument is present, then the specified
+        *   array will be filled with every line of output from the command.
+        *   Trailing whitespace, such as \n, is not included in this array.
+        * @return The exit code of the command.
+        * @throws E_EXEC_FAILED
+        */
+       private function exec($command, &$output = NULL) {
+               OMVUtil::exec($command, $output, $result);
+               return $result;
+       }
+
+       private function safeExec($disk, $add = true, $change = false) {
+               $result = 1;
+
+               if ($add) {
+                       if ($change || $this->type == OMVModuleZFSVdevType::OMVMODULEZFSMIRROR) {
+                               $disk1 = $this->disks[0];
+                               $result = exec("zpool attach {$this->pool} $disk1 $disk", $err);
+                       } else {
+                               $result = exec("zpool add {$this->pool} $disk", $err);
+                       }
+               } else {
+                       if ($this->type == OMVModuleZFSVdevType::OMVMODULEZFSMIRROR) {
+                               $disk1 = $this->disks[0];
+                               if (($res = exec("zpool offline {$this->pool} $disk", $err)) > 0)
+                                       $result = $res;
+                               else
+                                       $result = exec("zpool detach {$this->pool} $disk", $err);
+                       } else {
+                               $result = 1;
+                               $err = "Cannot remove $disk from {$this->pool}";
+                       }
+               }
+
+               return ($result) ? $err : null;
+       }
+
     /**
-     * XXX
+     * Add a disk to this Vdev
      *
-     * @param  Disk $disk XXX
-     * @return void XXX
+     * @param  $disk the disk
+     * @throws OMVModuleZFSException
      * @access public
      */
-    public function addDisk($disk) {
-        trigger_error('Not Implemented!', E_USER_WARNING);
+    public function addDisk($disk, $changeType = false) {
+               if ($this->type != OMVModuleZFSVdevType::OMVMODULEZFSPLAIN ||
+                               $this->type != OMVModuleZFSVdevType::OMVMODULEZFSMIRROR)
+                       throw new OMVModuleZFSException("A Raidz Vdev cannot be changed");
+
+               if (in_array($disk, $this->disks))
+                       throw new OMVModuleZFSException("$disk: Already part of Vdev");
+
+               if ($this->type == OMVModuleZFSVdevType::OMVMODULEZFSPLAIN &&
+                               count($this->disks) < 2 && $changeType) {
+                       $this->type = OMVModuleZFSVdevType::OMVMODULEZFSMIRROR;
+               }
+
+               if (($err = safeExec($disk, true, $changeType)) != null)
+                       throw new OMVModuleZFSException($err);
+               else
+                       array_push($this->disks, $disk);
     }
 
     /**
-     * XXX
+     * Remove a disk from Vdev
      *
-     * @param  Disk $disk XXX
-     * @return void XXX
+     * @param  $disk disk to remove
+     * @throws OMVModuleZFSException
      * @access public
      */
-    public abstract function removeDisk($disk) {
-        trigger_error('Not Implemented!', E_USER_WARNING);
-    }
+    public function removeDisk($disk, $changeType = false) {
+               $new_disks = array();
+
+               if ($this->type != OMVModuleZFSVdevType::OMVMODULEZFSMIRROR)
+                       throw new OMVModuleZFSException("Only inactive hot spares," .
+                               "cache, top-level, or log devices can be removed");
+
+               if (count($this->disks) < 3 && ! $changeType)
+                       throw new OMVModuleZFSException("A mirror must contain at least 2 disks");
+
+               if (! in_array($disk, $this->disks))
+                       throw new OMVModuleZFSException("$disk: Not part of Vdev");
+
+               if (($err = safeExec($disk, false, $changeType)) != null)
+                       throw new OMVModuleZFSException($err);
+               else {
+                       foreach ($this->disks as $_disk) {
+                               if (strcmp($_disk, $disk) != 0)
+                                       array_push($new_disks, $_disk);
+                       }
+               }
+
+               $this->disks = $new_disks;
+       }
 
     /**
-     * XXX
+     * Get disk array
      *
-     * @return list<Disk> XXX
+     * @return array with disks
      * @access public
      */
     public function getDisks() {
-        trigger_error('Not Implemented!', E_USER_WARNING);
+        return $this->disks;
+    }
+
+    /**
+     * Get pool
+     *
+     * @return string pool
+     * @access public
+     */
+    public function getPool() {
+        return $pool;
     }
 
 }
diff --git a/src/VdevType.php b/src/VdevType.php
new file mode 100644 (file)
index 0000000..81797b8
--- /dev/null
@@ -0,0 +1,45 @@
+<?php
+
+/**
+ * OMVModuleZFSVdevType class
+ *
+ * @author    Michael Rasmussen
+ * @version   0.1
+ * @copyright Michael Rasmussen <mir@datanom.net>
+ * @abstract
+ */
+abstract class OMVModuleZFSVdevType {
+
+    /**
+     * @var  OMVMODULEZFSPLAIN
+     * @access public
+     */
+       const OMVMODULEZFSPLAIN = 0;
+
+    /**
+     * @var  OMVMODULEZFSMIRROR
+     * @access public
+     */
+       const OMVMODULEZFSMIRROR = 1;
+
+    /**
+     * @var  OMVMODULEZFSRAIDZ1
+     * @access public
+     */
+       const OMVMODULEZFSRAIDZ1 = 2;
+
+    /**
+     * @var  OMVMODULEZFSRAIDZ2
+     * @access public
+     */
+       const OMVMODULEZFSRAIDZ2 = 3;
+
+    /**
+     * @var  OMVMODULEZFSRAIDZ3
+     * @access public
+     */
+       const OMVMODULEZFSRAIDZ3 = 4;
+
+}
+
+?>
index a2fee739939e34a540a5c2c7bbd753fb69d7092a..ef4f83589cef20db1bae111e7b69f0c897d8605c 100644 (file)
@@ -11,7 +11,7 @@ require_once 'Zvol.php';
  * @version   XXX
  * @copyright XXX
  */
-class Zpool {
+class OMVModuleZFSZpool {
     // Attributes
     /**
      * XXX
index b5b594146cd6166af0f961c54ffbcb8b8c091aad..286d6abf4f3e9710a67c1e10fcf368beaf3c2563 100644 (file)
@@ -7,7 +7,7 @@
  * @version   XXX
  * @copyright XXX
  */
-class Zvol {
+class OMVModuleZFSZvol {
     // Attributes
     /**
      * XXX
This page took 0.050121 seconds and 5 git commands to generate.