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