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']) {
84 if (file_exists("/dev/disk/by-path/")) {
86 foreach ($disks as $disk) {
87 $tmp_disks[] = OMVModuleZFSUtil::getDiskPath($disk);
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
97 $pool->import($pool->getName());
100 public function getObjectTree($params, $context) {
101 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
102 $objects = OMVModuleZFSUtil::getZFSFlatArray();
104 foreach ($objects as $a){
105 $new[$a['parentid']][] = $a;
107 $tree = OMVModuleZFSUtil::createTree($new, $new['root']);
108 OMVModuleZFSUtil::addMissingOMVMntEnt(); //Adds missing ZFS filesystems to the OMV core
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, '{
118 "key":{"type":"string"},
119 "value":{"type":"string"}
122 return array($params['key'] => $params['value']);
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, '{
131 "type":{"type":"string","enum":["filesystem","snapshot",' .
133 "path":{"type":"string"},
134 "name":{"type":"string"},
135 "size":{"type":"string"},
136 "clonename":{"type":"string"},
137 "mountpoint":{"type":"string"}
140 switch ($params['type']) {
142 $tmp = new OMVModuleZFSSnapshot($params['path']);
143 $tmp->clonesnap($params['clonename']);
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);
154 $name = $params['path'] . "@" . $params['name'];
155 $tmp = new OMVModuleZFSSnapshot($name);
158 $name = $params['path'] . "/" . $params['name'];
159 $tmp = new OMVModuleZFSZvol($name);
160 $tmp->create($params['size']);
163 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
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, '{
174 "type":{"type":"string","enum":["Filesystem","Snapshot",' .
176 "name":{"type":"string"}
180 $name = $params['name'];
181 switch ($params['type']) {
183 OMVModuleZFSUtil::deleteShares($name);
184 $tmp = new OMVModuleZFSDataset($name);
188 $tmp = new OMVModuleZFSSnapshot($name);
192 $tmp = new OMVModuleZFSZvol($name);
196 $disks = OMVModuleZFSUtil::getDevDisksByPool($name);
197 $pooluuid = OMVModuleZFSUtil::getUUIDbyName($name);
198 $tmp = new OMVModuleZFSZpool($name);
200 $xpath = "//system/fstab/mntent[fsname='" . $pooluuid . "' and type='zfs']";
201 $object = $xmlConfig->get($xpath);
202 $xmlConfig->delete($xpath);
203 OMVModuleZFSUtil::clearZFSLabel($disks);
206 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
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, '{
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'].'}
226 $name = $params['name'];
227 switch ($params['type']) {
229 $tmp = new OMVModuleZFSDataset($name);
232 $tmp = new OMVModuleZFSSnapshot($name);
235 $tmp = new OMVModuleZFSZvol($name);
238 $tmp = new OMVModuleZFSZpool($name);
241 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
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");
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, '{
262 "type":{"type":"string","enum":["Filesystem","Snapshot",' .
264 "name":{"type":"string"},
265 "properties":{"type":"array","items":{
268 "property":{"type":"string"},
269 "value":{"type":"string"}}}}
273 switch ($params['type']) {
275 $tmp = new OMVModuleZFSDataset($params['name']);
278 $tmp = new OMVModuleZFSSnapshot($params['name']);
281 $tmp = new OMVModuleZFSZvol($params['name']);
284 $tmp = new OMVModuleZFSZpool($params['name']);
287 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
290 foreach ($params['properties'] as $property) {
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']);
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, '{
307 "type":{"type":"string","enum":["Filesystem","Snapshot",' .
309 "name":{"type":"string"},
310 "property":{"type":"string"}
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;
322 $bgOutputFilename = $this->createBgProcOutput();
323 $this->updateBgProcStatus($bgStatusFilename, "outputfilename", $bgOutputFilename);
324 switch ($params['type']) {
326 $tmp = new OMVModuleZFSDataset($params['name']);
329 $tmp = new OMVModuleZFSSnapshot($params['name']);
332 $tmp = new OMVModuleZFSZvol($params['name']);
335 $tmp = new OMVModuleZFSZpool($params['name']);
338 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
341 $tmp->inherit($params['property']);
342 $this->finalizeBgProcStatus($bgStatusFilename, $output);
344 } catch(Exception $e) {
345 $this->finalizeBgProcStatus($bgStatusFilename, "", $e);
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, '{
356 "type":{"type":"string"},
357 "name":{"type":"string"}
361 //$ds = new OMVModuleZFSDataset($params['name']);
362 //$mountpoint = $ds->getMountPoint();
364 //"mountpoint" => $mountpoint,
365 "name" => $params['name'],
366 "type" => $params['type']);
369 public function createShare($params, $context) {
371 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
372 // Validate the parameters of the RPC service method.
373 $this->validateMethodParams($params, '{
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"}
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"));
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']) {
398 $tmp = new OMVModuleZFSDataset($params['name']);
401 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
405 $poolname = OMVModuleZFSUtil::getPoolname($params['name']);
406 $pooluuid = OMVModuleZFSUtil::getUUIDbyName($poolname);
407 $dir = $tmp->getMountPoint();
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'];
414 $uuid = OMVUtil::uuid();
415 $pathName = $dir . "/" . trim($params['mountpoint'], "/");
416 $reldirpath = trim($params['mountpoint'], "/");
419 "name" => $params['sharename'],
420 "comment" => $params['comment'] . "*** ZFS share on " . $params['name'] . " ***",
421 "mntentref" => $mntentref,
422 "reldirpath" => $reldirpath
425 // Set the configuration object.
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
430 $xpath = sprintf("//system/shares/sharedfolder[name='%s']",
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"));
437 // Add empty list of privileges per default.
438 $object['privileges'] = array();
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);
447 // Append the file mode field to the notification object if set.
449 $object['mode'] = "775";
450 if(array_key_exists("mode", $params)) {
451 $object['mode'] = $params['mode'];
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
460 if(FALSE === mkdir($pathName, 0700, TRUE)) {
461 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
462 sprintf("Failed to create the directory '%s'", $pathName));
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));
472 // Change group owner of directory to configured default group,
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));
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'",
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.
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, '{
503 "name":{"type":"string"},
504 "type":{"type":"string"}
508 switch ($params['type']) {
510 $output .= "Filesystem details (zfs get all):\n\r\n\r";
511 $cmd = "zfs get all {$params['name']}";
514 $output .= "Volume details (zfs get all):\n\r\n\r";
515 $cmd = "zfs get all {$params['name']}";
518 $output .= "Snapshot details (zfs get all):\n\r\n\r";
519 $cmd = "zfs get all {$params['name']}";
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);
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);
532 $output .= "\n\r\n\rPool filesystem details (zfs get all):\n\r\n\r";
533 $cmd = "zfs get all {$params['name']}";
536 throw new OMVModuleZFSException("Incorrect type provided");
538 OMVModuleZFSUtil::exec($cmd,$out,$res);
539 $output .= implode("\n\r", $out);
540 return array("details" => $output);
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, '{
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"}
557 $pool = new OMVModuleZFSZpool($params['name']);
558 switch ($params['vdevtype']) {
560 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSPLAIN;
563 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSMIRROR;
566 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ1;
569 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ2;
572 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ3;
575 throw new OMVModuleZFSException("Incorrect pool type specified");
578 if ($params['force']) {
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);
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
596 $pool->import($pool->getName());
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