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"}
51 switch ($params['pooltype']) {
53 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSPLAIN;
56 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSMIRROR;
59 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ1;
62 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ2;
65 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ3;
68 throw new OMVModuleZFSException("Incorrect pool type specified");
71 //Check for user supplied options
73 if ($params['force']) {
76 if (strlen($params['mountpoint']) > 0) {
77 $opts .= "-m " . $params['mountpoint'] . " ";
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/")) {
84 foreach ($disks as $disk) {
85 $tmp_disks[] = OMVModuleZFSUtil::getDiskPath($disk);
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
94 $pool->import($pool->getName());
97 public function getObjectTree($params, $context) {
98 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
99 $objects = OMVModuleZFSUtil::getZFSFlatArray();
101 foreach ($objects as $a){
102 $new[$a['parentid']][] = $a;
104 $tree = OMVModuleZFSUtil::createTree($new, $new['root']);
105 OMVModuleZFSUtil::addMissingOMVMntEnt(); //Adds missing ZFS filesystems to the OMV core
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, '{
115 "key":{"type":"string"},
116 "value":{"type":"string"}
119 return array($params['key'] => $params['value']);
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, '{
128 "type":{"type":"string","enum":["filesystem","snapshot",' .
130 "path":{"type":"string"},
131 "name":{"type":"string"},
132 "size":{"type":"string"},
133 "clonename":{"type":"string"}
136 switch ($params['type']) {
138 $tmp = new OMVModuleZFSSnapshot($params['path']);
139 $tmp->clonesnap($params['clonename']);
142 $name = $params['path'] . "/" . $params['name'];
143 $tmp = new OMVModuleZFSDataset($name);
146 $name = $params['path'] . "@" . $params['name'];
147 $tmp = new OMVModuleZFSSnapshot($name);
150 $name = $params['path'] . "/" . $params['name'];
151 $tmp = new OMVModuleZFSZvol($name);
152 $tmp->create($params['size']);
155 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
160 public function deleteObject($params, $context) {
161 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
162 // Validate the parameters of the RPC service method.
163 $this->validateMethodParams($params, '{
166 "type":{"type":"string","enum":["Filesystem","Snapshot",' .
168 "name":{"type":"string"}
172 $name = $params['name'];
173 switch ($params['type']) {
175 OMVModuleZFSUtil::deleteShares($name);
176 $tmp = new OMVModuleZFSDataset($name);
180 $tmp = new OMVModuleZFSSnapshot($name);
184 $tmp = new OMVModuleZFSZvol($name);
188 $disks = OMVModuleZFSUtil::getDevDisksByPool($name);
189 $pooluuid = OMVModuleZFSUtil::getUUIDbyName($name);
190 $tmp = new OMVModuleZFSZpool($name);
192 $xpath = "//system/fstab/mntent[fsname='" . $pooluuid . "' and type='zfs']";
193 $object = $xmlConfig->get($xpath);
194 $xmlConfig->delete($xpath);
195 OMVModuleZFSUtil::clearZFSLabel($disks);
198 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
203 public function getProperties($params, $context) {
204 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
205 // Validate the parameters of the RPC service method.
206 $this->validateMethodParams($params, '{
209 "type":{"type":"string"},
210 "name":{"type":"string"},
211 "start":{"type":"integer"},
212 "limit":{'.$GLOBALS['OMV_JSONSCHEMA_COUNTFIELD'].'},
213 "sortfield":{'.$GLOBALS['OMV_JSONSCHEMA_SORTFIELD'].'},
214 "sortdir":{'.$GLOBALS['OMV_JSONSCHEMA_SORTDIR'].'}
218 $name = $params['name'];
219 switch ($params['type']) {
221 $tmp = new OMVModuleZFSDataset($name);
224 $tmp = new OMVModuleZFSSnapshot($name);
227 $tmp = new OMVModuleZFSZvol($name);
230 $tmp = new OMVModuleZFSZpool($name);
233 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
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");
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, '{
254 "type":{"type":"string","enum":["Filesystem","Snapshot",' .
256 "name":{"type":"string"},
257 "properties":{"type":"array","items":{
260 "property":{"type":"string"},
261 "value":{"type":"string"}}}}
265 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",' .
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']) {
318 $tmp = new OMVModuleZFSDataset($params['name']);
321 $tmp = new OMVModuleZFSSnapshot($params['name']);
324 $tmp = new OMVModuleZFSZvol($params['name']);
327 $tmp = new OMVModuleZFSZpool($params['name']);
330 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
333 $tmp->inherit($params['property']);
334 $this->finalizeBgProcStatus($bgStatusFilename, $output);
336 } catch(Exception $e) {
337 $this->finalizeBgProcStatus($bgStatusFilename, "", $e);
342 public function getSharedParams($params, $context) {
343 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
344 // Validate the parameters of the RPC service method.
345 $this->validateMethodParams($params, '{
348 "type":{"type":"string"},
349 "name":{"type":"string"}
353 //$ds = new OMVModuleZFSDataset($params['name']);
354 //$mountpoint = $ds->getMountPoint();
356 //"mountpoint" => $mountpoint,
357 "name" => $params['name'],
358 "type" => $params['type']);
361 public function createShare($params, $context) {
363 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
364 // Validate the parameters of the RPC service method.
365 $this->validateMethodParams($params, '{
368 "name":{"type":"string"},
369 "type":{"type":"string","enum":["Filesystem"]},
370 "sharename":{'.$GLOBALS['OMV_JSONSCHEMA_SHARENAME'].'},
371 "comment":{"type":"string"},
372 "mode":{"type":"string","enum":["700","750","755",'.
373 '"770","775","777"],"optional":true},
374 "mountpoint":{"type":"string"}
378 // The field 'reldirpath' may not contain the characters '..'. This
379 // is because of security reasons: the given canonicalized absolute
380 // path MUST be below the given mount point.
381 if(1 == preg_match("/\.\./", $params['mountpoint'])) {
382 throw new OMVException(OMVErrorMsg::E_RPC_SERVICE_METHOD_INVALID_PARAMS,
383 sprintf(gettext("The field '%s' contains forbidden two-dot symbols"), "mountpoint"));
386 // Prepare the configuration object. Use the name of the shared
387 // folder as the relative directory name of the share.
388 switch ($params['type']) {
390 $tmp = new OMVModuleZFSDataset($params['name']);
393 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
397 $poolname = OMVModuleZFSUtil::getPoolname($params['name']);
398 $pooluuid = OMVModuleZFSUtil::getUUIDbyName($poolname);
399 $dir = $tmp->getMountPoint();
401 //Get the mntent object and fetch it's uuid.
402 $xpath = "//system/fstab/mntent[fsname='" . $pooluuid . "' and dir='" . $dir . "' and type='zfs']";
403 $mountpoint = $xmlConfig->get($xpath);
404 $mntentref = $mountpoint['uuid'];
406 $uuid = OMVUtil::uuid();
407 $pathName = $dir . "/" . trim($params['mountpoint'], "/");
408 $reldirpath = trim($params['mountpoint'], "/");
411 "name" => $params['sharename'],
412 "comment" => $params['comment'] . "*** ZFS share on " . $params['name'] . " ***",
413 "mntentref" => $mntentref,
414 "reldirpath" => $reldirpath
417 // Set the configuration object.
419 // Check uniqueness. The share name must be global unique because
420 // the name is also used when exporting a shared folder via NFS for
422 $xpath = sprintf("//system/shares/sharedfolder[name='%s']",
424 if(TRUE === $xmlConfig->exists($xpath)) {
425 throw new OMVException(OMVErrorMsg::E_CONFIG_OBJECT_UNIQUENESS,
426 gettext("A shared folder with the given name already exists"));
429 // Add empty list of privileges per default.
430 $object['privileges'] = array();
432 // Append object to configuration.
433 $success = $xmlConfig->set("//system/shares",
434 array("sharedfolder" => $object));
435 if(FALSE === $success) {
436 throw new OMVException(OMVErrorMsg::E_CONFIG_SET_OBJECT_FAILED);
439 // Append the file mode field to the notification object if set.
441 $object['mode'] = "775";
442 if(array_key_exists("mode", $params)) {
443 $object['mode'] = $params['mode'];
446 // Create the shared folder directory if necessary.
447 if(FALSE === file_exists($pathName)) {
448 // Create the directory. Note, the function seems to have a bug
449 // when using the mask parameter. E.g. octdec("777") does not
450 // create the correct permissions as expected, thus change the
452 if(FALSE === mkdir($pathName, 0700, TRUE)) {
453 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
454 sprintf("Failed to create the directory '%s'", $pathName));
456 // Change the directory mode.
457 if(FALSE === chmod($pathName, octdec($object['mode']))) {
458 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
459 sprintf("Failed to set file mode to '%s' for '%s'",
460 $object['mode'], $pathName));
464 // Change group owner of directory to configured default group,
466 if(FALSE === chgrp($pathName, $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'])) {
467 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
468 sprintf("Failed to set file group to '%s' for '%s'",
469 $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'], $pathName));
472 // Set the setgid bit. Setting this permission means that all files
473 // created in the folder will inherit the group of the folder rather
474 // than the primary group of the user who creates the file.
475 $mode = fileperms($pathName) | 02000;
476 if(FALSE === chmod($pathName, $mode)) {
477 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
478 sprintf("Failed to set file mode to '%o' for '%s'",
482 // Notify configuration changes.
483 $dispatcher = &OMVNotifyDispatcher::getInstance();
484 $dispatcher->notify(OMV_NOTIFY_CREATE,"org.openmediavault.system.shares.sharedfolder", $object);
485 // Return the configuration object.
489 public function getObjectDetails($params, $context) {
490 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
491 // Validate the parameters of the RPC service method.
492 $this->validateMethodParams($params, '{
495 "name":{"type":"string"},
496 "type":{"type":"string"}
500 switch ($params['type']) {
502 $output .= "Filesystem details (zfs get all):\n\r\n\r";
503 $cmd = "zfs get all {$params['name']}";
506 $output .= "Volume details (zfs get all):\n\r\n\r";
507 $cmd = "zfs get all {$params['name']}";
510 $output .= "Snapshot details (zfs get all):\n\r\n\r";
511 $cmd = "zfs get all {$params['name']}";
514 $output .= "Pool details (zpool get all):\n\r\n\r";
515 $cmd = "zpool get all {$params['name']}";
518 throw new OMVModuleZFSException("Incorrect type provided");
520 OMVModuleZFSUtil::exec($cmd,$out,$res);
521 $output .= implode("\n\r", $out);
522 return array("details" => $output);
525 public function expandPool($params, $context) {
526 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
527 // Validate the parameters of the RPC service method.
528 $this->validateMethodParams($params, '{
531 "vdevtype":{"type":"string","enum":["basic","mirror",' .
532 '"raidz1","raidz2","raidz3"]},
533 "name":{"type":"string"},
534 "devices":{"type":"string"},
535 "force":{"type":"boolean"}
538 $pool = new OMVModuleZFSZpool($params['name']);
539 switch ($params['vdevtype']) {
541 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSPLAIN;
544 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSMIRROR;
547 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ1;
550 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ2;
553 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ3;
556 throw new OMVModuleZFSException("Incorrect pool type specified");
559 if ($params['force']) {
562 //Use /dev/disk/by-path as deafult when creating new pools as suggested in ZoL FAQ.
563 $disks = preg_split("/[,;]/", $params['devices']);
564 if (file_exists("/dev/disk/by-path/")) {
565 $tmp_disks = array();
566 foreach ($disks as $disk) {
567 $tmp_disks[] = OMVModuleZFSUtil::getDiskPath($disk);
571 $vdev[] = new OMVModuleZFSVdev($params['name'], $pooltype, $disks);
572 $pool->addVdev($vdev, $opts);
573 //Ugly fix to solve the problem of blkid not displaying info on newly created pools
575 $pool->import($pool->getName());
579 // Register the RPC service.
580 $rpcServiceMgr = &OMVRpcServiceMgr::getInstance(); // Get the "root" instance for the Services
581 $rpcServiceMgr->registerService(new OMVRpcServiceZFS()); // Register a new instance of the RPC service described above