]>
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 | ||
13b8ca82 NB |
11 | /** |
12 | * Get UUID of ZFS pool by name | |
13 | * | |
14 | * @return string UUID of the pool | |
15 | */ | |
16 | public static function getUUIDbyName($name) { | |
17 | preg_match('/^([A-Za-z0-9]+)\/?.*$/', $name, $result); | |
18 | $name = $result[1]; | |
19 | unset($result); | |
20 | $cmd = "blkid -o full"; | |
21 | OMVModuleZFSUtil::exec($cmd, $out, $res); | |
22 | foreach($out as $line) { | |
23 | if(preg_match('/^.*LABEL=\"' . $name . '\" UUID=\"([A-Za-z0-9]+)\".*TYPE=\"zfs_member\"$/', $line, $result)) { | |
24 | return($result[1]); | |
25 | } | |
26 | } | |
27 | return null; | |
28 | } | |
29 | ||
b633468b NB |
30 | /** |
31 | * Get an array with all ZFS objects | |
32 | * | |
33 | * @return An array with all ZFS objects | |
34 | */ | |
35 | public static function getZFSFlatArray() { | |
36 | $prefix = "root/pool-"; | |
37 | $objects = array(); | |
38 | $cmd = "zfs list -H -t all -o name,type 2>&1"; | |
c4ed2435 | 39 | $expanded = true; |
b633468b NB |
40 | OMVModuleZFSUtil::exec($cmd,$out,$res); |
41 | foreach ($out as $line) { | |
42 | $parts = preg_split('/\t/',$line); | |
c4ed2435 NB |
43 | $path = $parts[0]; |
44 | $type = $parts[1]; | |
45 | $subdirs = preg_split('/\//',$path); | |
46 | $root = $subdirs[0]; | |
47 | $tmp = array(); | |
13b8ca82 | 48 | |
c4ed2435 NB |
49 | switch ($type) { |
50 | case "filesystem": | |
51 | if (strpos($path,'/') === false) { | |
52 | //This is a Pool, thus create both the Pool entry and a Filesystem entry corresponding to the Pool. | |
53 | $tmp = array('id'=>$prefix . $path, | |
54 | 'parentid'=>'root', | |
55 | 'name'=>$path, | |
56 | 'type'=>'Pool', | |
57 | 'icon'=>'images/raid.png', | |
58 | 'expanded'=>$expanded, | |
59 | 'path'=>$path); | |
60 | array_push($objects,$tmp); | |
61 | $tmp = array('id'=>$prefix . $path . '/' . $path, | |
62 | 'parentid'=>$prefix . $path, | |
63 | 'name'=>$path, | |
64 | 'type'=>'Filesystem', | |
65 | 'icon'=>'images/filesystem.png', | |
66 | 'path'=>$path, | |
67 | 'expanded'=>$expanded); | |
68 | array_push($objects,$tmp); | |
69 | } else { | |
70 | //This is a Filesystem other than the Pool | |
71 | preg_match('/(.*)\/(.*)$/', $path, $result); | |
72 | $tmp = array('id'=>$prefix . $root . "/" . $path, | |
73 | 'parentid'=>$prefix . $root . "/" . $result[1], | |
74 | 'name'=>$result[2], | |
75 | 'icon'=>"images/filesystem.png", | |
76 | 'path'=>$path, | |
77 | 'expanded'=>$expanded); | |
78 | $ds = new OMVModuleZFSDataset($path); | |
1434b745 NB |
79 | if ($ds->isClone()) { |
80 | //This is a cloned Filesystem | |
c4ed2435 NB |
81 | $tmp['type'] = "Clone"; |
82 | $tmp['origin'] = $ds->getOrigin(); | |
1434b745 NB |
83 | } else { |
84 | //This is a standard Filesystem. | |
c4ed2435 | 85 | $tmp['type']= ucfirst($type); |
1434b745 | 86 | } |
1434b745 | 87 | array_push($objects,$tmp); |
b633468b | 88 | } |
c4ed2435 NB |
89 | break; |
90 | ||
91 | case "volume": | |
92 | preg_match('/(.*)\/(.*)$/', $path, $result); | |
93 | $tmp = array('id'=>$prefix . $root . "/" . $path, | |
94 | 'parentid'=>$prefix . $root . "/" . $result[1], | |
95 | 'name'=>$result[2], | |
96 | 'type'=>ucfirst($type), | |
97 | 'icon'=>"images/zfs_disk.png", | |
98 | 'path'=>$path, | |
99 | 'expanded'=>$expanded); | |
100 | array_push($objects,$tmp); | |
101 | break; | |
102 | ||
103 | case "snapshot": | |
104 | preg_match('/(.*)\@(.*)$/', $path, $result); | |
105 | $subdirs = preg_split('/\//',$result[1]); | |
106 | $root = $subdirs[0]; | |
107 | $tmp = array('id'=>$prefix . $root . "/" . $path, | |
108 | 'parentid'=>$prefix . $root . "/" . $result[1], | |
109 | 'name'=>$result[2], | |
110 | 'type'=>ucfirst($type), | |
111 | 'icon'=>'images/zfs_snap.png', | |
112 | 'path'=>$path, | |
113 | 'expanded'=>$expanded); | |
114 | array_push($objects,$tmp); | |
115 | break; | |
116 | ||
117 | default: | |
118 | break; | |
b633468b NB |
119 | } |
120 | } | |
121 | return $objects; | |
122 | } | |
123 | ||
124 | /** | |
125 | * Create a tree structured array | |
126 | * | |
127 | * @param &$list The flat array to convert to a tree structure | |
128 | * @param $parent Root node of the tree to create | |
129 | * @return Tree structured array | |
130 | * | |
131 | */ | |
132 | public static function createTree(&$list, $parent){ | |
133 | $tree = array(); | |
134 | foreach ($parent as $k=>$l){ | |
135 | if(isset($list[$l['id']])){ | |
136 | $l['leaf'] = false; | |
137 | $l['children'] = OMVModuleZFSUtil::createTree($list, $list[$l['id']]); | |
138 | } else { | |
139 | $l['leaf'] = true; | |
140 | } | |
141 | $tree[] = $l; | |
142 | } | |
143 | return $tree; | |
144 | } | |
145 | ||
146 | /** | |
147 | * Get all Datasets as objects | |
148 | * | |
149 | * @return An array with all the Datasets | |
150 | */ | |
151 | public static function getAllDatasets() { | |
152 | $datasets = array(); | |
153 | $cmd = "zfs list -H -t filesystem -o name 2>&1"; | |
154 | OMVModuleZFSUtil::exec($cmd, $out, $res); | |
155 | foreach ($out as $name) { | |
156 | $ds = new OMVModuleZFSDataset($name); | |
157 | array_push($datasets, $ds); | |
158 | } | |
159 | return $datasets; | |
160 | } | |
161 | ||
162 | /** | |
163 | * Helper function to execute a command and throw an exception on error | |
164 | * (requires stderr redirected to stdout for proper exception message). | |
165 | * | |
166 | * @param string $cmd Command to execute | |
167 | * @param array &$out If provided will contain output in an array | |
168 | * @param int &$res If provided will contain Exit status of the command | |
169 | * @return string Last line of output when executing the command | |
170 | * @throws OMVModuleZFSException | |
171 | * @access public | |
172 | */ | |
173 | public static function exec($cmd, &$out = null, &$res = null) { | |
174 | $tmp = OMVUtil::exec($cmd, $out, $res); | |
175 | if ($res) { | |
176 | throw new OMVModuleZFSException(implode("\n", $out)); | |
177 | } | |
178 | return $tmp; | |
179 | } | |
180 | ||
181 | } | |
182 | ||
183 | ||
184 | ?> |