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");
14 class OMVRpcServiceZFS extends OMVRpcServiceAbstract {
15 public function getName() { return "ZFS";} // RPC Service name. Same as in .js files
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");
30 public function getObjectTree($params, $context) {
31 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
32 $objects = OMVModuleZFSUtil::getZFSFlatArray();
34 foreach ($objects as $a){
35 $new[$a['parentid']][] = $a;
37 $tree = OMVModuleZFSUtil::createTree($new, $new['root']);
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']);
48 public function addObject($params, $context) {
49 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
50 switch ($params['type']) {
52 $name = $params['path'] . "/" . $params['name'];
53 $tmp = new OMVModuleZFSDataset($name);
56 $name = $params['path'] . "@" . $params['name'];
57 $tmp = new OMVModuleZFSSnapshot($name);
60 $name = $params['path'] . "/" . $params['name'];
61 $tmp = new OMVModuleZFSZvol($name);
62 $tmp->create($params['size']);
65 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
70 public function deleteObject($params, $context) {
71 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
72 switch ($params['type']) {
75 $name = $params['name'];
76 $tmp = new OMVModuleZFSDataset($name);
80 $name = $params['name'];
81 $tmp = new OMVModuleZFSSnapshot($name);
85 $name = $params['name'];
86 $tmp = new OMVModuleZFSZvol($name);
90 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
95 public function getProperties($params, $context) {
96 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
98 $name = $params['name'];
99 switch ($params['type']) {
100 case "Filesystem" || "Clone":
101 $tmp = new OMVModuleZFSDataset($name);
104 $tmp = new OMVModuleZFSSnapshot($name);
107 $tmp = new OMVModuleZFSZvol($name);
110 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
113 $properties = $tmp->getProperties();
114 foreach ($properties as $propertyk => $propertyv) {
115 if (!(strcmp($propertyv['source'], "-") == 0)) {
116 $objects[] = array('property' => $propertyk,
117 'value' => $propertyv['value'],
118 'source' => $propertyv['source'],
119 'modified' => "false");
125 public function setProperties($params, $context) {
126 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
128 switch ($params['type']) {
129 case "Filesystem" || "Clone":
130 $tmp = new OMVModuleZFSDataset($params['name']);
133 $tmp = new OMVModuleZFSSnapshot($params['name']);
136 $tmp = new OMVModuleZFSZvol($params['name']);
139 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
142 foreach ($params['properties'] as $property) {
143 $objects[$property['property']] = $property['value'];
145 $tmp->setProperties($objects);
148 public function inherit($params, $context) {
149 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
150 // Create a background process.
151 $bgStatusFilename = $this->createBgProcStatus();
152 $pid = $this->fork();
153 if($pid > 0) { // Parent process.
154 $this->initializeBgProcStatus($bgStatusFilename, $pid);
155 return $bgStatusFilename;
159 $bgOutputFilename = $this->createBgProcOutput();
160 $this->updateBgProcStatus($bgStatusFilename, "outputfilename", $bgOutputFilename);
161 switch ($params['type']) {
162 case "Filesystem" || "Clone":
163 $tmp = new OMVModuleZFSDataset($params['name']);
166 $tmp = new OMVModuleZFSSnapshot($params['name']);
169 $tmp = new OMVModuleZFSZvol($params['name']);
172 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
175 $tmp->inherit($params['property']);
176 $this->finalizeBgProcStatus($bgStatusFilename, $output);
178 } catch(Exception $e) {
179 $this->finalizeBgProcStatus($bgStatusFilename, "", $e);
184 public function getSharedParams($params, $context) {
185 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
187 $ds = new OMVModuleZFSDataset($params['name']);
188 $mountpoint = $ds->getMountPoint();
190 "mountpoint" => $mountpoint,
191 "name" => $params['name'],
192 "type" => $params['type']);
195 public function createShare($params, $context) {
197 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
199 //Get the UUID of the Pool
200 $pooluuid = OMVModuleZFSUtil::getUUIDbyName($params['name']);
201 preg_match('/^([A-Za-z0-9]+)\/?.*$/', $params['name'], $result);
202 $poolname = $result[1];
205 //Check if the UUID is already stored as an mntent object. If it isn't then create it.
206 $xpath = "//system/fstab/mntent[fsname=" . $pooluuid . "]";
207 $object = $xmlConfig->get($xpath);
208 if(is_null($object)) {
209 $uuid = OMVUtil::uuid();
210 $ds = new OMVModuleZFSDataset($poolname);
211 $dir = $ds->getMountPoint();
214 "fsname" => $pooluuid,
217 "opts" => "rw,relatime,xattr",
221 $xmlConfig->set("//system/fstab",array("mntent" => $object));
222 $dispatcher = &OMVNotifyDispatcher::getInstance();
223 $dispatcher->notify(OMV_NOTIFY_CREATE,"org.openmediavault.system.fstab.mntent", $object);
226 //Get the mntent object and fetch it's uuid.
227 $object = $xmlConfig->get($xpath);
228 $mntentref = $object['uuid'];
230 // Prepare the configuration object. Use the name of the shared
231 // folder as the relative directory name of the share.
232 switch ($params['type']) {
233 case "Filesystem" || "Clone":
234 $tmp = new OMVModuleZFSDataset($name);
237 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
241 $uuid = OMVUtil::uuid();
242 $pathName = $tmp->getMountPoint();
243 $subdirs = preg_split('/\//',$pathName);
244 $reldirpath = $subdirs[count($subdirs)-1];
247 "name" => $params['sharename'],
248 "comment" => $params['comment'],
249 "mntentref" => $mntentref,
250 "reldirpath" => $reldirpath
253 // Set the configuration object.
255 // Check uniqueness. The share name must be global unique because
256 // the name is also used when exporting a shared folder via NFS for
258 $xpath = sprintf("//system/shares/sharedfolder[name='%s']",
260 if(TRUE === $xmlConfig->exists($xpath)) {
261 throw new OMVException(OMVErrorMsg::E_CONFIG_OBJECT_UNIQUENESS,
262 gettext("A shared folder with the given name already exists"));
265 // Add empty list of privileges per default.
266 $object['privileges'] = array();
268 // Append object to configuration.
269 $success = $xmlConfig->set("//system/shares",
270 array("sharedfolder" => $object));
271 if(FALSE === $success) {
272 throw new OMVException(OMVErrorMsg::E_CONFIG_SET_OBJECT_FAILED);
275 // Append the file mode field to the notification object if set.
277 $object['mode'] = "775";
278 if(array_key_exists("mode", $params)) {
279 $object['mode'] = $params['mode'];
282 // Change group owner of directory to configured default group,
284 if(FALSE === chgrp($pathName, $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'])) {
285 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
286 sprintf("Failed to set file group to '%s' for '%s'",
287 $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'], $pathName));
290 // Set the setgid bit. Setting this permission means that all files
291 // created in the folder will inherit the group of the folder rather
292 // than the primary group of the user who creates the file.
293 $mode = fileperms($pathName) | 02000;
294 if(FALSE === chmod($pathName, $mode)) {
295 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
296 sprintf("Failed to set file mode to '%o' for '%s'",
300 // Notify configuration changes.
301 $dispatcher = &OMVNotifyDispatcher::getInstance();
302 $dispatcher->notify(OMV_NOTIFY_CREATE,"org.openmediavault.system.shares.sharedfolder", $object);
303 // Return the configuration object.
310 // Register the RPC service.
311 $rpcServiceMgr = &OMVRpcServiceMgr::getInstance(); // Get the "root" instance for the Services
312 $rpcServiceMgr->registerService(new OMVRpcServiceZFS()); // Register a new instance of the RPC service described above