]>
Commit | Line | Data |
---|---|---|
6b3ce31b MR |
1 | <?php |
2 | ||
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 | ||
14 | class OMVRpcServiceZFS extends OMVRpcServiceAbstract { | |
15 | public function getName() { return "ZFS";} // RPC Service name. Same as in .js files | |
16 | ||
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"); | |
28 | } | |
29 | ||
30 | public function getObjectTree($params, $context) { | |
31 | $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR)); | |
32 | $objects = OMVModuleZFSUtil::getZFSFlatArray(); | |
33 | $new = array(); | |
34 | foreach ($objects as $a){ | |
35 | $new[$a['parentid']][] = $a; | |
36 | } | |
37 | $tree = OMVModuleZFSUtil::createTree($new, $new['root']); | |
38 | return $tree; | |
39 | } | |
40 | ||
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']); | |
46 | } | |
47 | ||
48 | public function addObject($params, $context) { | |
49 | $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR)); | |
50 | switch ($params['type']) { | |
51 | case "filesystem": | |
52 | $name = $params['path'] . "/" . $params['name']; | |
53 | $tmp = new OMVModuleZFSDataset($name); | |
54 | break; | |
55 | case "snapshot": | |
56 | $name = $params['path'] . "@" . $params['name']; | |
57 | $tmp = new OMVModuleZFSSnapshot($name); | |
58 | break; | |
59 | case "volume": | |
60 | $name = $params['path'] . "/" . $params['name']; | |
61 | $tmp = new OMVModuleZFSZvol($name); | |
62 | $tmp->create($params['size']); | |
63 | break; | |
64 | default: | |
65 | throw new OMVModuleZFSException("Illegal type provided: " . $params['type']); | |
66 | break; | |
67 | } | |
68 | } | |
69 | ||
70 | public function deleteObject($params, $context) { | |
71 | $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR)); | |
72 | switch ($params['type']) { | |
81500319 MR |
73 | case "Filesystem": |
74 | case "Clone": | |
6b3ce31b MR |
75 | $name = $params['name']; |
76 | $tmp = new OMVModuleZFSDataset($name); | |
77 | $tmp->destroy(); | |
78 | break; | |
79 | case "Snapshot": | |
80 | $name = $params['name']; | |
81 | $tmp = new OMVModuleZFSSnapshot($name); | |
82 | $tmp->destroy(); | |
83 | break; | |
84 | case "Volume": | |
85 | $name = $params['name']; | |
86 | $tmp = new OMVModuleZFSZvol($name); | |
87 | $tmp->destroy(); | |
88 | break; | |
89 | default: | |
90 | throw new OMVModuleZFSException("Illegal type provided: " . $params['type']); | |
91 | break; | |
92 | } | |
93 | } | |
94 | ||
95 | public function getProperties($params, $context) { | |
96 | $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR)); | |
97 | $objects = array(); | |
98 | $name = $params['name']; | |
99 | switch ($params['type']) { | |
100 | case "Filesystem" || "Clone": | |
101 | $tmp = new OMVModuleZFSDataset($name); | |
102 | break; | |
103 | case "Snapshot": | |
104 | $tmp = new OMVModuleZFSSnapshot($name); | |
105 | break; | |
106 | case "Volume": | |
107 | $tmp = new OMVModuleZFSZvol($name); | |
108 | break; | |
109 | default: | |
110 | throw new OMVModuleZFSException("Illegal type provided: " . $params['type']); | |
111 | break; | |
112 | } | |
113 | $properties = $tmp->getProperties(); | |
114 | foreach ($properties as $propertyk => $propertyv) { | |
115 | if (!(strcmp($propertyv['source'], "-") == 0)) { | |
116 | $objects[] = array('property' => $propertyk, | |
117 | 'value' => $propertyv['value'], | |
118 | 'source' => $propertyv['source'], | |
119 | 'modified' => "false"); | |
120 | } | |
121 | } | |
122 | return $objects; | |
123 | } | |
124 | ||
125 | public function setProperties($params, $context) { | |
126 | $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR)); | |
127 | $objects = array(); | |
128 | switch ($params['type']) { | |
129 | case "Filesystem" || "Clone": | |
130 | $tmp = new OMVModuleZFSDataset($params['name']); | |
131 | break; | |
132 | case "Snapshot": | |
133 | $tmp = new OMVModuleZFSSnapshot($params['name']); | |
134 | break; | |
135 | case "Volume": | |
136 | $tmp = new OMVModuleZFSZvol($params['name']); | |
137 | break; | |
138 | default: | |
139 | throw new OMVModuleZFSException("Illegal type provided: " . $params['type']); | |
140 | break; | |
81500319 | 141 | } |
6b3ce31b MR |
142 | foreach ($params['properties'] as $property) { |
143 | $objects[$property['property']] = $property['value']; | |
144 | } | |
145 | $tmp->setProperties($objects); | |
146 | } | |
147 | ||
148 | public function inherit($params, $context) { | |
149 | $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR)); | |
150 | // Create a background process. | |
151 | $bgStatusFilename = $this->createBgProcStatus(); | |
152 | $pid = $this->fork(); | |
153 | if($pid > 0) { // Parent process. | |
154 | $this->initializeBgProcStatus($bgStatusFilename, $pid); | |
155 | return $bgStatusFilename; | |
156 | } | |
157 | // Child process. | |
158 | try { | |
159 | $bgOutputFilename = $this->createBgProcOutput(); | |
160 | $this->updateBgProcStatus($bgStatusFilename, "outputfilename", $bgOutputFilename); | |
161 | switch ($params['type']) { | |
162 | case "Filesystem" || "Clone": | |
163 | $tmp = new OMVModuleZFSDataset($params['name']); | |
164 | break; | |
165 | case "Snapshot": | |
166 | $tmp = new OMVModuleZFSSnapshot($params['name']); | |
167 | break; | |
168 | case "Volume": | |
169 | $tmp = new OMVModuleZFSZvol($params['name']); | |
170 | break; | |
171 | default: | |
172 | throw new OMVModuleZFSException("Illegal type provided: " . $params['type']); | |
173 | break; | |
174 | } | |
175 | $tmp->inherit($params['property']); | |
176 | $this->finalizeBgProcStatus($bgStatusFilename, $output); | |
177 | exit(0); | |
178 | } catch(Exception $e) { | |
179 | $this->finalizeBgProcStatus($bgStatusFilename, "", $e); | |
180 | exit(1); | |
181 | } | |
182 | } | |
183 | ||
184 | public function getSharedParams($params, $context) { | |
185 | $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR)); | |
186 | $objects = array(); | |
187 | $ds = new OMVModuleZFSDataset($params['name']); | |
188 | $mountpoint = $ds->getMountPoint(); | |
189 | return array( | |
190 | "mountpoint" => $mountpoint, | |
191 | "name" => $params['name'], | |
192 | "type" => $params['type']); | |
193 | } | |
194 | ||
195 | public function createShare($params, $context) { | |
196 | global $xmlConfig; | |
197 | $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR)); | |
198 | ||
199 | //Get the UUID of the Pool | |
200 | $pooluuid = OMVModuleZFSUtil::getUUIDbyName($params['name']); | |
201 | preg_match('/^([A-Za-z0-9]+)\/?.*$/', $params['name'], $result); | |
202 | $poolname = $result[1]; | |
203 | unset($result); | |
204 | ||
205 | //Check if the UUID is already stored as an mntent object. If it isn't then create it. | |
206 | $xpath = "//system/fstab/mntent[fsname=" . $pooluuid . "]"; | |
207 | $object = $xmlConfig->get($xpath); | |
208 | if(is_null($object)) { | |
209 | $uuid = OMVUtil::uuid(); | |
210 | $ds = new OMVModuleZFSDataset($poolname); | |
211 | $dir = $ds->getMountPoint(); | |
212 | $object = array( | |
213 | "uuid" => $uuid, | |
214 | "fsname" => $pooluuid, | |
215 | "dir" => $dir, | |
216 | "type" => "zfs", | |
217 | "opts" => "rw,relatime,xattr", | |
218 | "freq" => "0", | |
219 | "passno" => "2" | |
220 | ); | |
221 | $xmlConfig->set("//system/fstab",array("mntent" => $object)); | |
222 | $dispatcher = &OMVNotifyDispatcher::getInstance(); | |
223 | $dispatcher->notify(OMV_NOTIFY_CREATE,"org.openmediavault.system.fstab.mntent", $object); | |
224 | } | |
225 | ||
226 | //Get the mntent object and fetch it's uuid. | |
227 | $object = $xmlConfig->get($xpath); | |
228 | $mntentref = $object['uuid']; | |
229 | ||
230 | // Prepare the configuration object. Use the name of the shared | |
231 | // folder as the relative directory name of the share. | |
232 | switch ($params['type']) { | |
233 | case "Filesystem" || "Clone": | |
234 | $tmp = new OMVModuleZFSDataset($name); | |
235 | break; | |
236 | default: | |
237 | throw new OMVModuleZFSException("Illegal type provided: " . $params['type']); | |
238 | break; | |
239 | } | |
240 | ||
241 | $uuid = OMVUtil::uuid(); | |
242 | $pathName = $tmp->getMountPoint(); | |
81500319 | 243 | $subdirs = preg_split('/\//',$pathName); |
6b3ce31b MR |
244 | $reldirpath = $subdirs[count($subdirs)-1]; |
245 | $object = array( | |
246 | "uuid" => $uuid, | |
247 | "name" => $params['sharename'], | |
248 | "comment" => $params['comment'], | |
249 | "mntentref" => $mntentref, | |
250 | "reldirpath" => $reldirpath | |
251 | ); | |
252 | ||
253 | // Set the configuration object. | |
254 | $success = FALSE; | |
255 | // Check uniqueness. The share name must be global unique because | |
256 | // the name is also used when exporting a shared folder via NFS for | |
257 | // example. | |
258 | $xpath = sprintf("//system/shares/sharedfolder[name='%s']", | |
259 | $params['name']); | |
260 | if(TRUE === $xmlConfig->exists($xpath)) { | |
261 | throw new OMVException(OMVErrorMsg::E_CONFIG_OBJECT_UNIQUENESS, | |
262 | gettext("A shared folder with the given name already exists")); | |
263 | } | |
264 | ||
265 | // Add empty list of privileges per default. | |
266 | $object['privileges'] = array(); | |
267 | ||
268 | // Append object to configuration. | |
269 | $success = $xmlConfig->set("//system/shares", | |
270 | array("sharedfolder" => $object)); | |
271 | if(FALSE === $success) { | |
272 | throw new OMVException(OMVErrorMsg::E_CONFIG_SET_OBJECT_FAILED); | |
273 | } | |
274 | ||
275 | // Append the file mode field to the notification object if set. | |
276 | // Defaults to 775. | |
277 | $object['mode'] = "775"; | |
278 | if(array_key_exists("mode", $params)) { | |
279 | $object['mode'] = $params['mode']; | |
280 | } | |
281 | ||
282 | // Change group owner of directory to configured default group, | |
283 | // e.g. 'users'. | |
284 | if(FALSE === chgrp($pathName, $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'])) { | |
285 | throw new OMVException(OMVErrorMsg::E_MISC_FAILURE, | |
286 | sprintf("Failed to set file group to '%s' for '%s'", | |
287 | $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'], $pathName)); | |
288 | } | |
289 | ||
290 | // Set the setgid bit. Setting this permission means that all files | |
291 | // created in the folder will inherit the group of the folder rather | |
292 | // than the primary group of the user who creates the file. | |
293 | $mode = fileperms($pathName) | 02000; | |
294 | if(FALSE === chmod($pathName, $mode)) { | |
295 | throw new OMVException(OMVErrorMsg::E_MISC_FAILURE, | |
296 | sprintf("Failed to set file mode to '%o' for '%s'", | |
297 | $mode, $pathName)); | |
298 | } | |
299 | ||
300 | // Notify configuration changes. | |
301 | $dispatcher = &OMVNotifyDispatcher::getInstance(); | |
302 | $dispatcher->notify(OMV_NOTIFY_CREATE,"org.openmediavault.system.shares.sharedfolder", $object); | |
303 | // Return the configuration object. | |
304 | return $object; | |
305 | ||
306 | } | |
307 | ||
308 | } | |
309 | ||
310 | // Register the RPC service. | |
81500319 | 311 | $rpcServiceMgr = &OMVRpcServiceMgr::getInstance(); // Get the "root" instance for the Services |
6b3ce31b MR |
312 | $rpcServiceMgr->registerService(new OMVRpcServiceZFS()); // Register a new instance of the RPC service described above |
313 | ?> | |
314 |