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