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