]> git.datanom.net - omvzfs.git/blob - gui/rpc/zfs.inc
fda53b0209dcf00f43f6d0e09f35dddce28b35dd
[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"]},
130 "path":{"type":"string"},
131 "name":{"type":"string"},
132 "size":{"type":"string"}
133 }
134 }');
135 switch ($params['type']) {
136 case "filesystem":
137 $name = $params['path'] . "/" . $params['name'];
138 $tmp = new OMVModuleZFSDataset($name);
139 break;
140 case "snapshot":
141 $name = $params['path'] . "@" . $params['name'];
142 $tmp = new OMVModuleZFSSnapshot($name);
143 break;
144 case "volume":
145 $name = $params['path'] . "/" . $params['name'];
146 $tmp = new OMVModuleZFSZvol($name);
147 $tmp->create($params['size']);
148 break;
149 default:
150 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
151 break;
152 }
153 }
154
155 public function deleteObject($params, $context) {
156 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
157 // Validate the parameters of the RPC service method.
158 $this->validateMethodParams($params, '{
159 "type":"object",
160 "properties":{
161 "type":{"type":"string","enum":["Filesystem","Snapshot",' .
162 '"Volume","Clone","Pool"]},
163 "name":{"type":"string"}
164 }
165 }');
166 global $xmlConfig;
167 $name = $params['name'];
168 switch ($params['type']) {
169 case "Filesystem":
170 OMVModuleZFSUtil::deleteShares($name);
171 $tmp = new OMVModuleZFSDataset($name);
172 $tmp->destroy();
173 break;
174 case "Clone":
175 $tmp = new OMVModuleZFSDataset($name);
176 $tmp->destroy();
177 break;
178 case "Snapshot":
179 $tmp = new OMVModuleZFSSnapshot($name);
180 $tmp->destroy();
181 break;
182 case "Volume":
183 $tmp = new OMVModuleZFSZvol($name);
184 $tmp->destroy();
185 break;
186 case "Pool":
187 $disks = OMVModuleZFSUtil::getDevDisksByPool($name);
188 $pooluuid = OMVModuleZFSUtil::getUUIDbyName($name);
189 $tmp = new OMVModuleZFSZpool($name);
190 $tmp->destroy();
191 $xpath = "//system/fstab/mntent[fsname='" . $pooluuid . "' and type='zfs']";
192 $object = $xmlConfig->get($xpath);
193 $xmlConfig->delete($xpath);
194 OMVModuleZFSUtil::clearZFSLabel($disks);
195 break;
196 default:
197 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
198 break;
199 }
200 }
201
202 public function getProperties($params, $context) {
203 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
204 // Validate the parameters of the RPC service method.
205 $this->validateMethodParams($params, '{
206 "type":"object",
207 "properties":{
208 "type":{"type":"string"},
209 "name":{"type":"string"},
210 "start":{"type":"integer"},
211 "limit":{'.$GLOBALS['OMV_JSONSCHEMA_COUNTFIELD'].'},
212 "sortfield":{'.$GLOBALS['OMV_JSONSCHEMA_SORTFIELD'].'},
213 "sortdir":{'.$GLOBALS['OMV_JSONSCHEMA_SORTDIR'].'}
214 }
215 }');
216 $objects = array();
217 $name = $params['name'];
218 switch ($params['type']) {
219 case "Filesystem":
220 case "Clone":
221 $tmp = new OMVModuleZFSDataset($name);
222 break;
223 case "Snapshot":
224 $tmp = new OMVModuleZFSSnapshot($name);
225 break;
226 case "Volume":
227 $tmp = new OMVModuleZFSZvol($name);
228 break;
229 case "Pool":
230 $tmp = new OMVModuleZFSZpool($name);
231 break;
232 default:
233 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
234 break;
235 }
236 $properties = $tmp->getProperties();
237 foreach ($properties as $propertyk => $propertyv) {
238 if (!(strcmp($propertyv['source'], "-") == 0)) {
239 $objects[] = array('property' => $propertyk,
240 'value' => $propertyv['value'],
241 'source' => $propertyv['source'],
242 'modified' => "false");
243 }
244 }
245 return $objects;
246 }
247
248 public function setProperties($params, $context) {
249 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
250 // Validate the parameters of the RPC service method.
251 $this->validateMethodParams($params, '{
252 "type":"object",
253 "properties":{
254 "type":{"type":"string","enum":["Filesystem","Snapshot",' .
255 '"Volume","Clone","Pool"]},
256 "name":{"type":"string"},
257 "properties":{"type":"array","items":{
258 "type":"object",
259 "properties":{
260 "property":{"type":"string"},
261 "value":{"type":"string"}}}}
262 }
263 }');
264 global $xmlConfig;
265 switch ($params['type']) {
266 case "Filesystem":
267 case "Clone":
268 $tmp = new OMVModuleZFSDataset($params['name']);
269 break;
270 case "Snapshot":
271 $tmp = new OMVModuleZFSSnapshot($params['name']);
272 break;
273 case "Volume":
274 $tmp = new OMVModuleZFSZvol($params['name']);
275 break;
276 case "Pool":
277 $tmp = new OMVModuleZFSZpool($params['name']);
278 break;
279 default:
280 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
281 break;
282 }
283 foreach ($params['properties'] as $property) {
284 unset($objects);
285 $objects = array();
286 $objects[$property['property']] = $property['value'];
287 $tmp->setProperties($objects);
288 if ((strcmp($property['property'], "mountpoint") === 0) && (strcmp($params['type'], "Filesystem") === 0)) {
289 OMVModuleZFSUtil::relocateFilesystem($params['name']);
290 }
291 }
292 }
293
294 public function inherit($params, $context) {
295 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
296 // Validate the parameters of the RPC service method.
297 $this->validateMethodParams($params, '{
298 "type":"object",
299 "properties":{
300 "type":{"type":"string","enum":["Filesystem","Snapshot",' .
301 '"Volume","Clone","Pool"]},
302 "name":{"type":"string"},
303 "property":{"type":"string"}
304 }
305 }');
306 // Create a background process.
307 $bgStatusFilename = $this->createBgProcStatus();
308 $pid = $this->fork();
309 if($pid > 0) { // Parent process.
310 $this->initializeBgProcStatus($bgStatusFilename, $pid);
311 return $bgStatusFilename;
312 }
313 // Child process.
314 try {
315 $bgOutputFilename = $this->createBgProcOutput();
316 $this->updateBgProcStatus($bgStatusFilename, "outputfilename", $bgOutputFilename);
317 switch ($params['type']) {
318 case "Filesystem":
319 case "Clone":
320 $tmp = new OMVModuleZFSDataset($params['name']);
321 break;
322 case "Snapshot":
323 $tmp = new OMVModuleZFSSnapshot($params['name']);
324 break;
325 case "Volume":
326 $tmp = new OMVModuleZFSZvol($params['name']);
327 break;
328 case "Pool":
329 $tmp = new OMVModuleZFSZpool($params['name']);
330 break;
331 default:
332 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
333 break;
334 }
335 $tmp->inherit($params['property']);
336 $this->finalizeBgProcStatus($bgStatusFilename, $output);
337 exit(0);
338 } catch(Exception $e) {
339 $this->finalizeBgProcStatus($bgStatusFilename, "", $e);
340 exit(1);
341 }
342 }
343
344 public function getSharedParams($params, $context) {
345 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
346 // Validate the parameters of the RPC service method.
347 $this->validateMethodParams($params, '{
348 "type":"object",
349 "properties":{
350 "type":{"type":"string"},
351 "name":{"type":"string"}
352 }
353 }');
354 $objects = array();
355 //$ds = new OMVModuleZFSDataset($params['name']);
356 //$mountpoint = $ds->getMountPoint();
357 return array(
358 //"mountpoint" => $mountpoint,
359 "name" => $params['name'],
360 "type" => $params['type']);
361 }
362
363 public function createShare($params, $context) {
364 global $xmlConfig;
365 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
366 // Validate the parameters of the RPC service method.
367 $this->validateMethodParams($params, '{
368 "type":"object",
369 "properties":{
370 "name":{"type":"string"},
371 "type":{"type":"string","enum":["Filesystem","Clone"]},
372 "sharename":{'.$GLOBALS['OMV_JSONSCHEMA_SHARENAME'].'},
373 "comment":{"type":"string"},
374 "mode":{"type":"string","enum":["700","750","755",'.
375 '"770","775","777"],"optional":true},
376 "mountpoint":{"type":"string"}
377 }
378 }');
379
380 // The field 'reldirpath' may not contain the characters '..'. This
381 // is because of security reasons: the given canonicalized absolute
382 // path MUST be below the given mount point.
383 if(1 == preg_match("/\.\./", $params['mountpoint'])) {
384 throw new OMVException(OMVErrorMsg::E_RPC_SERVICE_METHOD_INVALID_PARAMS,
385 sprintf(gettext("The field '%s' contains forbidden two-dot symbols"), "mountpoint"));
386 }
387
388 // Prepare the configuration object. Use the name of the shared
389 // folder as the relative directory name of the share.
390 switch ($params['type']) {
391 case "Filesystem":
392 $tmp = new OMVModuleZFSDataset($params['name']);
393 break;
394 case "Clone":
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 details (zpool get all):\n\r\n\r";
520 $cmd = "zpool get all {$params['name']}";
521 break;
522 default:
523 throw new OMVModuleZFSException("Incorrect type provided");
524 }
525 OMVModuleZFSUtil::exec($cmd,$out,$res);
526 $output .= implode("\n\r", $out);
527 return array("details" => $output);
528 }
529
530 public function expandPool($params, $context) {
531 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
532 // Validate the parameters of the RPC service method.
533 $this->validateMethodParams($params, '{
534 "type":"object",
535 "properties":{
536 "vdevtype":{"type":"string","enum":["basic","mirror",' .
537 '"raidz1","raidz2","raidz3"]},
538 "name":{"type":"string"},
539 "devices":{"type":"string"},
540 "force":{"type":"boolean"}
541 }
542 }');
543 $pool = new OMVModuleZFSZpool($params['name']);
544 switch ($params['vdevtype']) {
545 case "basic":
546 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSPLAIN;
547 break;
548 case "mirror":
549 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSMIRROR;
550 break;
551 case "raidz1":
552 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ1;
553 break;
554 case "raidz2":
555 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ2;
556 break;
557 case "raidz3":
558 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ3;
559 break;
560 default:
561 throw new OMVModuleZFSException("Incorrect pool type specified");
562 break;
563 }
564 if ($params['force']) {
565 $opts .= "-f ";
566 }
567 //Use /dev/disk/by-path as deafult when creating new pools as suggested in ZoL FAQ.
568 $disks = preg_split("/[,;]/", $params['devices']);
569 if (file_exists("/dev/disk/by-path/")) {
570 $tmp_disks = array();
571 foreach ($disks as $disk) {
572 $tmp_disks[] = OMVModuleZFSUtil::getDiskPath($disk);
573 }
574 $disks = $tmp_disks;
575 }
576 $vdev[] = new OMVModuleZFSVdev($params['name'], $pooltype, $disks);
577 $pool->addVdev($vdev, $opts);
578 //Ugly fix to solve the problem of blkid not displaying info on newly created pools
579 $pool->export();
580 $pool->import($pool->getName());
581 }
582 }
583
584 // Register the RPC service.
585 $rpcServiceMgr = &OMVRpcServiceMgr::getInstance(); // Get the "root" instance for the Services
586 $rpcServiceMgr->registerService(new OMVRpcServiceZFS()); // Register a new instance of the RPC service described above
587 ?>
588
This page took 0.094373 seconds and 4 git commands to generate.