]>
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 = "zpool get guid " . $name . " 2>&1";
21 OMVModuleZFSUtil
::exec($cmd, $out, $res);
23 $headers = preg_split('/[\s]+/', $out[0]);
24 for ($i=0; $i<count($headers); $i++
) {
25 if (strcmp($headers[$i], "VALUE") === 0) {
30 $line = preg_split('/[\s]+/', $out[1]);
31 return $line[$valuecol];
37 * Add any missing ZFS pool to the OMV backend
40 public static function addMissingOMVMntEnt() {
43 $cmd = "zpool list -H -o name";
44 OMVModuleZFSUtil
::exec($cmd, $out, $res);
45 foreach($out as $name) {
46 $pooluuid = OMVModuleZFSUtil
::getUUIDbyName($name);
47 if (isset($pooluuid)) {
48 $xpath = "//system/fstab/mntent[fsname=" . $pooluuid . "]";
49 $object = $xmlConfig->get($xpath);
50 if(is_null($object)) {
51 $uuid = OMVUtil
::uuid();
52 $ds = new OMVModuleZFSDataset($name);
53 $dir = $ds->getMountPoint();
56 "fsname" => $pooluuid,
59 "opts" => "rw,relatime,xattr",
63 $xmlConfig->set("//system/fstab",array("mntent" => $object));
64 $dispatcher = &OMVNotifyDispatcher
::getInstance();
65 $dispatcher->notify(OMV_NOTIFY_CREATE
,"org.openmediavault.system.fstab.mntent", $object);
73 * Get an array with all ZFS objects
75 * @return An array with all ZFS objects
77 public static function getZFSFlatArray() {
78 $prefix = "root/pool-";
80 $cmd = "zfs list -H -t all -o name,type 2>&1";
82 OMVModuleZFSUtil
::exec($cmd,$out,$res);
83 foreach ($out as $line) {
84 $parts = preg_split('/\t/',$line);
87 $subdirs = preg_split('/\//',$path);
93 if (strpos($path,'/') === false) {
94 //This is a Pool, thus create both the Pool entry and a Filesystem entry corresponding to the Pool.
95 $tmp = array('id'=>$prefix . $path,
99 'icon'=>'images/raid.png',
100 'expanded'=>$expanded,
102 array_push($objects,$tmp);
103 $tmp = array('id'=>$prefix . $path . '/' . $path,
104 'parentid'=>$prefix . $path,
106 'type'=>'Filesystem',
107 'icon'=>'images/filesystem.png',
109 'expanded'=>$expanded);
110 array_push($objects,$tmp);
112 //This is a Filesystem other than the Pool
113 preg_match('/(.*)\/(.*)$/', $path, $result);
114 $tmp = array('id'=>$prefix . $root . "/" . $path,
115 'parentid'=>$prefix . $root . "/" . $result[1],
117 'icon'=>"images/filesystem.png",
119 'expanded'=>$expanded);
120 $ds = new OMVModuleZFSDataset($path);
121 if ($ds->isClone()) {
122 //This is a cloned Filesystem
123 $tmp['type'] = "Clone";
124 $tmp['origin'] = $ds->getOrigin();
126 //This is a standard Filesystem.
127 $tmp['type']= ucfirst($type);
129 array_push($objects,$tmp);
134 preg_match('/(.*)\/(.*)$/', $path, $result);
135 $tmp = array('id'=>$prefix . $root . "/" . $path,
136 'parentid'=>$prefix . $root . "/" . $result[1],
138 'type'=>ucfirst($type),
139 'icon'=>"images/zfs_disk.png",
141 'expanded'=>$expanded);
142 array_push($objects,$tmp);
146 preg_match('/(.*)\@(.*)$/', $path, $result);
147 $subdirs = preg_split('/\//',$result[1]);
149 $tmp = array('id'=>$prefix . $root . "/" . $path,
150 'parentid'=>$prefix . $root . "/" . $result[1],
152 'type'=>ucfirst($type),
153 'icon'=>'images/zfs_snap.png',
155 'expanded'=>$expanded);
156 array_push($objects,$tmp);
167 * Create a tree structured array
169 * @param &$list The flat array to convert to a tree structure
170 * @param $parent Root node of the tree to create
171 * @return Tree structured array
174 public static function createTree(&$list, $parent){
176 foreach ($parent as $k=>$l){
177 if(isset($list[$l['id']])){
179 $l['children'] = OMVModuleZFSUtil
::createTree($list, $list[$l['id']]);
189 * Get all Datasets as objects
191 * @return An array with all the Datasets
193 public static function getAllDatasets() {
195 $cmd = "zfs list -H -t filesystem -o name 2>&1";
196 OMVModuleZFSUtil
::exec($cmd, $out, $res);
197 foreach ($out as $name) {
198 $ds = new OMVModuleZFSDataset($name);
199 array_push($datasets, $ds);
205 * Helper function to execute a command and throw an exception on error
206 * (requires stderr redirected to stdout for proper exception message).
208 * @param string $cmd Command to execute
209 * @param array &$out If provided will contain output in an array
210 * @param int &$res If provided will contain Exit status of the command
211 * @return string Last line of output when executing the command
212 * @throws OMVModuleZFSException
215 public static function exec($cmd, &$out = null, &$res = null) {
216 $tmp = OMVUtil
::exec($cmd, $out, $res);
218 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.669505 seconds and 6 git commands to generate.