]>
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("Vdev.php");
6 require_once("Zpool.php");
9 * Helper class for ZFS module
11 class OMVModuleZFSUtil
{
14 * Clears all ZFS labels on specified devices.
15 * Needed for blkid to display proper data.
18 public static function clearZFSLabel($disks) {
19 foreach ($disks as $disk) {
20 $cmd = "zpool labelclear /dev/" . $disk . "1";
21 OMVModuleZFSUtil
::exec($cmd,$out,$res);
26 * Return all disks in /dev/sdXX used by the pool
28 * @return array An array with all the disks
30 public static function getDevDisksByPool($name) {
31 $pool = new OMVModuleZFSZpool($name);
33 $vdevs = $pool->getVdevs();
34 foreach ($vdevs as $vdev) {
35 $vdisks = $vdev->getDisks();
36 foreach ($vdisks as $vdisk) {
37 if (preg_match('/^[a-z0-9]+$/', $vdisk)) {
41 $cmd = "ls -la /dev/disk/by-path/" . $vdisk;
43 OMVModuleZFSUtil
::exec($cmd,$out,$res);
44 if (count($out) === 1) {
45 if (preg_match('/^.*\/([a-z0-9]+)$/', $out[0], $match)) {
55 * Deletes all shared folders pointing to the specifc path
58 public static function deleteShares($name) {
60 $tmp = new OMVModuleZFSDataset($name);
61 $reldirpath = OMVModuleZFSUtil
::getReldirpath($tmp->getMountPoint());
62 $poolname = OMVModuleZFSUtil
::getPoolname($name);
63 $pooluuid = OMVModuleZFSUtil
::getUUIDbyName($poolname);
64 $xpath = "//system/fstab/mntent[fsname='" . $pooluuid . "']";
65 $mountpoint = $xmlConfig->get($xpath);
66 $mntentuuid = $mountpoint['uuid'];
67 $xpath = "//system/shares/sharedfolder[mntentref='" . $mntentuuid . "' and reldirpath='" . $reldirpath . "']";
68 $object = $xmlConfig->get($xpath);
69 $xmlConfig->delete($xpath);
70 $dispatcher = &OMVNotifyDispatcher
::getInstance();
71 $dispatcher->notify(OMV_NOTIFY_DELETE
,"org.openmediavault.system.shares.sharedfolder",$object);
75 * Get the relative path by complete path
77 * @return string Relative path of the complet path
79 public static function getReldirpath($path) {
80 $subdirs = preg_split('/\//',$path);
82 for ($i=2;$i<count($subdirs);$i++
) {
83 $reldirpath .= $subdirs[$i] . "/";
85 return(rtrim($reldirpath, "/"));
90 * Get /dev/disk/by-path from /dev/sdX
92 * @return string Disk identifier
94 public static function getDiskPath($disk) {
95 preg_match("/^.*\/([A-Za-z0-9]+)$/", $disk, $identifier);
96 $cmd = "ls -la /dev/disk/by-path | grep '$identifier[1]$'";
97 OMVModuleZFSUtil
::exec($cmd, $out, $res);
99 $cols = preg_split('/[\s]+/', $out[0]);
100 return($cols[count($cols)-3]);
106 * Get poolname from name of dataset/volume etc.
108 * @return string Name of the pool
110 public static function getPoolname($name) {
111 $tmp = preg_split('/[\/]+/', $name);
116 * Get UUID of ZFS pool by name
118 * @return string UUID of the pool
120 public static function getUUIDbyName($poolname) {
121 $cmd = "zpool get guid " . $poolname . " 2>&1";
122 OMVModuleZFSUtil
::exec($cmd, $out, $res);
124 $headers = preg_split('/[\s]+/', $out[0]);
125 for ($i=0; $i<count($headers); $i++
) {
126 if (strcmp($headers[$i], "VALUE") === 0) {
131 $line = preg_split('/[\s]+/', $out[1]);
132 return $line[$valuecol];
138 * Add any missing ZFS pool to the OMV backend
141 public static function addMissingOMVMntEnt() {
143 $cmd = "zpool list -H -o name";
144 OMVModuleZFSUtil
::exec($cmd, $out, $res);
145 foreach($out as $name) {
146 $pooluuid = OMVModuleZFSUtil
::getUUIDbyName($name);
147 $xpath = "//system/fstab/mntent[fsname='" . $pooluuid . "']";
148 $mountpoint = $xmlConfig->get($xpath);
149 if (is_null($mountpoint)) {
150 $uuid = OMVUtil
::uuid();
151 $pool = new OMVModuleZFSZpool($name);
152 $dir = $pool->getMountPoint();
155 "fsname" => $pooluuid,
158 "opts" => "rw,relatime,xattr,noacl",
162 $xmlConfig->set("//system/fstab",array("mntent" => $object));
163 $dispatcher = &OMVNotifyDispatcher
::getInstance();
164 $dispatcher->notify(OMV_NOTIFY_CREATE
,"org.openmediavault.system.fstab.mntent", $object);
171 * Get an array with all ZFS objects
173 * @return An array with all ZFS objects
175 public static function getZFSFlatArray() {
176 $prefix = "root/pool-";
178 $cmd = "zfs list -H -t all -o name,type 2>&1";
180 OMVModuleZFSUtil
::exec($cmd,$out,$res);
181 foreach ($out as $line) {
182 $parts = preg_split('/\t/',$line);
185 $subdirs = preg_split('/\//',$path);
191 if (strpos($path,'/') === false) {
193 $tmp = array('id'=>$prefix . $path,
197 'icon'=>'images/raid.png',
198 'expanded'=>$expanded,
200 array_push($objects,$tmp);
202 //This is a Filesystem
203 preg_match('/(.*)\/(.*)$/', $path, $result);
204 $tmp = array('id'=>$prefix . $path,
205 'parentid'=>$prefix . $result[1],
207 'icon'=>"images/filesystem.png",
209 'expanded'=>$expanded);
210 $ds = new OMVModuleZFSDataset($path);
211 if ($ds->isClone()) {
212 //This is a cloned Filesystem
213 $tmp['type'] = "Clone";
214 $tmp['origin'] = $ds->getOrigin();
216 //This is a standard Filesystem.
217 $tmp['type']= ucfirst($type);
219 array_push($objects,$tmp);
224 preg_match('/(.*)\/(.*)$/', $path, $result);
225 $tmp = array('id'=>$prefix . $path,
226 'parentid'=>$prefix . $result[1],
228 'type'=>ucfirst($type),
229 'icon'=>"images/save.png",
231 'expanded'=>$expanded);
232 array_push($objects,$tmp);
236 preg_match('/(.*)\@(.*)$/', $path, $result);
237 $subdirs = preg_split('/\//',$result[1]);
239 $tmp = array('id'=>$prefix . $path,
240 'parentid'=>$prefix . $result[1],
242 'type'=>ucfirst($type),
243 'icon'=>'images/zfs_snap.png',
245 'expanded'=>$expanded);
246 array_push($objects,$tmp);
257 * Create a tree structured array
259 * @param &$list The flat array to convert to a tree structure
260 * @param $parent Root node of the tree to create
261 * @return Tree structured array
264 public static function createTree(&$list, $parent){
266 foreach ($parent as $k=>$l){
267 if(isset($list[$l['id']])){
269 $l['children'] = OMVModuleZFSUtil
::createTree($list, $list[$l['id']]);
279 * Get all Datasets as objects
281 * @return An array with all the Datasets
283 public static function getAllDatasets() {
285 $cmd = "zfs list -H -t filesystem -o name 2>&1";
286 OMVModuleZFSUtil
::exec($cmd, $out, $res);
287 foreach ($out as $name) {
288 $ds = new OMVModuleZFSDataset($name);
289 array_push($datasets, $ds);
295 * Helper function to execute a command and throw an exception on error
296 * (requires stderr redirected to stdout for proper exception message).
298 * @param string $cmd Command to execute
299 * @param array &$out If provided will contain output in an array
300 * @param int &$res If provided will contain Exit status of the command
301 * @return string Last line of output when executing the command
302 * @throws OMVModuleZFSException
305 public static function exec($cmd, &$out = null, &$res = null) {
306 $tmp = OMVUtil
::exec($cmd, $out, $res);
308 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.416571 seconds and 6 git commands to generate.