]>
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 * Add any missing ZFS pool to the OMV backend
34 public static function addMissingOMVMntEnt() {
37 $cmd = "zpool list -H -o name";
38 OMVModuleZFSUtil
::exec($cmd, $out, $res);
39 foreach($out as $name) {
40 $pooluuid = OMVModuleZFSUtil
::getUUIDbyName($name);
41 $xpath = "//system/fstab/mntent[fsname=" . $pooluuid . "]";
42 $object = $xmlConfig->get($xpath);
43 if(is_null($object)) {
44 $uuid = OMVUtil
::uuid();
45 $ds = new OMVModuleZFSDataset($name);
46 $dir = $ds->getMountPoint();
49 "fsname" => $pooluuid,
52 "opts" => "rw,relatime,xattr",
56 $xmlConfig->set("//system/fstab",array("mntent" => $object));
57 $dispatcher = &OMVNotifyDispatcher
::getInstance();
58 $dispatcher->notify(OMV_NOTIFY_CREATE
,"org.openmediavault.system.fstab.mntent", $object);
65 * Get an array with all ZFS objects
67 * @return An array with all ZFS objects
69 public static function getZFSFlatArray() {
70 $prefix = "root/pool-";
72 $cmd = "zfs list -H -t all -o name,type 2>&1";
74 OMVModuleZFSUtil
::exec($cmd,$out,$res);
75 foreach ($out as $line) {
76 $parts = preg_split('/\t/',$line);
79 $subdirs = preg_split('/\//',$path);
85 if (strpos($path,'/') === false) {
86 //This is a Pool, thus create both the Pool entry and a Filesystem entry corresponding to the Pool.
87 $tmp = array('id'=>$prefix . $path,
91 'icon'=>'images/raid.png',
92 'expanded'=>$expanded,
94 array_push($objects,$tmp);
95 $tmp = array('id'=>$prefix . $path . '/' . $path,
96 'parentid'=>$prefix . $path,
99 'icon'=>'images/filesystem.png',
101 'expanded'=>$expanded);
102 array_push($objects,$tmp);
104 //This is a Filesystem other than the Pool
105 preg_match('/(.*)\/(.*)$/', $path, $result);
106 $tmp = array('id'=>$prefix . $root . "/" . $path,
107 'parentid'=>$prefix . $root . "/" . $result[1],
109 'icon'=>"images/filesystem.png",
111 'expanded'=>$expanded);
112 $ds = new OMVModuleZFSDataset($path);
113 if ($ds->isClone()) {
114 //This is a cloned Filesystem
115 $tmp['type'] = "Clone";
116 $tmp['origin'] = $ds->getOrigin();
118 //This is a standard Filesystem.
119 $tmp['type']= ucfirst($type);
121 array_push($objects,$tmp);
126 preg_match('/(.*)\/(.*)$/', $path, $result);
127 $tmp = array('id'=>$prefix . $root . "/" . $path,
128 'parentid'=>$prefix . $root . "/" . $result[1],
130 'type'=>ucfirst($type),
131 'icon'=>"images/zfs_disk.png",
133 'expanded'=>$expanded);
134 array_push($objects,$tmp);
138 preg_match('/(.*)\@(.*)$/', $path, $result);
139 $subdirs = preg_split('/\//',$result[1]);
141 $tmp = array('id'=>$prefix . $root . "/" . $path,
142 'parentid'=>$prefix . $root . "/" . $result[1],
144 'type'=>ucfirst($type),
145 'icon'=>'images/zfs_snap.png',
147 'expanded'=>$expanded);
148 array_push($objects,$tmp);
159 * Create a tree structured array
161 * @param &$list The flat array to convert to a tree structure
162 * @param $parent Root node of the tree to create
163 * @return Tree structured array
166 public static function createTree(&$list, $parent){
168 foreach ($parent as $k=>$l){
169 if(isset($list[$l['id']])){
171 $l['children'] = OMVModuleZFSUtil
::createTree($list, $list[$l['id']]);
181 * Get all Datasets as objects
183 * @return An array with all the Datasets
185 public static function getAllDatasets() {
187 $cmd = "zfs list -H -t filesystem -o name 2>&1";
188 OMVModuleZFSUtil
::exec($cmd, $out, $res);
189 foreach ($out as $name) {
190 $ds = new OMVModuleZFSDataset($name);
191 array_push($datasets, $ds);
197 * Helper function to execute a command and throw an exception on error
198 * (requires stderr redirected to stdout for proper exception message).
200 * @param string $cmd Command to execute
201 * @param array &$out If provided will contain output in an array
202 * @param int &$res If provided will contain Exit status of the command
203 * @return string Last line of output when executing the command
204 * @throws OMVModuleZFSException
207 public static function exec($cmd, &$out = null, &$res = null) {
208 $tmp = OMVUtil
::exec($cmd, $out, $res);
210 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.117993 seconds and 6 git commands to generate.