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");
14 class OMVRpcServiceZFS extends OMVRpcServiceAbstract {
15 public function getName() { return "ZFS";} // RPC Service name. Same as in .js files
17 /* Initialize the RPC service. Different methods of the RPC service are declared here*/
18 public function initialize() {
19 $this->registerMethod("getObjectTree");
20 $this->registermethod("passParam");
21 $this->registermethod("addObject");
22 $this->registermethod("deleteObject");
23 $this->registermethod("getProperties");
24 $this->registermethod("setProperties");
25 $this->registermethod("inherit");
26 $this->registermethod("getSharedParams");
27 $this->registermethod("createShare");
30 public function getObjectTree($params, $context) {
31 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
32 $objects = OMVModuleZFSUtil::getZFSFlatArray();
34 foreach ($objects as $a){
35 $new[$a['parentid']][] = $a;
37 $tree = OMVModuleZFSUtil::createTree($new, $new['root']);
41 public function passParam($params, $context) {
42 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
43 //$msg = "Key=" . $params['key'] . ";Value=" . $params['value'] . ";";
44 //throw new OMVModuleZFSException($msg);
45 return array($params['key'] => $params['value']);
48 public function addObject($params, $context) {
49 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
50 switch ($params['type']) {
52 $name = $params['path'] . "/" . $params['name'];
53 $tmp = new OMVModuleZFSDataset($name);
56 $name = $params['path'] . "@" . $params['name'];
57 $tmp = new OMVModuleZFSSnapshot($name);
60 $name = $params['path'] . "/" . $params['name'];
61 $tmp = new OMVModuleZFSZvol($name);
62 $tmp->create($params['size']);
65 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
70 public function deleteObject($params, $context) {
71 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
72 switch ($params['type']) {
75 $name = $params['name'];
76 $tmp = new OMVModuleZFSDataset($name);
80 $name = $params['name'];
81 $tmp = new OMVModuleZFSSnapshot($name);
85 $name = $params['name'];
86 $tmp = new OMVModuleZFSZvol($name);
90 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
95 public function getProperties($params, $context) {
96 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
98 $name = $params['name'];
99 switch ($params['type']) {
102 $tmp = new OMVModuleZFSDataset($name);
105 $tmp = new OMVModuleZFSSnapshot($name);
108 $tmp = new OMVModuleZFSZvol($name);
111 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
114 $properties = $tmp->getProperties();
115 foreach ($properties as $propertyk => $propertyv) {
116 if (!(strcmp($propertyv['source'], "-") == 0)) {
117 $objects[] = array('property' => $propertyk,
118 'value' => $propertyv['value'],
119 'source' => $propertyv['source'],
120 'modified' => "false");
126 public function setProperties($params, $context) {
127 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
129 switch ($params['type']) {
132 $tmp = new OMVModuleZFSDataset($params['name']);
135 $tmp = new OMVModuleZFSSnapshot($params['name']);
138 $tmp = new OMVModuleZFSZvol($params['name']);
141 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
144 foreach ($params['properties'] as $property) {
145 $objects[$property['property']] = $property['value'];
147 $tmp->setProperties($objects);
150 public function inherit($params, $context) {
151 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
152 // Create a background process.
153 $bgStatusFilename = $this->createBgProcStatus();
154 $pid = $this->fork();
155 if($pid > 0) { // Parent process.
156 $this->initializeBgProcStatus($bgStatusFilename, $pid);
157 return $bgStatusFilename;
161 $bgOutputFilename = $this->createBgProcOutput();
162 $this->updateBgProcStatus($bgStatusFilename, "outputfilename", $bgOutputFilename);
163 switch ($params['type']) {
166 $tmp = new OMVModuleZFSDataset($params['name']);
169 $tmp = new OMVModuleZFSSnapshot($params['name']);
172 $tmp = new OMVModuleZFSZvol($params['name']);
175 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
178 $tmp->inherit($params['property']);
179 $this->finalizeBgProcStatus($bgStatusFilename, $output);
181 } catch(Exception $e) {
182 $this->finalizeBgProcStatus($bgStatusFilename, "", $e);
187 public function getSharedParams($params, $context) {
188 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
190 $ds = new OMVModuleZFSDataset($params['name']);
191 $mountpoint = $ds->getMountPoint();
193 "mountpoint" => $mountpoint,
194 "name" => $params['name'],
195 "type" => $params['type']);
198 public function createShare($params, $context) {
200 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
202 //Get the UUID of the Pool
203 $pooluuid = OMVModuleZFSUtil::getUUIDbyName($params['name']);
204 preg_match('/^([A-Za-z0-9]+)\/?.*$/', $params['name'], $result);
205 $poolname = $result[1];
208 //Check if the UUID is already stored as an mntent object. If it isn't then create it.
209 $xpath = "//system/fstab/mntent[fsname=" . $pooluuid . "]";
210 $object = $xmlConfig->get($xpath);
211 if(is_null($object)) {
212 $uuid = OMVUtil::uuid();
213 $ds = new OMVModuleZFSDataset($poolname);
214 $dir = $ds->getMountPoint();
217 "fsname" => $pooluuid,
220 "opts" => "rw,relatime,xattr",
224 $xmlConfig->set("//system/fstab",array("mntent" => $object));
225 $dispatcher = &OMVNotifyDispatcher::getInstance();
226 $dispatcher->notify(OMV_NOTIFY_CREATE,"org.openmediavault.system.fstab.mntent", $object);
229 //Get the mntent object and fetch it's uuid.
230 $object = $xmlConfig->get($xpath);
231 $mntentref = $object['uuid'];
233 // Prepare the configuration object. Use the name of the shared
234 // folder as the relative directory name of the share.
235 switch ($params['type']) {
238 $tmp = new OMVModuleZFSDataset($name);
241 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
245 $uuid = OMVUtil::uuid();
246 $pathName = $tmp->getMountPoint();
247 $subdirs = preg_split('/\//',$pathName);
248 $reldirpath = $subdirs[count($subdirs)-1];
251 "name" => $params['sharename'],
252 "comment" => $params['comment'],
253 "mntentref" => $mntentref,
254 "reldirpath" => $reldirpath
257 // Set the configuration object.
259 // Check uniqueness. The share name must be global unique because
260 // the name is also used when exporting a shared folder via NFS for
262 $xpath = sprintf("//system/shares/sharedfolder[name='%s']",
264 if(TRUE === $xmlConfig->exists($xpath)) {
265 throw new OMVException(OMVErrorMsg::E_CONFIG_OBJECT_UNIQUENESS,
266 gettext("A shared folder with the given name already exists"));
269 // Add empty list of privileges per default.
270 $object['privileges'] = array();
272 // Append object to configuration.
273 $success = $xmlConfig->set("//system/shares",
274 array("sharedfolder" => $object));
275 if(FALSE === $success) {
276 throw new OMVException(OMVErrorMsg::E_CONFIG_SET_OBJECT_FAILED);
279 // Append the file mode field to the notification object if set.
281 $object['mode'] = "775";
282 if(array_key_exists("mode", $params)) {
283 $object['mode'] = $params['mode'];
286 // Change group owner of directory to configured default group,
288 if(FALSE === chgrp($pathName, $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'])) {
289 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
290 sprintf("Failed to set file group to '%s' for '%s'",
291 $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'], $pathName));
294 // Set the setgid bit. Setting this permission means that all files
295 // created in the folder will inherit the group of the folder rather
296 // than the primary group of the user who creates the file.
297 $mode = fileperms($pathName) | 02000;
298 if(FALSE === chmod($pathName, $mode)) {
299 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
300 sprintf("Failed to set file mode to '%o' for '%s'",
304 // Notify configuration changes.
305 $dispatcher = &OMVNotifyDispatcher::getInstance();
306 $dispatcher->notify(OMV_NOTIFY_CREATE,"org.openmediavault.system.shares.sharedfolder", $object);
307 // Return the configuration object.
314 // Register the RPC service.
315 $rpcServiceMgr = &OMVRpcServiceMgr::getInstance(); // Get the "root" instance for the Services
316 $rpcServiceMgr->registerService(new OMVRpcServiceZFS()); // Register a new instance of the RPC service described above