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 switch ($params['pooltype']) {
39 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSPLAIN;
42 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSMIRROR;
45 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ1;
48 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ2;
51 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ3;
54 throw new OMVModuleZFSException("Incorrect pool type specified");
57 $disks = preg_split("/[,;]/", $params['devices']);
58 $vdev = new OMVModuleZFSVdev($params['name'], $pooltype, $disks);
59 $pool = new OMVModuleZFSZpool($vdev);
60 //Ugly fix to solve the problem of blkid not displaying info on newly created pools
62 $pool->import($pool->getName());
65 public function getObjectTree($params, $context) {
66 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
67 $objects = OMVModuleZFSUtil::getZFSFlatArray();
69 foreach ($objects as $a){
70 $new[$a['parentid']][] = $a;
72 $tree = OMVModuleZFSUtil::createTree($new, $new['root']);
73 OMVModuleZFSUtil::addMissingOMVMntEnt(); //Adds missing ZFS filesystems to the OMV core
77 public function passParam($params, $context) {
78 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
79 //$msg = "Key=" . $params['key'] . ";Value=" . $params['value'] . ";";
80 //throw new OMVModuleZFSException($msg);
81 return array($params['key'] => $params['value']);
84 public function addObject($params, $context) {
85 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
86 switch ($params['type']) {
88 $name = $params['path'] . "/" . $params['name'];
89 $tmp = new OMVModuleZFSDataset($name);
92 $name = $params['path'] . "@" . $params['name'];
93 $tmp = new OMVModuleZFSSnapshot($name);
96 $name = $params['path'] . "/" . $params['name'];
97 $tmp = new OMVModuleZFSZvol($name);
98 $tmp->create($params['size']);
101 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
106 public function deleteObject($params, $context) {
107 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
108 switch ($params['type']) {
111 $name = $params['name'];
112 $tmp = new OMVModuleZFSDataset($name);
116 $name = $params['name'];
117 $tmp = new OMVModuleZFSSnapshot($name);
121 $name = $params['name'];
122 $tmp = new OMVModuleZFSZvol($name);
126 $name = $params['name'];
127 $tmp = new OMVModuleZFSZpool($name);
131 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
136 public function getProperties($params, $context) {
137 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
139 $name = $params['name'];
140 switch ($params['type']) {
143 $tmp = new OMVModuleZFSDataset($name);
146 $tmp = new OMVModuleZFSSnapshot($name);
149 $tmp = new OMVModuleZFSZvol($name);
152 $tmp = new OMVModuleZFSZpool($name);
155 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
158 $properties = $tmp->getProperties();
159 foreach ($properties as $propertyk => $propertyv) {
160 if (!(strcmp($propertyv['source'], "-") == 0)) {
161 $objects[] = array('property' => $propertyk,
162 'value' => $propertyv['value'],
163 'source' => $propertyv['source'],
164 'modified' => "false");
170 public function setProperties($params, $context) {
171 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
173 switch ($params['type']) {
176 $tmp = new OMVModuleZFSDataset($params['name']);
179 $tmp = new OMVModuleZFSSnapshot($params['name']);
182 $tmp = new OMVModuleZFSZvol($params['name']);
185 $tmp = new OMVModuleZFSZpool($params['name']);
188 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
191 foreach ($params['properties'] as $property) {
192 $objects[$property['property']] = $property['value'];
194 $tmp->setProperties($objects);
197 public function inherit($params, $context) {
198 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
199 // Create a background process.
200 $bgStatusFilename = $this->createBgProcStatus();
201 $pid = $this->fork();
202 if($pid > 0) { // Parent process.
203 $this->initializeBgProcStatus($bgStatusFilename, $pid);
204 return $bgStatusFilename;
208 $bgOutputFilename = $this->createBgProcOutput();
209 $this->updateBgProcStatus($bgStatusFilename, "outputfilename", $bgOutputFilename);
210 switch ($params['type']) {
213 $tmp = new OMVModuleZFSDataset($params['name']);
216 $tmp = new OMVModuleZFSSnapshot($params['name']);
219 $tmp = new OMVModuleZFSZvol($params['name']);
222 $tmp = new OMVModuleZFSZpool($params['name']);
225 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
228 $tmp->inherit($params['property']);
229 $this->finalizeBgProcStatus($bgStatusFilename, $output);
231 } catch(Exception $e) {
232 $this->finalizeBgProcStatus($bgStatusFilename, "", $e);
237 public function getSharedParams($params, $context) {
238 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
240 $ds = new OMVModuleZFSDataset($params['name']);
241 $mountpoint = $ds->getMountPoint();
243 "mountpoint" => $mountpoint,
244 "name" => $params['name'],
245 "type" => $params['type']);
248 public function createShare($params, $context) {
250 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
252 //Get the UUID of the Pool
253 $pooluuid = OMVModuleZFSUtil::getUUIDbyName($params['name']);
254 preg_match('/^([A-Za-z0-9]+)\/?.*$/', $params['name'], $result);
255 $poolname = $result[1];
258 //Get the mntent object and fetch it's uuid.
259 $xpath = "//system/fstab/mntent[fsname=" . $pooluuid . "]";
260 $object = $xmlConfig->get($xpath);
261 $mntentref = $object['uuid'];
263 // Prepare the configuration object. Use the name of the shared
264 // folder as the relative directory name of the share.
265 switch ($params['type']) {
267 $tmp = new OMVModuleZFSDataset($params['name']);
270 $tmp = new OMVModuleZFSDataset($params['name']);
273 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
277 $uuid = OMVUtil::uuid();
278 $pathName = $tmp->getMountPoint();
279 $subdirs = preg_split('/\//',$pathName);
280 $reldirpath = $subdirs[count($subdirs)-1];
283 "name" => $params['sharename'],
284 "comment" => $params['comment'],
285 "mntentref" => $mntentref,
286 "reldirpath" => $reldirpath
289 // Set the configuration object.
291 // Check uniqueness. The share name must be global unique because
292 // the name is also used when exporting a shared folder via NFS for
294 $xpath = sprintf("//system/shares/sharedfolder[name='%s']",
296 if(TRUE === $xmlConfig->exists($xpath)) {
297 throw new OMVException(OMVErrorMsg::E_CONFIG_OBJECT_UNIQUENESS,
298 gettext("A shared folder with the given name already exists"));
301 // Add empty list of privileges per default.
302 $object['privileges'] = array();
304 // Append object to configuration.
305 $success = $xmlConfig->set("//system/shares",
306 array("sharedfolder" => $object));
307 if(FALSE === $success) {
308 throw new OMVException(OMVErrorMsg::E_CONFIG_SET_OBJECT_FAILED);
311 // Append the file mode field to the notification object if set.
313 $object['mode'] = "775";
314 if(array_key_exists("mode", $params)) {
315 $object['mode'] = $params['mode'];
318 // Change group owner of directory to configured default group,
320 if(FALSE === chgrp($pathName, $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'])) {
321 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
322 sprintf("Failed to set file group to '%s' for '%s'",
323 $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'], $pathName));
326 // Set the setgid bit. Setting this permission means that all files
327 // created in the folder will inherit the group of the folder rather
328 // than the primary group of the user who creates the file.
329 $mode = fileperms($pathName) | 02000;
330 if(FALSE === chmod($pathName, $mode)) {
331 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
332 sprintf("Failed to set file mode to '%o' for '%s'",
336 // Notify configuration changes.
337 $dispatcher = &OMVNotifyDispatcher::getInstance();
338 $dispatcher->notify(OMV_NOTIFY_CREATE,"org.openmediavault.system.shares.sharedfolder", $object);
339 // Return the configuration object.
345 // Register the RPC service.
346 $rpcServiceMgr = &OMVRpcServiceMgr::getInstance(); // Get the "root" instance for the Services
347 $rpcServiceMgr->registerService(new OMVRpcServiceZFS()); // Register a new instance of the RPC service described above