]> git.datanom.net - omvzfs.git/blame - src/Vdev.php
Initial implementation of GUI for adding ZFS pools plus minor bugfixes.
[omvzfs.git] / src / Vdev.php
CommitLineData
f891182f 1<?php
63617ac2
MR
2require_once("Exception.php");
3require_once("VdevType.php");
4require_once("openmediavault/util.inc");
f891182f
MR
5
6/**
63617ac2 7 * Contains a Vdev
f891182f 8 *
63617ac2
MR
9 * @author Michael Rasmussen
10 * @version 0.1
11 * @copyright Michael Rasmussen <mir@datanom.net>
f891182f 12 */
63617ac2 13class OMVModuleZFSVdev {
f891182f
MR
14 // Attributes
15 /**
63617ac2 16 * Array holding disks
f891182f 17 *
63617ac2 18 * @var array $disks
f891182f
MR
19 * @access private
20 */
63617ac2
MR
21 private $disks;
22
23 /**
24 * Name of pool
25 *
26 * @var string $pool pool name
27 * @access private
28 */
29 private $pool;
30
31 /**
aa5925ea 32 * This vdev type
63617ac2
MR
33 *
34 * @var OMVModuleZFSVdevType $type Vdev type
35 * @access private
36 */
37 private $type;
f891182f
MR
38
39 // Associations
40 // Operations
63617ac2
MR
41 /**
42 * Constructor
43 *
44 * @param $pool pool this mirror belongs to
b76f4e17 45 * @throws OMVModuleZFSException
63617ac2
MR
46 */
47
1a609c51 48 public function __construct($pool, $type, array $disks) {
63617ac2
MR
49 switch ($type) {
50 case OMVModuleZFSVdevType::OMVMODULEZFSPLAIN:
51 break;
52 case OMVModuleZFSVdevType::OMVMODULEZFSMIRROR:
53 if (count($disks) < 2)
54 throw new OMVModuleZFSException("A mirror must contain at least 2 disks");
55 break;
56 case OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ1:
57 if (count($disks) < 3)
58 throw new OMVModuleZFSException("A Raidz1 must contain at least 3 disks");
59 break;
60 case OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ2:
61 if (count($disks) < 4)
62 throw new OMVModuleZFSException("A Raidz2 must contain at least 4 disks");
63 break;
64 case OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ3:
65 if (count($disks) < 5)
66 throw new OMVModuleZFSException("A Raidz3 must contain at least 5 disks");
67 break;
1a609c51
MR
68 default:
69 throw new OMVModuleZFSException("$type: Unknown zpool type");
63617ac2
MR
70 }
71 $this->pool = $pool;
72 $this->disks = $disks;
73 $this->type = $type;
74 }
75
76 /**
77 * Helper function to execute an external program.
78 * @param command The command that will be executed.
79 * @param output If the output argument is present, then the specified
80 * array will be filled with every line of output from the command.
81 * Trailing whitespace, such as \n, is not included in this array.
82 * @return The exit code of the command.
83 * @throws E_EXEC_FAILED
84 */
85 private function exec($command, &$output = NULL) {
86 OMVUtil::exec($command, $output, $result);
87 return $result;
88 }
89
90 private function safeExec($disk, $add = true, $change = false) {
91 $result = 1;
92
93 if ($add) {
94 if ($change || $this->type == OMVModuleZFSVdevType::OMVMODULEZFSMIRROR) {
95 $disk1 = $this->disks[0];
96 $result = exec("zpool attach {$this->pool} $disk1 $disk", $err);
97 } else {
98 $result = exec("zpool add {$this->pool} $disk", $err);
99 }
100 } else {
101 if ($this->type == OMVModuleZFSVdevType::OMVMODULEZFSMIRROR) {
102 $disk1 = $this->disks[0];
103 if (($res = exec("zpool offline {$this->pool} $disk", $err)) > 0)
104 $result = $res;
105 else
106 $result = exec("zpool detach {$this->pool} $disk", $err);
107 } else {
108 $result = 1;
109 $err = "Cannot remove $disk from {$this->pool}";
110 }
111 }
112
113 return ($result) ? $err : null;
114 }
115
f891182f 116 /**
63617ac2 117 * Add a disk to this Vdev
f891182f 118 *
63617ac2
MR
119 * @param $disk the disk
120 * @throws OMVModuleZFSException
f891182f
MR
121 * @access public
122 */
63617ac2
MR
123 public function addDisk($disk, $changeType = false) {
124 if ($this->type != OMVModuleZFSVdevType::OMVMODULEZFSPLAIN ||
125 $this->type != OMVModuleZFSVdevType::OMVMODULEZFSMIRROR)
126 throw new OMVModuleZFSException("A Raidz Vdev cannot be changed");
127
128 if (in_array($disk, $this->disks))
129 throw new OMVModuleZFSException("$disk: Already part of Vdev");
130
131 if ($this->type == OMVModuleZFSVdevType::OMVMODULEZFSPLAIN &&
132 count($this->disks) < 2 && $changeType) {
133 $this->type = OMVModuleZFSVdevType::OMVMODULEZFSMIRROR;
134 }
135
136 if (($err = safeExec($disk, true, $changeType)) != null)
137 throw new OMVModuleZFSException($err);
138 else
139 array_push($this->disks, $disk);
f891182f
MR
140 }
141
142 /**
63617ac2 143 * Remove a disk from Vdev
f891182f 144 *
63617ac2
MR
145 * @param $disk disk to remove
146 * @throws OMVModuleZFSException
f891182f
MR
147 * @access public
148 */
63617ac2
MR
149 public function removeDisk($disk, $changeType = false) {
150 $new_disks = array();
151
152 if ($this->type != OMVModuleZFSVdevType::OMVMODULEZFSMIRROR)
153 throw new OMVModuleZFSException("Only inactive hot spares," .
154 "cache, top-level, or log devices can be removed");
155
156 if (count($this->disks) < 3 && ! $changeType)
157 throw new OMVModuleZFSException("A mirror must contain at least 2 disks");
158
159 if (! in_array($disk, $this->disks))
160 throw new OMVModuleZFSException("$disk: Not part of Vdev");
161
162 if (($err = safeExec($disk, false, $changeType)) != null)
163 throw new OMVModuleZFSException($err);
164 else {
165 foreach ($this->disks as $_disk) {
166 if (strcmp($_disk, $disk) != 0)
167 array_push($new_disks, $_disk);
168 }
169 }
170
171 $this->disks = $new_disks;
172 }
f891182f
MR
173
174 /**
63617ac2 175 * Get disk array
f891182f 176 *
63617ac2 177 * @return array with disks
f891182f
MR
178 * @access public
179 */
180 public function getDisks() {
63617ac2
MR
181 return $this->disks;
182 }
183
184 /**
185 * Get pool
186 *
187 * @return string pool
188 * @access public
189 */
190 public function getPool() {
42856e8b 191 return $this->pool;
f891182f
MR
192 }
193
b76f4e17
MR
194 /**
195 * Get type
196 *
197 * @return OMVModuleZFSVdevType
198 * @access public
199 */
200 public function getType() {
42856e8b 201 return $this->type;
b76f4e17
MR
202 }
203
f891182f
MR
204}
205
206?>
This page took 0.081994 seconds and 5 git commands to generate.