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 require_once("zfs/NotifyListener.php");
16 class OMVRpcServiceZFS extends OMVRpcServiceAbstract {
17 public function getName() {
18 return "ZFS"; // RPC Service name. Same as in .js files
21 /* Initialize the RPC service. Different methods of the RPC service are declared here*/
22 public function initialize() {
23 $this->registerMethod("addPool");
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");
35 public function addPool($params, $context) {
36 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
37 switch ($params['pooltype']) {
39 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSPLAIN;
42 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSMIRROR;
45 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ1;
48 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ2;
51 $pooltype = OMVModuleZFSVdevType::OMVMODULEZFSRAIDZ3;
54 throw new OMVModuleZFSException("Incorrect pool type specified");
57 //Check for user supplied options
59 if ($params['force']) {
62 if (strlen($params['mountpoint']) > 0) {
63 $opts .= "-m " . $params['mountpoint'] . " ";
66 $disks = preg_split("/[,;]/", $params['devices']);
67 $vdev = new OMVModuleZFSVdev($params['name'], $pooltype, $disks);
68 $pool = new OMVModuleZFSZpool($vdev, $opts);
69 //Ugly fix to solve the problem of blkid not displaying info on newly created pools
71 $pool->import($pool->getName());
74 public function getObjectTree($params, $context) {
75 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
76 $objects = OMVModuleZFSUtil::getZFSFlatArray();
78 foreach ($objects as $a){
79 $new[$a['parentid']][] = $a;
81 $tree = OMVModuleZFSUtil::createTree($new, $new['root']);
82 OMVModuleZFSUtil::addMissingOMVMntEnt(); //Adds missing ZFS filesystems to the OMV core
86 public function passParam($params, $context) {
87 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
88 //$msg = "Key=" . $params['key'] . ";Value=" . $params['value'] . ";";
89 //throw new OMVModuleZFSException($msg);
90 return array($params['key'] => $params['value']);
93 public function addObject($params, $context) {
94 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
95 switch ($params['type']) {
97 $name = $params['path'] . "/" . $params['name'];
98 $tmp = new OMVModuleZFSDataset($name);
101 $name = $params['path'] . "@" . $params['name'];
102 $tmp = new OMVModuleZFSSnapshot($name);
105 $name = $params['path'] . "/" . $params['name'];
106 $tmp = new OMVModuleZFSZvol($name);
107 $tmp->create($params['size']);
110 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
115 public function deleteObject($params, $context) {
116 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
117 switch ($params['type']) {
120 $name = $params['name'];
121 $tmp = new OMVModuleZFSDataset($name);
125 $name = $params['name'];
126 $tmp = new OMVModuleZFSSnapshot($name);
130 $name = $params['name'];
131 $tmp = new OMVModuleZFSZvol($name);
135 $name = $params['name'];
136 $tmp = new OMVModuleZFSZpool($name);
140 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
145 public function getProperties($params, $context) {
146 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
148 $name = $params['name'];
149 switch ($params['type']) {
152 $tmp = new OMVModuleZFSDataset($name);
155 $tmp = new OMVModuleZFSSnapshot($name);
158 $tmp = new OMVModuleZFSZvol($name);
161 $tmp = new OMVModuleZFSZpool($name);
164 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
167 $properties = $tmp->getProperties();
168 foreach ($properties as $propertyk => $propertyv) {
169 if (!(strcmp($propertyv['source'], "-") == 0)) {
170 $objects[] = array('property' => $propertyk,
171 'value' => $propertyv['value'],
172 'source' => $propertyv['source'],
173 'modified' => "false");
179 public function setProperties($params, $context) {
180 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
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 foreach ($params['properties'] as $property) {
201 $objects[$property['property']] = $property['value'];
203 $tmp->setProperties($objects);
206 public function inherit($params, $context) {
207 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
208 // Create a background process.
209 $bgStatusFilename = $this->createBgProcStatus();
210 $pid = $this->fork();
211 if($pid > 0) { // Parent process.
212 $this->initializeBgProcStatus($bgStatusFilename, $pid);
213 return $bgStatusFilename;
217 $bgOutputFilename = $this->createBgProcOutput();
218 $this->updateBgProcStatus($bgStatusFilename, "outputfilename", $bgOutputFilename);
219 switch ($params['type']) {
222 $tmp = new OMVModuleZFSDataset($params['name']);
225 $tmp = new OMVModuleZFSSnapshot($params['name']);
228 $tmp = new OMVModuleZFSZvol($params['name']);
231 $tmp = new OMVModuleZFSZpool($params['name']);
234 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
237 $tmp->inherit($params['property']);
238 $this->finalizeBgProcStatus($bgStatusFilename, $output);
240 } catch(Exception $e) {
241 $this->finalizeBgProcStatus($bgStatusFilename, "", $e);
246 public function getSharedParams($params, $context) {
247 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
249 $ds = new OMVModuleZFSDataset($params['name']);
250 $mountpoint = $ds->getMountPoint();
252 "mountpoint" => $mountpoint,
253 "name" => $params['name'],
254 "type" => $params['type']);
257 public function createShare($params, $context) {
259 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
261 //Get the UUID of the Pool
262 $pooluuid = OMVModuleZFSUtil::getUUIDbyName($params['name']);
263 preg_match('/^([A-Za-z0-9]+)\/?.*$/', $params['name'], $result);
264 $poolname = $result[1];
267 //Get the mntent object and fetch it's uuid.
268 $xpath = "//system/fstab/mntent[fsname=" . $pooluuid . "]";
269 $object = $xmlConfig->get($xpath);
270 $mntentref = $object['uuid'];
272 // Prepare the configuration object. Use the name of the shared
273 // folder as the relative directory name of the share.
274 switch ($params['type']) {
276 $tmp = new OMVModuleZFSDataset($params['name']);
279 $tmp = new OMVModuleZFSDataset($params['name']);
282 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
286 $uuid = OMVUtil::uuid();
287 $pathName = $tmp->getMountPoint();
288 $subdirs = preg_split('/\//',$pathName);
289 $reldirpath = $subdirs[count($subdirs)-1];
292 "name" => $params['sharename'],
293 "comment" => $params['comment'],
294 "mntentref" => $mntentref,
295 "reldirpath" => $reldirpath
298 // Set the configuration object.
300 // Check uniqueness. The share name must be global unique because
301 // the name is also used when exporting a shared folder via NFS for
303 $xpath = sprintf("//system/shares/sharedfolder[name='%s']",
305 if(TRUE === $xmlConfig->exists($xpath)) {
306 throw new OMVException(OMVErrorMsg::E_CONFIG_OBJECT_UNIQUENESS,
307 gettext("A shared folder with the given name already exists"));
310 // Add empty list of privileges per default.
311 $object['privileges'] = array();
313 // Append object to configuration.
314 $success = $xmlConfig->set("//system/shares",
315 array("sharedfolder" => $object));
316 if(FALSE === $success) {
317 throw new OMVException(OMVErrorMsg::E_CONFIG_SET_OBJECT_FAILED);
320 // Append the file mode field to the notification object if set.
322 $object['mode'] = "775";
323 if(array_key_exists("mode", $params)) {
324 $object['mode'] = $params['mode'];
327 // Change group owner of directory to configured default group,
329 if(FALSE === chgrp($pathName, $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'])) {
330 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
331 sprintf("Failed to set file group to '%s' for '%s'",
332 $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'], $pathName));
335 // Set the setgid bit. Setting this permission means that all files
336 // created in the folder will inherit the group of the folder rather
337 // than the primary group of the user who creates the file.
338 $mode = fileperms($pathName) | 02000;
339 if(FALSE === chmod($pathName, $mode)) {
340 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
341 sprintf("Failed to set file mode to '%o' for '%s'",
345 // Notify configuration changes.
346 $dispatcher = &OMVNotifyDispatcher::getInstance();
347 $dispatcher->notify(OMV_NOTIFY_CREATE,"org.openmediavault.system.shares.sharedfolder", $object);
348 // Return the configuration object.
354 // Register the RPC service.
355 $rpcServiceMgr = &OMVRpcServiceMgr::getInstance(); // Get the "root" instance for the Services
356 $rpcServiceMgr->registerService(new OMVRpcServiceZFS()); // Register a new instance of the RPC service described above