]>
git.datanom.net - omvzfs.git/blob - src/Utils.php
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 $tmp = array('id'=>$prefix . $parts[0],
28 'icon'=>'images/raid.png',
31 array_push($objects,$tmp);
32 $tmp = array('id'=>$prefix . $parts[0] . '/' . $parts[0],
33 'parentid'=>$prefix . $parts[0],
36 'icon'=>'images/filesystem.png',
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],
46 'icon'=>'images/zfs_snap.png',
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];
56 $icon = "images/zfs_snap.png";
57 $tmp = array('id'=>$id,
58 'parentid'=>$parentid,
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];
70 $type = ucfirst($parts[1]);
71 if (strcmp($type, "Filesystem") == 0) {
72 $icon = "images/filesystem.png";
74 $icon = "images/zfs_disk.png";
76 $tmp = array('id'=>$id,
77 'parentid'=>$parentid,
83 array_push($objects,$tmp);
90 * Create a tree structured array
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
97 public static function createTree(&$list, $parent){
99 foreach ($parent as $k=>$l){
100 if(isset($list[$l['id']])){
102 $l['children'] = OMVModuleZFSUtil
::createTree($list, $list[$l['id']]);
112 * Get all Datasets as objects
114 * @return An array with all the Datasets
116 public static function getAllDatasets() {
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);
128 * Helper function to execute a command and throw an exception on error
129 * (requires stderr redirected to stdout for proper exception message).
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
138 public static function exec($cmd, &$out = null, &$res = null) {
139 $tmp = OMVUtil
::exec($cmd, $out, $res);
141 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.198188 seconds and 6 git commands to generate.