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