]> git.datanom.net - omvzfs.git/blob - gui/rpc/zfs.inc
Initial implementation of GUI for adding ZFS pools plus minor bugfixes.
[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() {
17 return "ZFS"; // RPC Service name. Same as in .js files
18 }
19
20 /* Initialize the RPC service. Different methods of the RPC service are declared here*/
21 public function initialize() {
22 $this->registerMethod("addPool");
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 }
33
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
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']);
69 OMVModuleZFSUtil::addMissingOMVMntEnt(); //Adds missing ZFS filesystems to the OMV core
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']) {
83 case "Filesystem":
84 $name = $params['path'] . "/" . $params['name'];
85 $tmp = new OMVModuleZFSDataset($name);
86 break;
87 case "Snapshot":
88 $name = $params['path'] . "@" . $params['name'];
89 $tmp = new OMVModuleZFSSnapshot($name);
90 break;
91 case "Volume":
92 $name = $params['path'] . "/" . $params['name'];
93 $tmp = new OMVModuleZFSZvol($name);
94 $tmp->create($params['size']);
95 break;
96 case "Pool":
97 $name = $params['path'] . "/" . $params['name'];
98 $tmp = new OMVModuleZFSZpool($name);
99 break;
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']) {
109 case "Filesystem":
110 case "Clone":
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;
125 case "Pool":
126 $name = $params['name'];
127 $tmp = new OMVModuleZFSZpool($name);
128 $tmp->destroy();
129 break;
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']) {
141 case "Filesystem":
142 case "Clone":
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;
151 case "Pool":
152 $tmp = new OMVModuleZFSZpool($name);
153 break;
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']) {
174 case "Filesystem":
175 case "Clone":
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;
184 case "Pool":
185 $tmp = new OMVModuleZFSZpool($params['name']);
186 break;
187 default:
188 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
189 break;
190 }
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']) {
211 case "Filesystem":
212 case "Clone":
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;
221 case "Pool":
222 $tmp = new OMVModuleZFSZpool($params['name']);
223 break;
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']);
254 preg_match('/^([A-Za-z0-9]+)\/?.*$/', $params['name'], $result);
255 $poolname = $result[1];
256 unset($result);
257
258 //Get the mntent object and fetch it's uuid.
259 $xpath = "//system/fstab/mntent[fsname=" . $pooluuid . "]";
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']) {
266 case "Filesystem":
267 $tmp = new OMVModuleZFSDataset($params['name']);
268 break;
269 case "Clone":
270 $tmp = new OMVModuleZFSDataset($params['name']);
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();
279 $subdirs = preg_split('/\//',$pathName);
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;
341 }
342
343 }
344
345 // Register the RPC service.
346 $rpcServiceMgr = &OMVRpcServiceMgr::getInstance(); // Get the "root" instance for the Services
347 $rpcServiceMgr->registerService(new OMVRpcServiceZFS()); // Register a new instance of the RPC service described above
348 ?>
349
This page took 0.108468 seconds and 6 git commands to generate.