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");
15 class OMVRpcServiceZFS extends OMVRpcServiceAbstract {
16 public function getName() {
17 return "ZFS"; // RPC Service name. Same as in .js files
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");
33 public function getObjectTree($params, $context) {
34 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
35 $objects = OMVModuleZFSUtil::getZFSFlatArray();
37 foreach ($objects as $a){
38 $new[$a['parentid']][] = $a;
40 $tree = OMVModuleZFSUtil::createTree($new, $new['root']);
41 OMVModuleZFSUtil::addMissingOMVMntEnt(); //Adds missing ZFS filesystems to the OMV core
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']);
52 public function addObject($params, $context) {
53 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
54 switch ($params['type']) {
56 $name = $params['path'] . "/" . $params['name'];
57 $tmp = new OMVModuleZFSDataset($name);
60 $name = $params['path'] . "@" . $params['name'];
61 $tmp = new OMVModuleZFSSnapshot($name);
64 $name = $params['path'] . "/" . $params['name'];
65 $tmp = new OMVModuleZFSZvol($name);
66 $tmp->create($params['size']);
69 $name = $params['path'] . "/" . $params['name'];
70 $tmp = new OMVModuleZFSZpool($name);
73 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
78 public function deleteObject($params, $context) {
79 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
80 switch ($params['type']) {
83 $name = $params['name'];
84 $tmp = new OMVModuleZFSDataset($name);
88 $name = $params['name'];
89 $tmp = new OMVModuleZFSSnapshot($name);
93 $name = $params['name'];
94 $tmp = new OMVModuleZFSZvol($name);
98 $name = $params['name'];
99 $tmp = new OMVModuleZFSZpool($name);
103 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
108 public function getProperties($params, $context) {
109 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
111 $name = $params['name'];
112 switch ($params['type']) {
115 $tmp = new OMVModuleZFSDataset($name);
118 $tmp = new OMVModuleZFSSnapshot($name);
121 $tmp = new OMVModuleZFSZvol($name);
124 $tmp = new OMVModuleZFSZpool($name);
127 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
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");
142 public function setProperties($params, $context) {
143 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
145 switch ($params['type']) {
148 $tmp = new OMVModuleZFSDataset($params['name']);
151 $tmp = new OMVModuleZFSSnapshot($params['name']);
154 $tmp = new OMVModuleZFSZvol($params['name']);
157 $tmp = new OMVModuleZFSZpool($params['name']);
160 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
163 foreach ($params['properties'] as $property) {
164 $objects[$property['property']] = $property['value'];
166 $tmp->setProperties($objects);
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;
180 $bgOutputFilename = $this->createBgProcOutput();
181 $this->updateBgProcStatus($bgStatusFilename, "outputfilename", $bgOutputFilename);
182 switch ($params['type']) {
185 $tmp = new OMVModuleZFSDataset($params['name']);
188 $tmp = new OMVModuleZFSSnapshot($params['name']);
191 $tmp = new OMVModuleZFSZvol($params['name']);
194 $tmp = new OMVModuleZFSZpool($params['name']);
197 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
200 $tmp->inherit($params['property']);
201 $this->finalizeBgProcStatus($bgStatusFilename, $output);
203 } catch(Exception $e) {
204 $this->finalizeBgProcStatus($bgStatusFilename, "", $e);
209 public function getSharedParams($params, $context) {
210 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
212 $ds = new OMVModuleZFSDataset($params['name']);
213 $mountpoint = $ds->getMountPoint();
215 "mountpoint" => $mountpoint,
216 "name" => $params['name'],
217 "type" => $params['type']);
220 public function createShare($params, $context) {
222 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
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];
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'];
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']) {
239 $tmp = new OMVModuleZFSDataset($params['name']);
242 $tmp = new OMVModuleZFSDataset($params['name']);
245 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
249 $uuid = OMVUtil::uuid();
250 $pathName = $tmp->getMountPoint();
251 $subdirs = preg_split('/\//',$pathName);
252 $reldirpath = $subdirs[count($subdirs)-1];
255 "name" => $params['sharename'],
256 "comment" => $params['comment'],
257 "mntentref" => $mntentref,
258 "reldirpath" => $reldirpath
261 // Set the configuration object.
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
266 $xpath = sprintf("//system/shares/sharedfolder[name='%s']",
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"));
273 // Add empty list of privileges per default.
274 $object['privileges'] = array();
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);
283 // Append the file mode field to the notification object if set.
285 $object['mode'] = "775";
286 if(array_key_exists("mode", $params)) {
287 $object['mode'] = $params['mode'];
290 // Change group owner of directory to configured default group,
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));
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'",
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.
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