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