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 $disks = preg_split("/[,;]/", $params['devices']);
63 $vdev = new OMVModuleZFSVdev($params['name'], $pooltype, $disks);
64 $pool = new OMVModuleZFSZpool($vdev, $opts);
65 //Ugly fix to solve the problem of blkid not displaying info on newly created pools
67 $pool->import($pool->getName());
70 public function getObjectTree($params, $context) {
71 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
72 $objects = OMVModuleZFSUtil::getZFSFlatArray();
74 foreach ($objects as $a){
75 $new[$a['parentid']][] = $a;
77 $tree = OMVModuleZFSUtil::createTree($new, $new['root']);
78 OMVModuleZFSUtil::addMissingOMVMntEnt(); //Adds missing ZFS filesystems to the OMV core
82 public function passParam($params, $context) {
83 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
84 //$msg = "Key=" . $params['key'] . ";Value=" . $params['value'] . ";";
85 //throw new OMVModuleZFSException($msg);
86 return array($params['key'] => $params['value']);
89 public function addObject($params, $context) {
90 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
91 switch ($params['type']) {
93 $name = $params['path'] . "/" . $params['name'];
94 $tmp = new OMVModuleZFSDataset($name);
97 $name = $params['path'] . "@" . $params['name'];
98 $tmp = new OMVModuleZFSSnapshot($name);
101 $name = $params['path'] . "/" . $params['name'];
102 $tmp = new OMVModuleZFSZvol($name);
103 $tmp->create($params['size']);
106 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
111 public function deleteObject($params, $context) {
112 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
113 switch ($params['type']) {
116 $name = $params['name'];
117 $tmp = new OMVModuleZFSDataset($name);
121 $name = $params['name'];
122 $tmp = new OMVModuleZFSSnapshot($name);
126 $name = $params['name'];
127 $tmp = new OMVModuleZFSZvol($name);
131 $name = $params['name'];
132 $tmp = new OMVModuleZFSZpool($name);
136 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
141 public function getProperties($params, $context) {
142 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
144 $name = $params['name'];
145 switch ($params['type']) {
148 $tmp = new OMVModuleZFSDataset($name);
151 $tmp = new OMVModuleZFSSnapshot($name);
154 $tmp = new OMVModuleZFSZvol($name);
157 $tmp = new OMVModuleZFSZpool($name);
160 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
163 $properties = $tmp->getProperties();
164 foreach ($properties as $propertyk => $propertyv) {
165 if (!(strcmp($propertyv['source'], "-") == 0)) {
166 $objects[] = array('property' => $propertyk,
167 'value' => $propertyv['value'],
168 'source' => $propertyv['source'],
169 'modified' => "false");
175 public function setProperties($params, $context) {
176 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
178 switch ($params['type']) {
181 $tmp = new OMVModuleZFSDataset($params['name']);
184 $tmp = new OMVModuleZFSSnapshot($params['name']);
187 $tmp = new OMVModuleZFSZvol($params['name']);
190 $tmp = new OMVModuleZFSZpool($params['name']);
193 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
196 foreach ($params['properties'] as $property) {
197 $objects[$property['property']] = $property['value'];
199 $tmp->setProperties($objects);
202 public function inherit($params, $context) {
203 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
204 // Create a background process.
205 $bgStatusFilename = $this->createBgProcStatus();
206 $pid = $this->fork();
207 if($pid > 0) { // Parent process.
208 $this->initializeBgProcStatus($bgStatusFilename, $pid);
209 return $bgStatusFilename;
213 $bgOutputFilename = $this->createBgProcOutput();
214 $this->updateBgProcStatus($bgStatusFilename, "outputfilename", $bgOutputFilename);
215 switch ($params['type']) {
218 $tmp = new OMVModuleZFSDataset($params['name']);
221 $tmp = new OMVModuleZFSSnapshot($params['name']);
224 $tmp = new OMVModuleZFSZvol($params['name']);
227 $tmp = new OMVModuleZFSZpool($params['name']);
230 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
233 $tmp->inherit($params['property']);
234 $this->finalizeBgProcStatus($bgStatusFilename, $output);
236 } catch(Exception $e) {
237 $this->finalizeBgProcStatus($bgStatusFilename, "", $e);
242 public function getSharedParams($params, $context) {
243 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
245 $ds = new OMVModuleZFSDataset($params['name']);
246 $mountpoint = $ds->getMountPoint();
248 "mountpoint" => $mountpoint,
249 "name" => $params['name'],
250 "type" => $params['type']);
253 public function createShare($params, $context) {
255 $this->validateMethodContext($context, array("role" => OMV_ROLE_ADMINISTRATOR));
257 //Get the UUID of the Pool
258 $pooluuid = OMVModuleZFSUtil::getUUIDbyName($params['name']);
259 preg_match('/^([A-Za-z0-9]+)\/?.*$/', $params['name'], $result);
260 $poolname = $result[1];
263 //Get the mntent object and fetch it's uuid.
264 $xpath = "//system/fstab/mntent[fsname=" . $pooluuid . "]";
265 $object = $xmlConfig->get($xpath);
266 $mntentref = $object['uuid'];
268 // Prepare the configuration object. Use the name of the shared
269 // folder as the relative directory name of the share.
270 switch ($params['type']) {
272 $tmp = new OMVModuleZFSDataset($params['name']);
275 $tmp = new OMVModuleZFSDataset($params['name']);
278 throw new OMVModuleZFSException("Illegal type provided: " . $params['type']);
282 $uuid = OMVUtil::uuid();
283 $pathName = $tmp->getMountPoint();
284 $subdirs = preg_split('/\//',$pathName);
285 $reldirpath = $subdirs[count($subdirs)-1];
288 "name" => $params['sharename'],
289 "comment" => $params['comment'],
290 "mntentref" => $mntentref,
291 "reldirpath" => $reldirpath
294 // Set the configuration object.
296 // Check uniqueness. The share name must be global unique because
297 // the name is also used when exporting a shared folder via NFS for
299 $xpath = sprintf("//system/shares/sharedfolder[name='%s']",
301 if(TRUE === $xmlConfig->exists($xpath)) {
302 throw new OMVException(OMVErrorMsg::E_CONFIG_OBJECT_UNIQUENESS,
303 gettext("A shared folder with the given name already exists"));
306 // Add empty list of privileges per default.
307 $object['privileges'] = array();
309 // Append object to configuration.
310 $success = $xmlConfig->set("//system/shares",
311 array("sharedfolder" => $object));
312 if(FALSE === $success) {
313 throw new OMVException(OMVErrorMsg::E_CONFIG_SET_OBJECT_FAILED);
316 // Append the file mode field to the notification object if set.
318 $object['mode'] = "775";
319 if(array_key_exists("mode", $params)) {
320 $object['mode'] = $params['mode'];
323 // Change group owner of directory to configured default group,
325 if(FALSE === chgrp($pathName, $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'])) {
326 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
327 sprintf("Failed to set file group to '%s' for '%s'",
328 $GLOBALS['OMV_USERMGMT_DEFAULT_GROUP'], $pathName));
331 // Set the setgid bit. Setting this permission means that all files
332 // created in the folder will inherit the group of the folder rather
333 // than the primary group of the user who creates the file.
334 $mode = fileperms($pathName) | 02000;
335 if(FALSE === chmod($pathName, $mode)) {
336 throw new OMVException(OMVErrorMsg::E_MISC_FAILURE,
337 sprintf("Failed to set file mode to '%o' for '%s'",
341 // Notify configuration changes.
342 $dispatcher = &OMVNotifyDispatcher::getInstance();
343 $dispatcher->notify(OMV_NOTIFY_CREATE,"org.openmediavault.system.shares.sharedfolder", $object);
344 // Return the configuration object.
350 // Register the RPC service.
351 $rpcServiceMgr = &OMVRpcServiceMgr::getInstance(); // Get the "root" instance for the Services
352 $rpcServiceMgr->registerService(new OMVRpcServiceZFS()); // Register a new instance of the RPC service described above