]> git.datanom.net - omvzfs.git/blob - gui/rpc/zfs.inc
Fix blkid not showing newly created pools plus a small typo fix.
[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 //Ugly fix to solve the problem of blkid not displaying info on newly created pools
60 $pool->export();
61 $pool->import($pool->getName());
62 }
63
64 public function getObjectTree($params, $context) {
65 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
66 $objects = OMVModuleZFSUtil::getZFSFlatArray();
67 $new = array();
68 foreach ($objects as $a){
69 $new[$a['parentid']][] = $a;
70 }
71 $tree = OMVModuleZFSUtil::createTree($new, $new['root']);
72 OMVModuleZFSUtil::addMissingOMVMntEnt(); //Adds missing ZFS filesystems to the OMV core
73 return $tree;
74 }
75
76 public function passParam($params, $context) {
77 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
78 //$msg = "Key=" . $params['key'] . ";Value=" . $params['value'] . ";";
79 //throw new OMVModuleZFSException($msg);
80 return array($params['key'] => $params['value']);
81 }
82
83 public function addObject($params, $context) {
84 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
85 switch ($params['type']) {
86 case "filesystem":
87 $name = $params['path'] . "/" . $params['name'];
88 $tmp = new OMVModuleZFSDataset($name);
89 break;
90 case "snapshot":
91 $name = $params['path'] . "@" . $params['name'];
92 $tmp = new OMVModuleZFSSnapshot($name);
93 break;
94 case "volume":
95 $name = $params['path'] . "/" . $params['name'];
96 $tmp = new OMVModuleZFSZvol($name);
97 $tmp->create($params['size']);
98 break;
99 default:
100 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
101 break;
102 }
103 }
104
105 public function deleteObject($params, $context) {
106 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
107 switch ($params['type']) {
108 case "Filesystem":
109 case "Clone":
110 $name = $params['name'];
111 $tmp = new OMVModuleZFSDataset($name);
112 $tmp->destroy();
113 break;
114 case "Snapshot":
115 $name = $params['name'];
116 $tmp = new OMVModuleZFSSnapshot($name);
117 $tmp->destroy();
118 break;
119 case "Volume":
120 $name = $params['name'];
121 $tmp = new OMVModuleZFSZvol($name);
122 $tmp->destroy();
123 break;
124 case "Pool":
125 $name = $params['name'];
126 $tmp = new OMVModuleZFSZpool($name);
127 $tmp->destroy();
128 break;
129 default:
130 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
131 break;
132 }
133 }
134
135 public function getProperties($params, $context) {
136 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
137 $objects = array();
138 $name = $params['name'];
139 switch ($params['type']) {
140 case "Filesystem":
141 case "Clone":
142 $tmp = new OMVModuleZFSDataset($name);
143 break;
144 case "Snapshot":
145 $tmp = new OMVModuleZFSSnapshot($name);
146 break;
147 case "Volume":
148 $tmp = new OMVModuleZFSZvol($name);
149 break;
150 case "Pool":
151 $tmp = new OMVModuleZFSZpool($name);
152 break;
153 default:
154 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
155 break;
156 }
157 $properties = $tmp->getProperties();
158 foreach ($properties as $propertyk => $propertyv) {
159 if (!(strcmp($propertyv['source'], "-") == 0)) {
160 $objects[] = array('property' => $propertyk,
161 'value' => $propertyv['value'],
162 'source' => $propertyv['source'],
163 'modified' => "false");
164 }
165 }
166 return $objects;
167 }
168
169 public function setProperties($params, $context) {
170 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
171 $objects = array();
172 switch ($params['type']) {
173 case "Filesystem":
174 case "Clone":
175 $tmp = new OMVModuleZFSDataset($params['name']);
176 break;
177 case "Snapshot":
178 $tmp = new OMVModuleZFSSnapshot($params['name']);
179 break;
180 case "Volume":
181 $tmp = new OMVModuleZFSZvol($params['name']);
182 break;
183 case "Pool":
184 $tmp = new OMVModuleZFSZpool($params['name']);
185 break;
186 default:
187 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
188 break;
189 }
190 foreach ($params['properties'] as $property) {
191 $objects[$property['property']] = $property['value'];
192 }
193 $tmp->setProperties($objects);
194 }
195
196 public function inherit($params, $context) {
197 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
198 // Create a background process.
199 $bgStatusFilename = $this->createBgProcStatus();
200 $pid = $this->fork();
201 if($pid > 0) { // Parent process.
202 $this->initializeBgProcStatus($bgStatusFilename, $pid);
203 return $bgStatusFilename;
204 }
205 // Child process.
206 try {
207 $bgOutputFilename = $this->createBgProcOutput();
208 $this->updateBgProcStatus($bgStatusFilename, "outputfilename", $bgOutputFilename);
209 switch ($params['type']) {
210 case "Filesystem":
211 case "Clone":
212 $tmp = new OMVModuleZFSDataset($params['name']);
213 break;
214 case "Snapshot":
215 $tmp = new OMVModuleZFSSnapshot($params['name']);
216 break;
217 case "Volume":
218 $tmp = new OMVModuleZFSZvol($params['name']);
219 break;
220 case "Pool":
221 $tmp = new OMVModuleZFSZpool($params['name']);
222 break;
223 default:
224 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
225 break;
226 }
227 $tmp->inherit($params['property']);
228 $this->finalizeBgProcStatus($bgStatusFilename, $output);
229 exit(0);
230 } catch(Exception $e) {
231 $this->finalizeBgProcStatus($bgStatusFilename, "", $e);
232 exit(1);
233 }
234 }
235
236 public function getSharedParams($params, $context) {
237 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
238 $objects = array();
239 $ds = new OMVModuleZFSDataset($params['name']);
240 $mountpoint = $ds->getMountPoint();
241 return array(
242 "mountpoint" => $mountpoint,
243 "name" => $params['name'],
244 "type" => $params['type']);
245 }
246
247 public function createShare($params, $context) {
248 global $xmlConfig;
249 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
250
251 //Get the UUID of the Pool
252 $pooluuid = OMVModuleZFSUtil::getUUIDbyName($params['name']);
253 preg_match('/^([A-Za-z0-9]+)\/?.*$/', $params['name'], $result);
254 $poolname = $result[1];
255 unset($result);
256
257 //Get the mntent object and fetch it's uuid.
258 $xpath = "//system/fstab/mntent[fsname=" . $pooluuid . "]";
259 $object = $xmlConfig->get($xpath);
260 $mntentref = $object['uuid'];
261
262 // Prepare the configuration object. Use the name of the shared
263 // folder as the relative directory name of the share.
264 switch ($params['type']) {
265 case "Filesystem":
266 $tmp = new OMVModuleZFSDataset($params['name']);
267 break;
268 case "Clone":
269 $tmp = new OMVModuleZFSDataset($params['name']);
270 break;
271 default:
272 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
273 break;
274 }
275
276 $uuid = OMVUtil::uuid();
277 $pathName = $tmp->getMountPoint();
278 $subdirs = preg_split('/\//',$pathName);
279 $reldirpath = $subdirs[count($subdirs)-1];
280 $object = array(
281 "uuid" => $uuid,
282 "name" => $params['sharename'],
283 "comment" => $params['comment'],
284 "mntentref" => $mntentref,
285 "reldirpath" => $reldirpath
286 );
287
288 // Set the configuration object.
289 $success = FALSE;
290 // Check uniqueness. The share name must be global unique because
291 // the name is also used when exporting a shared folder via NFS for
292 // example.
293 $xpath = sprintf("//system/shares/sharedfolder[name='%s']",
294 $params['name']);
295 if(TRUE === $xmlConfig->exists($xpath)) {
296 throw new OMVException(OMVErrorMsg::E_CONFIG_OBJECT_UNIQUENESS,
297 gettext("A shared folder with the given name already exists"));
298 }
299
300 // Add empty list of privileges per default.
301 $object['privileges'] = array();
302
303 // Append object to configuration.
304 $success = $xmlConfig->set("//system/shares",
305 array("sharedfolder" => $object));
306 if(FALSE === $success) {
307 throw new OMVException(OMVErrorMsg::E_CONFIG_SET_OBJECT_FAILED);
308 }
309
310 // Append the file mode field to the notification object if set.
311 // Defaults to 775.
312 $object['mode'] = "775";
313 if(array_key_exists("mode", $params)) {
314 $object['mode'] = $params['mode'];
315 }
316
317 // Change group owner of directory to configured default group,
318 // e.g. 'users'.
319 if(FALSE === chgrp($pathName, $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'])) {
320 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
321 sprintf("Failed to set file group to '%s' for '%s'",
322 $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'], $pathName));
323 }
324
325 // Set the setgid bit. Setting this permission means that all files
326 // created in the folder will inherit the group of the folder rather
327 // than the primary group of the user who creates the file.
328 $mode = fileperms($pathName) | 02000;
329 if(FALSE === chmod($pathName, $mode)) {
330 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
331 sprintf("Failed to set file mode to '%o' for '%s'",
332 $mode, $pathName));
333 }
334
335 // Notify configuration changes.
336 $dispatcher = &OMVNotifyDispatcher::getInstance();
337 $dispatcher->notify(OMV_NOTIFY_CREATE,"org.openmediavault.system.shares.sharedfolder", $object);
338 // Return the configuration object.
339 return $object;
340 }
341
342 }
343
344 // Register the RPC service.
345 $rpcServiceMgr = &OMVRpcServiceMgr::getInstance(); // Get the "root" instance for the Services
346 $rpcServiceMgr->registerService(new OMVRpcServiceZFS()); // Register a new instance of the RPC service described above
347 ?>
348
This page took 0.160567 seconds and 6 git commands to generate.