]>
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) {
114 //This is a Pool, thus create both the Pool entry and a Filesystem entry corresponding to the Pool.
115 $tmp = array('id'=>$prefix . $path,
119 'icon'=>'images/raid.png',
120 'expanded'=>$expanded,
122 array_push($objects,$tmp);
123 $tmp = array('id'=>$prefix . $path . '/' . $path,
124 'parentid'=>$prefix . $path,
126 'type'=>'Filesystem',
127 'icon'=>'images/filesystem.png',
129 'expanded'=>$expanded);
130 array_push($objects,$tmp);
132 //This is a Filesystem other than the Pool
133 preg_match('/(.*)\/(.*)$/', $path, $result);
134 $tmp = array('id'=>$prefix . $root . "/" . $path,
135 'parentid'=>$prefix . $root . "/" . $result[1],
137 'icon'=>"images/filesystem.png",
139 'expanded'=>$expanded);
140 $ds = new OMVModuleZFSDataset($path);
141 if ($ds->isClone()) {
142 //This is a cloned Filesystem
143 $tmp['type'] = "Clone";
144 $tmp['origin'] = $ds->getOrigin();
146 //This is a standard Filesystem.
147 $tmp['type']= ucfirst($type);
149 array_push($objects,$tmp);
154 preg_match('/(.*)\/(.*)$/', $path, $result);
155 $tmp = array('id'=>$prefix . $root . "/" . $path,
156 'parentid'=>$prefix . $root . "/" . $result[1],
158 'type'=>ucfirst($type),
159 'icon'=>"images/save.png",
161 'expanded'=>$expanded);
162 array_push($objects,$tmp);
166 preg_match('/(.*)\@(.*)$/', $path, $result);
167 $subdirs = preg_split('/\//',$result[1]);
169 $tmp = array('id'=>$prefix . $root . "/" . $path,
170 'parentid'=>$prefix . $root . "/" . $result[1],
172 'type'=>ucfirst($type),
173 'icon'=>'images/zfs_snap.png',
175 'expanded'=>$expanded);
176 array_push($objects,$tmp);
187 * Create a tree structured array
189 * @param &$list The flat array to convert to a tree structure
190 * @param $parent Root node of the tree to create
191 * @return Tree structured array
194 public static function createTree(&$list, $parent){
196 foreach ($parent as $k=>$l){
197 if(isset($list[$l['id']])){
199 $l['children'] = OMVModuleZFSUtil
::createTree($list, $list[$l['id']]);
209 * Get all Datasets as objects
211 * @return An array with all the Datasets
213 public static function getAllDatasets() {
215 $cmd = "zfs list -H -t filesystem -o name 2>&1";
216 OMVModuleZFSUtil
::exec($cmd, $out, $res);
217 foreach ($out as $name) {
218 $ds = new OMVModuleZFSDataset($name);
219 array_push($datasets, $ds);
225 * Helper function to execute a command and throw an exception on error
226 * (requires stderr redirected to stdout for proper exception message).
228 * @param string $cmd Command to execute
229 * @param array &$out If provided will contain output in an array
230 * @param int &$res If provided will contain Exit status of the command
231 * @return string Last line of output when executing the command
232 * @throws OMVModuleZFSException
235 public static function exec($cmd, &$out = null, &$res = null) {
236 $tmp = OMVUtil
::exec($cmd, $out, $res);
238 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.107036 seconds and 6 git commands to generate.