]>
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 UUID of ZFS pool by name
14 * @return string UUID of the pool
16 public static function getUUIDbyName($name) {
17 preg_match('/^([A-Za-z0-9]+)\/?.*$/', $name, $result);
20 $cmd = "blkid -o full";
21 OMVModuleZFSUtil
::exec($cmd, $out, $res);
22 foreach($out as $line) {
23 if(preg_match('/^.*LABEL=\"' . $name . '\" UUID=\"([A-Za-z0-9]+)\".*TYPE=\"zfs_member\"$/', $line, $result)) {
31 * Get an array with all ZFS objects
33 * @return An array with all ZFS objects
35 public static function getZFSFlatArray() {
36 $prefix = "root/pool-";
38 $cmd = "zfs list -H -t all -o name,type 2>&1";
40 OMVModuleZFSUtil
::exec($cmd,$out,$res);
41 foreach ($out as $line) {
42 $parts = preg_split('/\t/',$line);
45 $subdirs = preg_split('/\//',$path);
51 if (strpos($path,'/') === false) {
52 //This is a Pool, thus create both the Pool entry and a Filesystem entry corresponding to the Pool.
53 $tmp = array('id'=>$prefix . $path,
57 'icon'=>'images/raid.png',
58 'expanded'=>$expanded,
60 array_push($objects,$tmp);
61 $tmp = array('id'=>$prefix . $path . '/' . $path,
62 'parentid'=>$prefix . $path,
65 'icon'=>'images/filesystem.png',
67 'expanded'=>$expanded);
68 array_push($objects,$tmp);
70 //This is a Filesystem other than the Pool
71 preg_match('/(.*)\/(.*)$/', $path, $result);
72 $tmp = array('id'=>$prefix . $root . "/" . $path,
73 'parentid'=>$prefix . $root . "/" . $result[1],
75 'icon'=>"images/filesystem.png",
77 'expanded'=>$expanded);
78 $ds = new OMVModuleZFSDataset($path);
80 //This is a cloned Filesystem
81 $tmp['type'] = "Clone";
82 $tmp['origin'] = $ds->getOrigin();
84 //This is a standard Filesystem.
85 $tmp['type']= ucfirst($type);
87 array_push($objects,$tmp);
92 preg_match('/(.*)\/(.*)$/', $path, $result);
93 $tmp = array('id'=>$prefix . $root . "/" . $path,
94 'parentid'=>$prefix . $root . "/" . $result[1],
96 'type'=>ucfirst($type),
97 'icon'=>"images/zfs_disk.png",
99 'expanded'=>$expanded);
100 array_push($objects,$tmp);
104 preg_match('/(.*)\@(.*)$/', $path, $result);
105 $subdirs = preg_split('/\//',$result[1]);
107 $tmp = array('id'=>$prefix . $root . "/" . $path,
108 'parentid'=>$prefix . $root . "/" . $result[1],
110 'type'=>ucfirst($type),
111 'icon'=>'images/zfs_snap.png',
113 'expanded'=>$expanded);
114 array_push($objects,$tmp);
125 * Create a tree structured array
127 * @param &$list The flat array to convert to a tree structure
128 * @param $parent Root node of the tree to create
129 * @return Tree structured array
132 public static function createTree(&$list, $parent){
134 foreach ($parent as $k=>$l){
135 if(isset($list[$l['id']])){
137 $l['children'] = OMVModuleZFSUtil
::createTree($list, $list[$l['id']]);
147 * Get all Datasets as objects
149 * @return An array with all the Datasets
151 public static function getAllDatasets() {
153 $cmd = "zfs list -H -t filesystem -o name 2>&1";
154 OMVModuleZFSUtil
::exec($cmd, $out, $res);
155 foreach ($out as $name) {
156 $ds = new OMVModuleZFSDataset($name);
157 array_push($datasets, $ds);
163 * Helper function to execute a command and throw an exception on error
164 * (requires stderr redirected to stdout for proper exception message).
166 * @param string $cmd Command to execute
167 * @param array &$out If provided will contain output in an array
168 * @param int &$res If provided will contain Exit status of the command
169 * @return string Last line of output when executing the command
170 * @throws OMVModuleZFSException
173 public static function exec($cmd, &$out = null, &$res = null) {
174 $tmp = OMVUtil
::exec($cmd, $out, $res);
176 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.126694 seconds and 6 git commands to generate.