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 //Check for user supplied options
59 if ($params['force']) {
62 if (strlen($params['mountpoint']) > 0) {
63 $opts .= "-m " . $params['mountpoint'] . " ";
66 $disks = preg_split("/[,;]/", $params['devices']);
67 $vdev = new OMVModuleZFSVdev($params['name'], $pooltype, $disks);
68 $pool = new OMVModuleZFSZpool($vdev, $opts);
69 //Ugly fix to solve the problem of blkid not displaying info on newly created pools
71 $pool->import($pool->getName());
74 public function getObjectTree($params, $context) {
75 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
76 $objects = OMVModuleZFSUtil::getZFSFlatArray();
78 foreach ($objects as $a){
79 $new[$a['parentid']][] = $a;
81 $tree = OMVModuleZFSUtil::createTree($new, $new['root']);
82 OMVModuleZFSUtil::addMissingOMVMntEnt(); //Adds missing ZFS filesystems to the OMV core
86 public function passParam($params, $context) {
87 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
88 //$msg = "Key=" . $params['key'] . ";Value=" . $params['value'] . ";";
89 //throw new OMVModuleZFSException($msg);
90 return array($params['key'] => $params['value']);
93 public function addObject($params, $context) {
94 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
95 switch ($params['type']) {
97 $name = $params['path'] . "/" . $params['name'];
98 $tmp = new OMVModuleZFSDataset($name);
101 $name = $params['path'] . "@" . $params['name'];
102 $tmp = new OMVModuleZFSSnapshot($name);
105 $name = $params['path'] . "/" . $params['name'];
106 $tmp = new OMVModuleZFSZvol($name);
107 $tmp->create($params['size']);
110 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
115 public function deleteObject($params, $context) {
116 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
117 switch ($params['type']) {
120 $name = $params['name'];
121 $tmp = new OMVModuleZFSDataset($name);
125 $name = $params['name'];
126 $tmp = new OMVModuleZFSSnapshot($name);
130 $name = $params['name'];
131 $tmp = new OMVModuleZFSZvol($name);
135 $name = $params['name'];
136 $tmp = new OMVModuleZFSZpool($name);
140 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
145 public function getProperties($params, $context) {
146 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
148 $name = $params['name'];
149 switch ($params['type']) {
152 $tmp = new OMVModuleZFSDataset($name);
155 $tmp = new OMVModuleZFSSnapshot($name);
158 $tmp = new OMVModuleZFSZvol($name);
161 $tmp = new OMVModuleZFSZpool($name);
164 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
167 $properties = $tmp->getProperties();
168 foreach ($properties as $propertyk => $propertyv) {
169 if (!(strcmp($propertyv['source'], "-") == 0)) {
170 $objects[] = array('property' => $propertyk,
171 'value' => $propertyv['value'],
172 'source' => $propertyv['source'],
173 'modified' => "false");
179 public function setProperties($params, $context) {
180 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
182 switch ($params['type']) {
185 $tmp = new OMVModuleZFSDataset($params['name']);
188 $tmp = new OMVModuleZFSSnapshot($params['name']);
191 $tmp = new OMVModuleZFSZvol($params['name']);
194 $tmp = new OMVModuleZFSZpool($params['name']);
197 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
200 foreach ($params['properties'] as $property) {
201 $objects[$property['property']] = $property['value'];
203 $tmp->setProperties($objects);
206 public function inherit($params, $context) {
207 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
208 // Create a background process.
209 $bgStatusFilename = $this->createBgProcStatus();
210 $pid = $this->fork();
211 if($pid > 0) { // Parent process.
212 $this->initializeBgProcStatus($bgStatusFilename, $pid);
213 return $bgStatusFilename;
217 $bgOutputFilename = $this->createBgProcOutput();
218 $this->updateBgProcStatus($bgStatusFilename, "outputfilename", $bgOutputFilename);
219 switch ($params['type']) {
222 $tmp = new OMVModuleZFSDataset($params['name']);
225 $tmp = new OMVModuleZFSSnapshot($params['name']);
228 $tmp = new OMVModuleZFSZvol($params['name']);
231 $tmp = new OMVModuleZFSZpool($params['name']);
234 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
237 $tmp->inherit($params['property']);
238 $this->finalizeBgProcStatus($bgStatusFilename, $output);
240 } catch(Exception $e) {
241 $this->finalizeBgProcStatus($bgStatusFilename, "", $e);
246 public function getSharedParams($params, $context) {
247 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
249 $ds = new OMVModuleZFSDataset($params['name']);
250 $mountpoint = $ds->getMountPoint();
252 "mountpoint" => $mountpoint,
253 "name" => $params['name'],
254 "type" => $params['type']);
257 public function createShare($params, $context) {
259 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
261 //Get the UUID of the Pool
262 $poolname = OMVModuleZFSUtil::getPoolname($params['name']);
263 $pooluuid = OMVModuleZFSUtil::getUUIDbyName($poolname);
265 //Get the mntent object and fetch it's uuid.
266 $xpath = "//system/fstab/mntent[fsname=" . $pooluuid . "]";
267 $mountpoint = $xmlConfig->get($xpath);
268 $mntentref = $mountpoint['uuid'];
270 // Prepare the configuration object. Use the name of the shared
271 // folder as the relative directory name of the share.
272 switch ($params['type']) {
274 $tmp = new OMVModuleZFSDataset($params['name']);
277 $tmp = new OMVModuleZFSDataset($params['name']);
280 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
284 $uuid = OMVUtil::uuid();
285 $pathName = $tmp->getMountPoint();
286 $subdirs = preg_split('/\//',$pathName);
287 $reldirpath = $subdirs[count($subdirs)-1];
290 "name" => $params['sharename'],
291 "comment" => $params['comment'],
292 "mntentref" => $mntentref,
293 "reldirpath" => $reldirpath
296 // Set the configuration object.
298 // Check uniqueness. The share name must be global unique because
299 // the name is also used when exporting a shared folder via NFS for
301 $xpath = sprintf("//system/shares/sharedfolder[name='%s']",
303 if(TRUE === $xmlConfig->exists($xpath)) {
304 throw new OMVException(OMVErrorMsg::E_CONFIG_OBJECT_UNIQUENESS,
305 gettext("A shared folder with the given name already exists"));
308 // Add empty list of privileges per default.
309 $object['privileges'] = array();
311 // Append object to configuration.
312 $success = $xmlConfig->set("//system/shares",
313 array("sharedfolder" => $object));
314 if(FALSE === $success) {
315 throw new OMVException(OMVErrorMsg::E_CONFIG_SET_OBJECT_FAILED);
318 // Append the file mode field to the notification object if set.
320 $object['mode'] = "775";
321 if(array_key_exists("mode", $params)) {
322 $object['mode'] = $params['mode'];
325 // Change group owner of directory to configured default group,
327 if(FALSE === chgrp($pathName, $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'])) {
328 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
329 sprintf("Failed to set file group to '%s' for '%s'",
330 $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'], $pathName));
333 // Set the setgid bit. Setting this permission means that all files
334 // created in the folder will inherit the group of the folder rather
335 // than the primary group of the user who creates the file.
336 $mode = fileperms($pathName) | 02000;
337 if(FALSE === chmod($pathName, $mode)) {
338 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
339 sprintf("Failed to set file mode to '%o' for '%s'",
343 // Notify configuration changes.
344 $dispatcher = &OMVNotifyDispatcher::getInstance();
345 $dispatcher->notify(OMV_NOTIFY_CREATE,"org.openmediavault.system.shares.sharedfolder", $object);
346 // Return the configuration object.
352 // Register the RPC service.
353 $rpcServiceMgr = &OMVRpcServiceMgr::getInstance(); // Get the "root" instance for the Services
354 $rpcServiceMgr->registerService(new OMVRpcServiceZFS()); // Register a new instance of the RPC service described above