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