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