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");
16 class OMVRpcServiceZFS extends OMVRpcServiceAbstract {
17 public function getName() {
18 return "ZFS"; // RPC Service name. Same as in .js files
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");
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, '{
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"}
52 switch ($params['pooltype']) {
54 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSPLAIN;
57 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSMIRROR;
60 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ1;
63 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ2;
66 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ3;
69 throw new OMVModuleZFSException("Incorrect pool type specified");
72 //Check for user supplied options
74 if ($params['force']) {
77 if (strlen($params['mountpoint']) > 0) {
78 $opts .= "-m " . $params['mountpoint'] . " ";
81 $disks = preg_split("/[,;]/", $params['devices']);
82 //Use /dev/disk/by-path as suggested in ZoL FAQ.
83 if ($params['diskpath']) {
85 if (file_exists("/dev/disk/by-path/")) {
87 foreach ($disks as $disk) {
88 $tmp_disks[] = OMVModuleZFSUtil::getDiskPath($disk);
92 } catch (OMVModuleZFSException $e) {
93 //Do nothing if an excpetion is thrown
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
101 $pool->import($pool->getName());
104 public function getObjectTree($params, $context) {
105 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
106 $objects = OMVModuleZFSUtil::getZFSFlatArray();
108 foreach ($objects as $a){
109 $new[$a['parentid']][] = $a;
111 $tree = OMVModuleZFSUtil::createTree($new, $new['root']);
112 OMVModuleZFSUtil::addMissingOMVMntEnt(); //Adds missing ZFS filesystems to the OMV core
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, '{
122 "key":{"type":"string"},
123 "value":{"type":"string"}
126 return array($params['key'] => $params['value']);
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, '{
135 "type":{"type":"string","enum":["filesystem","snapshot",' .
137 "path":{"type":"string"},
138 "name":{"type":"string"},
139 "size":{"type":"string"},
140 "clonename":{"type":"string"},
141 "mountpoint":{"type":"string"}
144 switch ($params['type']) {
146 $tmp = new OMVModuleZFSSnapshot($params['path']);
147 $tmp->clonesnap($params['clonename']);
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);
158 $name = $params['path'] . "@" . $params['name'];
159 $tmp = new OMVModuleZFSSnapshot($name);
162 $name = $params['path'] . "/" . $params['name'];
163 $tmp = new OMVModuleZFSZvol($name);
164 $tmp->create($params['size']);
167 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
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, '{
178 "type":{"type":"string","enum":["Filesystem","Snapshot",' .
180 "name":{"type":"string"}
184 $name = $params['name'];
185 switch ($params['type']) {
187 OMVModuleZFSUtil::deleteShares($name);
188 $tmp = new OMVModuleZFSDataset($name);
192 $tmp = new OMVModuleZFSSnapshot($name);
196 $tmp = new OMVModuleZFSZvol($name);
200 $disks = OMVModuleZFSUtil::getDevDisksByPool($name);
201 $pooluuid = OMVModuleZFSUtil::getUUIDbyName($name);
202 $tmp = new OMVModuleZFSZpool($name);
204 $xpath = "//system/fstab/mntent[fsname='" . $pooluuid . "' and type='zfs']";
205 $object = $xmlConfig->get($xpath);
206 $xmlConfig->delete($xpath);
207 OMVModuleZFSUtil::clearZFSLabel($disks);
210 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
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, '{
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'].'}
230 $name = $params['name'];
231 switch ($params['type']) {
233 $tmp = new OMVModuleZFSDataset($name);
236 $tmp = new OMVModuleZFSSnapshot($name);
239 $tmp = new OMVModuleZFSZvol($name);
242 $tmp = new OMVModuleZFSZpool($name);
245 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
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");
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, '{
266 "type":{"type":"string","enum":["Filesystem","Snapshot",' .
268 "name":{"type":"string"},
269 "properties":{"type":"array","items":{
272 "property":{"type":"string"},
273 "value":{"type":"string"}}}}
277 switch ($params['type']) {
279 $tmp = new OMVModuleZFSDataset($params['name']);
282 $tmp = new OMVModuleZFSSnapshot($params['name']);
285 $tmp = new OMVModuleZFSZvol($params['name']);
288 $tmp = new OMVModuleZFSZpool($params['name']);
291 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
294 foreach ($params['properties'] as $property) {
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']);
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, '{
311 "type":{"type":"string","enum":["Filesystem","Snapshot",' .
313 "name":{"type":"string"},
314 "property":{"type":"string"}
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;
326 $bgOutputFilename = $this->createBgProcOutput();
327 $this->updateBgProcStatus($bgStatusFilename, "outputfilename", $bgOutputFilename);
328 switch ($params['type']) {
330 $tmp = new OMVModuleZFSDataset($params['name']);
333 $tmp = new OMVModuleZFSSnapshot($params['name']);
336 $tmp = new OMVModuleZFSZvol($params['name']);
339 $tmp = new OMVModuleZFSZpool($params['name']);
342 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
345 $tmp->inherit($params['property']);
346 $this->finalizeBgProcStatus($bgStatusFilename, $output);
348 } catch(Exception $e) {
349 $this->finalizeBgProcStatus($bgStatusFilename, "", $e);
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, '{
360 "type":{"type":"string"},
361 "name":{"type":"string"}
365 //$ds = new OMVModuleZFSDataset($params['name']);
366 //$mountpoint = $ds->getMountPoint();
368 //"mountpoint" => $mountpoint,
369 "name" => $params['name'],
370 "type" => $params['type']);
373 public function createShare($params, $context) {
375 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
376 // Validate the parameters of the RPC service method.
377 $this->validateMethodParams($params, '{
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"}
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"));
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']) {
402 $tmp = new OMVModuleZFSDataset($params['name']);
405 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
409 $poolname = OMVModuleZFSUtil::getPoolname($params['name']);
410 $pooluuid = OMVModuleZFSUtil::getUUIDbyName($poolname);
411 $dir = $tmp->getMountPoint();
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'];
418 $uuid = OMVUtil::uuid();
419 $pathName = $dir . "/" . trim($params['mountpoint'], "/");
420 $reldirpath = trim($params['mountpoint'], "/");
423 "name" => $params['sharename'],
424 "comment" => $params['comment'] . "*** ZFS share on " . $params['name'] . " ***",
425 "mntentref" => $mntentref,
426 "reldirpath" => $reldirpath
429 // Set the configuration object.
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
434 $xpath = sprintf("//system/shares/sharedfolder[name='%s']",
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"));
441 // Add empty list of privileges per default.
442 $object['privileges'] = array();
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);
451 // Append the file mode field to the notification object if set.
453 $object['mode'] = "775";
454 if(array_key_exists("mode", $params)) {
455 $object['mode'] = $params['mode'];
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
464 if(FALSE === mkdir($pathName, 0700, TRUE)) {
465 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
466 sprintf("Failed to create the directory '%s'", $pathName));
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));
476 // Change group owner of directory to configured default group,
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));
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'",
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.
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, '{
507 "name":{"type":"string"},
508 "type":{"type":"string"}
512 switch ($params['type']) {
514 $output .= "Filesystem details (zfs get all):\n\r\n\r";
515 $cmd = "zfs get all {$params['name']}";
518 $output .= "Volume details (zfs get all):\n\r\n\r";
519 $cmd = "zfs get all {$params['name']}";
522 $output .= "Snapshot details (zfs get all):\n\r\n\r";
523 $cmd = "zfs get all {$params['name']}";
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);
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);
536 $output .= "\n\r\n\rPool filesystem details (zfs get all):\n\r\n\r";
537 $cmd = "zfs get all {$params['name']}";
540 throw new OMVModuleZFSException("Incorrect type provided");
542 OMVModuleZFSUtil::exec($cmd,$out,$res);
543 $output .= implode("\n\r", $out);
544 return array("details" => $output);
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, '{
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"}
561 $pool = new OMVModuleZFSZpool($params['name']);
562 switch ($params['vdevtype']) {
564 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSPLAIN;
567 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSMIRROR;
570 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ1;
573 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ2;
576 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ3;
579 throw new OMVModuleZFSException("Incorrect pool type specified");
582 if ($params['force']) {
585 $disks = preg_split("/[,;]/", $params['devices']);
586 //Use /dev/disk/by-path as suggested in ZoL FAQ.
587 if ($params['diskpath']) {
589 if (file_exists("/dev/disk/by-path/")) {
590 $tmp_disks = array();
591 foreach ($disks as $disk) {
592 $tmp_disks[] = OMVModuleZFSUtil::getDiskPath($disk);
596 } catch (OMVModuleZFSException $e) {
597 //Do nothing if an exception is thrown
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
604 $pool->import($pool->getName());
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