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