]>
git.datanom.net - omvzfs.git/blob - src/Utils.php
bfc5f8a53334402494cbaa09f5834ac742176d7c
2 require_once("Exception.php");
3 require_once("openmediavault/util.inc");
4 require_once("Dataset.php");
7 * Helper class for ZFS module
9 class OMVModuleZFSUtil
{
12 * Get an array with all ZFS objects
14 * @return An array with all ZFS objects
16 public static function getZFSFlatArray() {
17 $prefix = "root/pool-";
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],
29 'icon'=>'images/raid.png',
32 array_push($objects,$tmp);
33 $tmp = array('id'=>$prefix . $parts[0] . '/' . $parts[0],
34 'parentid'=>$prefix . $parts[0],
37 'icon'=>'images/filesystem.png',
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],
48 'icon'=>'images/zfs_snap.png',
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];
59 $icon = "images/zfs_snap.png";
60 $tmp = array('id'=>$id,
61 'parentid'=>$parentid,
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];
74 $type = ucfirst($parts[1]);
75 if (strcmp($type, "Filesystem") == 0) {
76 $icon = "images/filesystem.png";
77 $ds = new OMVModuleZFSDataset($parts[0]);
79 //This is a cloned Filesystem
80 $tmp = array('id'=>$id,
81 'parentid'=>$parentid,
87 'origin' => $ds->getOrigin());
88 array_push($objects,$tmp);
90 //This is a standard Filesystem.
91 $tmp = array('id'=>$id,
92 'parentid'=>$parentid,
98 'origin' => $ds->getOrigin());
99 array_push($objects,$tmp);
103 $icon = "images/zfs_disk.png";
104 $tmp = array('id'=>$id,
105 'parentid'=>$parentid,
111 array_push($objects,$tmp);
119 * Create a tree structured array
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
126 public static function createTree(&$list, $parent){
128 foreach ($parent as $k=>$l){
129 if(isset($list[$l['id']])){
131 $l['children'] = OMVModuleZFSUtil
::createTree($list, $list[$l['id']]);
141 * Get all Datasets as objects
143 * @return An array with all the Datasets
145 public static function getAllDatasets() {
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);
157 * Helper function to execute a command and throw an exception on error
158 * (requires stderr redirected to stdout for proper exception message).
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
167 public static function exec($cmd, &$out = null, &$res = null) {
168 $tmp = OMVUtil
::exec($cmd, $out, $res);
170 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.094028 seconds and 4 git commands to generate.