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