]>
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"); | |
503ffcc8 | 13 | require_once("zfs/Zpool.php"); |
6b3ce31b MR |
14 | |
15 | class OMVRpcServiceZFS extends OMVRpcServiceAbstract { | |
a36352f7 NB |
16 | public function getName() { |
17 | return "ZFS"; // RPC Service name. Same as in .js files | |
18 | } | |
6b3ce31b | 19 | |
a36352f7 NB |
20 | /* Initialize the RPC service. Different methods of the RPC service are declared here*/ |
21 | public function initialize() { | |
42856e8b | 22 | $this->registerMethod("addPool"); |
a36352f7 NB |
23 | $this->registerMethod("getObjectTree"); |
24 | $this->registermethod("passParam"); | |
25 | $this->registermethod("addObject"); | |
26 | $this->registermethod("deleteObject"); | |
27 | $this->registermethod("getProperties"); | |
28 | $this->registermethod("setProperties"); | |
29 | $this->registermethod("inherit"); | |
30 | $this->registermethod("getSharedParams"); | |
31 | $this->registermethod("createShare"); | |
32 | } | |
6b3ce31b | 33 | |
42856e8b NB |
34 | public function addPool($params, $context) { |
35 | $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR)); | |
36 | switch ($params['pooltype']) { | |
37 | case "basic": | |
38 | $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSPLAIN; | |
39 | break; | |
40 | case "mirror": | |
41 | $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSMIRROR; | |
42 | break; | |
43 | case "raidz1": | |
44 | $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ1; | |
45 | break; | |
46 | case "raidz2": | |
47 | $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ2; | |
48 | break; | |
49 | case "raidz3": | |
50 | $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ3; | |
51 | break; | |
52 | default: | |
53 | throw new OMVModuleZFSException("Incorrect pool type specified"); | |
54 | break; | |
55 | } | |
56 | $disks = preg_split("/[,;]/", $params['devices']); | |
57 | $vdev = new OMVModuleZFSVdev($params['name'], $pooltype, $disks); | |
58 | $pool = new OMVModuleZFSZpool($vdev); | |
59 | } | |
60 | ||
6b3ce31b MR |
61 | public function getObjectTree($params, $context) { |
62 | $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR)); | |
63 | $objects = OMVModuleZFSUtil::getZFSFlatArray(); | |
64 | $new = array(); | |
65 | foreach ($objects as $a){ | |
66 | $new[$a['parentid']][] = $a; | |
67 | } | |
68 | $tree = OMVModuleZFSUtil::createTree($new, $new['root']); | |
a36352f7 | 69 | OMVModuleZFSUtil::addMissingOMVMntEnt(); //Adds missing ZFS filesystems to the OMV core |
6b3ce31b MR |
70 | return $tree; |
71 | } | |
72 | ||
73 | public function passParam($params, $context) { | |
74 | $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR)); | |
75 | //$msg = "Key=" . $params['key'] . ";Value=" . $params['value'] . ";"; | |
76 | //throw new OMVModuleZFSException($msg); | |
77 | return array($params['key'] => $params['value']); | |
78 | } | |
79 | ||
80 | public function addObject($params, $context) { | |
81 | $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR)); | |
82 | switch ($params['type']) { | |
503ffcc8 | 83 | case "Filesystem": |
6b3ce31b MR |
84 | $name = $params['path'] . "/" . $params['name']; |
85 | $tmp = new OMVModuleZFSDataset($name); | |
86 | break; | |
503ffcc8 | 87 | case "Snapshot": |
6b3ce31b MR |
88 | $name = $params['path'] . "@" . $params['name']; |
89 | $tmp = new OMVModuleZFSSnapshot($name); | |
90 | break; | |
503ffcc8 | 91 | case "Volume": |
6b3ce31b MR |
92 | $name = $params['path'] . "/" . $params['name']; |
93 | $tmp = new OMVModuleZFSZvol($name); | |
94 | $tmp->create($params['size']); | |
95 | break; | |
503ffcc8 MR |
96 | case "Pool": |
97 | $name = $params['path'] . "/" . $params['name']; | |
98 | $tmp = new OMVModuleZFSZpool($name); | |
99 | break; | |
6b3ce31b MR |
100 | default: |
101 | throw new OMVModuleZFSException("Illegal type provided: " . $params['type']); | |
102 | break; | |
103 | } | |
104 | } | |
105 | ||
106 | public function deleteObject($params, $context) { | |
107 | $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR)); | |
108 | switch ($params['type']) { | |
81500319 MR |
109 | case "Filesystem": |
110 | case "Clone": | |
6b3ce31b MR |
111 | $name = $params['name']; |
112 | $tmp = new OMVModuleZFSDataset($name); | |
113 | $tmp->destroy(); | |
114 | break; | |
115 | case "Snapshot": | |
116 | $name = $params['name']; | |
117 | $tmp = new OMVModuleZFSSnapshot($name); | |
118 | $tmp->destroy(); | |
119 | break; | |
120 | case "Volume": | |
121 | $name = $params['name']; | |
122 | $tmp = new OMVModuleZFSZvol($name); | |
123 | $tmp->destroy(); | |
124 | break; | |
503ffcc8 MR |
125 | case "Pool": |
126 | $name = $params['name']; | |
127 | $tmp = new OMVModuleZFSZpool($name); | |
128 | $tmp->destroy(); | |
129 | break; | |
6b3ce31b MR |
130 | default: |
131 | throw new OMVModuleZFSException("Illegal type provided: " . $params['type']); | |
132 | break; | |
133 | } | |
134 | } | |
135 | ||
136 | public function getProperties($params, $context) { | |
137 | $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR)); | |
138 | $objects = array(); | |
139 | $name = $params['name']; | |
140 | switch ($params['type']) { | |
5ff626ba MR |
141 | case "Filesystem": |
142 | case "Clone": | |
6b3ce31b MR |
143 | $tmp = new OMVModuleZFSDataset($name); |
144 | break; | |
145 | case "Snapshot": | |
146 | $tmp = new OMVModuleZFSSnapshot($name); | |
147 | break; | |
148 | case "Volume": | |
149 | $tmp = new OMVModuleZFSZvol($name); | |
150 | break; | |
503ffcc8 | 151 | case "Pool": |
a36352f7 NB |
152 | $tmp = new OMVModuleZFSZpool($name); |
153 | break; | |
6b3ce31b MR |
154 | default: |
155 | throw new OMVModuleZFSException("Illegal type provided: " . $params['type']); | |
156 | break; | |
157 | } | |
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"); | |
165 | } | |
166 | } | |
167 | return $objects; | |
168 | } | |
169 | ||
170 | public function setProperties($params, $context) { | |
171 | $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR)); | |
172 | $objects = array(); | |
173 | switch ($params['type']) { | |
5ff626ba MR |
174 | case "Filesystem": |
175 | case "Clone": | |
6b3ce31b MR |
176 | $tmp = new OMVModuleZFSDataset($params['name']); |
177 | break; | |
178 | case "Snapshot": | |
179 | $tmp = new OMVModuleZFSSnapshot($params['name']); | |
180 | break; | |
181 | case "Volume": | |
182 | $tmp = new OMVModuleZFSZvol($params['name']); | |
183 | break; | |
503ffcc8 MR |
184 | case "Pool": |
185 | $tmp = new OMVModuleZFSZpool($params['name']); | |
186 | break; | |
6b3ce31b MR |
187 | default: |
188 | throw new OMVModuleZFSException("Illegal type provided: " . $params['type']); | |
189 | break; | |
81500319 | 190 | } |
6b3ce31b MR |
191 | foreach ($params['properties'] as $property) { |
192 | $objects[$property['property']] = $property['value']; | |
193 | } | |
194 | $tmp->setProperties($objects); | |
195 | } | |
196 | ||
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; | |
205 | } | |
206 | // Child process. | |
207 | try { | |
208 | $bgOutputFilename = $this->createBgProcOutput(); | |
209 | $this->updateBgProcStatus($bgStatusFilename, "outputfilename", $bgOutputFilename); | |
210 | switch ($params['type']) { | |
5ff626ba MR |
211 | case "Filesystem": |
212 | case "Clone": | |
6b3ce31b MR |
213 | $tmp = new OMVModuleZFSDataset($params['name']); |
214 | break; | |
215 | case "Snapshot": | |
216 | $tmp = new OMVModuleZFSSnapshot($params['name']); | |
217 | break; | |
218 | case "Volume": | |
219 | $tmp = new OMVModuleZFSZvol($params['name']); | |
220 | break; | |
503ffcc8 MR |
221 | case "Pool": |
222 | $tmp = new OMVModuleZFSZpool($params['name']); | |
223 | break; | |
6b3ce31b MR |
224 | default: |
225 | throw new OMVModuleZFSException("Illegal type provided: " . $params['type']); | |
226 | break; | |
227 | } | |
228 | $tmp->inherit($params['property']); | |
229 | $this->finalizeBgProcStatus($bgStatusFilename, $output); | |
230 | exit(0); | |
231 | } catch(Exception $e) { | |
232 | $this->finalizeBgProcStatus($bgStatusFilename, "", $e); | |
233 | exit(1); | |
234 | } | |
235 | } | |
236 | ||
237 | public function getSharedParams($params, $context) { | |
238 | $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR)); | |
239 | $objects = array(); | |
240 | $ds = new OMVModuleZFSDataset($params['name']); | |
241 | $mountpoint = $ds->getMountPoint(); | |
242 | return array( | |
243 | "mountpoint" => $mountpoint, | |
244 | "name" => $params['name'], | |
245 | "type" => $params['type']); | |
246 | } | |
247 | ||
248 | public function createShare($params, $context) { | |
249 | global $xmlConfig; | |
250 | $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR)); | |
251 | ||
252 | //Get the UUID of the Pool | |
253 | $pooluuid = OMVModuleZFSUtil::getUUIDbyName($params['name']); | |
a36352f7 | 254 | preg_match('/^([A-Za-z0-9]+)\/?.*$/', $params['name'], $result); |
6b3ce31b MR |
255 | $poolname = $result[1]; |
256 | unset($result); | |
257 | ||
6b3ce31b | 258 | //Get the mntent object and fetch it's uuid. |
a36352f7 | 259 | $xpath = "//system/fstab/mntent[fsname=" . $pooluuid . "]"; |
6b3ce31b MR |
260 | $object = $xmlConfig->get($xpath); |
261 | $mntentref = $object['uuid']; | |
262 | ||
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']) { | |
5ff626ba | 266 | case "Filesystem": |
a36352f7 NB |
267 | $tmp = new OMVModuleZFSDataset($params['name']); |
268 | break; | |
5ff626ba | 269 | case "Clone": |
a36352f7 | 270 | $tmp = new OMVModuleZFSDataset($params['name']); |
6b3ce31b MR |
271 | break; |
272 | default: | |
273 | throw new OMVModuleZFSException("Illegal type provided: " . $params['type']); | |
274 | break; | |
275 | } | |
276 | ||
277 | $uuid = OMVUtil::uuid(); | |
278 | $pathName = $tmp->getMountPoint(); | |
81500319 | 279 | $subdirs = preg_split('/\//',$pathName); |
6b3ce31b MR |
280 | $reldirpath = $subdirs[count($subdirs)-1]; |
281 | $object = array( | |
282 | "uuid" => $uuid, | |
283 | "name" => $params['sharename'], | |
284 | "comment" => $params['comment'], | |
285 | "mntentref" => $mntentref, | |
286 | "reldirpath" => $reldirpath | |
287 | ); | |
288 | ||
289 | // Set the configuration object. | |
290 | $success = FALSE; | |
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 | |
293 | // example. | |
294 | $xpath = sprintf("//system/shares/sharedfolder[name='%s']", | |
295 | $params['name']); | |
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")); | |
299 | } | |
300 | ||
301 | // Add empty list of privileges per default. | |
302 | $object['privileges'] = array(); | |
303 | ||
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); | |
309 | } | |
310 | ||
311 | // Append the file mode field to the notification object if set. | |
312 | // Defaults to 775. | |
313 | $object['mode'] = "775"; | |
314 | if(array_key_exists("mode", $params)) { | |
315 | $object['mode'] = $params['mode']; | |
316 | } | |
317 | ||
318 | // Change group owner of directory to configured default group, | |
319 | // e.g. 'users'. | |
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)); | |
324 | } | |
325 | ||
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'", | |
333 | $mode, $pathName)); | |
334 | } | |
335 | ||
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. | |
340 | return $object; | |
6b3ce31b MR |
341 | } |
342 | ||
343 | } | |
344 | ||
345 | // Register the RPC service. | |
81500319 | 346 | $rpcServiceMgr = &OMVRpcServiceMgr::getInstance(); // Get the "root" instance for the Services |
6b3ce31b MR |
347 | $rpcServiceMgr->registerService(new OMVRpcServiceZFS()); // Register a new instance of the RPC service described above |
348 | ?> | |
349 |