]> git.datanom.net - omvzfs.git/blob - src/Zpool.php
Added create method and property helper methods to Snapshot class.
[omvzfs.git] / src / Zpool.php
1 <?php
2 require_once("Vdev.php");
3 require_once("Snapshot.php");
4 require_once("Dataset.php");
5 require_once("Zvol.php");
6 require_once("Exception.php");
7
8 /**
9 * Class containing information about the pool
10 *
11 * @author Michael Rasmussen
12 * @version 0.1
13 * @copyright Michael Rasmussen <mir@datanom.net>
14 */
15 class OMVModuleZFSZpool extends OMVModuleAbstract
16 implements OMVNotifyListener {
17 // Attributes
18 /**
19 * Name of pool
20 *
21 * @var string $name
22 * @access private
23 */
24 private $name;
25
26 /**
27 * List of Vdev
28 *
29 * @var array $vdevs
30 * @access private
31 * @accociation OMVModuleZFSVdev to vdevs
32 */
33 private $vdevs;
34
35 /**
36 * List of spares
37 *
38 * @var array $spare
39 * @access private
40 * @accociation OMVModuleZFSVdev to spare
41 */
42 private $spare;
43
44 /**
45 * List of log
46 *
47 * @var array $log
48 * @access private
49 * @accociation OMVModuleZFSVdev to log
50 */
51 private $log;
52
53 /**
54 * List of cache
55 *
56 * @var array $cache
57 * @access private
58 * @accociation OMVModuleZFSVdev to cache
59 */
60 private $cache;
61
62 /**
63 * Pool size
64 *
65 * @var int $size
66 * @access private
67 */
68 private $size;
69
70 /**
71 * Pool's mountpoint
72 *
73 * @var string $mountPoint
74 * @access private
75 */
76 private $mountPoint;
77
78 /**
79 * List of features
80 *
81 * @var array $features
82 * @access private
83 */
84 private $features;
85
86 // Associations
87 /**
88 * Array of OMVModuleZFSSnapshot.
89 *
90 * @var array $snapshot
91 * @access private
92 * @accociation OMVModuleZFSSnapshot to snapshot
93 */
94 private $snapshot;
95
96 /**
97 * Array of OMVModuleZFSDataset
98 *
99 * @var Dataset $dataset
100 * @access private
101 * @accociation OMVModuleZFSDataset to dataset
102 */
103 private $dataset;
104
105 /**
106 * Array of OMVModuleZFSZvol
107 *
108 * @var Zvol $zvol
109 * @access private
110 * @accociation OMVModuleZFSZvol to zvol
111 */
112 private $zvol;
113
114 // Operations
115 /**
116 * Constructor
117 *
118 * @param $pool pool this mirror belongs to
119 * @throws OMVModuleZFSException
120 */
121
122 public function __construct($vdev) {
123 if (is_array($vdev)) {
124 $cmd = $this->getCommandString($vdev);
125 $name = $vdev[0]->getPool();
126 $type = $vdev[0]->getType();
127 }
128 else {
129 $cmd = $this->getCommandString(array($vdev));
130 $name = $vdev->getPool();
131 $type = $vdev->getType();
132 }
133 $cmd = "zpool create $name $cmd";
134
135 OMVUtil::exec($cmd, $output, $result);
136 if ($result)
137 throw new OMVModuleZFSException($output);
138 else {
139 $this->vdevs = array();
140 $this->spare = array();
141 $this->log = array();
142 $this->cache = array();
143 $this->features = array();
144 $this->name = $name;
145 $this->type = $type;
146 if (is_array($vdev))
147 $this->vdevs = $vdev;
148 else
149 array_push ($this->vdevs, $vdev);
150 $this->size = $this->getAttribute("size");
151 $this->mountPoint = $this->getAttribute("mountpoint");
152 }
153 }
154
155 /**
156 * Get pool name
157 *
158 * @return string
159 * @access public
160 */
161 public function getName() {
162 return $this->name;
163 }
164
165 /**
166 * Get array of Vdev
167 *
168 * @return array
169 * @access public
170 */
171 public function getVdevs() {
172 return $this->vdevs;
173 }
174
175 /**
176 * Add Vdev to pool
177 *
178 * @param array $vdev array of OMVModuleZFSVdev
179 * @return void
180 * @throws OMVModuleZFSException
181 * @access public
182 */
183 public function addVdev(array $vdevs) {
184 $cmd = "zpool add " . $this->getName() . " " . $this->getCommandString($vdevs);
185 OMVUtil::exec($cmd, $output, $result);
186 if ($result)
187 throw new OMVModuleZFSException($output);
188 else
189 $this->vdevs = array_merge($this->vdevs, $vdevs);
190 }
191
192 /**
193 * XXX
194 *
195 * @param OMVModuleZFSVdev $vdev
196 * @return void
197 * @throws OMVModuleZFSException
198 * @access public
199 */
200 public function removeVdev(OMVModuleZFSVdev $vdev) {
201 throw new OMVModuleZFSException("Cannot remove vdevs from a pool");
202 }
203
204 /**
205 * XXX
206 *
207 * @param OMVModuleZFSVdev $cache
208 * @return void
209 * @throws OMVModuleZFSException
210 * @access public
211 */
212 public function addCache(OMVModuleZFSVdev $cache) {
213 if ($cache->getType() != OMVModuleZFSVdevType::OMVMODULEZFSPLAIN)
214 throw new OMVModuleZFSException("Only a plain Vdev can be added as cache");
215
216 $cmd = "zpool add " . $this->getName() . " cache " . $this->getCommandString($vdevs);
217 OMVUtil::exec($cmd, $output, $result);
218 if ($result)
219 throw new OMVModuleZFSException($output);
220
221 $disks = $cache->getDisks();
222 foreach ($disks as $disk) {
223 array_push ($this->cache, $disk);
224 }
225 }
226
227 /**
228 * XXX
229 *
230 * @param array $disks
231 * @return void
232 * @throws OMVModuleZFSException
233 * @access public
234 */
235 public function removeCache(array $disks = null) {
236 $errors = array();
237 $exception = null;
238
239 if (! $disks)
240 $disks = $this->cache;
241
242 foreach ($disks as $disk) {
243 $cmd = "zpool remove " . $this->getName() . " $disk";
244 OMVUtil::exec($cmd, $output, $result);
245 if ($result)
246 array_push ($errors, $output);
247 else
248 $this->cache = $this->removeDisk($this->cache, $disk);
249 }
250
251 foreach ($errors as $error) {
252 if ($exception)
253 $exception .= "\n$error";
254 else
255 $exception = $error;
256 }
257
258 if ($exception)
259 throw new OMVModuleZFSException($exception);
260 }
261
262 /**
263 * XXX
264 *
265 * @return Cache
266 * @access public
267 */
268 public function getCache() {
269 return $this->cache;
270 }
271
272 /**
273 * XXX
274 *
275 * @param OMVModuleZFSVdev $log
276 * @return void
277 * @access public
278 */
279 public function addLog(OMVModuleZFSVdev $log) {
280 trigger_error('Not Implemented!', E_USER_WARNING);
281 }
282
283 /**
284 * XXX
285 *
286 * @return void
287 * @access public
288 */
289 public function removeLog() {
290 trigger_error('Not Implemented!', E_USER_WARNING);
291 }
292
293 /**
294 * XXX
295 *
296 * @return Log
297 * @access public
298 */
299 public function getLog() {
300 trigger_error('Not Implemented!', E_USER_WARNING);
301 }
302
303 /**
304 * XXX
305 *
306 * @param array $spares
307 * @return void
308 * @access public
309 */
310 public function addSpare(array $spares) {
311 trigger_error('Not Implemented!', E_USER_WARNING);
312 }
313
314 /**
315 * XXX
316 *
317 * @param Disk $spare
318 * @return void
319 * @access public
320 */
321 public function removeSpare($spare) {
322 trigger_error('Not Implemented!', E_USER_WARNING);
323 }
324
325 /**
326 * XXX
327 *
328 * @return list<Disk>
329 * @access public
330 */
331 public function getSpares() {
332 trigger_error('Not Implemented!', E_USER_WARNING);
333 }
334
335 /**
336 * XXX
337 *
338 * @return int
339 * @access public
340 */
341 public function getSize() {
342 trigger_error('Not Implemented!', E_USER_WARNING);
343 }
344
345 /**
346 * XXX
347 *
348 * @return string
349 * @access public
350 */
351 public function getMountPoint() {
352 trigger_error('Not Implemented!', E_USER_WARNING);
353 }
354
355 /**
356 * XXX
357 *
358 * @param array $features
359 * @return void
360 * @access public
361 */
362 public function setFeatures(array $features) {
363 trigger_error('Not Implemented!', E_USER_WARNING);
364 }
365
366 /**
367 * XXX
368 *
369 * @return list<Feature>
370 * @access public
371 */
372 public function getFeatures() {
373 trigger_error('Not Implemented!', E_USER_WARNING);
374 }
375
376 /**
377 * XXX
378 *
379 * @return void
380 * @access public
381 */
382 public function export() {
383 trigger_error('Not Implemented!', E_USER_WARNING);
384 }
385
386 /**
387 * XXX
388 *
389 * @param string $name
390 * @return void
391 * @access public
392 */
393 public function import($name) {
394 trigger_error('Not Implemented!', E_USER_WARNING);
395 }
396
397 /**
398 * XXX
399 *
400 * @return void
401 * @access public
402 */
403 public function scrub() {
404 trigger_error('Not Implemented!', E_USER_WARNING);
405 }
406
407 /**
408 * XXX
409 *
410 * @return string
411 * @access public
412 */
413 public function status() {
414 trigger_error('Not Implemented!', E_USER_WARNING);
415 }
416
417 public function bindListeners(OMVNotifyDispatcher $dispatcher) {
418 $dispatcher->addListener(
419 OMV_NOTIFY_EVENT,
420 "org.openmediavault.module.service.nfs.start",
421 array($this, "onNotify"));
422 $dispatcher->addListener(
423 OMV_NOTIFY_EVENT,
424 "org.openmediavault.module.service.nfs.stop",
425 array($this, "onNotify"));
426 $dispatcher->addListener(
427 OMV_NOTIFY_EVENT,
428 "org.openmediavault.module.service.nfs.applyconfig",
429 array($this, "onNotify"));
430 }
431
432 /**
433 * XXX
434 * org.openmediavault.module.service.<servicename>.start
435 * org.openmediavault.module.service.<servicename>.stop
436 * org.openmediavault.module.service.<servicename>.applyconfig
437 *
438 * @param string event
439 * @access public
440 */
441 public function onNotify($event) {
442 trigger_error('Not Implemented!', E_USER_WARNING);
443 }
444
445 /**
446 * Convert array of Vdev to command string
447 *
448 * @param array $vdevs
449 * @return string
450 * @throws OMVMODULEZFSException
451 */
452 private function getCommandString(array $vdevs) {
453 $adds = array();
454
455 foreach ($vdevs as $vdev) {
456 $type = $vdev->getType();
457 $command = "";
458
459 switch ($type) {
460 case OMVModuleZFSVdevType::OMVMODULEZFSPLAIN: break;
461 case OMVModuleZFSVdevType::OMVMODULEZFSMIRROR: $command = "mirror"; break;
462 case OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ1: $command = "raidz1"; break;
463 case OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ2: $command = "raidz2"; break;
464 case OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ3: $command = "raidz3"; break;
465 default:
466 throw new OMVMODULEZFSException("Unknown Vdev type");
467 }
468 $disks = $vdev->getDisks();
469 $diskStr = "";
470 foreach($disks as $disk) {
471 $diskStr .= " $disk";
472 }
473
474 array_push ($adds, $command . $diskStr);
475 }
476
477 return join(" ", $adds);
478 }
479
480 /**
481 * Get an attribute from pool
482 *
483 * @param string $attribute
484 * @return string value
485 */
486 private function getAttribute($attribute) {
487 $cmd = "zpool list -H -o $attribute {$this->name}";
488 OMVUtil::exec($cmd, $output, $result);
489 if ($result) {
490 $cmd = "zfs list -H -o $attribute {$this->name}";
491 OMVUtil::exec($cmd, $output, $result);
492 if ($result)
493 return null;
494 }
495
496 return $output;
497 }
498
499 /**
500 * Remove a disk from array
501 *
502 * @param array $array
503 * @param string $disk
504 * @return array
505 */
506 private function removeDisk(array $array, $disk) {
507 $new_disks = array();
508
509 foreach ($array as $item) {
510 if (strcmp($item, $disk) != 0)
511 array_push ($new_disks, $item);
512 }
513
514 return $new_disks;
515 }
516 }
517
518 ?>
This page took 0.616095 seconds and 6 git commands to generate.