]>
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 /dev/disk/by-path from /dev/sdX
14 * @return string Disk identifier
16 public static function getDiskPath($disk) {
17 preg_match("/^.*\/([A-Za-z0-9]+)$/", $disk, $identifier);
18 $cmd = "ls -la /dev/disk/by-path | grep '$identifier[1]$'";
19 OMVModuleZFSUtil
::exec($cmd, $out, $res);
21 $cols = preg_split('/[\s]+/', $out[0]);
22 return($cols[count($cols)-3]);
28 * Get poolname from name of dataset/volume etc.
30 * @return string Name of the pool
32 public static function getPoolname($name) {
33 $tmp = preg_split('/[\/]+/', $name);
38 * Get UUID of ZFS pool by name
40 * @return string UUID of the pool
42 public static function getUUIDbyName($poolname) {
43 $cmd = "zpool get guid " . $poolname . " 2>&1";
44 OMVModuleZFSUtil
::exec($cmd, $out, $res);
46 $headers = preg_split('/[\s]+/', $out[0]);
47 for ($i=0; $i<count($headers); $i++
) {
48 if (strcmp($headers[$i], "VALUE") === 0) {
53 $line = preg_split('/[\s]+/', $out[1]);
54 return $line[$valuecol];
60 * Add any missing ZFS pool to the OMV backend
63 public static function addMissingOMVMntEnt() {
65 $cmd = "zpool list -H -o name";
66 OMVModuleZFSUtil
::exec($cmd, $out, $res);
67 foreach($out as $name) {
68 $pooluuid = OMVModuleZFSUtil
::getUUIDbyName($name);
69 $xpath = "//system/fstab/mntent[fsname=" . $pooluuid . "]";
70 $mountpoint = $xmlConfig->get($xpath);
71 if (is_null($mountpoint)) {
72 $uuid = OMVUtil
::uuid();
73 $pool = new OMVModuleZFSZpool($name);
74 $dir = $pool->getMountPoint();
77 "fsname" => $pooluuid,
80 "opts" => "rw,relatime,xattr",
84 $xmlConfig->set("//system/fstab",array("mntent" => $object));
85 $dispatcher = &OMVNotifyDispatcher
::getInstance();
86 $dispatcher->notify(OMV_NOTIFY_CREATE
,"org.openmediavault.system.fstab.mntent", $object);
93 * Get an array with all ZFS objects
95 * @return An array with all ZFS objects
97 public static function getZFSFlatArray() {
98 $prefix = "root/pool-";
100 $cmd = "zfs list -H -t all -o name,type 2>&1";
102 OMVModuleZFSUtil
::exec($cmd,$out,$res);
103 foreach ($out as $line) {
104 $parts = preg_split('/\t/',$line);
107 $subdirs = preg_split('/\//',$path);
113 if (strpos($path,'/') === false) {
115 $tmp = array('id'=>$prefix . $path,
119 'icon'=>'images/raid.png',
120 'expanded'=>$expanded,
122 array_push($objects,$tmp);
124 //This is a Filesystem
125 preg_match('/(.*)\/(.*)$/', $path, $result);
126 $tmp = array('id'=>$prefix . $path,
127 'parentid'=>$prefix . $result[1],
129 'icon'=>"images/filesystem.png",
131 'expanded'=>$expanded);
132 $ds = new OMVModuleZFSDataset($path);
133 if ($ds->isClone()) {
134 //This is a cloned Filesystem
135 $tmp['type'] = "Clone";
136 $tmp['origin'] = $ds->getOrigin();
138 //This is a standard Filesystem.
139 $tmp['type']= ucfirst($type);
141 array_push($objects,$tmp);
146 preg_match('/(.*)\/(.*)$/', $path, $result);
147 $tmp = array('id'=>$prefix . $path,
148 'parentid'=>$prefix . $result[1],
150 'type'=>ucfirst($type),
151 'icon'=>"images/save.png",
153 'expanded'=>$expanded);
154 array_push($objects,$tmp);
158 preg_match('/(.*)\@(.*)$/', $path, $result);
159 $subdirs = preg_split('/\//',$result[1]);
161 $tmp = array('id'=>$prefix . $path,
162 'parentid'=>$prefix . $result[1],
164 'type'=>ucfirst($type),
165 'icon'=>'images/zfs_snap.png',
167 'expanded'=>$expanded);
168 array_push($objects,$tmp);
179 * Create a tree structured array
181 * @param &$list The flat array to convert to a tree structure
182 * @param $parent Root node of the tree to create
183 * @return Tree structured array
186 public static function createTree(&$list, $parent){
188 foreach ($parent as $k=>$l){
189 if(isset($list[$l['id']])){
191 $l['children'] = OMVModuleZFSUtil
::createTree($list, $list[$l['id']]);
201 * Get all Datasets as objects
203 * @return An array with all the Datasets
205 public static function getAllDatasets() {
207 $cmd = "zfs list -H -t filesystem -o name 2>&1";
208 OMVModuleZFSUtil
::exec($cmd, $out, $res);
209 foreach ($out as $name) {
210 $ds = new OMVModuleZFSDataset($name);
211 array_push($datasets, $ds);
217 * Helper function to execute a command and throw an exception on error
218 * (requires stderr redirected to stdout for proper exception message).
220 * @param string $cmd Command to execute
221 * @param array &$out If provided will contain output in an array
222 * @param int &$res If provided will contain Exit status of the command
223 * @return string Last line of output when executing the command
224 * @throws OMVModuleZFSException
227 public static function exec($cmd, &$out = null, &$res = null) {
228 $tmp = OMVUtil
::exec($cmd, $out, $res);
230 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.191747 seconds and 6 git commands to generate.