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");
36 public function addPool($params, $context) {
37 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
38 // Validate the parameters of the RPC service method.
39 $this->validateMethodParams($params, '{
42 "pooltype":{"type":"string","enum":["basic","mirror",' .
43 '"raidz1","raidz2","raidz3"]},
44 "force":{"type":"boolean"},
45 "mountpoint":{"type":"string"},
46 "name":{"type":"string"},
47 "devices":{"type":"string"}
50 switch ($params['pooltype']) {
52 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSPLAIN;
55 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSMIRROR;
58 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ1;
61 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ2;
64 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ3;
67 throw new OMVModuleZFSException("Incorrect pool type specified");
70 //Check for user supplied options
72 if ($params['force']) {
75 if (strlen($params['mountpoint']) > 0) {
76 $opts .= "-m " . $params['mountpoint'] . " ";
79 //Use /dev/disk/by-path as deafult when creating new pools as suggested in ZoL FAQ.
80 $disks = preg_split("/[,;]/", $params['devices']);
81 if (file_exists("/dev/disk/by-path/")) {
83 foreach ($disks as $disk) {
84 $tmp_disks[] = OMVModuleZFSUtil::getDiskPath($disk);
89 $vdev = new OMVModuleZFSVdev($params['name'], $pooltype, $disks);
90 $pool = new OMVModuleZFSZpool($vdev, $opts);
91 //Ugly fix to solve the problem of blkid not displaying info on newly created pools
93 $pool->import($pool->getName());
96 public function getObjectTree($params, $context) {
97 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
98 $objects = OMVModuleZFSUtil::getZFSFlatArray();
100 foreach ($objects as $a){
101 $new[$a['parentid']][] = $a;
103 $tree = OMVModuleZFSUtil::createTree($new, $new['root']);
104 OMVModuleZFSUtil::addMissingOMVMntEnt(); //Adds missing ZFS filesystems to the OMV core
108 public function passParam($params, $context) {
109 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
110 // Validate the parameters of the RPC service method.
111 $this->validateMethodParams($params, '{
114 "key":{"type":"string"},
115 "value":{"type":"string"}
118 return array($params['key'] => $params['value']);
121 public function addObject($params, $context) {
122 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
123 // Validate the parameters of the RPC service method.
124 $this->validateMethodParams($params, '{
127 "type":{"type":"string","enum":["filesystem","snapshot",' .
129 "path":{"type":"string"},
130 "name":{"type":"string"},
131 "size":{"type":"string"}
134 switch ($params['type']) {
136 $name = $params['path'] . "/" . $params['name'];
137 $tmp = new OMVModuleZFSDataset($name);
140 $name = $params['path'] . "@" . $params['name'];
141 $tmp = new OMVModuleZFSSnapshot($name);
144 $name = $params['path'] . "/" . $params['name'];
145 $tmp = new OMVModuleZFSZvol($name);
146 $tmp->create($params['size']);
149 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
154 public function deleteObject($params, $context) {
155 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
156 // Validate the parameters of the RPC service method.
157 $this->validateMethodParams($params, '{
160 "type":{"type":"string","enum":["Filesystem","Snapshot",' .
161 '"Volume","Clone","Pool"]},
162 "name":{"type":"string"}
166 $name = $params['name'];
167 switch ($params['type']) {
169 OMVModuleZFSUtil::deleteShares($name);
170 $tmp = new OMVModuleZFSDataset($name);
174 $tmp = new OMVModuleZFSDataset($name);
178 $tmp = new OMVModuleZFSSnapshot($name);
182 $tmp = new OMVModuleZFSZvol($name);
186 $disks = OMVModuleZFSUtil::getDevDisksByPool($name);
187 $pooluuid = OMVModuleZFSUtil::getUUIDbyName($name);
188 $tmp = new OMVModuleZFSZpool($name);
190 $xpath = "//system/fstab/mntent[fsname='" . $pooluuid . "' and type='zfs']";
191 $object = $xmlConfig->get($xpath);
192 $xmlConfig->delete($xpath);
193 OMVModuleZFSUtil::clearZFSLabel($disks);
196 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
201 public function getProperties($params, $context) {
202 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
203 // Validate the parameters of the RPC service method.
204 $this->validateMethodParams($params, '{
207 "type":{"type":"string"},
208 "name":{"type":"string"},
209 "start":{"type":"integer"},
210 "limit":{'.$GLOBALS['OMV_JSONSCHEMA_COUNTFIELD'].'},
211 "sortfield":{'.$GLOBALS['OMV_JSONSCHEMA_SORTFIELD'].'},
212 "sortdir":{'.$GLOBALS['OMV_JSONSCHEMA_SORTDIR'].'}
216 $name = $params['name'];
217 switch ($params['type']) {
220 $tmp = new OMVModuleZFSDataset($name);
223 $tmp = new OMVModuleZFSSnapshot($name);
226 $tmp = new OMVModuleZFSZvol($name);
229 $tmp = new OMVModuleZFSZpool($name);
232 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
235 $properties = $tmp->getProperties();
236 foreach ($properties as $propertyk => $propertyv) {
237 if (!(strcmp($propertyv['source'], "-") == 0)) {
238 $objects[] = array('property' => $propertyk,
239 'value' => $propertyv['value'],
240 'source' => $propertyv['source'],
241 'modified' => "false");
247 public function setProperties($params, $context) {
248 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
249 // Validate the parameters of the RPC service method.
250 $this->validateMethodParams($params, '{
253 "type":{"type":"string","enum":["Filesystem","Snapshot",' .
254 '"Volume","Clone","Pool"]},
255 "name":{"type":"string"},
256 "properties":{"type":"array","items":{
259 "property":{"type":"string"},
260 "value":{"type":"string"}}}}
264 switch ($params['type']) {
267 $tmp = new OMVModuleZFSDataset($params['name']);
270 $tmp = new OMVModuleZFSSnapshot($params['name']);
273 $tmp = new OMVModuleZFSZvol($params['name']);
276 $tmp = new OMVModuleZFSZpool($params['name']);
279 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
282 foreach ($params['properties'] as $property) {
285 $objects[$property['property']] = $property['value'];
286 $tmp->setProperties($objects);
287 if ((strcmp($property['property'], "mountpoint") === 0) && (strcmp($params['type'], "Filesystem") === 0)) {
288 OMVModuleZFSUtil::relocateFilesystem($params['name']);
293 public function inherit($params, $context) {
294 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
295 // Validate the parameters of the RPC service method.
296 $this->validateMethodParams($params, '{
299 "type":{"type":"string","enum":["Filesystem","Snapshot",' .
300 '"Volume","Clone","Pool"]},
301 "name":{"type":"string"},
302 "property":{"type":"string"}
305 // Create a background process.
306 $bgStatusFilename = $this->createBgProcStatus();
307 $pid = $this->fork();
308 if($pid > 0) { // Parent process.
309 $this->initializeBgProcStatus($bgStatusFilename, $pid);
310 return $bgStatusFilename;
314 $bgOutputFilename = $this->createBgProcOutput();
315 $this->updateBgProcStatus($bgStatusFilename, "outputfilename", $bgOutputFilename);
316 switch ($params['type']) {
319 $tmp = new OMVModuleZFSDataset($params['name']);
322 $tmp = new OMVModuleZFSSnapshot($params['name']);
325 $tmp = new OMVModuleZFSZvol($params['name']);
328 $tmp = new OMVModuleZFSZpool($params['name']);
331 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
334 $tmp->inherit($params['property']);
335 $this->finalizeBgProcStatus($bgStatusFilename, $output);
337 } catch(Exception $e) {
338 $this->finalizeBgProcStatus($bgStatusFilename, "", $e);
343 public function getSharedParams($params, $context) {
344 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
345 // Validate the parameters of the RPC service method.
346 $this->validateMethodParams($params, '{
349 "type":{"type":"string"},
350 "name":{"type":"string"}
354 //$ds = new OMVModuleZFSDataset($params['name']);
355 //$mountpoint = $ds->getMountPoint();
357 //"mountpoint" => $mountpoint,
358 "name" => $params['name'],
359 "type" => $params['type']);
362 public function createShare($params, $context) {
364 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
365 // Validate the parameters of the RPC service method.
366 $this->validateMethodParams($params, '{
369 "name":{"type":"string"},
370 "type":{"type":"string","enum":["Filesystem","Clone"]},
371 "sharename":{'.$GLOBALS['OMV_JSONSCHEMA_SHARENAME'].'},
372 "comment":{"type":"string"},
373 "mode":{"type":"string","enum":["700","750","755",'.
374 '"770","775","777"],"optional":true},
375 "mountpoint":{"type":"string"}
379 // The field 'reldirpath' may not contain the characters '..'. This
380 // is because of security reasons: the given canonicalized absolute
381 // path MUST be below the given mount point.
382 if(1 == preg_match("/\.\./", $params['mountpoint'])) {
383 throw new OMVException(OMVErrorMsg::E_RPC_SERVICE_METHOD_INVALID_PARAMS,
384 sprintf(gettext("The field '%s' contains forbidden two-dot symbols"), "mountpoint"));
387 // Prepare the configuration object. Use the name of the shared
388 // folder as the relative directory name of the share.
389 switch ($params['type']) {
391 $tmp = new OMVModuleZFSDataset($params['name']);
394 $tmp = new OMVModuleZFSDataset($params['name']);
397 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
401 $poolname = OMVModuleZFSUtil::getPoolname($params['name']);
402 $pooluuid = OMVModuleZFSUtil::getUUIDbyName($poolname);
403 $dir = $tmp->getMountPoint();
405 //Get the mntent object and fetch it's uuid.
406 $xpath = "//system/fstab/mntent[fsname='" . $pooluuid . "' and dir='" . $dir . "' and type='zfs']";
407 $mountpoint = $xmlConfig->get($xpath);
408 $mntentref = $mountpoint['uuid'];
410 $uuid = OMVUtil::uuid();
411 $pathName = $dir . "/" . trim($params['mountpoint'], "/");
412 $reldirpath = trim($params['mountpoint'], "/");
415 "name" => $params['sharename'],
416 "comment" => $params['comment'] . "*** ZFS share on " . $params['name'] . " ***",
417 "mntentref" => $mntentref,
418 "reldirpath" => $reldirpath
421 // Set the configuration object.
423 // Check uniqueness. The share name must be global unique because
424 // the name is also used when exporting a shared folder via NFS for
426 $xpath = sprintf("//system/shares/sharedfolder[name='%s']",
428 if(TRUE === $xmlConfig->exists($xpath)) {
429 throw new OMVException(OMVErrorMsg::E_CONFIG_OBJECT_UNIQUENESS,
430 gettext("A shared folder with the given name already exists"));
433 // Add empty list of privileges per default.
434 $object['privileges'] = array();
436 // Append object to configuration.
437 $success = $xmlConfig->set("//system/shares",
438 array("sharedfolder" => $object));
439 if(FALSE === $success) {
440 throw new OMVException(OMVErrorMsg::E_CONFIG_SET_OBJECT_FAILED);
443 // Append the file mode field to the notification object if set.
445 $object['mode'] = "775";
446 if(array_key_exists("mode", $params)) {
447 $object['mode'] = $params['mode'];
450 // Create the shared folder directory if necessary.
451 if(FALSE === file_exists($pathName)) {
452 // Create the directory. Note, the function seems to have a bug
453 // when using the mask parameter. E.g. octdec("777") does not
454 // create the correct permissions as expected, thus change the
456 if(FALSE === mkdir($pathName, 0700, TRUE)) {
457 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
458 sprintf("Failed to create the directory '%s'", $pathName));
460 // Change the directory mode.
461 if(FALSE === chmod($pathName, octdec($object['mode']))) {
462 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
463 sprintf("Failed to set file mode to '%s' for '%s'",
464 $object['mode'], $pathName));
468 // Change group owner of directory to configured default group,
470 if(FALSE === chgrp($pathName, $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'])) {
471 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
472 sprintf("Failed to set file group to '%s' for '%s'",
473 $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'], $pathName));
476 // Set the setgid bit. Setting this permission means that all files
477 // created in the folder will inherit the group of the folder rather
478 // than the primary group of the user who creates the file.
479 $mode = fileperms($pathName) | 02000;
480 if(FALSE === chmod($pathName, $mode)) {
481 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
482 sprintf("Failed to set file mode to '%o' for '%s'",
486 // Notify configuration changes.
487 $dispatcher = &OMVNotifyDispatcher::getInstance();
488 $dispatcher->notify(OMV_NOTIFY_CREATE,"org.openmediavault.system.shares.sharedfolder", $object);
489 // Return the configuration object.
493 public function getObjectDetails($params, $context) {
494 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
495 // Validate the parameters of the RPC service method.
496 $this->validateMethodParams($params, '{
499 "name":{"type":"string"},
500 "type":{"type":"string"}
504 switch ($params['type']) {
506 $output .= "Filesystem details (zfs get all):\n\r\n\r";
507 $cmd = "zfs get all {$params['name']}";
510 $output .= "Volume details (zfs get all):\n\r\n\r";
511 $cmd = "zfs get all {$params['name']}";
514 $output .= "Snapshot details (zfs get all):\n\r\n\r";
515 $cmd = "zfs get all {$params['name']}";
518 $output .= "Pool details (zpool get all):\n\r\n\r";
519 $cmd = "zpool get all {$params['name']}";
522 throw new OMVModuleZFSException("Incorrect type provided");
524 OMVModuleZFSUtil::exec($cmd,$out,$res);
525 $output .= implode("\n\r", $out);
526 return array("details" => $output);
530 // Register the RPC service.
531 $rpcServiceMgr = &OMVRpcServiceMgr::getInstance(); // Get the "root" instance for the Services
532 $rpcServiceMgr->registerService(new OMVRpcServiceZFS()); // Register a new instance of the RPC service described above