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