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