]> git.datanom.net - omvzfs.git/blame - src/Utils.php
Fix reldirpath value when creating a shared folder.
[omvzfs.git] / src / Utils.php
CommitLineData
b633468b
NB
1<?php
2require_once("Exception.php");
3require_once("openmediavault/util.inc");
4require_once("Dataset.php");
5
6/**
7 * Helper class for ZFS module
8 */
9class OMVModuleZFSUtil {
10
e20fe312
NB
11 /**
12 * Get /dev/disk/by-path from /dev/sdX
13 *
14 * @return string Disk identifier
15 */
16 public static function getDiskPath($disk) {
17 preg_match("/^.*\/([A-Za-z0-9]+)$/", $disk, $identifier);
18 $cmd = "ls -la /dev/disk/by-path | grep '$identifier[1]$'";
19 OMVModuleZFSUtil::exec($cmd, $out, $res);
20 if (is_array($out)) {
21 $cols = preg_split('/[\s]+/', $out[0]);
22 return($cols[count($cols)-3]);
23 }
24 }
25
26
4163f889
NB
27 /**
28 * Get poolname from name of dataset/volume etc.
29 *
30 * @return string Name of the pool
31 */
32 public static function getPoolname($name) {
33 $tmp = preg_split('/[\/]+/', $name);
34 return($tmp[0]);
35 }
36
13b8ca82
NB
37 /**
38 * Get UUID of ZFS pool by name
39 *
40 * @return string UUID of the pool
41 */
4163f889
NB
42 public static function getUUIDbyName($poolname) {
43 $cmd = "zpool get guid " . $poolname . " 2>&1";
13b8ca82 44 OMVModuleZFSUtil::exec($cmd, $out, $res);
42856e8b
NB
45 if (isset($out)) {
46 $headers = preg_split('/[\s]+/', $out[0]);
47 for ($i=0; $i<count($headers); $i++) {
48 if (strcmp($headers[$i], "VALUE") === 0) {
49 $valuecol=$i;
50 break;
51 }
13b8ca82 52 }
42856e8b
NB
53 $line = preg_split('/[\s]+/', $out[1]);
54 return $line[$valuecol];
13b8ca82
NB
55 }
56 return null;
57 }
58
a36352f7
NB
59 /**
60 * Add any missing ZFS pool to the OMV backend
61 *
62 */
63 public static function addMissingOMVMntEnt() {
64 global $xmlConfig;
a36352f7
NB
65 $cmd = "zpool list -H -o name";
66 OMVModuleZFSUtil::exec($cmd, $out, $res);
67 foreach($out as $name) {
68 $pooluuid = OMVModuleZFSUtil::getUUIDbyName($name);
4163f889
NB
69 $xpath = "//system/fstab/mntent[fsname=" . $pooluuid . "]";
70 $mountpoint = $xmlConfig->get($xpath);
71 if (is_null($mountpoint)) {
72 $uuid = OMVUtil::uuid();
73 $pool = new OMVModuleZFSZpool($name);
74 $dir = $pool->getMountPoint();
75 $object = array(
76 "uuid" => $uuid,
77 "fsname" => $pooluuid,
78 "dir" => $dir,
79 "type" => "zfs",
80 "opts" => "rw,relatime,xattr",
81 "freq" => "0",
82 "passno" => "2"
83 );
84 $xmlConfig->set("//system/fstab",array("mntent" => $object));
85 $dispatcher = &OMVNotifyDispatcher::getInstance();
86 $dispatcher->notify(OMV_NOTIFY_CREATE,"org.openmediavault.system.fstab.mntent", $object);
a36352f7
NB
87 }
88 }
89 return null;
90 }
91
b633468b
NB
92 /**
93 * Get an array with all ZFS objects
94 *
95 * @return An array with all ZFS objects
96 */
97 public static function getZFSFlatArray() {
98 $prefix = "root/pool-";
99 $objects = array();
100 $cmd = "zfs list -H -t all -o name,type 2>&1";
c4ed2435 101 $expanded = true;
b633468b
NB
102 OMVModuleZFSUtil::exec($cmd,$out,$res);
103 foreach ($out as $line) {
104 $parts = preg_split('/\t/',$line);
c4ed2435
NB
105 $path = $parts[0];
106 $type = $parts[1];
107 $subdirs = preg_split('/\//',$path);
108 $root = $subdirs[0];
109 $tmp = array();
13b8ca82 110
c4ed2435
NB
111 switch ($type) {
112 case "filesystem":
113 if (strpos($path,'/') === false) {
5627dfd7 114 //This is a Pool
c4ed2435
NB
115 $tmp = array('id'=>$prefix . $path,
116 'parentid'=>'root',
117 'name'=>$path,
118 'type'=>'Pool',
119 'icon'=>'images/raid.png',
120 'expanded'=>$expanded,
121 'path'=>$path);
122 array_push($objects,$tmp);
c4ed2435 123 } else {
5627dfd7 124 //This is a Filesystem
c4ed2435 125 preg_match('/(.*)\/(.*)$/', $path, $result);
5627dfd7
NB
126 $tmp = array('id'=>$prefix . $path,
127 'parentid'=>$prefix . $result[1],
c4ed2435
NB
128 'name'=>$result[2],
129 'icon'=>"images/filesystem.png",
130 'path'=>$path,
131 'expanded'=>$expanded);
132 $ds = new OMVModuleZFSDataset($path);
1434b745
NB
133 if ($ds->isClone()) {
134 //This is a cloned Filesystem
c4ed2435
NB
135 $tmp['type'] = "Clone";
136 $tmp['origin'] = $ds->getOrigin();
1434b745
NB
137 } else {
138 //This is a standard Filesystem.
c4ed2435 139 $tmp['type']= ucfirst($type);
1434b745 140 }
1434b745 141 array_push($objects,$tmp);
b633468b 142 }
c4ed2435
NB
143 break;
144
145 case "volume":
146 preg_match('/(.*)\/(.*)$/', $path, $result);
5627dfd7
NB
147 $tmp = array('id'=>$prefix . $path,
148 'parentid'=>$prefix . $result[1],
c4ed2435
NB
149 'name'=>$result[2],
150 'type'=>ucfirst($type),
18516c1e 151 'icon'=>"images/save.png",
c4ed2435
NB
152 'path'=>$path,
153 'expanded'=>$expanded);
154 array_push($objects,$tmp);
155 break;
156
157 case "snapshot":
158 preg_match('/(.*)\@(.*)$/', $path, $result);
159 $subdirs = preg_split('/\//',$result[1]);
160 $root = $subdirs[0];
5627dfd7
NB
161 $tmp = array('id'=>$prefix . $path,
162 'parentid'=>$prefix . $result[1],
c4ed2435
NB
163 'name'=>$result[2],
164 'type'=>ucfirst($type),
165 'icon'=>'images/zfs_snap.png',
166 'path'=>$path,
167 'expanded'=>$expanded);
168 array_push($objects,$tmp);
169 break;
170
171 default:
172 break;
b633468b
NB
173 }
174 }
175 return $objects;
176 }
177
178 /**
179 * Create a tree structured array
180 *
181 * @param &$list The flat array to convert to a tree structure
182 * @param $parent Root node of the tree to create
183 * @return Tree structured array
184 *
185 */
186 public static function createTree(&$list, $parent){
187 $tree = array();
188 foreach ($parent as $k=>$l){
189 if(isset($list[$l['id']])){
190 $l['leaf'] = false;
191 $l['children'] = OMVModuleZFSUtil::createTree($list, $list[$l['id']]);
192 } else {
193 $l['leaf'] = true;
194 }
195 $tree[] = $l;
196 }
197 return $tree;
198 }
199
200 /**
201 * Get all Datasets as objects
202 *
203 * @return An array with all the Datasets
204 */
205 public static function getAllDatasets() {
206 $datasets = array();
207 $cmd = "zfs list -H -t filesystem -o name 2>&1";
208 OMVModuleZFSUtil::exec($cmd, $out, $res);
209 foreach ($out as $name) {
210 $ds = new OMVModuleZFSDataset($name);
211 array_push($datasets, $ds);
212 }
213 return $datasets;
214 }
215
216 /**
217 * Helper function to execute a command and throw an exception on error
218 * (requires stderr redirected to stdout for proper exception message).
219 *
220 * @param string $cmd Command to execute
221 * @param array &$out If provided will contain output in an array
222 * @param int &$res If provided will contain Exit status of the command
223 * @return string Last line of output when executing the command
224 * @throws OMVModuleZFSException
225 * @access public
226 */
227 public static function exec($cmd, &$out = null, &$res = null) {
228 $tmp = OMVUtil::exec($cmd, $out, $res);
229 if ($res) {
230 throw new OMVModuleZFSException(implode("\n", $out));
231 }
232 return $tmp;
233 }
234
235}
236
237
238?>
This page took 0.119418 seconds and 5 git commands to generate.