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