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