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");
15 class OMVRpcServiceZFS extends OMVRpcServiceAbstract {
16 public function getName() { return "ZFS";} // RPC Service name. Same as in .js files
18 /* Initialize the RPC service. Different methods of the RPC service are declared here*/
19 public function initialize() {
20 $this->registerMethod("getObjectTree");
21 $this->registermethod("passParam");
22 $this->registermethod("addObject");
23 $this->registermethod("deleteObject");
24 $this->registermethod("getProperties");
25 $this->registermethod("setProperties");
26 $this->registermethod("inherit");
27 $this->registermethod("getSharedParams");
28 $this->registermethod("createShare");
31 public function getObjectTree($params, $context) {
32 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
33 $objects = OMVModuleZFSUtil::getZFSFlatArray();
35 foreach ($objects as $a){
36 $new[$a['parentid']][] = $a;
38 $tree = OMVModuleZFSUtil::createTree($new, $new['root']);
42 public function passParam($params, $context) {
43 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
44 //$msg = "Key=" . $params['key'] . ";Value=" . $params['value'] . ";";
45 //throw new OMVModuleZFSException($msg);
46 return array($params['key'] => $params['value']);
49 public function addObject($params, $context) {
50 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
51 switch ($params['type']) {
53 $name = $params['path'] . "/" . $params['name'];
54 $tmp = new OMVModuleZFSDataset($name);
57 $name = $params['path'] . "@" . $params['name'];
58 $tmp = new OMVModuleZFSSnapshot($name);
61 $name = $params['path'] . "/" . $params['name'];
62 $tmp = new OMVModuleZFSZvol($name);
63 $tmp->create($params['size']);
66 $name = $params['path'] . "/" . $params['name'];
67 $tmp = new OMVModuleZFSZpool($name);
70 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
75 public function deleteObject($params, $context) {
76 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
77 switch ($params['type']) {
80 $name = $params['name'];
81 $tmp = new OMVModuleZFSDataset($name);
85 $name = $params['name'];
86 $tmp = new OMVModuleZFSSnapshot($name);
90 $name = $params['name'];
91 $tmp = new OMVModuleZFSZvol($name);
95 $name = $params['name'];
96 $tmp = new OMVModuleZFSZpool($name);
100 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
105 public function getProperties($params, $context) {
106 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
108 $name = $params['name'];
109 switch ($params['type']) {
112 $tmp = new OMVModuleZFSDataset($name);
115 $tmp = new OMVModuleZFSSnapshot($name);
118 $tmp = new OMVModuleZFSZvol($name);
121 $tmp = new OMVModuleZFSZpool($name);
124 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
127 $properties = $tmp->getProperties();
128 foreach ($properties as $propertyk => $propertyv) {
129 if (!(strcmp($propertyv['source'], "-") == 0)) {
130 $objects[] = array('property' => $propertyk,
131 'value' => $propertyv['value'],
132 'source' => $propertyv['source'],
133 'modified' => "false");
139 public function setProperties($params, $context) {
140 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
142 switch ($params['type']) {
145 $tmp = new OMVModuleZFSDataset($params['name']);
148 $tmp = new OMVModuleZFSSnapshot($params['name']);
151 $tmp = new OMVModuleZFSZvol($params['name']);
154 $tmp = new OMVModuleZFSZpool($params['name']);
157 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
160 foreach ($params['properties'] as $property) {
161 $objects[$property['property']] = $property['value'];
163 $tmp->setProperties($objects);
166 public function inherit($params, $context) {
167 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
168 // Create a background process.
169 $bgStatusFilename = $this->createBgProcStatus();
170 $pid = $this->fork();
171 if($pid > 0) { // Parent process.
172 $this->initializeBgProcStatus($bgStatusFilename, $pid);
173 return $bgStatusFilename;
177 $bgOutputFilename = $this->createBgProcOutput();
178 $this->updateBgProcStatus($bgStatusFilename, "outputfilename", $bgOutputFilename);
179 switch ($params['type']) {
182 $tmp = new OMVModuleZFSDataset($params['name']);
185 $tmp = new OMVModuleZFSSnapshot($params['name']);
188 $tmp = new OMVModuleZFSZvol($params['name']);
191 $tmp = new OMVModuleZFSZpool($params['name']);
194 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
197 $tmp->inherit($params['property']);
198 $this->finalizeBgProcStatus($bgStatusFilename, $output);
200 } catch(Exception $e) {
201 $this->finalizeBgProcStatus($bgStatusFilename, "", $e);
206 public function getSharedParams($params, $context) {
207 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
209 $ds = new OMVModuleZFSDataset($params['name']);
210 $mountpoint = $ds->getMountPoint();
212 "mountpoint" => $mountpoint,
213 "name" => $params['name'],
214 "type" => $params['type']);
217 public function createShare($params, $context) {
219 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
221 //Get the UUID of the Pool
222 $pooluuid = OMVModuleZFSUtil::getUUIDbyName($params['name']);
223 preg_match('/^([A-Za-z0-9]+)\/?.*$/', $params['name'], $result);
224 $poolname = $result[1];
227 //Check if the UUID is already stored as an mntent object. If it isn't then create it.
228 $xpath = "//system/fstab/mntent[fsname=" . $pooluuid . "]";
229 $object = $xmlConfig->get($xpath);
230 if(is_null($object)) {
231 $uuid = OMVUtil::uuid();
232 $ds = new OMVModuleZFSDataset($poolname);
233 $dir = $ds->getMountPoint();
236 "fsname" => $pooluuid,
239 "opts" => "rw,relatime,xattr",
243 $xmlConfig->set("//system/fstab",array("mntent" => $object));
244 $dispatcher = &OMVNotifyDispatcher::getInstance();
245 $dispatcher->notify(OMV_NOTIFY_CREATE,"org.openmediavault.system.fstab.mntent", $object);
248 //Get the mntent object and fetch it's uuid.
249 $object = $xmlConfig->get($xpath);
250 $mntentref = $object['uuid'];
252 // Prepare the configuration object. Use the name of the shared
253 // folder as the relative directory name of the share.
254 switch ($params['type']) {
257 $tmp = new OMVModuleZFSDataset($name);
260 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
264 $uuid = OMVUtil::uuid();
265 $pathName = $tmp->getMountPoint();
266 $subdirs = preg_split('/\//',$pathName);
267 $reldirpath = $subdirs[count($subdirs)-1];
270 "name" => $params['sharename'],
271 "comment" => $params['comment'],
272 "mntentref" => $mntentref,
273 "reldirpath" => $reldirpath
276 // Set the configuration object.
278 // Check uniqueness. The share name must be global unique because
279 // the name is also used when exporting a shared folder via NFS for
281 $xpath = sprintf("//system/shares/sharedfolder[name='%s']",
283 if(TRUE === $xmlConfig->exists($xpath)) {
284 throw new OMVException(OMVErrorMsg::E_CONFIG_OBJECT_UNIQUENESS,
285 gettext("A shared folder with the given name already exists"));
288 // Add empty list of privileges per default.
289 $object['privileges'] = array();
291 // Append object to configuration.
292 $success = $xmlConfig->set("//system/shares",
293 array("sharedfolder" => $object));
294 if(FALSE === $success) {
295 throw new OMVException(OMVErrorMsg::E_CONFIG_SET_OBJECT_FAILED);
298 // Append the file mode field to the notification object if set.
300 $object['mode'] = "775";
301 if(array_key_exists("mode", $params)) {
302 $object['mode'] = $params['mode'];
305 // Change group owner of directory to configured default group,
307 if(FALSE === chgrp($pathName, $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'])) {
308 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
309 sprintf("Failed to set file group to '%s' for '%s'",
310 $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'], $pathName));
313 // Set the setgid bit. Setting this permission means that all files
314 // created in the folder will inherit the group of the folder rather
315 // than the primary group of the user who creates the file.
316 $mode = fileperms($pathName) | 02000;
317 if(FALSE === chmod($pathName, $mode)) {
318 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
319 sprintf("Failed to set file mode to '%o' for '%s'",
323 // Notify configuration changes.
324 $dispatcher = &OMVNotifyDispatcher::getInstance();
325 $dispatcher->notify(OMV_NOTIFY_CREATE,"org.openmediavault.system.shares.sharedfolder", $object);
326 // Return the configuration object.
333 // Register the RPC service.
334 $rpcServiceMgr = &OMVRpcServiceMgr::getInstance(); // Get the "root" instance for the Services
335 $rpcServiceMgr->registerService(new OMVRpcServiceZFS()); // Register a new instance of the RPC service described above