]>
Commit | Line | Data |
---|---|---|
f891182f | 1 | <?php |
9be18e88 MR |
2 | require_once('openmediavault/object.inc'); |
3 | require_once('openmediavault/module.inc'); | |
b76f4e17 MR |
4 | require_once("Vdev.php"); |
5 | require_once("Snapshot.php"); | |
6 | require_once("Dataset.php"); | |
7 | require_once("Zvol.php"); | |
8 | require_once("Exception.php"); | |
f891182f MR |
9 | |
10 | /** | |
b76f4e17 | 11 | * Class containing information about the pool |
f891182f | 12 | * |
b76f4e17 MR |
13 | * @author Michael Rasmussen |
14 | * @version 0.1 | |
15 | * @copyright Michael Rasmussen <mir@datanom.net> | |
f891182f | 16 | */ |
b76f4e17 | 17 | class OMVModuleZFSZpool extends OMVModuleAbstract |
3c87a106 | 18 | implements OMVINotifyListener { |
f891182f MR |
19 | // Attributes |
20 | /** | |
b76f4e17 | 21 | * Name of pool |
f891182f MR |
22 | * |
23 | * @var string $name | |
24 | * @access private | |
25 | */ | |
b76f4e17 | 26 | private $name; |
f891182f MR |
27 | |
28 | /** | |
b76f4e17 | 29 | * List of Vdev |
f891182f | 30 | * |
b76f4e17 | 31 | * @var array $vdevs |
f891182f | 32 | * @access private |
e39afba8 | 33 | * @association OMVModuleZFSVdev to vdevs |
f891182f | 34 | */ |
b76f4e17 | 35 | private $vdevs; |
f891182f MR |
36 | |
37 | /** | |
b76f4e17 | 38 | * List of spares |
f891182f | 39 | * |
b76f4e17 | 40 | * @var array $spare |
f891182f | 41 | * @access private |
e39afba8 | 42 | * @association OMVModuleZFSVdev to spare |
f891182f | 43 | */ |
b76f4e17 | 44 | private $spare; |
f891182f MR |
45 | |
46 | /** | |
b76f4e17 | 47 | * List of log |
f891182f | 48 | * |
b76f4e17 | 49 | * @var array $log |
f891182f | 50 | * @access private |
e39afba8 | 51 | * @association OMVModuleZFSVdev to log |
f891182f | 52 | */ |
b76f4e17 | 53 | private $log; |
f891182f MR |
54 | |
55 | /** | |
b76f4e17 | 56 | * List of cache |
f891182f | 57 | * |
b76f4e17 | 58 | * @var array $cache |
f891182f | 59 | * @access private |
e39afba8 | 60 | * @association OMVModuleZFSVdev to cache |
f891182f | 61 | */ |
b76f4e17 | 62 | private $cache; |
f891182f MR |
63 | |
64 | /** | |
b76f4e17 | 65 | * Pool size |
f891182f MR |
66 | * |
67 | * @var int $size | |
68 | * @access private | |
69 | */ | |
b76f4e17 | 70 | private $size; |
f891182f MR |
71 | |
72 | /** | |
b76f4e17 | 73 | * Pool's mountpoint |
f891182f MR |
74 | * |
75 | * @var string $mountPoint | |
76 | * @access private | |
77 | */ | |
b76f4e17 | 78 | private $mountPoint; |
f891182f MR |
79 | |
80 | /** | |
b76f4e17 | 81 | * List of features |
f891182f | 82 | * |
b76f4e17 | 83 | * @var array $features |
f891182f MR |
84 | * @access private |
85 | */ | |
b76f4e17 | 86 | private $features; |
f891182f MR |
87 | |
88 | // Associations | |
89 | /** | |
b76f4e17 | 90 | * Array of OMVModuleZFSSnapshot. |
f891182f | 91 | * |
b76f4e17 | 92 | * @var array $snapshot |
f891182f | 93 | * @access private |
e39afba8 | 94 | * @association OMVModuleZFSSnapshot to snapshot |
f891182f | 95 | */ |
b76f4e17 | 96 | private $snapshot; |
f891182f MR |
97 | |
98 | /** | |
b76f4e17 | 99 | * Array of OMVModuleZFSDataset |
f891182f | 100 | * |
b76f4e17 | 101 | * @var Dataset $dataset |
f891182f | 102 | * @access private |
e39afba8 | 103 | * @association OMVModuleZFSDataset to dataset |
f891182f | 104 | */ |
b76f4e17 | 105 | private $dataset; |
f891182f MR |
106 | |
107 | /** | |
b76f4e17 | 108 | * Array of OMVModuleZFSZvol |
f891182f | 109 | * |
b76f4e17 | 110 | * @var Zvol $zvol |
f891182f | 111 | * @access private |
e39afba8 | 112 | * @association OMVModuleZFSZvol to zvol |
f891182f | 113 | */ |
b76f4e17 | 114 | private $zvol; |
f891182f MR |
115 | |
116 | // Operations | |
b76f4e17 MR |
117 | /** |
118 | * Constructor | |
119 | * | |
e39afba8 | 120 | * @param $vdev OMVModuleZFSVdev or array(OMVModuleZFSVdev) |
b76f4e17 MR |
121 | * @throws OMVModuleZFSException |
122 | */ | |
123 | ||
124 | public function __construct($vdev) { | |
5e58d5e2 MR |
125 | $create_pool = true; |
126 | ||
b76f4e17 MR |
127 | if (is_array($vdev)) { |
128 | $cmd = $this->getCommandString($vdev); | |
129 | $name = $vdev[0]->getPool(); | |
130 | $type = $vdev[0]->getType(); | |
5e58d5e2 | 131 | } else if ($vdev instanceof OMVModuleZFSVdev) { |
b76f4e17 MR |
132 | $cmd = $this->getCommandString(array($vdev)); |
133 | $name = $vdev->getPool(); | |
134 | $type = $vdev->getType(); | |
5e58d5e2 MR |
135 | } else { |
136 | // Assume we make an instance of an existing pool | |
137 | $create_pool = false; | |
b76f4e17 | 138 | } |
b76f4e17 | 139 | |
5e58d5e2 MR |
140 | $this->vdevs = array(); |
141 | $this->spare = null; | |
142 | $this->log = null; | |
143 | $this->cache = null; | |
144 | $this->features = array(); | |
145 | if ($create_pool) { | |
146 | $cmd = "zpool create $name $cmd"; | |
147 | ||
148 | OMVUtil::exec($cmd, $output, $result); | |
149 | if ($result) | |
150 | throw new OMVModuleZFSException($output); | |
151 | else { | |
152 | $this->name = $name; | |
153 | $this->type = $type; | |
154 | if (is_array($vdev)) | |
155 | $this->vdevs = $vdev; | |
156 | else | |
157 | array_push ($this->vdevs, $vdev); | |
158 | $this->size = $this->getAttribute("size"); | |
159 | $this->mountPoint = $this->getAttribute("mountpoint"); | |
160 | } | |
161 | } else { | |
162 | $this->assemblePool($vdev); | |
b76f4e17 MR |
163 | } |
164 | } | |
165 | ||
f891182f | 166 | /** |
b76f4e17 | 167 | * Get pool name |
f891182f | 168 | * |
b76f4e17 | 169 | * @return string |
f891182f MR |
170 | * @access public |
171 | */ | |
172 | public function getName() { | |
b76f4e17 | 173 | return $this->name; |
f891182f MR |
174 | } |
175 | ||
176 | /** | |
b76f4e17 | 177 | * Get array of Vdev |
f891182f | 178 | * |
b76f4e17 | 179 | * @return array |
f891182f MR |
180 | * @access public |
181 | */ | |
182 | public function getVdevs() { | |
b76f4e17 | 183 | return $this->vdevs; |
f891182f MR |
184 | } |
185 | ||
186 | /** | |
b76f4e17 | 187 | * Add Vdev to pool |
f891182f | 188 | * |
b76f4e17 MR |
189 | * @param array $vdev array of OMVModuleZFSVdev |
190 | * @return void | |
191 | * @throws OMVModuleZFSException | |
f891182f MR |
192 | * @access public |
193 | */ | |
b76f4e17 | 194 | public function addVdev(array $vdevs) { |
e39afba8 | 195 | $cmd = "zpool add " . $this->name . " " . $this->getCommandString($vdevs); |
b76f4e17 MR |
196 | OMVUtil::exec($cmd, $output, $result); |
197 | if ($result) | |
198 | throw new OMVModuleZFSException($output); | |
199 | else | |
200 | $this->vdevs = array_merge($this->vdevs, $vdevs); | |
e39afba8 | 201 | $this->size = $this->getAttribute("size"); |
f891182f MR |
202 | } |
203 | ||
204 | /** | |
205 | * XXX | |
206 | * | |
b76f4e17 MR |
207 | * @param OMVModuleZFSVdev $vdev |
208 | * @return void | |
209 | * @throws OMVModuleZFSException | |
f891182f MR |
210 | * @access public |
211 | */ | |
b76f4e17 MR |
212 | public function removeVdev(OMVModuleZFSVdev $vdev) { |
213 | throw new OMVModuleZFSException("Cannot remove vdevs from a pool"); | |
f891182f MR |
214 | } |
215 | ||
216 | /** | |
217 | * XXX | |
218 | * | |
b76f4e17 MR |
219 | * @param OMVModuleZFSVdev $cache |
220 | * @return void | |
221 | * @throws OMVModuleZFSException | |
f891182f MR |
222 | * @access public |
223 | */ | |
b76f4e17 MR |
224 | public function addCache(OMVModuleZFSVdev $cache) { |
225 | if ($cache->getType() != OMVModuleZFSVdevType::OMVMODULEZFSPLAIN) | |
226 | throw new OMVModuleZFSException("Only a plain Vdev can be added as cache"); | |
227 | ||
e39afba8 | 228 | $cmd = "zpool add " . $this->name . " cache " . $this->getCommandString($vdevs); |
b76f4e17 MR |
229 | OMVUtil::exec($cmd, $output, $result); |
230 | if ($result) | |
231 | throw new OMVModuleZFSException($output); | |
232 | ||
233 | $disks = $cache->getDisks(); | |
234 | foreach ($disks as $disk) { | |
235 | array_push ($this->cache, $disk); | |
236 | } | |
f891182f MR |
237 | } |
238 | ||
239 | /** | |
240 | * XXX | |
241 | * | |
b76f4e17 MR |
242 | * @param array $disks |
243 | * @return void | |
244 | * @throws OMVModuleZFSException | |
f891182f MR |
245 | * @access public |
246 | */ | |
b76f4e17 | 247 | public function removeCache(array $disks = null) { |
b76f4e17 MR |
248 | if (! $disks) |
249 | $disks = $this->cache; | |
250 | ||
e39afba8 MR |
251 | foreach ($disks as $disk) |
252 | $dist_str .= "$disk "; | |
b76f4e17 | 253 | |
e39afba8 MR |
254 | $cmd = "zpool remove " . $this->name . " $dist_str"; |
255 | OMVUtil::exec($cmd, $output, $result); | |
256 | if ($result) | |
257 | throw new OMVModuleZFSException($output); | |
258 | else { | |
259 | foreach ($disks as $disk) | |
260 | $this->cache = $this->removeDisk($this->cache, $disk); | |
b76f4e17 | 261 | } |
f891182f MR |
262 | } |
263 | ||
264 | /** | |
265 | * XXX | |
266 | * | |
b76f4e17 | 267 | * @return Cache |
f891182f MR |
268 | * @access public |
269 | */ | |
270 | public function getCache() { | |
b76f4e17 | 271 | return $this->cache; |
f891182f MR |
272 | } |
273 | ||
274 | /** | |
275 | * XXX | |
276 | * | |
b76f4e17 MR |
277 | * @param OMVModuleZFSVdev $log |
278 | * @return void | |
5e58d5e2 | 279 | * @throws OMVModuleZFSException |
f891182f MR |
280 | * @access public |
281 | */ | |
b76f4e17 | 282 | public function addLog(OMVModuleZFSVdev $log) { |
e39afba8 MR |
283 | if ($log->getType() == OMVModuleZFSVdevType::OMVMODULEZFSPLAIN || |
284 | $log->getType() == OMVModuleZFSVdevType::OMVMODULEZFSMIRROR) { | |
285 | $cmd = "zpool add " . $this->name . " log " . $this->getCommandString($vdevs); | |
286 | OMVUtil::exec($cmd, $output, $result); | |
287 | if ($result) | |
288 | throw new OMVModuleZFSException($output); | |
289 | ||
290 | $this->log = $log; | |
291 | } else | |
292 | throw new OMVModuleZFSException("Only a plain Vdev or mirror Vdev can be added as log"); | |
f891182f MR |
293 | } |
294 | ||
295 | /** | |
296 | * XXX | |
297 | * | |
b76f4e17 | 298 | * @return void |
5e58d5e2 | 299 | * @throws OMVModuleZFSException |
f891182f MR |
300 | * @access public |
301 | */ | |
302 | public function removeLog() { | |
e39afba8 MR |
303 | foreach ($this->log as $vdev) { |
304 | if ($vdev->getType() == OMVModuleZFSVdevType::OMVMODULEZFSMIRROR) { | |
305 | $cmd = "zpool remove " . $this->name . " mirror-$i"; | |
306 | } else { | |
307 | $disks = $vdev->getDisks(); | |
308 | foreach ($disks as $disk) | |
309 | $dist_str .= "$disk "; | |
310 | $cmd = "zpool remove " . $this->name . " $disk_str"; | |
311 | } | |
312 | OMVUtil::exec($cmd, $output, $result); | |
313 | if ($result) | |
314 | throw new OMVModuleZFSException($output); | |
315 | else | |
316 | $this->log = array(); | |
317 | } | |
f891182f MR |
318 | } |
319 | ||
320 | /** | |
321 | * XXX | |
322 | * | |
b76f4e17 | 323 | * @return Log |
f891182f MR |
324 | * @access public |
325 | */ | |
326 | public function getLog() { | |
e39afba8 | 327 | return $this->log; |
f891182f MR |
328 | } |
329 | ||
330 | /** | |
331 | * XXX | |
332 | * | |
e39afba8 | 333 | * @param OMVModuleZFSVdev $spares |
b76f4e17 | 334 | * @return void |
5e58d5e2 | 335 | * @throws OMVModuleZFSException |
f891182f MR |
336 | * @access public |
337 | */ | |
e39afba8 MR |
338 | public function addSpare(OMVModuleZFSVdev $spares) { |
339 | if ($spares->getType() != OMVModuleZFSVdevType::OMVMODULEZFSPLAIN) | |
340 | throw new OMVModuleZFSException("Only a plain Vdev can be added as spares"); | |
341 | ||
342 | $cmd = "zpool add " . $this->name . " spare " . $this->getCommandString($vdevs); | |
343 | OMVUtil::exec($cmd, $output, $result); | |
344 | if ($result) | |
345 | throw new OMVModuleZFSException($output); | |
346 | ||
347 | $disks = $spares->getDisks(); | |
348 | foreach ($disks as $disk) { | |
349 | array_push ($this->spare, $disk); | |
350 | } | |
f891182f MR |
351 | } |
352 | ||
353 | /** | |
354 | * XXX | |
355 | * | |
e39afba8 | 356 | * @param array $disks |
b76f4e17 | 357 | * @return void |
5e58d5e2 | 358 | * @throws OMVModuleZFSException |
f891182f MR |
359 | * @access public |
360 | */ | |
e39afba8 MR |
361 | public function removeSpare(array $disks = null) { |
362 | if (! $disks) | |
363 | $disks = $this->spare; | |
364 | ||
365 | foreach ($disks as $disk) | |
366 | $dist_str .= "$disk "; | |
367 | ||
368 | $cmd = "zpool remove " . $this->name . " $dist_str"; | |
369 | OMVUtil::exec($cmd, $output, $result); | |
370 | if ($result) | |
371 | throw new OMVModuleZFSException($output); | |
372 | else { | |
373 | foreach ($disks as $disk) | |
374 | $this->spare = $this->removeDisk($this->spare, $disk); | |
375 | } | |
f891182f MR |
376 | } |
377 | ||
378 | /** | |
379 | * XXX | |
380 | * | |
b76f4e17 | 381 | * @return list<Disk> |
f891182f MR |
382 | * @access public |
383 | */ | |
384 | public function getSpares() { | |
e39afba8 | 385 | return $this->spare; |
f891182f MR |
386 | } |
387 | ||
388 | /** | |
389 | * XXX | |
390 | * | |
b76f4e17 | 391 | * @return int |
f891182f MR |
392 | * @access public |
393 | */ | |
394 | public function getSize() { | |
e39afba8 | 395 | return $this->size; |
f891182f MR |
396 | } |
397 | ||
398 | /** | |
399 | * XXX | |
400 | * | |
b76f4e17 | 401 | * @return string |
f891182f MR |
402 | * @access public |
403 | */ | |
404 | public function getMountPoint() { | |
e39afba8 | 405 | return $this->mountPoint; |
f891182f MR |
406 | } |
407 | ||
408 | /** | |
409 | * XXX | |
410 | * | |
b76f4e17 MR |
411 | * @param array $features |
412 | * @return void | |
5e58d5e2 | 413 | * @throws OMVModuleZFSException |
f891182f MR |
414 | * @access public |
415 | */ | |
b76f4e17 | 416 | public function setFeatures(array $features) { |
e39afba8 MR |
417 | foreach ($features as $feature => $value) { |
418 | $cmd = "zpool set $feature=$value " . $this->name; | |
419 | OMVUtil::exec($cmd, $output, $result); | |
420 | if ($result) | |
421 | throw new OMVModuleZFSException($output); | |
422 | } | |
423 | $this->features = $this->getAllAttributes(); | |
f891182f MR |
424 | } |
425 | ||
426 | /** | |
e39afba8 MR |
427 | * We only return array of features for which the user can |
428 | * change in GUI. | |
f891182f | 429 | * |
e39afba8 | 430 | * @return array of features |
f891182f MR |
431 | * @access public |
432 | */ | |
433 | public function getFeatures() { | |
e39afba8 MR |
434 | $attrs = array(); |
435 | $featureSet = array( | |
436 | 'recordsize', /* default 131072. 512 <= n^2 <= 131072*/ | |
437 | 'checksum', /* on | off */ | |
438 | 'compression', /* off | lzjb | gzip | zle | lz4 */ | |
439 | 'atime', /* on | off */ | |
440 | 'aclmode', /* discard | groupmask | passthrough | restricted */ | |
441 | 'aclinherit', /* discard | noallow | restricted | passthrough | passthrough-x */ | |
442 | 'casesensitivity', /* sensitive | insensitive | mixed */ | |
443 | 'primarycache', /* all | none | metadata */ | |
444 | 'secondarycache', /* all | none | metadata */ | |
445 | 'logbias', /* latency | throughput */ | |
446 | 'dedup', /* on | off */ | |
447 | 'sync' /* standard | always | disabled */ | |
448 | ); | |
9be18e88 MR |
449 | if (count($this->features) < 1) |
450 | $this->features = $this->getAllAttributes(); | |
e39afba8 MR |
451 | foreach ($this->features as $attr => $val) { |
452 | if (in_array($attr, $featureSet)) | |
453 | $attrs[$attr] = $val; | |
454 | } | |
455 | ||
456 | return $attrs; | |
f891182f MR |
457 | } |
458 | ||
459 | /** | |
460 | * XXX | |
461 | * | |
b76f4e17 | 462 | * @return void |
5e58d5e2 | 463 | * @throws OMVModuleZFSException |
f891182f MR |
464 | * @access public |
465 | */ | |
466 | public function export() { | |
e39afba8 MR |
467 | $cmd = "zpool export " . $this->name; |
468 | OMVUtil::exec($cmd, $output, $result); | |
469 | if ($result) | |
470 | throw new OMVModuleZFSException($output); | |
f891182f MR |
471 | } |
472 | ||
473 | /** | |
474 | * XXX | |
475 | * | |
b76f4e17 MR |
476 | * @param string $name |
477 | * @return void | |
5e58d5e2 | 478 | * @throws OMVModuleZFSException |
f891182f MR |
479 | * @access public |
480 | */ | |
e39afba8 MR |
481 | public function import($name = null) { |
482 | if ($name) | |
483 | $cmd = "zpool import $name"; | |
484 | else | |
485 | $cmd = "zpool import"; | |
486 | OMVUtil::exec($cmd, $output, $result); | |
487 | if ($result) | |
488 | throw new OMVModuleZFSException($output); | |
f891182f MR |
489 | } |
490 | ||
491 | /** | |
492 | * XXX | |
493 | * | |
b76f4e17 | 494 | * @return void |
5e58d5e2 | 495 | * @throws OMVModuleZFSException |
f891182f MR |
496 | * @access public |
497 | */ | |
498 | public function scrub() { | |
e39afba8 MR |
499 | $cmd = "zpool scrub " . $this->name; |
500 | OMVUtil::exec($cmd, $output, $result); | |
501 | if ($result) | |
502 | throw new OMVModuleZFSException($output); | |
f891182f MR |
503 | } |
504 | ||
505 | /** | |
506 | * XXX | |
507 | * | |
b76f4e17 | 508 | * @return string |
5e58d5e2 | 509 | * @throws OMVModuleZFSException |
f891182f MR |
510 | * @access public |
511 | */ | |
512 | public function status() { | |
e39afba8 MR |
513 | $cmd = "zpool status " . $this->name; |
514 | OMVUtil::exec($cmd, $output, $result); | |
515 | if ($result) | |
516 | throw new OMVModuleZFSException($output); | |
f891182f MR |
517 | } |
518 | ||
b76f4e17 | 519 | public function bindListeners(OMVNotifyDispatcher $dispatcher) { |
e39afba8 MR |
520 | // Update service if configuration has been modified |
521 | $dispatcher->addListener( | |
522 | OMV_NOTIFY_MODIFY, | |
523 | "org.openmediavault.services.nfs", | |
524 | array($this, "onUpdateNFSService")); | |
525 | $dispatcher->addListener( | |
526 | OMV_NOTIFY_CREATE, | |
527 | "org.openmediavault.services.nfs.shares.share", | |
528 | array($this, "onCreateNFSShare")); | |
529 | $dispatcher->addListener( | |
530 | OMV_NOTIFY_DELETE, | |
531 | "org.openmediavault.services.nfs.shares.share", | |
532 | array($this, "onDeleteNFSShare")); | |
533 | $dispatcher->addListener( | |
534 | OMV_NOTIFY_MODIFY, | |
535 | "org.openmediavault.services.nfs.shares.share", | |
536 | array($this, "onUpdateNFSShare")); | |
b76f4e17 MR |
537 | } |
538 | ||
f891182f MR |
539 | /** |
540 | * XXX | |
e39afba8 | 541 | * org.openmediavault.services.nfs |
f891182f MR |
542 | * |
543 | * @param string event | |
544 | * @access public | |
545 | */ | |
e39afba8 MR |
546 | public function onUpdateNFSService($args) { |
547 | $this->debug(sprintf("onUpdateNFSService args=%s", var_export($args, true))); | |
548 | } | |
549 | ||
550 | /** | |
551 | * XXX | |
552 | * org.openmediavault.services.nfs.shares.share | |
553 | * | |
554 | * @param string event | |
555 | * @access public | |
556 | */ | |
557 | public function onCreateNFSShare($args) { | |
558 | $this->debug(sprintf("onCreateNFSShare args=%s", var_export($args, true))); | |
559 | } | |
560 | ||
561 | /** | |
562 | * XXX | |
563 | * org.openmediavault.services.nfs.shares.share | |
564 | * | |
565 | * @param string event | |
566 | * @access public | |
567 | */ | |
568 | public function onDeleteNFSShare($args) { | |
569 | $this->debug(sprintf("onDeleteNFSShare args=%s", var_export($args, true))); | |
570 | } | |
571 | ||
572 | /** | |
573 | * XXX | |
574 | * org.openmediavault.services.nfs.shares.share | |
575 | * | |
576 | * @param string event | |
577 | * @access public | |
578 | */ | |
579 | public function onUpdateNFSShare($args) { | |
580 | $this->debug(sprintf("onUpdateNFSShare args=%s", var_export($args, true))); | |
f891182f MR |
581 | } |
582 | ||
583 | /** | |
b76f4e17 | 584 | * Convert array of Vdev to command string |
f891182f | 585 | * |
b76f4e17 MR |
586 | * @param array $vdevs |
587 | * @return string | |
588 | * @throws OMVMODULEZFSException | |
f891182f | 589 | */ |
b76f4e17 MR |
590 | private function getCommandString(array $vdevs) { |
591 | $adds = array(); | |
592 | ||
593 | foreach ($vdevs as $vdev) { | |
e39afba8 MR |
594 | if (is_object($vdev) == false) |
595 | throw new OMVMODULEZFSException("Not object of class OMVModuleZFSVdev"); | |
596 | if (is_a($vdev, OMVModuleZFSVdev) == false) | |
597 | throw new OMVMODULEZFSException("Object is not of class OMVModuleZFSVdev"); | |
b76f4e17 MR |
598 | $type = $vdev->getType(); |
599 | $command = ""; | |
600 | ||
601 | switch ($type) { | |
602 | case OMVModuleZFSVdevType::OMVMODULEZFSPLAIN: break; | |
603 | case OMVModuleZFSVdevType::OMVMODULEZFSMIRROR: $command = "mirror"; break; | |
604 | case OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ1: $command = "raidz1"; break; | |
605 | case OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ2: $command = "raidz2"; break; | |
606 | case OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ3: $command = "raidz3"; break; | |
607 | default: | |
608 | throw new OMVMODULEZFSException("Unknown Vdev type"); | |
609 | } | |
610 | $disks = $vdev->getDisks(); | |
611 | $diskStr = ""; | |
612 | foreach($disks as $disk) { | |
613 | $diskStr .= " $disk"; | |
614 | } | |
615 | ||
616 | array_push ($adds, $command . $diskStr); | |
617 | } | |
618 | ||
da22577f | 619 | return implode(" ", $adds); |
f891182f MR |
620 | } |
621 | ||
622 | /** | |
b76f4e17 | 623 | * Get an attribute from pool |
f891182f | 624 | * |
b76f4e17 MR |
625 | * @param string $attribute |
626 | * @return string value | |
f891182f | 627 | */ |
b76f4e17 MR |
628 | private function getAttribute($attribute) { |
629 | $cmd = "zpool list -H -o $attribute {$this->name}"; | |
630 | OMVUtil::exec($cmd, $output, $result); | |
631 | if ($result) { | |
632 | $cmd = "zfs list -H -o $attribute {$this->name}"; | |
633 | OMVUtil::exec($cmd, $output, $result); | |
634 | if ($result) | |
635 | return null; | |
636 | } | |
637 | ||
638 | return $output; | |
f891182f MR |
639 | } |
640 | ||
e39afba8 MR |
641 | /** |
642 | * Get all attributes from pool | |
643 | * @return array of attributes | |
5e58d5e2 | 644 | * @throws OMVModuleZFSException |
e39afba8 MR |
645 | */ |
646 | private function getAllAttributes() { | |
647 | $attrs = array(); | |
648 | $cmd = "zfs get -H all {$this->name}"; | |
649 | ||
650 | OMVUtil::exec($cmd, $output, $result); | |
651 | if ($result) | |
652 | throw new OMVModuleZFSException($output); | |
9be18e88 MR |
653 | $output = implode("\n", $output); |
654 | $res = preg_match_all("/{$this->name}\s+(\w+)\s+([\w\d\.]+).*/", $output, $matches, PREG_SET_ORDER); | |
e39afba8 MR |
655 | if ($res == false || $res == 0) |
656 | throw new OMVModuleZFSException("Error return by zpool get all: $output"); | |
657 | foreach ($matches as $match) { | |
658 | $attrs[$match[1]] = $match[2]; | |
659 | } | |
660 | ||
661 | return $attrs; | |
662 | } | |
663 | ||
f891182f | 664 | /** |
b76f4e17 | 665 | * Remove a disk from array |
f891182f | 666 | * |
b76f4e17 MR |
667 | * @param array $array |
668 | * @param string $disk | |
669 | * @return array | |
f891182f | 670 | */ |
b76f4e17 MR |
671 | private function removeDisk(array $array, $disk) { |
672 | $new_disks = array(); | |
673 | ||
674 | foreach ($array as $item) { | |
675 | if (strcmp($item, $disk) != 0) | |
676 | array_push ($new_disks, $item); | |
677 | } | |
678 | ||
679 | return $new_disks; | |
f891182f | 680 | } |
5e58d5e2 MR |
681 | |
682 | /** | |
683 | * Construct existing pool | |
684 | * | |
685 | * @param string $name | |
686 | * @return void | |
687 | * @throws OMVModuleZFSException | |
688 | */ | |
689 | private function assemblePool($name) { | |
9be18e88 | 690 | $cmd = "zpool status -v $name"; |
5e58d5e2 MR |
691 | $types = 'mirror|raidz1|raidz2|raidz3'; |
692 | $dev = null; | |
693 | $type = null; | |
694 | $log = false; | |
695 | $cache = false; | |
696 | $start = true; | |
697 | ||
698 | OMVUtil::exec($cmd, $output, $result); | |
699 | if ($result) | |
700 | throw new OMVModuleZFSException($output); | |
5e58d5e2 MR |
701 | |
702 | $this->name = $name; | |
9be18e88 MR |
703 | foreach($output as $line) { |
704 | if (! strstr($line, PHP_EOL)) | |
705 | $line .= PHP_EOL; | |
5e58d5e2 | 706 | if ($start) { |
9be18e88 MR |
707 | if (preg_match("/^\s*NAME/", $line)) |
708 | $start = false; | |
709 | continue; | |
5e58d5e2 MR |
710 | } else { |
711 | if (preg_match("/^\s*$/", $line)) { | |
712 | if ($dev) { | |
9be18e88 | 713 | $this->output($part, $type, $dev); |
5e58d5e2 MR |
714 | } |
715 | break; | |
716 | } else if (preg_match("/^\s*($name|logs|cache|spares)/", $line, $match)) { | |
717 | if ($dev) { | |
9be18e88 | 718 | $this->output($part, $type, $dev); |
5e58d5e2 MR |
719 | $dev = null; |
720 | $type = null; | |
721 | } | |
722 | $part = $match[1]; | |
723 | } else { | |
724 | switch ($part) { | |
725 | case $name: | |
726 | if (preg_match("/^\s*($types)/", $line, $match)) { | |
727 | /* new vdev */ | |
728 | if ($type) { | |
9be18e88 MR |
729 | $this->output(null, $type, $dev); |
730 | $dev = null; | |
5e58d5e2 MR |
731 | } |
732 | $type = $match[1]; | |
9be18e88 | 733 | } else if (preg_match("/^\s*([\w\d-]+)\s+/", $line, $match)) { |
5e58d5e2 MR |
734 | if ($dev) |
735 | $dev .= " $match[1]"; | |
736 | else | |
737 | $dev = "$match[1]"; | |
738 | } | |
739 | break; | |
740 | case 'logs': | |
9be18e88 | 741 | if (preg_match("/^\s*([\w\d-]+)\s+/", $line, $match)) { |
5e58d5e2 MR |
742 | if ($dev) |
743 | $dev .= " $match[1]"; | |
744 | else | |
745 | $dev = "$match[1]"; | |
746 | } | |
747 | break; | |
748 | case 'cache': | |
749 | case 'spares': | |
9be18e88 | 750 | if (preg_match("/^\s*([\w\d-]+)\s+/", $line, $match)) { |
5e58d5e2 MR |
751 | if ($dev) |
752 | $dev .= " $match[1]"; | |
753 | else | |
754 | $dev = "$match[1]"; | |
755 | } | |
756 | break; | |
757 | default: | |
758 | throw new Exception("$part: Unknown pool part"); | |
759 | } | |
760 | } | |
761 | } | |
762 | } | |
763 | $this->size = $this->getAttribute("size"); | |
764 | $this->mountPoint = $this->getAttribute("mountpoint"); | |
765 | } | |
766 | ||
767 | /** | |
768 | * Create pool config from parsed input | |
769 | * | |
770 | * @param string $part | |
771 | * @param string $type | |
772 | * @param string $dev | |
773 | * @return void | |
774 | * @throws OMVModuleZFSException | |
775 | */ | |
776 | private function output($part, $type, $dev) { | |
777 | $disks = split(" ", $dev); | |
778 | switch ($part) { | |
779 | case 'logs': | |
780 | if ($type && $type != 'mirror') | |
781 | throw new Exception("$type: Logs can only be mirror or plain"); | |
782 | if ($type) | |
783 | $this->log = new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSMIRROR, $disks); | |
784 | else | |
785 | $this->log = new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSPLAIN, $disks); | |
786 | break; | |
787 | case 'cache': | |
788 | if ($type) | |
789 | throw new Exception("$type: cache can only be plain"); | |
790 | $this->cache = new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSPLAIN, $disks); | |
791 | break; | |
792 | case 'spares': | |
793 | if ($type) | |
794 | throw new Exception("$type: spares can only be plain"); | |
795 | $this->spare = new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSPLAIN, $disks); | |
796 | break; | |
797 | default: | |
798 | if ($type) { | |
799 | switch ($type) { | |
800 | case 'mirror': | |
801 | array_push($this->vdevs, new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSMIRROR, $disks)); | |
802 | $this->type = OMVModuleZFSVdevType::OMVMODULEZFSMIRROR; | |
803 | break; | |
804 | case 'raidz1': | |
805 | array_push($this->vdevs, new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ1, $disks)); | |
806 | $this->type = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ1; | |
807 | break; | |
808 | case 'raidz2': | |
809 | array_push($this->vdevs, new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ2, $disks)); | |
810 | $this->type = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ2; | |
811 | break; | |
812 | case 'raidz3': | |
813 | array_push($this->vdevs, new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ3, $disks)); | |
814 | $this->type = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ3; | |
815 | break; | |
816 | } | |
817 | } else { | |
818 | array_push($this->vdevs, new OMVModuleZFSVdev($this->name, OMVModuleZFSVdevType::OMVMODULEZFSPLAIN, $disks)); | |
819 | $this->type = OMVModuleZFSVdevType::OMVMODULEZFSPLAIN; | |
820 | } | |
9be18e88 | 821 | break; |
5e58d5e2 MR |
822 | } |
823 | } | |
824 | ||
f891182f MR |
825 | } |
826 | ||
827 | ?> |