]>
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";
21 OMVModuleZFSUtil
::exec($cmd,$out,$res);
22 foreach ($out as $line) {
23 $parts = preg_split('/\t/',$line);
26 $subdirs = preg_split('/\//',$path);
32 if (strpos($path,'/') === false) {
33 //This is a Pool, thus create both the Pool entry and a Filesystem entry corresponding to the Pool.
34 $tmp = array('id'=>$prefix . $path,
38 'icon'=>'images/raid.png',
39 'expanded'=>$expanded,
41 array_push($objects,$tmp);
42 $tmp = array('id'=>$prefix . $path . '/' . $path,
43 'parentid'=>$prefix . $path,
46 'icon'=>'images/filesystem.png',
48 'expanded'=>$expanded);
49 array_push($objects,$tmp);
51 //This is a Filesystem other than the Pool
52 preg_match('/(.*)\/(.*)$/', $path, $result);
53 $tmp = array('id'=>$prefix . $root . "/" . $path,
54 'parentid'=>$prefix . $root . "/" . $result[1],
56 'icon'=>"images/filesystem.png",
58 'expanded'=>$expanded);
59 $ds = new OMVModuleZFSDataset($path);
61 //This is a cloned Filesystem
62 $tmp['type'] = "Clone";
63 $tmp['origin'] = $ds->getOrigin();
65 //This is a standard Filesystem.
66 $tmp['type']= ucfirst($type);
68 array_push($objects,$tmp);
73 preg_match('/(.*)\/(.*)$/', $path, $result);
74 $tmp = array('id'=>$prefix . $root . "/" . $path,
75 'parentid'=>$prefix . $root . "/" . $result[1],
77 'type'=>ucfirst($type),
78 'icon'=>"images/zfs_disk.png",
80 'expanded'=>$expanded);
81 array_push($objects,$tmp);
85 preg_match('/(.*)\@(.*)$/', $path, $result);
86 $subdirs = preg_split('/\//',$result[1]);
88 $tmp = array('id'=>$prefix . $root . "/" . $path,
89 'parentid'=>$prefix . $root . "/" . $result[1],
91 'type'=>ucfirst($type),
92 'icon'=>'images/zfs_snap.png',
94 'expanded'=>$expanded);
95 array_push($objects,$tmp);
106 * Create a tree structured array
108 * @param &$list The flat array to convert to a tree structure
109 * @param $parent Root node of the tree to create
110 * @return Tree structured array
113 public static function createTree(&$list, $parent){
115 foreach ($parent as $k=>$l){
116 if(isset($list[$l['id']])){
118 $l['children'] = OMVModuleZFSUtil
::createTree($list, $list[$l['id']]);
128 * Get all Datasets as objects
130 * @return An array with all the Datasets
132 public static function getAllDatasets() {
134 $cmd = "zfs list -H -t filesystem -o name 2>&1";
135 OMVModuleZFSUtil
::exec($cmd, $out, $res);
136 foreach ($out as $name) {
137 $ds = new OMVModuleZFSDataset($name);
138 array_push($datasets, $ds);
144 * Helper function to execute a command and throw an exception on error
145 * (requires stderr redirected to stdout for proper exception message).
147 * @param string $cmd Command to execute
148 * @param array &$out If provided will contain output in an array
149 * @param int &$res If provided will contain Exit status of the command
150 * @return string Last line of output when executing the command
151 * @throws OMVModuleZFSException
154 public static function exec($cmd, &$out = null, &$res = null) {
155 $tmp = OMVUtil
::exec($cmd, $out, $res);
157 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.097299 seconds and 6 git commands to generate.