]>
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 poolname from name of dataset/volume etc.
14 * @return string Name of the pool
16 public static function getPoolname($name) {
17 $tmp = preg_split('/[\/]+/', $name);
22 * Get UUID of ZFS pool by name
24 * @return string UUID of the pool
26 public static function getUUIDbyName($poolname) {
27 $cmd = "zpool get guid " . $poolname . " 2>&1";
28 OMVModuleZFSUtil
::exec($cmd, $out, $res);
30 $headers = preg_split('/[\s]+/', $out[0]);
31 for ($i=0; $i<count($headers); $i++
) {
32 if (strcmp($headers[$i], "VALUE") === 0) {
37 $line = preg_split('/[\s]+/', $out[1]);
38 return $line[$valuecol];
44 * Add any missing ZFS pool to the OMV backend
47 public static function addMissingOMVMntEnt() {
49 $cmd = "zpool list -H -o name";
50 OMVModuleZFSUtil
::exec($cmd, $out, $res);
51 foreach($out as $name) {
52 $pooluuid = OMVModuleZFSUtil
::getUUIDbyName($name);
53 $xpath = "//system/fstab/mntent[fsname=" . $pooluuid . "]";
54 $mountpoint = $xmlConfig->get($xpath);
55 if (is_null($mountpoint)) {
56 $uuid = OMVUtil
::uuid();
57 $pool = new OMVModuleZFSZpool($name);
58 $dir = $pool->getMountPoint();
61 "fsname" => $pooluuid,
64 "opts" => "rw,relatime,xattr",
68 $xmlConfig->set("//system/fstab",array("mntent" => $object));
69 $dispatcher = &OMVNotifyDispatcher
::getInstance();
70 $dispatcher->notify(OMV_NOTIFY_CREATE
,"org.openmediavault.system.fstab.mntent", $object);
77 * Get an array with all ZFS objects
79 * @return An array with all ZFS objects
81 public static function getZFSFlatArray() {
82 $prefix = "root/pool-";
84 $cmd = "zfs list -H -t all -o name,type 2>&1";
86 OMVModuleZFSUtil
::exec($cmd,$out,$res);
87 foreach ($out as $line) {
88 $parts = preg_split('/\t/',$line);
91 $subdirs = preg_split('/\//',$path);
97 if (strpos($path,'/') === false) {
98 //This is a Pool, thus create both the Pool entry and a Filesystem entry corresponding to the Pool.
99 $tmp = array('id'=>$prefix . $path,
103 'icon'=>'images/raid.png',
104 'expanded'=>$expanded,
106 array_push($objects,$tmp);
107 $tmp = array('id'=>$prefix . $path . '/' . $path,
108 'parentid'=>$prefix . $path,
110 'type'=>'Filesystem',
111 'icon'=>'images/filesystem.png',
113 'expanded'=>$expanded);
114 array_push($objects,$tmp);
116 //This is a Filesystem other than the Pool
117 preg_match('/(.*)\/(.*)$/', $path, $result);
118 $tmp = array('id'=>$prefix . $root . "/" . $path,
119 'parentid'=>$prefix . $root . "/" . $result[1],
121 'icon'=>"images/filesystem.png",
123 'expanded'=>$expanded);
124 $ds = new OMVModuleZFSDataset($path);
125 if ($ds->isClone()) {
126 //This is a cloned Filesystem
127 $tmp['type'] = "Clone";
128 $tmp['origin'] = $ds->getOrigin();
130 //This is a standard Filesystem.
131 $tmp['type']= ucfirst($type);
133 array_push($objects,$tmp);
138 preg_match('/(.*)\/(.*)$/', $path, $result);
139 $tmp = array('id'=>$prefix . $root . "/" . $path,
140 'parentid'=>$prefix . $root . "/" . $result[1],
142 'type'=>ucfirst($type),
143 'icon'=>"images/save.png",
145 'expanded'=>$expanded);
146 array_push($objects,$tmp);
150 preg_match('/(.*)\@(.*)$/', $path, $result);
151 $subdirs = preg_split('/\//',$result[1]);
153 $tmp = array('id'=>$prefix . $root . "/" . $path,
154 'parentid'=>$prefix . $root . "/" . $result[1],
156 'type'=>ucfirst($type),
157 'icon'=>'images/zfs_snap.png',
159 'expanded'=>$expanded);
160 array_push($objects,$tmp);
171 * Create a tree structured array
173 * @param &$list The flat array to convert to a tree structure
174 * @param $parent Root node of the tree to create
175 * @return Tree structured array
178 public static function createTree(&$list, $parent){
180 foreach ($parent as $k=>$l){
181 if(isset($list[$l['id']])){
183 $l['children'] = OMVModuleZFSUtil
::createTree($list, $list[$l['id']]);
193 * Get all Datasets as objects
195 * @return An array with all the Datasets
197 public static function getAllDatasets() {
199 $cmd = "zfs list -H -t filesystem -o name 2>&1";
200 OMVModuleZFSUtil
::exec($cmd, $out, $res);
201 foreach ($out as $name) {
202 $ds = new OMVModuleZFSDataset($name);
203 array_push($datasets, $ds);
209 * Helper function to execute a command and throw an exception on error
210 * (requires stderr redirected to stdout for proper exception message).
212 * @param string $cmd Command to execute
213 * @param array &$out If provided will contain output in an array
214 * @param int &$res If provided will contain Exit status of the command
215 * @return string Last line of output when executing the command
216 * @throws OMVModuleZFSException
219 public static function exec($cmd, &$out = null, &$res = null) {
220 $tmp = OMVUtil
::exec($cmd, $out, $res);
222 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.279378 seconds and 6 git commands to generate.