]> git.datanom.net - omvzfs.git/blame - src/Zpool.php
Added methods to add/delete Snapshots to/from Datasets.
[omvzfs.git] / src / Zpool.php
CommitLineData
f891182f 1<?php
b76f4e17
MR
2require_once("Vdev.php");
3require_once("Snapshot.php");
4require_once("Dataset.php");
5require_once("Zvol.php");
6require_once("Exception.php");
f891182f
MR
7
8/**
b76f4e17 9 * Class containing information about the pool
f891182f 10 *
b76f4e17
MR
11 * @author Michael Rasmussen
12 * @version 0.1
13 * @copyright Michael Rasmussen <mir@datanom.net>
f891182f 14 */
b76f4e17
MR
15class OMVModuleZFSZpool extends OMVModuleAbstract
16 implements OMVNotifyListener {
f891182f
MR
17 // Attributes
18 /**
b76f4e17 19 * Name of pool
f891182f
MR
20 *
21 * @var string $name
22 * @access private
23 */
b76f4e17 24 private $name;
f891182f
MR
25
26 /**
b76f4e17 27 * List of Vdev
f891182f 28 *
b76f4e17 29 * @var array $vdevs
f891182f 30 * @access private
b76f4e17 31 * @accociation OMVModuleZFSVdev to vdevs
f891182f 32 */
b76f4e17 33 private $vdevs;
f891182f
MR
34
35 /**
b76f4e17 36 * List of spares
f891182f 37 *
b76f4e17 38 * @var array $spare
f891182f 39 * @access private
b76f4e17 40 * @accociation OMVModuleZFSVdev to spare
f891182f 41 */
b76f4e17 42 private $spare;
f891182f
MR
43
44 /**
b76f4e17 45 * List of log
f891182f 46 *
b76f4e17 47 * @var array $log
f891182f 48 * @access private
b76f4e17 49 * @accociation OMVModuleZFSVdev to log
f891182f 50 */
b76f4e17 51 private $log;
f891182f
MR
52
53 /**
b76f4e17 54 * List of cache
f891182f 55 *
b76f4e17 56 * @var array $cache
f891182f 57 * @access private
b76f4e17 58 * @accociation OMVModuleZFSVdev to cache
f891182f 59 */
b76f4e17 60 private $cache;
f891182f
MR
61
62 /**
b76f4e17 63 * Pool size
f891182f
MR
64 *
65 * @var int $size
66 * @access private
67 */
b76f4e17 68 private $size;
f891182f
MR
69
70 /**
b76f4e17 71 * Pool's mountpoint
f891182f
MR
72 *
73 * @var string $mountPoint
74 * @access private
75 */
b76f4e17 76 private $mountPoint;
f891182f
MR
77
78 /**
b76f4e17 79 * List of features
f891182f 80 *
b76f4e17 81 * @var array $features
f891182f
MR
82 * @access private
83 */
b76f4e17 84 private $features;
f891182f
MR
85
86 // Associations
87 /**
b76f4e17 88 * Array of OMVModuleZFSSnapshot.
f891182f 89 *
b76f4e17 90 * @var array $snapshot
f891182f 91 * @access private
b76f4e17 92 * @accociation OMVModuleZFSSnapshot to snapshot
f891182f 93 */
b76f4e17 94 private $snapshot;
f891182f
MR
95
96 /**
b76f4e17 97 * Array of OMVModuleZFSDataset
f891182f 98 *
b76f4e17 99 * @var Dataset $dataset
f891182f 100 * @access private
b76f4e17 101 * @accociation OMVModuleZFSDataset to dataset
f891182f 102 */
b76f4e17 103 private $dataset;
f891182f
MR
104
105 /**
b76f4e17 106 * Array of OMVModuleZFSZvol
f891182f 107 *
b76f4e17 108 * @var Zvol $zvol
f891182f 109 * @access private
b76f4e17 110 * @accociation OMVModuleZFSZvol to zvol
f891182f 111 */
b76f4e17 112 private $zvol;
f891182f
MR
113
114 // Operations
b76f4e17
MR
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
f891182f 155 /**
b76f4e17 156 * Get pool name
f891182f 157 *
b76f4e17 158 * @return string
f891182f
MR
159 * @access public
160 */
161 public function getName() {
b76f4e17 162 return $this->name;
f891182f
MR
163 }
164
165 /**
b76f4e17 166 * Get array of Vdev
f891182f 167 *
b76f4e17 168 * @return array
f891182f
MR
169 * @access public
170 */
171 public function getVdevs() {
b76f4e17 172 return $this->vdevs;
f891182f
MR
173 }
174
175 /**
b76f4e17 176 * Add Vdev to pool
f891182f 177 *
b76f4e17
MR
178 * @param array $vdev array of OMVModuleZFSVdev
179 * @return void
180 * @throws OMVModuleZFSException
f891182f
MR
181 * @access public
182 */
b76f4e17
MR
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);
f891182f
MR
190 }
191
192 /**
193 * XXX
194 *
b76f4e17
MR
195 * @param OMVModuleZFSVdev $vdev
196 * @return void
197 * @throws OMVModuleZFSException
f891182f
MR
198 * @access public
199 */
b76f4e17
MR
200 public function removeVdev(OMVModuleZFSVdev $vdev) {
201 throw new OMVModuleZFSException("Cannot remove vdevs from a pool");
f891182f
MR
202 }
203
204 /**
205 * XXX
206 *
b76f4e17
MR
207 * @param OMVModuleZFSVdev $cache
208 * @return void
209 * @throws OMVModuleZFSException
f891182f
MR
210 * @access public
211 */
b76f4e17
MR
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 }
f891182f
MR
225 }
226
227 /**
228 * XXX
229 *
b76f4e17
MR
230 * @param array $disks
231 * @return void
232 * @throws OMVModuleZFSException
f891182f
MR
233 * @access public
234 */
b76f4e17
MR
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);
f891182f
MR
260 }
261
262 /**
263 * XXX
264 *
b76f4e17 265 * @return Cache
f891182f
MR
266 * @access public
267 */
268 public function getCache() {
b76f4e17 269 return $this->cache;
f891182f
MR
270 }
271
272 /**
273 * XXX
274 *
b76f4e17
MR
275 * @param OMVModuleZFSVdev $log
276 * @return void
f891182f
MR
277 * @access public
278 */
b76f4e17 279 public function addLog(OMVModuleZFSVdev $log) {
f891182f
MR
280 trigger_error('Not Implemented!', E_USER_WARNING);
281 }
282
283 /**
284 * XXX
285 *
b76f4e17 286 * @return void
f891182f
MR
287 * @access public
288 */
289 public function removeLog() {
290 trigger_error('Not Implemented!', E_USER_WARNING);
291 }
292
293 /**
294 * XXX
295 *
b76f4e17 296 * @return Log
f891182f
MR
297 * @access public
298 */
299 public function getLog() {
300 trigger_error('Not Implemented!', E_USER_WARNING);
301 }
302
303 /**
304 * XXX
305 *
b76f4e17
MR
306 * @param array $spares
307 * @return void
f891182f
MR
308 * @access public
309 */
b76f4e17 310 public function addSpare(array $spares) {
f891182f
MR
311 trigger_error('Not Implemented!', E_USER_WARNING);
312 }
313
314 /**
315 * XXX
316 *
b76f4e17
MR
317 * @param Disk $spare
318 * @return void
f891182f
MR
319 * @access public
320 */
321 public function removeSpare($spare) {
322 trigger_error('Not Implemented!', E_USER_WARNING);
323 }
324
325 /**
326 * XXX
327 *
b76f4e17 328 * @return list<Disk>
f891182f
MR
329 * @access public
330 */
331 public function getSpares() {
332 trigger_error('Not Implemented!', E_USER_WARNING);
333 }
334
335 /**
336 * XXX
337 *
b76f4e17 338 * @return int
f891182f
MR
339 * @access public
340 */
341 public function getSize() {
342 trigger_error('Not Implemented!', E_USER_WARNING);
343 }
344
345 /**
346 * XXX
347 *
b76f4e17 348 * @return string
f891182f
MR
349 * @access public
350 */
351 public function getMountPoint() {
352 trigger_error('Not Implemented!', E_USER_WARNING);
353 }
354
355 /**
356 * XXX
357 *
b76f4e17
MR
358 * @param array $features
359 * @return void
f891182f
MR
360 * @access public
361 */
b76f4e17 362 public function setFeatures(array $features) {
f891182f
MR
363 trigger_error('Not Implemented!', E_USER_WARNING);
364 }
365
366 /**
367 * XXX
368 *
b76f4e17 369 * @return list<Feature>
f891182f
MR
370 * @access public
371 */
372 public function getFeatures() {
373 trigger_error('Not Implemented!', E_USER_WARNING);
374 }
375
376 /**
377 * XXX
378 *
b76f4e17 379 * @return void
f891182f
MR
380 * @access public
381 */
382 public function export() {
383 trigger_error('Not Implemented!', E_USER_WARNING);
384 }
385
386 /**
387 * XXX
388 *
b76f4e17
MR
389 * @param string $name
390 * @return void
f891182f
MR
391 * @access public
392 */
393 public function import($name) {
394 trigger_error('Not Implemented!', E_USER_WARNING);
395 }
396
397 /**
398 * XXX
399 *
b76f4e17 400 * @return void
f891182f
MR
401 * @access public
402 */
403 public function scrub() {
404 trigger_error('Not Implemented!', E_USER_WARNING);
405 }
406
407 /**
408 * XXX
409 *
b76f4e17 410 * @return string
f891182f
MR
411 * @access public
412 */
413 public function status() {
414 trigger_error('Not Implemented!', E_USER_WARNING);
415 }
416
b76f4e17
MR
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
f891182f
MR
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 /**
b76f4e17 446 * Convert array of Vdev to command string
f891182f 447 *
b76f4e17
MR
448 * @param array $vdevs
449 * @return string
450 * @throws OMVMODULEZFSException
f891182f 451 */
b76f4e17
MR
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);
f891182f
MR
478 }
479
480 /**
b76f4e17 481 * Get an attribute from pool
f891182f 482 *
b76f4e17
MR
483 * @param string $attribute
484 * @return string value
f891182f 485 */
b76f4e17
MR
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;
f891182f
MR
497 }
498
499 /**
b76f4e17 500 * Remove a disk from array
f891182f 501 *
b76f4e17
MR
502 * @param array $array
503 * @param string $disk
504 * @return array
f891182f 505 */
b76f4e17
MR
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;
f891182f
MR
515 }
516}
517
518?>
This page took 0.179128 seconds and 5 git commands to generate.