]> git.datanom.net - omvzfs.git/blame - src/Utils.php
Added methods to Dataset to get it's origin if it's a clone.
[omvzfs.git] / src / Utils.php
CommitLineData
b633468b
NB
1<?php
2require_once("Exception.php");
3require_once("openmediavault/util.inc");
4require_once("Dataset.php");
5
6/**
7 * Helper class for ZFS module
8 */
9class 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 $tmp = array('id'=>$prefix . $parts[0],
25 'parentid'=>'root',
26 'name'=>$parts[0],
27 'type'=>'Pool',
28 'icon'=>'images/raid.png',
29 'expanded'=>true,
30 'path'=>$parts[0]);
31 array_push($objects,$tmp);
32 $tmp = array('id'=>$prefix . $parts[0] . '/' . $parts[0],
33 'parentid'=>$prefix . $parts[0],
34 'name'=>$parts[0],
35 'type'=>'Filesystem',
36 'icon'=>'images/filesystem.png',
37 'path'=>$parts[0],
38 'expanded'=>true);
39 array_push($objects,$tmp);
40 } elseif (strpos($parts[0],'/') === false) {
41 $pname = preg_split('/\@/',$parts[0]);
42 $tmp = array('id'=>$prefix . $pname[0] . '/' . $parts[0],
43 'parentid'=>$prefix . $pname[0]. '/' . $pname[0],
44 'name'=>$pname[1],
45 'type'=>'Snapshot',
46 'icon'=>'images/zfs_snap.png',
47 'path'=>$parts[0],
48 'expanded'=>true);
49 array_push($objects,$tmp);
50 } elseif (preg_match('/(.*)\@(.*)$/', $parts[0], $result)) {
51 $pname = preg_split('/\//',$parts[0]);
52 $id = $prefix . $pname[0] . "/" . $result[0];
53 $parentid = $prefix . $pname[0] . "/" . $result[1];
54 $name = $result[2];
55 $type = "Snapshot";
56 $icon = "images/zfs_snap.png";
57 $tmp = array('id'=>$id,
58 'parentid'=>$parentid,
59 'name'=>$name,
60 'type'=>$type,
61 'icon'=>$icon,
62 'path'=>$parts[0],
63 'expanded'=>true);
64 array_push($objects,$tmp);
65 } elseif (preg_match('/(.*)\/(.*)$/', $parts[0], $result)) {
66 $pname = preg_split('/\//',$parts[0]);
67 $id = $prefix . $pname[0] . "/" . $result[0];
68 $parentid = $prefix . $pname[0] . "/" . $result[1];
69 $name = $result[2];
70 $type = ucfirst($parts[1]);
71 if (strcmp($type, "Filesystem") == 0) {
72 $icon = "images/filesystem.png";
73 } else {
74 $icon = "images/zfs_disk.png";
75 }
76 $tmp = array('id'=>$id,
77 'parentid'=>$parentid,
78 'name'=>$name,
79 'type'=>$type,
80 'icon'=>$icon,
81 'path'=>$parts[0],
82 'expanded'=>true);
83 array_push($objects,$tmp);
84 }
85 }
86 return $objects;
87 }
88
89 /**
90 * Create a tree structured array
91 *
92 * @param &$list The flat array to convert to a tree structure
93 * @param $parent Root node of the tree to create
94 * @return Tree structured array
95 *
96 */
97 public static function createTree(&$list, $parent){
98 $tree = array();
99 foreach ($parent as $k=>$l){
100 if(isset($list[$l['id']])){
101 $l['leaf'] = false;
102 $l['children'] = OMVModuleZFSUtil::createTree($list, $list[$l['id']]);
103 } else {
104 $l['leaf'] = true;
105 }
106 $tree[] = $l;
107 }
108 return $tree;
109 }
110
111 /**
112 * Get all Datasets as objects
113 *
114 * @return An array with all the Datasets
115 */
116 public static function getAllDatasets() {
117 $datasets = array();
118 $cmd = "zfs list -H -t filesystem -o name 2>&1";
119 OMVModuleZFSUtil::exec($cmd, $out, $res);
120 foreach ($out as $name) {
121 $ds = new OMVModuleZFSDataset($name);
122 array_push($datasets, $ds);
123 }
124 return $datasets;
125 }
126
127 /**
128 * Helper function to execute a command and throw an exception on error
129 * (requires stderr redirected to stdout for proper exception message).
130 *
131 * @param string $cmd Command to execute
132 * @param array &$out If provided will contain output in an array
133 * @param int &$res If provided will contain Exit status of the command
134 * @return string Last line of output when executing the command
135 * @throws OMVModuleZFSException
136 * @access public
137 */
138 public static function exec($cmd, &$out = null, &$res = null) {
139 $tmp = OMVUtil::exec($cmd, $out, $res);
140 if ($res) {
141 throw new OMVModuleZFSException(implode("\n", $out));
142 }
143 return $tmp;
144 }
145
146}
147
148
149?>
This page took 0.05854 seconds and 5 git commands to generate.