]>
Commit | Line | Data |
---|---|---|
b633468b NB |
1 | <?php |
2 | require_once("Exception.php"); | |
3 | require_once("openmediavault/util.inc"); | |
4 | require_once("Dataset.php"); | |
5 | ||
6 | /** | |
7 | * Helper class for ZFS module | |
8 | */ | |
9 | class 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) { | |
114 | //This is a Pool, thus create both the Pool entry and a Filesystem entry corresponding to the Pool. | |
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); | |
123 | $tmp = array('id'=>$prefix . $path . '/' . $path, | |
124 | 'parentid'=>$prefix . $path, | |
125 | 'name'=>$path, | |
126 | 'type'=>'Filesystem', | |
127 | 'icon'=>'images/filesystem.png', | |
128 | 'path'=>$path, | |
129 | 'expanded'=>$expanded); | |
130 | array_push($objects,$tmp); | |
131 | } else { | |
132 | //This is a Filesystem other than the Pool | |
133 | preg_match('/(.*)\/(.*)$/', $path, $result); | |
134 | $tmp = array('id'=>$prefix . $root . "/" . $path, | |
135 | 'parentid'=>$prefix . $root . "/" . $result[1], | |
136 | 'name'=>$result[2], | |
137 | 'icon'=>"images/filesystem.png", | |
138 | 'path'=>$path, | |
139 | 'expanded'=>$expanded); | |
140 | $ds = new OMVModuleZFSDataset($path); | |
1434b745 NB |
141 | if ($ds->isClone()) { |
142 | //This is a cloned Filesystem | |
c4ed2435 NB |
143 | $tmp['type'] = "Clone"; |
144 | $tmp['origin'] = $ds->getOrigin(); | |
1434b745 NB |
145 | } else { |
146 | //This is a standard Filesystem. | |
c4ed2435 | 147 | $tmp['type']= ucfirst($type); |
1434b745 | 148 | } |
1434b745 | 149 | array_push($objects,$tmp); |
b633468b | 150 | } |
c4ed2435 NB |
151 | break; |
152 | ||
153 | case "volume": | |
154 | preg_match('/(.*)\/(.*)$/', $path, $result); | |
155 | $tmp = array('id'=>$prefix . $root . "/" . $path, | |
156 | 'parentid'=>$prefix . $root . "/" . $result[1], | |
157 | 'name'=>$result[2], | |
158 | 'type'=>ucfirst($type), | |
18516c1e | 159 | 'icon'=>"images/save.png", |
c4ed2435 NB |
160 | 'path'=>$path, |
161 | 'expanded'=>$expanded); | |
162 | array_push($objects,$tmp); | |
163 | break; | |
164 | ||
165 | case "snapshot": | |
166 | preg_match('/(.*)\@(.*)$/', $path, $result); | |
167 | $subdirs = preg_split('/\//',$result[1]); | |
168 | $root = $subdirs[0]; | |
169 | $tmp = array('id'=>$prefix . $root . "/" . $path, | |
170 | 'parentid'=>$prefix . $root . "/" . $result[1], | |
171 | 'name'=>$result[2], | |
172 | 'type'=>ucfirst($type), | |
173 | 'icon'=>'images/zfs_snap.png', | |
174 | 'path'=>$path, | |
175 | 'expanded'=>$expanded); | |
176 | array_push($objects,$tmp); | |
177 | break; | |
178 | ||
179 | default: | |
180 | break; | |
b633468b NB |
181 | } |
182 | } | |
183 | return $objects; | |
184 | } | |
185 | ||
186 | /** | |
187 | * Create a tree structured array | |
188 | * | |
189 | * @param &$list The flat array to convert to a tree structure | |
190 | * @param $parent Root node of the tree to create | |
191 | * @return Tree structured array | |
192 | * | |
193 | */ | |
194 | public static function createTree(&$list, $parent){ | |
195 | $tree = array(); | |
196 | foreach ($parent as $k=>$l){ | |
197 | if(isset($list[$l['id']])){ | |
198 | $l['leaf'] = false; | |
199 | $l['children'] = OMVModuleZFSUtil::createTree($list, $list[$l['id']]); | |
200 | } else { | |
201 | $l['leaf'] = true; | |
202 | } | |
203 | $tree[] = $l; | |
204 | } | |
205 | return $tree; | |
206 | } | |
207 | ||
208 | /** | |
209 | * Get all Datasets as objects | |
210 | * | |
211 | * @return An array with all the Datasets | |
212 | */ | |
213 | public static function getAllDatasets() { | |
214 | $datasets = array(); | |
215 | $cmd = "zfs list -H -t filesystem -o name 2>&1"; | |
216 | OMVModuleZFSUtil::exec($cmd, $out, $res); | |
217 | foreach ($out as $name) { | |
218 | $ds = new OMVModuleZFSDataset($name); | |
219 | array_push($datasets, $ds); | |
220 | } | |
221 | return $datasets; | |
222 | } | |
223 | ||
224 | /** | |
225 | * Helper function to execute a command and throw an exception on error | |
226 | * (requires stderr redirected to stdout for proper exception message). | |
227 | * | |
228 | * @param string $cmd Command to execute | |
229 | * @param array &$out If provided will contain output in an array | |
230 | * @param int &$res If provided will contain Exit status of the command | |
231 | * @return string Last line of output when executing the command | |
232 | * @throws OMVModuleZFSException | |
233 | * @access public | |
234 | */ | |
235 | public static function exec($cmd, &$out = null, &$res = null) { | |
236 | $tmp = OMVUtil::exec($cmd, $out, $res); | |
237 | if ($res) { | |
238 | throw new OMVModuleZFSException(implode("\n", $out)); | |
239 | } | |
240 | return $tmp; | |
241 | } | |
242 | ||
243 | } | |
244 | ||
245 | ||
246 | ?> |