+ /**
+ * Constructor
+ *
+ * @param $pool pool this mirror belongs to
+ * @throws OMVModuleZFSException
+ */
+
+ public function __construct($pool, $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;
+ default:
+ throw new OMVModuleZFSException("$type: Unknown zpool type");
+ }
+ $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;
+ }
+