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");
35 public function addPool($params, $context) {
36 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
37 // Validate the parameters of the RPC service method.
38 $this->validateMethodParams($params, '{
41 "pooltype":{"type":"string","enum":["basic","mirror",' .
42 '"raidz1","raidz2","raidz3"]},
43 "force":{"type":"boolean"},
44 "mountpoint":{"type":"string"},
45 "name":{"type":"string"},
46 "devices":{"type":"string"}
49 switch ($params['pooltype']) {
51 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSPLAIN;
54 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSMIRROR;
57 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ1;
60 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ2;
63 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ3;
66 throw new OMVModuleZFSException("Incorrect pool type specified");
69 //Check for user supplied options
71 if ($params['force']) {
74 if (strlen($params['mountpoint']) > 0) {
75 $opts .= "-m " . $params['mountpoint'] . " ";
78 //Use /dev/disk/by-path as deafult when creating new pools as suggested in ZoL FAQ.
79 $disks = preg_split("/[,;]/", $params['devices']);
80 if (file_exists("/dev/disk/by-path/")) {
82 foreach ($disks as $disk) {
83 $tmp_disks[] = OMVModuleZFSUtil::getDiskPath($disk);
88 $vdev = new OMVModuleZFSVdev($params['name'], $pooltype, $disks);
89 $pool = new OMVModuleZFSZpool($vdev, $opts);
90 //Ugly fix to solve the problem of blkid not displaying info on newly created pools
92 $pool->import($pool->getName());
95 public function getObjectTree($params, $context) {
96 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
97 $objects = OMVModuleZFSUtil::getZFSFlatArray();
99 foreach ($objects as $a){
100 $new[$a['parentid']][] = $a;
102 $tree = OMVModuleZFSUtil::createTree($new, $new['root']);
103 OMVModuleZFSUtil::addMissingOMVMntEnt(); //Adds missing ZFS filesystems to the OMV core
107 public function passParam($params, $context) {
108 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
109 // Validate the parameters of the RPC service method.
110 $this->validateMethodParams($params, '{
113 "key":{"type":"string"},
114 "value":{"type":"string"}
117 return array($params['key'] => $params['value']);
120 public function addObject($params, $context) {
121 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
122 // Validate the parameters of the RPC service method.
123 $this->validateMethodParams($params, '{
126 "type":{"type":"string","enum":["filesystem","snapshot",' .
128 "path":{"type":"string"},
129 "name":{"type":"string"},
130 "size":{"type":"string"}
133 switch ($params['type']) {
135 $name = $params['path'] . "/" . $params['name'];
136 $tmp = new OMVModuleZFSDataset($name);
139 $name = $params['path'] . "@" . $params['name'];
140 $tmp = new OMVModuleZFSSnapshot($name);
143 $name = $params['path'] . "/" . $params['name'];
144 $tmp = new OMVModuleZFSZvol($name);
145 $tmp->create($params['size']);
148 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
153 public function deleteObject($params, $context) {
154 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
155 // Validate the parameters of the RPC service method.
156 $this->validateMethodParams($params, '{
159 "type":{"type":"string","enum":["Filesystem","Snapshot",' .
160 '"Volume","Clone","Pool"]},
161 "name":{"type":"string"}
165 $name = $params['name'];
166 switch ($params['type']) {
168 OMVModuleZFSUtil::deleteShares($name);
169 $tmp = new OMVModuleZFSDataset($name);
173 $tmp = new OMVModuleZFSDataset($name);
177 $tmp = new OMVModuleZFSSnapshot($name);
181 $tmp = new OMVModuleZFSZvol($name);
185 $disks = OMVModuleZFSUtil::getDevDisksByPool($name);
186 $pooluuid = OMVModuleZFSUtil::getUUIDbyName($name);
187 $tmp = new OMVModuleZFSZpool($name);
189 $xpath = "//system/fstab/mntent[fsname='" . $pooluuid . "' and type='zfs']";
190 $object = $xmlConfig->get($xpath);
191 $xmlConfig->delete($xpath);
192 $dispatcher = &OMVNotifyDispatcher::getInstance();
193 $dispatcher->notify(OMV_NOTIFY_DELETE,"org.openmediavault.system.fstab.mntent", $object);
194 OMVModuleZFSUtil::clearZFSLabel($disks);
197 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
202 public function getProperties($params, $context) {
203 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
204 // Validate the parameters of the RPC service method.
205 $this->validateMethodParams($params, '{
208 "type":{"type":"string"},
209 "name":{"type":"string"},
210 "start":{"type":"integer"},
211 "limit":{'.$GLOBALS['OMV_JSONSCHEMA_COUNTFIELD'].'},
212 "sortfield":{'.$GLOBALS['OMV_JSONSCHEMA_SORTFIELD'].'},
213 "sortdir":{'.$GLOBALS['OMV_JSONSCHEMA_SORTDIR'].'}
217 $name = $params['name'];
218 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",' .
255 '"Volume","Clone","Pool"]},
256 "name":{"type":"string"},
257 "properties":{"type":"array","items":{
260 "property":{"type":"string"},
261 "value":{"type":"string"}}}}
265 switch ($params['type']) {
268 $tmp = new OMVModuleZFSDataset($params['name']);
271 $tmp = new OMVModuleZFSSnapshot($params['name']);
274 $tmp = new OMVModuleZFSZvol($params['name']);
277 $tmp = new OMVModuleZFSZpool($params['name']);
280 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
283 foreach ($params['properties'] as $property) {
284 $objects[$property['property']] = $property['value'];
286 $tmp->setProperties($objects);
289 public function inherit($params, $context) {
290 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
291 // Validate the parameters of the RPC service method.
292 $this->validateMethodParams($params, '{
295 "type":{"type":"string","enum":["Filesystem","Snapshot",' .
296 '"Volume","Clone","Pool"]},
297 "name":{"type":"string"},
298 "property":{"type":"string"}
301 // Create a background process.
302 $bgStatusFilename = $this->createBgProcStatus();
303 $pid = $this->fork();
304 if($pid > 0) { // Parent process.
305 $this->initializeBgProcStatus($bgStatusFilename, $pid);
306 return $bgStatusFilename;
310 $bgOutputFilename = $this->createBgProcOutput();
311 $this->updateBgProcStatus($bgStatusFilename, "outputfilename", $bgOutputFilename);
312 switch ($params['type']) {
315 $tmp = new OMVModuleZFSDataset($params['name']);
318 $tmp = new OMVModuleZFSSnapshot($params['name']);
321 $tmp = new OMVModuleZFSZvol($params['name']);
324 $tmp = new OMVModuleZFSZpool($params['name']);
327 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
330 $tmp->inherit($params['property']);
331 $this->finalizeBgProcStatus($bgStatusFilename, $output);
333 } catch(Exception $e) {
334 $this->finalizeBgProcStatus($bgStatusFilename, "", $e);
339 public function getSharedParams($params, $context) {
340 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
341 // Validate the parameters of the RPC service method.
342 $this->validateMethodParams($params, '{
345 "type":{"type":"string"},
346 "name":{"type":"string"}
350 $ds = new OMVModuleZFSDataset($params['name']);
351 $mountpoint = $ds->getMountPoint();
353 "mountpoint" => $mountpoint,
354 "name" => $params['name'],
355 "type" => $params['type']);
358 public function createShare($params, $context) {
360 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
361 // Validate the parameters of the RPC service method.
362 $this->validateMethodParams($params, '{
365 "name":{"type":"string"},
366 "type":{"type":"string","enum":["Filesystem","Clone"]},
367 "sharename":{'.$GLOBALS['OMV_JSONSCHEMA_SHARENAME'].'},
368 "comment":{"type":"string"},
369 "mode":{"type":"string","enum":["700","750","755",'.
370 '"770","775","777"],"optional":true},
371 "mountpoint":{"type":"string"}
375 //Get the UUID of the Pool
376 $poolname = OMVModuleZFSUtil::getPoolname($params['name']);
377 $pooluuid = OMVModuleZFSUtil::getUUIDbyName($poolname);
379 //Get the mntent object and fetch it's uuid.
380 $xpath = "//system/fstab/mntent[fsname='" . $pooluuid . "']";
381 $mountpoint = $xmlConfig->get($xpath);
382 $mntentref = $mountpoint['uuid'];
384 // Prepare the configuration object. Use the name of the shared
385 // folder as the relative directory name of the share.
386 switch ($params['type']) {
388 $tmp = new OMVModuleZFSDataset($params['name']);
391 $tmp = new OMVModuleZFSDataset($params['name']);
394 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
398 $uuid = OMVUtil::uuid();
399 $pathName = $tmp->getMountPoint();
400 $reldirpath = OMVModuleZFSUtil::getReldirpath($pathName);
403 "name" => $params['sharename'],
404 "comment" => $params['comment'],
405 "mntentref" => $mntentref,
406 "reldirpath" => $reldirpath
409 // Set the configuration object.
411 // Check uniqueness. The share name must be global unique because
412 // the name is also used when exporting a shared folder via NFS for
414 $xpath = sprintf("//system/shares/sharedfolder[name='%s']",
416 if(TRUE === $xmlConfig->exists($xpath)) {
417 throw new OMVException(OMVErrorMsg::E_CONFIG_OBJECT_UNIQUENESS,
418 gettext("A shared folder with the given name already exists"));
421 // Add empty list of privileges per default.
422 $object['privileges'] = array();
424 // Append object to configuration.
425 $success = $xmlConfig->set("//system/shares",
426 array("sharedfolder" => $object));
427 if(FALSE === $success) {
428 throw new OMVException(OMVErrorMsg::E_CONFIG_SET_OBJECT_FAILED);
431 // Append the file mode field to the notification object if set.
433 $object['mode'] = "775";
434 if(array_key_exists("mode", $params)) {
435 $object['mode'] = $params['mode'];
438 // Change group owner of directory to configured default group,
440 if(FALSE === chgrp($pathName, $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'])) {
441 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
442 sprintf("Failed to set file group to '%s' for '%s'",
443 $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'], $pathName));
446 // Set the setgid bit. Setting this permission means that all files
447 // created in the folder will inherit the group of the folder rather
448 // than the primary group of the user who creates the file.
449 $mode = fileperms($pathName) | 02000;
450 if(FALSE === chmod($pathName, $mode)) {
451 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
452 sprintf("Failed to set file mode to '%o' for '%s'",
456 // Notify configuration changes.
457 $dispatcher = &OMVNotifyDispatcher::getInstance();
458 $dispatcher->notify(OMV_NOTIFY_CREATE,"org.openmediavault.system.shares.sharedfolder", $object);
459 // Return the configuration object.
465 // Register the RPC service.
466 $rpcServiceMgr = &OMVRpcServiceMgr::getInstance(); // Get the "root" instance for the Services
467 $rpcServiceMgr->registerService(new OMVRpcServiceZFS()); // Register a new instance of the RPC service described above