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