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