]> git.datanom.net - omvzfs.git/blame - gui/rpc/zfs.inc
commit to be able to apply patch from Niclas
[omvzfs.git] / gui / rpc / zfs.inc
CommitLineData
6b3ce31b
MR
1<?php
2
3require_once("openmediavault/object.inc");
4require_once("openmediavault/config.inc");
5require_once("openmediavault/error.inc");
6require_once("openmediavault/util.inc");
7require_once("openmediavault/rpcservice.inc");
8require_once("openmediavault/notify.inc");
9require_once("zfs/Utils.php");
10require_once("zfs/Dataset.php");
11require_once("zfs/Snapshot.php");
12require_once("zfs/Zvol.php");
503ffcc8 13require_once("zfs/Zpool.php");
c0253592 14require_once("zfs/NotifyListener.php");
6b3ce31b
MR
15
16class OMVRpcServiceZFS extends OMVRpcServiceAbstract {
a36352f7
NB
17 public function getName() {
18 return "ZFS"; // RPC Service name. Same as in .js files
19 }
6b3ce31b 20
a36352f7
NB
21 /* Initialize the RPC service. Different methods of the RPC service are declared here*/
22 public function initialize() {
42856e8b 23 $this->registerMethod("addPool");
a36352f7
NB
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 }
6b3ce31b 34
42856e8b
NB
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 $disks = preg_split("/[,;]/", $params['devices']);
58 $vdev = new OMVModuleZFSVdev($params['name'], $pooltype, $disks);
59 $pool = new OMVModuleZFSZpool($vdev);
57667eb1
NB
60 //Ugly fix to solve the problem of blkid not displaying info on newly created pools
61 $pool->export();
62 $pool->import($pool->getName());
42856e8b
NB
63 }
64
6b3ce31b
MR
65 public function getObjectTree($params, $context) {
66 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
67 $objects = OMVModuleZFSUtil::getZFSFlatArray();
68 $new = array();
69 foreach ($objects as $a){
70 $new[$a['parentid']][] = $a;
71 }
72 $tree = OMVModuleZFSUtil::createTree($new, $new['root']);
a36352f7 73 OMVModuleZFSUtil::addMissingOMVMntEnt(); //Adds missing ZFS filesystems to the OMV core
6b3ce31b
MR
74 return $tree;
75 }
76
77 public function passParam($params, $context) {
78 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
79 //$msg = "Key=" . $params['key'] . ";Value=" . $params['value'] . ";";
80 //throw new OMVModuleZFSException($msg);
81 return array($params['key'] => $params['value']);
82 }
83
84 public function addObject($params, $context) {
85 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
86 switch ($params['type']) {
57667eb1 87 case "filesystem":
6b3ce31b
MR
88 $name = $params['path'] . "/" . $params['name'];
89 $tmp = new OMVModuleZFSDataset($name);
90 break;
57667eb1 91 case "snapshot":
6b3ce31b
MR
92 $name = $params['path'] . "@" . $params['name'];
93 $tmp = new OMVModuleZFSSnapshot($name);
94 break;
57667eb1 95 case "volume":
6b3ce31b
MR
96 $name = $params['path'] . "/" . $params['name'];
97 $tmp = new OMVModuleZFSZvol($name);
98 $tmp->create($params['size']);
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']) {
81500319
MR
109 case "Filesystem":
110 case "Clone":
6b3ce31b
MR
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;
503ffcc8
MR
125 case "Pool":
126 $name = $params['name'];
127 $tmp = new OMVModuleZFSZpool($name);
128 $tmp->destroy();
129 break;
6b3ce31b
MR
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']) {
5ff626ba
MR
141 case "Filesystem":
142 case "Clone":
6b3ce31b
MR
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;
503ffcc8 151 case "Pool":
a36352f7
NB
152 $tmp = new OMVModuleZFSZpool($name);
153 break;
6b3ce31b
MR
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']) {
5ff626ba
MR
174 case "Filesystem":
175 case "Clone":
6b3ce31b
MR
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;
503ffcc8
MR
184 case "Pool":
185 $tmp = new OMVModuleZFSZpool($params['name']);
186 break;
6b3ce31b
MR
187 default:
188 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
189 break;
81500319 190 }
6b3ce31b
MR
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']) {
5ff626ba
MR
211 case "Filesystem":
212 case "Clone":
6b3ce31b
MR
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;
503ffcc8
MR
221 case "Pool":
222 $tmp = new OMVModuleZFSZpool($params['name']);
223 break;
6b3ce31b
MR
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']);
a36352f7 254 preg_match('/^([A-Za-z0-9]+)\/?.*$/', $params['name'], $result);
6b3ce31b
MR
255 $poolname = $result[1];
256 unset($result);
257
6b3ce31b 258 //Get the mntent object and fetch it's uuid.
a36352f7 259 $xpath = "//system/fstab/mntent[fsname=" . $pooluuid . "]";
6b3ce31b
MR
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']) {
5ff626ba 266 case "Filesystem":
a36352f7
NB
267 $tmp = new OMVModuleZFSDataset($params['name']);
268 break;
5ff626ba 269 case "Clone":
a36352f7 270 $tmp = new OMVModuleZFSDataset($params['name']);
6b3ce31b
MR
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();
81500319 279 $subdirs = preg_split('/\//',$pathName);
6b3ce31b
MR
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;
6b3ce31b
MR
341 }
342
343}
344
345// Register the RPC service.
81500319 346$rpcServiceMgr = &OMVRpcServiceMgr::getInstance(); // Get the "root" instance for the Services
6b3ce31b
MR
347$rpcServiceMgr->registerService(new OMVRpcServiceZFS()); // Register a new instance of the RPC service described above
348?>
349
This page took 0.080712 seconds and 5 git commands to generate.