]> git.datanom.net - omvzfs.git/blob - gui/rpc/zfs.inc
9ab61a2e7c8d60e844cd6bc2de8fa31374a7c971
[omvzfs.git] / gui / rpc / zfs.inc
1 <?php
2
3 require_once("openmediavault/object.inc");
4 require_once("openmediavault/config.inc");
5 require_once("openmediavault/error.inc");
6 require_once("openmediavault/util.inc");
7 require_once("openmediavault/rpcservice.inc");
8 require_once("openmediavault/notify.inc");
9 require_once("zfs/Utils.php");
10 require_once("zfs/Dataset.php");
11 require_once("zfs/Snapshot.php");
12 require_once("zfs/Zvol.php");
13 require_once("zfs/Zpool.php");
14 require_once("zfs/NotifyListener.php");
15
16 class OMVRpcServiceZFS extends OMVRpcServiceAbstract {
17 public function getName() {
18 return "ZFS"; // RPC Service name. Same as in .js files
19 }
20
21 /* Initialize the RPC service. Different methods of the RPC service are declared here*/
22 public function initialize() {
23 $this->registerMethod("addPool");
24 $this->registerMethod("getObjectTree");
25 $this->registermethod("passParam");
26 $this->registermethod("addObject");
27 $this->registermethod("deleteObject");
28 $this->registermethod("getProperties");
29 $this->registermethod("setProperties");
30 $this->registermethod("inherit");
31 $this->registermethod("getSharedParams");
32 $this->registermethod("createShare");
33 $this->registermethod("getObjectDetails");
34 $this->registermethod("expandPool");
35 }
36
37 public function addPool($params, $context) {
38 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
39 // Validate the parameters of the RPC service method.
40 $this->validateMethodParams($params, '{
41 "type":"object",
42 "properties":{
43 "pooltype":{"type":"string","enum":["basic","mirror",' .
44 '"raidz1","raidz2","raidz3"]},
45 "force":{"type":"boolean"},
46 "mountpoint":{"type":"string"},
47 "name":{"type":"string"},
48 "devices":{"type":"string"},
49 "diskpath":{"type":"boolean"}
50 }
51 }');
52 switch ($params['pooltype']) {
53 case "basic":
54 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSPLAIN;
55 break;
56 case "mirror":
57 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSMIRROR;
58 break;
59 case "raidz1":
60 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ1;
61 break;
62 case "raidz2":
63 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ2;
64 break;
65 case "raidz3":
66 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ3;
67 break;
68 default:
69 throw new OMVModuleZFSException("Incorrect pool type specified");
70 break;
71 }
72 //Check for user supplied options
73 $opts = "";
74 if ($params['force']) {
75 $opts .= "-f ";
76 }
77 if (strlen($params['mountpoint']) > 0) {
78 $opts .= "-m " . $params['mountpoint'] . " ";
79 }
80
81 $disks = preg_split("/[,;]/", $params['devices']);
82 //Use /dev/disk/by-path as suggested in ZoL FAQ.
83 if ($params['diskpath']) {
84 try {
85 if (file_exists("/dev/disk/by-path/")) {
86 $tmp_disks = array();
87 foreach ($disks as $disk) {
88 $tmp_disks[] = OMVModuleZFSUtil::getDiskPath($disk);
89 }
90 $disks = $tmp_disks;
91 }
92 } catch (OMVModuleZFSException $e) {
93 //Do nothing if an excpetion is thrown
94 }
95 }
96
97 $vdev = new OMVModuleZFSVdev($params['name'], $pooltype, $disks);
98 $pool = new OMVModuleZFSZpool($vdev, $opts);
99 //Ugly fix to solve the problem of blkid not displaying info on newly created pools
100 $pool->export();
101 $pool->import($pool->getName());
102 }
103
104 public function getObjectTree($params, $context) {
105 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
106 $objects = OMVModuleZFSUtil::getZFSFlatArray();
107 $new = array();
108 foreach ($objects as $a){
109 $new[$a['parentid']][] = $a;
110 }
111 $tree = OMVModuleZFSUtil::createTree($new, $new['root']);
112 OMVModuleZFSUtil::addMissingOMVMntEnt(); //Adds missing ZFS filesystems to the OMV core
113 return $tree;
114 }
115
116 public function passParam($params, $context) {
117 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
118 // Validate the parameters of the RPC service method.
119 $this->validateMethodParams($params, '{
120 "type":"object",
121 "properties":{
122 "key":{"type":"string"},
123 "value":{"type":"string"}
124 }
125 }');
126 return array($params['key'] => $params['value']);
127 }
128
129 public function addObject($params, $context) {
130 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
131 // Validate the parameters of the RPC service method.
132 $this->validateMethodParams($params, '{
133 "type":"object",
134 "properties":{
135 "type":{"type":"string","enum":["filesystem","snapshot",' .
136 '"volume","clone"]},
137 "path":{"type":"string"},
138 "name":{"type":"string"},
139 "size":{"type":"string"},
140 "clonename":{"type":"string"},
141 "mountpoint":{"type":"string"}
142 }
143 }');
144 switch ($params['type']) {
145 case "clone":
146 $tmp = new OMVModuleZFSSnapshot($params['path']);
147 $tmp->clonesnap($params['clonename']);
148 break;
149 case "filesystem":
150 $name = $params['path'] . "/" . $params['name'];
151 $tmp = new OMVModuleZFSDataset($name);
152 if (strlen($params['mountpoint']) > 0) {
153 $properties = array("mountpoint"=>$params['mountpoint']);
154 $tmp->setProperties($properties);
155 }
156 break;
157 case "snapshot":
158 $name = $params['path'] . "@" . $params['name'];
159 $tmp = new OMVModuleZFSSnapshot($name);
160 break;
161 case "volume":
162 $name = $params['path'] . "/" . $params['name'];
163 $tmp = new OMVModuleZFSZvol($name);
164 $tmp->create($params['size']);
165 break;
166 default:
167 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
168 break;
169 }
170 }
171
172 public function deleteObject($params, $context) {
173 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
174 // Validate the parameters of the RPC service method.
175 $this->validateMethodParams($params, '{
176 "type":"object",
177 "properties":{
178 "type":{"type":"string","enum":["Filesystem","Snapshot",' .
179 '"Volume","Pool"]},
180 "name":{"type":"string"}
181 }
182 }');
183 global $xmlConfig;
184 $name = $params['name'];
185 switch ($params['type']) {
186 case "Filesystem":
187 OMVModuleZFSUtil::deleteShares($name);
188 $tmp = new OMVModuleZFSDataset($name);
189 $tmp->destroy();
190 break;
191 case "Snapshot":
192 $tmp = new OMVModuleZFSSnapshot($name);
193 $tmp->destroy();
194 break;
195 case "Volume":
196 $tmp = new OMVModuleZFSZvol($name);
197 $tmp->destroy();
198 break;
199 case "Pool":
200 $disks = OMVModuleZFSUtil::getDevDisksByPool($name);
201 $pooluuid = OMVModuleZFSUtil::getUUIDbyName($name);
202 $tmp = new OMVModuleZFSZpool($name);
203 $tmp->destroy();
204 $xpath = "//system/fstab/mntent[fsname='" . $pooluuid . "' and type='zfs']";
205 $object = $xmlConfig->get($xpath);
206 $xmlConfig->delete($xpath);
207 OMVModuleZFSUtil::clearZFSLabel($disks);
208 break;
209 default:
210 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
211 break;
212 }
213 }
214
215 public function getProperties($params, $context) {
216 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
217 // Validate the parameters of the RPC service method.
218 $this->validateMethodParams($params, '{
219 "type":"object",
220 "properties":{
221 "type":{"type":"string"},
222 "name":{"type":"string"},
223 "start":{"type":"integer"},
224 "limit":{'.$GLOBALS['OMV_JSONSCHEMA_COUNTFIELD'].'},
225 "sortfield":{'.$GLOBALS['OMV_JSONSCHEMA_SORTFIELD'].'},
226 "sortdir":{'.$GLOBALS['OMV_JSONSCHEMA_SORTDIR'].'}
227 }
228 }');
229 $objects = array();
230 $name = $params['name'];
231 switch ($params['type']) {
232 case "Filesystem":
233 $tmp = new OMVModuleZFSDataset($name);
234 break;
235 case "Snapshot":
236 $tmp = new OMVModuleZFSSnapshot($name);
237 break;
238 case "Volume":
239 $tmp = new OMVModuleZFSZvol($name);
240 break;
241 case "Pool":
242 $tmp = new OMVModuleZFSZpool($name);
243 break;
244 default:
245 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
246 break;
247 }
248 $properties = $tmp->getProperties();
249 foreach ($properties as $propertyk => $propertyv) {
250 if (!(strcmp($propertyv['source'], "-") == 0)) {
251 $objects[] = array('property' => $propertyk,
252 'value' => $propertyv['value'],
253 'source' => $propertyv['source'],
254 'modified' => "false");
255 }
256 }
257 return $objects;
258 }
259
260 public function setProperties($params, $context) {
261 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
262 // Validate the parameters of the RPC service method.
263 $this->validateMethodParams($params, '{
264 "type":"object",
265 "properties":{
266 "type":{"type":"string","enum":["Filesystem","Snapshot",' .
267 '"Volume","Pool"]},
268 "name":{"type":"string"},
269 "properties":{"type":"array","items":{
270 "type":"object",
271 "properties":{
272 "property":{"type":"string"},
273 "value":{"type":"string"}}}}
274 }
275 }');
276 global $xmlConfig;
277 switch ($params['type']) {
278 case "Filesystem":
279 $tmp = new OMVModuleZFSDataset($params['name']);
280 break;
281 case "Snapshot":
282 $tmp = new OMVModuleZFSSnapshot($params['name']);
283 break;
284 case "Volume":
285 $tmp = new OMVModuleZFSZvol($params['name']);
286 break;
287 case "Pool":
288 $tmp = new OMVModuleZFSZpool($params['name']);
289 break;
290 default:
291 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
292 break;
293 }
294 foreach ($params['properties'] as $property) {
295 unset($objects);
296 $objects = array();
297 $objects[$property['property']] = $property['value'];
298 $tmp->setProperties($objects);
299 if ((strcmp($property['property'], "mountpoint") === 0) && (strcmp($params['type'], "Filesystem") === 0)) {
300 OMVModuleZFSUtil::relocateFilesystem($params['name']);
301 }
302 }
303 }
304
305 public function inherit($params, $context) {
306 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
307 // Validate the parameters of the RPC service method.
308 $this->validateMethodParams($params, '{
309 "type":"object",
310 "properties":{
311 "type":{"type":"string","enum":["Filesystem","Snapshot",' .
312 '"Volume","Pool"]},
313 "name":{"type":"string"},
314 "property":{"type":"string"}
315 }
316 }');
317 // Create a background process.
318 $bgStatusFilename = $this->createBgProcStatus();
319 $pid = $this->fork();
320 if($pid > 0) { // Parent process.
321 $this->initializeBgProcStatus($bgStatusFilename, $pid);
322 return $bgStatusFilename;
323 }
324 // Child process.
325 try {
326 $bgOutputFilename = $this->createBgProcOutput();
327 $this->updateBgProcStatus($bgStatusFilename, "outputfilename", $bgOutputFilename);
328 switch ($params['type']) {
329 case "Filesystem":
330 $tmp = new OMVModuleZFSDataset($params['name']);
331 break;
332 case "Snapshot":
333 $tmp = new OMVModuleZFSSnapshot($params['name']);
334 break;
335 case "Volume":
336 $tmp = new OMVModuleZFSZvol($params['name']);
337 break;
338 case "Pool":
339 $tmp = new OMVModuleZFSZpool($params['name']);
340 break;
341 default:
342 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
343 break;
344 }
345 $tmp->inherit($params['property']);
346 $this->finalizeBgProcStatus($bgStatusFilename, $output);
347 exit(0);
348 } catch(Exception $e) {
349 $this->finalizeBgProcStatus($bgStatusFilename, "", $e);
350 exit(1);
351 }
352 }
353
354 public function getSharedParams($params, $context) {
355 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
356 // Validate the parameters of the RPC service method.
357 $this->validateMethodParams($params, '{
358 "type":"object",
359 "properties":{
360 "type":{"type":"string"},
361 "name":{"type":"string"}
362 }
363 }');
364 $objects = array();
365 //$ds = new OMVModuleZFSDataset($params['name']);
366 //$mountpoint = $ds->getMountPoint();
367 return array(
368 //"mountpoint" => $mountpoint,
369 "name" => $params['name'],
370 "type" => $params['type']);
371 }
372
373 public function createShare($params, $context) {
374 global $xmlConfig;
375 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
376 // Validate the parameters of the RPC service method.
377 $this->validateMethodParams($params, '{
378 "type":"object",
379 "properties":{
380 "name":{"type":"string"},
381 "type":{"type":"string","enum":["Filesystem"]},
382 "sharename":{'.$GLOBALS['OMV_JSONSCHEMA_SHARENAME'].'},
383 "comment":{"type":"string"},
384 "mode":{"type":"string","enum":["700","750","755",'.
385 '"770","775","777"],"optional":true},
386 "mountpoint":{"type":"string"}
387 }
388 }');
389
390 // The field 'reldirpath' may not contain the characters '..'. This
391 // is because of security reasons: the given canonicalized absolute
392 // path MUST be below the given mount point.
393 if(1 == preg_match("/\.\./", $params['mountpoint'])) {
394 throw new OMVException(OMVErrorMsg::E_RPC_SERVICE_METHOD_INVALID_PARAMS,
395 sprintf(gettext("The field '%s' contains forbidden two-dot symbols"), "mountpoint"));
396 }
397
398 // Prepare the configuration object. Use the name of the shared
399 // folder as the relative directory name of the share.
400 switch ($params['type']) {
401 case "Filesystem":
402 $tmp = new OMVModuleZFSDataset($params['name']);
403 break;
404 default:
405 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
406 break;
407 }
408
409 $poolname = OMVModuleZFSUtil::getPoolname($params['name']);
410 $pooluuid = OMVModuleZFSUtil::getUUIDbyName($poolname);
411 $dir = $tmp->getMountPoint();
412
413 //Get the mntent object and fetch it's uuid.
414 $xpath = "//system/fstab/mntent[fsname='" . $pooluuid . "' and dir='" . $dir . "' and type='zfs']";
415 $mountpoint = $xmlConfig->get($xpath);
416 $mntentref = $mountpoint['uuid'];
417
418 $uuid = OMVUtil::uuid();
419 $pathName = $dir . "/" . trim($params['mountpoint'], "/");
420 $reldirpath = trim($params['mountpoint'], "/");
421 $object = array(
422 "uuid" => $uuid,
423 "name" => $params['sharename'],
424 "comment" => $params['comment'] . "*** ZFS share on " . $params['name'] . " ***",
425 "mntentref" => $mntentref,
426 "reldirpath" => $reldirpath
427 );
428
429 // Set the configuration object.
430 $success = FALSE;
431 // Check uniqueness. The share name must be global unique because
432 // the name is also used when exporting a shared folder via NFS for
433 // example.
434 $xpath = sprintf("//system/shares/sharedfolder[name='%s']",
435 $params['name']);
436 if(TRUE === $xmlConfig->exists($xpath)) {
437 throw new OMVException(OMVErrorMsg::E_CONFIG_OBJECT_UNIQUENESS,
438 gettext("A shared folder with the given name already exists"));
439 }
440
441 // Add empty list of privileges per default.
442 $object['privileges'] = array();
443
444 // Append object to configuration.
445 $success = $xmlConfig->set("//system/shares",
446 array("sharedfolder" => $object));
447 if(FALSE === $success) {
448 throw new OMVException(OMVErrorMsg::E_CONFIG_SET_OBJECT_FAILED);
449 }
450
451 // Append the file mode field to the notification object if set.
452 // Defaults to 775.
453 $object['mode'] = "775";
454 if(array_key_exists("mode", $params)) {
455 $object['mode'] = $params['mode'];
456 }
457
458 // Create the shared folder directory if necessary.
459 if(FALSE === file_exists($pathName)) {
460 // Create the directory. Note, the function seems to have a bug
461 // when using the mask parameter. E.g. octdec("777") does not
462 // create the correct permissions as expected, thus change the
463 // mode using chmod.
464 if(FALSE === mkdir($pathName, 0700, TRUE)) {
465 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
466 sprintf("Failed to create the directory '%s'", $pathName));
467 }
468 // Change the directory mode.
469 if(FALSE === chmod($pathName, octdec($object['mode']))) {
470 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
471 sprintf("Failed to set file mode to '%s' for '%s'",
472 $object['mode'], $pathName));
473 }
474 }
475
476 // Change group owner of directory to configured default group,
477 // e.g. 'users'.
478 if(FALSE === chgrp($pathName, $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'])) {
479 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
480 sprintf("Failed to set file group to '%s' for '%s'",
481 $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'], $pathName));
482 }
483
484 // Set the setgid bit. Setting this permission means that all files
485 // created in the folder will inherit the group of the folder rather
486 // than the primary group of the user who creates the file.
487 $mode = fileperms($pathName) | 02000;
488 if(FALSE === chmod($pathName, $mode)) {
489 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
490 sprintf("Failed to set file mode to '%o' for '%s'",
491 $mode, $pathName));
492 }
493
494 // Notify configuration changes.
495 $dispatcher = &OMVNotifyDispatcher::getInstance();
496 $dispatcher->notify(OMV_NOTIFY_CREATE,"org.openmediavault.system.shares.sharedfolder", $object);
497 // Return the configuration object.
498 return $object;
499 }
500
501 public function getObjectDetails($params, $context) {
502 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
503 // Validate the parameters of the RPC service method.
504 $this->validateMethodParams($params, '{
505 "type":"object",
506 "properties":{
507 "name":{"type":"string"},
508 "type":{"type":"string"}
509 }
510 }');
511 $output = "";
512 switch ($params['type']) {
513 case "Filesystem":
514 $output .= "Filesystem details (zfs get all):\n\r\n\r";
515 $cmd = "zfs get all {$params['name']}";
516 break;
517 case "Volume":
518 $output .= "Volume details (zfs get all):\n\r\n\r";
519 $cmd = "zfs get all {$params['name']}";
520 break;
521 case "Snapshot":
522 $output .= "Snapshot details (zfs get all):\n\r\n\r";
523 $cmd = "zfs get all {$params['name']}";
524 break;
525 case "Pool":
526 $output .= "Pool status (zpool status):\n\r\n\r";
527 $cmd = "zpool status {$params['name']}";
528 OMVModuleZFSUtil::exec($cmd,$out,$res);
529 $output .= implode("\n\r", $out);
530 unset($out);
531 $output .= "\n\r\n\rPool details (zpool get all):\n\r\n\r";
532 $cmd = "zpool get all {$params['name']}";
533 OMVModuleZFSUtil::exec($cmd,$out,$res);
534 $output .= implode("\n\r", $out);
535 unset($out);
536 $output .= "\n\r\n\rPool filesystem details (zfs get all):\n\r\n\r";
537 $cmd = "zfs get all {$params['name']}";
538 break;
539 default:
540 throw new OMVModuleZFSException("Incorrect type provided");
541 }
542 OMVModuleZFSUtil::exec($cmd,$out,$res);
543 $output .= implode("\n\r", $out);
544 return array("details" => $output);
545 }
546
547 public function expandPool($params, $context) {
548 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
549 // Validate the parameters of the RPC service method.
550 $this->validateMethodParams($params, '{
551 "type":"object",
552 "properties":{
553 "vdevtype":{"type":"string","enum":["basic","mirror",' .
554 '"raidz1","raidz2","raidz3"]},
555 "name":{"type":"string"},
556 "devices":{"type":"string"},
557 "force":{"type":"boolean"},
558 "diskpath":{"type":"boolean"}
559 }
560 }');
561 $pool = new OMVModuleZFSZpool($params['name']);
562 switch ($params['vdevtype']) {
563 case "basic":
564 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSPLAIN;
565 break;
566 case "mirror":
567 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSMIRROR;
568 break;
569 case "raidz1":
570 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ1;
571 break;
572 case "raidz2":
573 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ2;
574 break;
575 case "raidz3":
576 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ3;
577 break;
578 default:
579 throw new OMVModuleZFSException("Incorrect pool type specified");
580 break;
581 }
582 if ($params['force']) {
583 $opts .= "-f ";
584 }
585 $disks = preg_split("/[,;]/", $params['devices']);
586 //Use /dev/disk/by-path as suggested in ZoL FAQ.
587 if ($params['diskpath']) {
588 try {
589 if (file_exists("/dev/disk/by-path/")) {
590 $tmp_disks = array();
591 foreach ($disks as $disk) {
592 $tmp_disks[] = OMVModuleZFSUtil::getDiskPath($disk);
593 }
594 $disks = $tmp_disks;
595 }
596 } catch (OMVModuleZFSException $e) {
597 //Do nothing if an exception is thrown
598 }
599 }
600 $vdev[] = new OMVModuleZFSVdev($params['name'], $pooltype, $disks);
601 $pool->addVdev($vdev, $opts);
602 //Ugly fix to solve the problem of blkid not displaying info on newly created pools
603 $pool->export();
604 $pool->import($pool->getName());
605 }
606 }
607
608 // Register the RPC service.
609 $rpcServiceMgr = &OMVRpcServiceMgr::getInstance(); // Get the "root" instance for the Services
610 $rpcServiceMgr->registerService(new OMVRpcServiceZFS()); // Register a new instance of the RPC service described above
611 ?>
612
This page took 0.096838 seconds and 4 git commands to generate.