]>
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");
5 require_once("Zvol.php");
6 require_once("Vdev.php");
7 require_once("Zpool.php");
10 * Helper class for ZFS module
12 class OMVModuleZFSUtil
{
15 * Clears all ZFS labels on specified devices.
16 * Needed for blkid to display proper data.
19 public static function clearZFSLabel($disks) {
20 foreach ($disks as $disk) {
21 $cmd = "zpool labelclear /dev/" . $disk . "1";
22 OMVModuleZFSUtil
::exec($cmd,$out,$res);
27 * Return all disks in /dev/sdXX used by the pool
29 * @return array An array with all the disks
31 public static function getDevDisksByPool($name) {
32 $pool = new OMVModuleZFSZpool($name);
34 $vdevs = $pool->getVdevs();
35 foreach ($vdevs as $vdev) {
36 $vdisks = $vdev->getDisks();
37 foreach ($vdisks as $vdisk) {
38 if (preg_match('/^[a-z0-9]+$/', $vdisk)) {
42 $cmd = "ls -la /dev/disk/by-path/" . $vdisk;
44 OMVModuleZFSUtil
::exec($cmd,$out,$res);
45 if (count($out) === 1) {
46 if (preg_match('/^.*\/([a-z0-9]+)$/', $out[0], $match)) {
56 * Deletes all shared folders pointing to the specifc path
59 public static function deleteShares($name) {
61 $tmp = new OMVModuleZFSDataset($name);
62 $reldirpath = OMVModuleZFSUtil
::getReldirpath($tmp->getMountPoint());
63 $poolname = OMVModuleZFSUtil
::getPoolname($name);
64 $pooluuid = OMVModuleZFSUtil
::getUUIDbyName($poolname);
65 $xpath = "//system/fstab/mntent[fsname='" . $pooluuid . "']";
66 $mountpoint = $xmlConfig->get($xpath);
67 $mntentuuid = $mountpoint['uuid'];
68 $xpath = "//system/shares/sharedfolder[mntentref='" . $mntentuuid . "' and reldirpath='" . $reldirpath . "']";
69 $object = $xmlConfig->get($xpath);
70 $xmlConfig->delete($xpath);
71 $dispatcher = &OMVNotifyDispatcher
::getInstance();
72 $dispatcher->notify(OMV_NOTIFY_DELETE
,"org.openmediavault.system.shares.sharedfolder",$object);
76 * Get the relative path by complete path
78 * @return string Relative path of the complet path
80 public static function getReldirpath($path) {
81 $subdirs = preg_split('/\//',$path);
83 for ($i=2;$i<count($subdirs);$i++
) {
84 $reldirpath .= $subdirs[$i] . "/";
86 return(rtrim($reldirpath, "/"));
91 * Get /dev/disk/by-path from /dev/sdX
93 * @return string Disk identifier
95 public static function getDiskPath($disk) {
96 preg_match("/^.*\/([A-Za-z0-9]+)$/", $disk, $identifier);
97 $cmd = "ls -la /dev/disk/by-path | grep '$identifier[1]$'";
98 OMVModuleZFSUtil
::exec($cmd, $out, $res);
100 $cols = preg_split('/[\s]+/', $out[0]);
101 return($cols[count($cols)-3]);
107 * Get poolname from name of dataset/volume etc.
109 * @return string Name of the pool
111 public static function getPoolname($name) {
112 $tmp = preg_split('/[\/]+/', $name);
117 * Get UUID of ZFS pool by name
119 * @return string UUID of the pool
121 public static function getUUIDbyName($poolname) {
122 $cmd = "zpool get guid " . $poolname . " 2>&1";
123 OMVModuleZFSUtil
::exec($cmd, $out, $res);
125 $headers = preg_split('/[\s]+/', $out[0]);
126 for ($i=0; $i<count($headers); $i++
) {
127 if (strcmp($headers[$i], "VALUE") === 0) {
132 $line = preg_split('/[\s]+/', $out[1]);
133 return $line[$valuecol];
139 * Add any missing ZFS pool to the OMV backend
142 public static function addMissingOMVMntEnt() {
144 $cmd = "zpool list -H -o name";
145 OMVModuleZFSUtil
::exec($cmd, $out, $res);
146 foreach($out as $name) {
147 $pooluuid = OMVModuleZFSUtil
::getUUIDbyName($name);
148 $xpath = "//system/fstab/mntent[fsname='" . $pooluuid . "']";
149 $mountpoint = $xmlConfig->get($xpath);
150 if (is_null($mountpoint)) {
151 $uuid = OMVUtil
::uuid();
152 $pool = new OMVModuleZFSZpool($name);
153 $dir = $pool->getMountPoint();
156 "fsname" => $pooluuid,
159 "opts" => "rw,relatime,xattr,noacl",
163 $xmlConfig->set("//system/fstab",array("mntent" => $object));
164 $dispatcher = &OMVNotifyDispatcher
::getInstance();
165 $dispatcher->notify(OMV_NOTIFY_CREATE
,"org.openmediavault.system.fstab.mntent", $object);
172 * Get an array with all ZFS objects
174 * @return An array with all ZFS objects
176 public static function getZFSFlatArray() {
177 $prefix = "root/pool-";
179 $cmd = "zfs list -H -t all -o name,type 2>&1";
181 OMVModuleZFSUtil
::exec($cmd,$out,$res);
182 foreach ($out as $line) {
183 $parts = preg_split('/\t/',$line);
186 $subdirs = preg_split('/\//',$path);
192 if (strpos($path,'/') === false) {
194 $tmp = array('id'=>$prefix . $path,
198 'icon'=>'images/raid.png',
199 'expanded'=>$expanded,
201 $pool = new OMVModuleZFSZpool($path);
202 $tmp['size'] = $pool->getSize();
203 $tmp['used'] = $pool->getAttribute("allocated");
204 $tmp['available'] = $pool->getAttribute("free");
205 $tmp['mountpoint'] = $pool->getMountPoint();
206 array_push($objects,$tmp);
208 //This is a Filesystem
209 preg_match('/(.*)\/(.*)$/', $path, $result);
210 $tmp = array('id'=>$prefix . $path,
211 'parentid'=>$prefix . $result[1],
213 'icon'=>"images/filesystem.png",
215 'expanded'=>$expanded);
216 $ds = new OMVModuleZFSDataset($path);
217 if ($ds->isClone()) {
218 //This is a cloned Filesystem
219 $tmp['type'] = "Clone";
220 $tmp['origin'] = $ds->getOrigin();
222 //This is a standard Filesystem.
223 $tmp['type']= ucfirst($type);
225 $tmp['size'] = "n/a";
226 $used = $ds->getProperty("used");
227 $tmp['used'] = $used['value'];
228 $available = $ds->getProperty("available");
229 $tmp['available'] = $available['value'];
230 $tmp['mountpoint'] = $ds->getMountPoint();
231 array_push($objects,$tmp);
236 preg_match('/(.*)\/(.*)$/', $path, $result);
237 $tmp = array('id'=>$prefix . $path,
238 'parentid'=>$prefix . $result[1],
240 'type'=>ucfirst($type),
241 'icon'=>"images/save.png",
243 'expanded'=>$expanded);
244 $vol = new OMVModuleZFSZvol();
245 $tmp['size'] = $vol->getSize();
246 $tmp['used'] = "n/a";
247 $tmp['available'] = "n/a";
248 $tmp['mountpoint'] = "n/a";
249 array_push($objects,$tmp);
253 preg_match('/(.*)\@(.*)$/', $path, $result);
254 $subdirs = preg_split('/\//',$result[1]);
256 $tmp = array('id'=>$prefix . $path,
257 'parentid'=>$prefix . $result[1],
259 'type'=>ucfirst($type),
260 'icon'=>'images/zfs_snap.png',
262 'expanded'=>$expanded);
263 $tmp['size'] = "n/a";
264 $tmp['used'] = "n/a";
265 $tmp['available'] = "n/a";
266 $tmp['mountpoint'] = "n/a";
267 array_push($objects,$tmp);
278 * Create a tree structured array
280 * @param &$list The flat array to convert to a tree structure
281 * @param $parent Root node of the tree to create
282 * @return Tree structured array
285 public static function createTree(&$list, $parent){
287 foreach ($parent as $k=>$l){
288 if(isset($list[$l['id']])){
290 $l['children'] = OMVModuleZFSUtil
::createTree($list, $list[$l['id']]);
300 * Get all Datasets as objects
302 * @return An array with all the Datasets
304 public static function getAllDatasets() {
306 $cmd = "zfs list -H -t filesystem -o name 2>&1";
307 OMVModuleZFSUtil
::exec($cmd, $out, $res);
308 foreach ($out as $name) {
309 $ds = new OMVModuleZFSDataset($name);
310 array_push($datasets, $ds);
316 * Helper function to execute a command and throw an exception on error
317 * (requires stderr redirected to stdout for proper exception message).
319 * @param string $cmd Command to execute
320 * @param array &$out If provided will contain output in an array
321 * @param int &$res If provided will contain Exit status of the command
322 * @return string Last line of output when executing the command
323 * @throws OMVModuleZFSException
326 public static function exec($cmd, &$out = null, &$res = null) {
327 $tmp = OMVUtil
::exec($cmd, $out, $res);
329 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.221427 seconds and 6 git commands to generate.