]>
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 * Sets a GPT label on a disk to prevent the zpool command from generating
19 public static function setGPTLabel($disk) {
20 $cmd = "parted -s " . $disk . " mklabel gpt 2>&1";
21 OMVModuleZFSUtil
::exec($cmd,$out,$res);
25 * Manages relocation of ZFS filesystem mountpoints in the OMV backend.
26 * Needed when the user changes mountpoint of a filesystem in the GUI.
29 public static function relocateFilesystem($name) {
31 $poolname = OMVModuleZFSUtil
::getPoolname($name);
32 $pooluuid = OMVModuleZFSUtil
::getUUIDbyName($poolname);
33 $ds = new OMVModuleZFSDataset($name);
34 $dir = $ds->getMountPoint();
35 $xpath = "//system/fstab/mntent[fsname='" . $pooluuid . "' and dir='" . $dir . "' and type='zfs']";
36 $object = $xmlConfig->get($xpath);
37 $object['dir'] = $property['value'];
38 $xmlConfig->replace($xpath, $object);
43 * Clears all ZFS labels on specified devices.
44 * Needed for blkid to display proper data.
47 public static function clearZFSLabel($disks) {
48 foreach ($disks as $disk) {
49 $cmd = "zpool labelclear /dev/" . $disk . "1";
50 OMVModuleZFSUtil
::exec($cmd,$out,$res);
56 * Return all disks in /dev/sdXX used by the pool
58 * @return array An array with all the disks
60 public static function getDevDisksByPool($name) {
61 $pool = new OMVModuleZFSZpool($name);
63 $vdevs = $pool->getVdevs();
64 foreach ($vdevs as $vdev) {
65 $vdisks = $vdev->getDisks();
66 foreach ($vdisks as $vdisk) {
67 if (preg_match('/^[a-z0-9]+$/', $vdisk)) {
71 $cmd = "ls -la /dev/disk/by-path/" . $vdisk;
73 OMVModuleZFSUtil
::exec($cmd,$out,$res);
74 if (count($out) === 1) {
75 if (preg_match('/^.*\/([a-z0-9]+)$/', $out[0], $match)) {
85 * Deletes all shared folders pointing to the specifc path
88 public static function deleteShares($name) {
90 $poolname = OMVModuleZFSUtil
::getPoolname($name);
91 $pooluuid = OMVModuleZFSUtil
::getUUIDbyName($poolname);
92 $ds = new OMVModuleZFSDataset($name);
93 $dir = $ds->getMountPoint();
94 $xpath = "//system/fstab/mntent[fsname='" . $pooluuid . "' and dir='" . $dir . "' and type='zfs']";
95 $mountpoint = $xmlConfig->get($xpath);
96 $mntentuuid = $mountpoint['uuid'];
97 $xpath = "//system/shares/sharedfolder[mntentref='" . $mntentuuid . "']";
98 $objects = $xmlConfig->getList($xpath);
99 foreach ($objects as $object) {
100 $tmpxpath = sprintf("//*[contains(name(),'sharedfolderref')]".
101 "[contains(.,'%s')]", $object['uuid']);
102 if ($xmlConfig->exists($tmpxpath)) {
103 throw new OMVModuleZFSException("The Filesystem is shared and in use. Please delete all references and try again.");
106 $xmlConfig->delete($xpath);
107 $dispatcher = &OMVNotifyDispatcher
::getInstance();
108 $dispatcher->notify(OMV_NOTIFY_DELETE
,"org.openmediavault.system.shares.sharedfolder",$object);
112 * Get the relative path by complete path
114 * @return string Relative path of the complet path
116 public static function getReldirpath($path) {
117 $subdirs = preg_split('/\//',$path);
119 for ($i=2;$i<count($subdirs);$i++
) {
120 $reldirpath .= $subdirs[$i] . "/";
122 return(rtrim($reldirpath, "/"));
127 * Get /dev/disk/by-path from /dev/sdX
129 * @return string Disk identifier
131 public static function getDiskPath($disk) {
132 preg_match("/^.*\/([A-Za-z0-9]+)$/", $disk, $identifier);
133 $cmd = "ls -la /dev/disk/by-path | grep '$identifier[1]$'";
134 OMVModuleZFSUtil
::exec($cmd, $out, $res);
135 if (is_array($out)) {
136 $cols = preg_split('/[\s]+/', $out[0]);
137 return($cols[count($cols)-3]);
143 * Get poolname from name of dataset/volume etc.
145 * @return string Name of the pool
147 public static function getPoolname($name) {
148 $tmp = preg_split('/[\/]+/', $name);
153 * Get UUID of ZFS pool by name
155 * @return string UUID of the pool
157 public static function getUUIDbyName($poolname) {
158 $cmd = "zpool get guid " . $poolname . " 2>&1";
159 OMVModuleZFSUtil
::exec($cmd, $out, $res);
161 $headers = preg_split('/[\s]+/', $out[0]);
162 for ($i=0; $i<count($headers); $i++
) {
163 if (strcmp($headers[$i], "VALUE") === 0) {
168 $line = preg_split('/[\s]+/', $out[1]);
169 return $line[$valuecol];
175 * Add any missing ZFS filesystems to the OMV backend
178 public static function addMissingOMVMntEnt() {
180 $cmd = "zfs list -H -o name -t filesystem";
181 OMVModuleZFSUtil
::exec($cmd, $out, $res);
182 foreach($out as $name) {
183 if (preg_match('/[\/]+/', $name)) {
184 $poolname = OMVModuleZFSUtil
::getPoolname($name);
185 $pooluuid = OMVModuleZFSUtil
::getUUIDbyName($poolname);
186 $ds = new OMVModuleZFSDataset($name);
187 $dir = $ds->getMountPoint();
188 $xpath = "//system/fstab/mntent[fsname='" . $pooluuid . "' and dir='" . $dir . "' and type='zfs']";
189 if (!($xmlConfig->exists($xpath))) {
190 $uuid = OMVUtil
::uuid();
193 "fsname" => $pooluuid,
196 "opts" => "rw,relatime,xattr,noacl",
201 $xmlConfig->set("//system/fstab",array("mntent" => $object));
209 * Get an array with all ZFS objects
211 * @return An array with all ZFS objects
213 public static function getZFSFlatArray() {
214 $prefix = "root/pool-";
216 $cmd = "zfs list -H -t all -o name,type 2>&1";
218 OMVModuleZFSUtil
::exec($cmd,$out,$res);
219 foreach ($out as $line) {
220 $parts = preg_split('/\t/',$line);
223 $subdirs = preg_split('/\//',$path);
229 if (strpos($path,'/') === false) {
231 $tmp = array('id'=>$prefix . $path,
235 'icon'=>'images/raid.png',
236 'expanded'=>$expanded,
238 $pool = new OMVModuleZFSZpool($path);
239 $tmp['origin'] = "n/a";
240 $tmp['size'] = $pool->getSize();
241 $tmp['used'] = $pool->getAttribute("allocated");
242 $tmp['available'] = $pool->getAttribute("free");
243 $tmp['mountpoint'] = $pool->getMountPoint();
244 array_push($objects,$tmp);
246 //This is a Filesystem
247 preg_match('/(.*)\/(.*)$/', $path, $result);
248 $tmp = array('id'=>$prefix . $path,
249 'parentid'=>$prefix . $result[1],
251 'icon'=>"images/filesystem.png",
253 'expanded'=>$expanded);
254 $ds = new OMVModuleZFSDataset($path);
255 if ($ds->isClone()) {
256 //This is a cloned Filesystem
257 $tmp['origin'] = $ds->getOrigin();
259 //This is a standard Filesystem.
260 $tmp['origin'] = "n/a";
262 $tmp['type']= ucfirst($type);
263 $tmp['size'] = "n/a";
264 $used = $ds->getProperty("used");
265 $tmp['used'] = $used['value'];
266 $available = $ds->getProperty("available");
267 $tmp['available'] = $available['value'];
268 $tmp['mountpoint'] = $ds->getMountPoint();
269 array_push($objects,$tmp);
274 preg_match('/(.*)\/(.*)$/', $path, $result);
275 $tmp = array('id'=>$prefix . $path,
276 'parentid'=>$prefix . $result[1],
278 'type'=>ucfirst($type),
279 'icon'=>"images/save.png",
281 'expanded'=>$expanded);
282 $vol = new OMVModuleZFSZvol($path);
283 if ($vol->isClone()) {
284 //This is a cloned Volume
285 $tmp['origin'] = $vol->getOrigin();
287 //This is a standard Volume
288 $tmp['origin'] = "n/a";
290 $tmp['type']= ucfirst($type);
291 $tmp['size'] = $vol->getSize();
292 $tmp['used'] = "n/a";
293 $tmp['available'] = "n/a";
294 $tmp['mountpoint'] = "n/a";
295 array_push($objects,$tmp);
299 preg_match('/(.*)\@(.*)$/', $path, $result);
300 $subdirs = preg_split('/\//',$result[1]);
302 $tmp = array('id'=>$prefix . $path,
303 'parentid'=>$prefix . $result[1],
305 'type'=>ucfirst($type),
306 'icon'=>'images/zfs_snap.png',
308 'expanded'=>$expanded);
309 $tmp['origin'] = "n/a";
310 $tmp['size'] = "n/a";
311 $tmp['used'] = "n/a";
312 $tmp['available'] = "n/a";
313 $tmp['mountpoint'] = "n/a";
314 array_push($objects,$tmp);
325 * Create a tree structured array
327 * @param &$list The flat array to convert to a tree structure
328 * @param $parent Root node of the tree to create
329 * @return Tree structured array
332 public static function createTree(&$list, $parent){
334 foreach ($parent as $k=>$l){
335 if(isset($list[$l['id']])){
337 $l['children'] = OMVModuleZFSUtil
::createTree($list, $list[$l['id']]);
347 * Get all Datasets as objects
349 * @return An array with all the Datasets
351 public static function getAllDatasets() {
353 $cmd = "zfs list -H -t filesystem -o name 2>&1";
354 OMVModuleZFSUtil
::exec($cmd, $out, $res);
355 foreach ($out as $name) {
356 $ds = new OMVModuleZFSDataset($name);
357 array_push($datasets, $ds);
363 * Helper function to execute a command and throw an exception on error
364 * (requires stderr redirected to stdout for proper exception message).
366 * @param string $cmd Command to execute
367 * @param array &$out If provided will contain output in an array
368 * @param int &$res If provided will contain Exit status of the command
369 * @return string Last line of output when executing the command
370 * @throws OMVModuleZFSException
373 public static function exec($cmd, &$out = null, &$res = null) {
374 $tmp = OMVUtil
::exec($cmd, $out, $res);
376 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.297411 seconds and 6 git commands to generate.