]> git.datanom.net - omvzfs.git/blob - gui/rpc/zfs.inc
c7fcb264d3e0088bed6aede3a93c9ea29fdda87f
[omvzfs.git] / gui / rpc / zfs.inc
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 require_once("zfs/Zpool.php");
14
15 class OMVRpcServiceZFS extends OMVRpcServiceAbstract {
16 public function getName() { return "ZFS";} // RPC Service name. Same as in .js files
17
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");
29 }
30
31 public function getObjectTree($params, $context) {
32 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
33 $objects = OMVModuleZFSUtil::getZFSFlatArray();
34 $new = array();
35 foreach ($objects as $a){
36 $new[$a['parentid']][] = $a;
37 }
38 $tree = OMVModuleZFSUtil::createTree($new, $new['root']);
39 return $tree;
40 }
41
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']);
47 }
48
49 public function addObject($params, $context) {
50 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
51 switch ($params['type']) {
52 case "Filesystem":
53 $name = $params['path'] . "/" . $params['name'];
54 $tmp = new OMVModuleZFSDataset($name);
55 break;
56 case "Snapshot":
57 $name = $params['path'] . "@" . $params['name'];
58 $tmp = new OMVModuleZFSSnapshot($name);
59 break;
60 case "Volume":
61 $name = $params['path'] . "/" . $params['name'];
62 $tmp = new OMVModuleZFSZvol($name);
63 $tmp->create($params['size']);
64 break;
65 case "Pool":
66 $name = $params['path'] . "/" . $params['name'];
67 $tmp = new OMVModuleZFSZpool($name);
68 break;
69 default:
70 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
71 break;
72 }
73 }
74
75 public function deleteObject($params, $context) {
76 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
77 switch ($params['type']) {
78 case "Filesystem":
79 case "Clone":
80 $name = $params['name'];
81 $tmp = new OMVModuleZFSDataset($name);
82 $tmp->destroy();
83 break;
84 case "Snapshot":
85 $name = $params['name'];
86 $tmp = new OMVModuleZFSSnapshot($name);
87 $tmp->destroy();
88 break;
89 case "Volume":
90 $name = $params['name'];
91 $tmp = new OMVModuleZFSZvol($name);
92 $tmp->destroy();
93 break;
94 case "Pool":
95 $name = $params['name'];
96 $tmp = new OMVModuleZFSZpool($name);
97 $tmp->destroy();
98 break;
99 default:
100 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
101 break;
102 }
103 }
104
105 public function getProperties($params, $context) {
106 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
107 $objects = array();
108 $name = $params['name'];
109 switch ($params['type']) {
110 case "Filesystem":
111 case "Clone":
112 $tmp = new OMVModuleZFSDataset($name);
113 break;
114 case "Snapshot":
115 $tmp = new OMVModuleZFSSnapshot($name);
116 break;
117 case "Volume":
118 $tmp = new OMVModuleZFSZvol($name);
119 break;
120 case "Pool":
121 $tmp = new OMVModuleZFSZpool($name);
122 break;
123 default:
124 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
125 break;
126 }
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");
134 }
135 }
136 return $objects;
137 }
138
139 public function setProperties($params, $context) {
140 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
141 $objects = array();
142 switch ($params['type']) {
143 case "Filesystem":
144 case "Clone":
145 $tmp = new OMVModuleZFSDataset($params['name']);
146 break;
147 case "Snapshot":
148 $tmp = new OMVModuleZFSSnapshot($params['name']);
149 break;
150 case "Volume":
151 $tmp = new OMVModuleZFSZvol($params['name']);
152 break;
153 case "Pool":
154 $tmp = new OMVModuleZFSZpool($params['name']);
155 break;
156 default:
157 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
158 break;
159 }
160 foreach ($params['properties'] as $property) {
161 $objects[$property['property']] = $property['value'];
162 }
163 $tmp->setProperties($objects);
164 }
165
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;
174 }
175 // Child process.
176 try {
177 $bgOutputFilename = $this->createBgProcOutput();
178 $this->updateBgProcStatus($bgStatusFilename, "outputfilename", $bgOutputFilename);
179 switch ($params['type']) {
180 case "Filesystem":
181 case "Clone":
182 $tmp = new OMVModuleZFSDataset($params['name']);
183 break;
184 case "Snapshot":
185 $tmp = new OMVModuleZFSSnapshot($params['name']);
186 break;
187 case "Volume":
188 $tmp = new OMVModuleZFSZvol($params['name']);
189 break;
190 case "Pool":
191 $tmp = new OMVModuleZFSZpool($params['name']);
192 break;
193 default:
194 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
195 break;
196 }
197 $tmp->inherit($params['property']);
198 $this->finalizeBgProcStatus($bgStatusFilename, $output);
199 exit(0);
200 } catch(Exception $e) {
201 $this->finalizeBgProcStatus($bgStatusFilename, "", $e);
202 exit(1);
203 }
204 }
205
206 public function getSharedParams($params, $context) {
207 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
208 $objects = array();
209 $ds = new OMVModuleZFSDataset($params['name']);
210 $mountpoint = $ds->getMountPoint();
211 return array(
212 "mountpoint" => $mountpoint,
213 "name" => $params['name'],
214 "type" => $params['type']);
215 }
216
217 public function createShare($params, $context) {
218 global $xmlConfig;
219 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
220
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];
225 unset($result);
226
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();
234 $object = array(
235 "uuid" => $uuid,
236 "fsname" => $pooluuid,
237 "dir" => $dir,
238 "type" => "zfs",
239 "opts" => "rw,relatime,xattr",
240 "freq" => "0",
241 "passno" => "2"
242 );
243 $xmlConfig->set("//system/fstab",array("mntent" => $object));
244 $dispatcher = &OMVNotifyDispatcher::getInstance();
245 $dispatcher->notify(OMV_NOTIFY_CREATE,"org.openmediavault.system.fstab.mntent", $object);
246 }
247
248 //Get the mntent object and fetch it's uuid.
249 $object = $xmlConfig->get($xpath);
250 $mntentref = $object['uuid'];
251
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']) {
255 case "Filesystem":
256 case "Clone":
257 $tmp = new OMVModuleZFSDataset($name);
258 break;
259 default:
260 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
261 break;
262 }
263
264 $uuid = OMVUtil::uuid();
265 $pathName = $tmp->getMountPoint();
266 $subdirs = preg_split('/\//',$pathName);
267 $reldirpath = $subdirs[count($subdirs)-1];
268 $object = array(
269 "uuid" => $uuid,
270 "name" => $params['sharename'],
271 "comment" => $params['comment'],
272 "mntentref" => $mntentref,
273 "reldirpath" => $reldirpath
274 );
275
276 // Set the configuration object.
277 $success = FALSE;
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
280 // example.
281 $xpath = sprintf("//system/shares/sharedfolder[name='%s']",
282 $params['name']);
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"));
286 }
287
288 // Add empty list of privileges per default.
289 $object['privileges'] = array();
290
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);
296 }
297
298 // Append the file mode field to the notification object if set.
299 // Defaults to 775.
300 $object['mode'] = "775";
301 if(array_key_exists("mode", $params)) {
302 $object['mode'] = $params['mode'];
303 }
304
305 // Change group owner of directory to configured default group,
306 // e.g. 'users'.
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));
311 }
312
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'",
320 $mode, $pathName));
321 }
322
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.
327 return $object;
328
329 }
330
331 }
332
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
336 ?>
337
This page took 0.077788 seconds and 4 git commands to generate.