]>
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 UUID of ZFS pool by name
14 * @return string UUID of the pool
16 public static function getUUIDbyName($name) {
17 preg_match('/^([A-Za-z0-9]+)\/?.*$/', $name, $result);
20 $cmd = "zpool get guid " . $name . " 2>&1";
21 OMVModuleZFSUtil
::exec($cmd, $out, $res);
23 $headers = preg_split('/[\s]+/', $out[0]);
24 for ($i=0; $i<count($headers); $i++
) {
25 if (strcmp($headers[$i], "VALUE") === 0) {
30 $line = preg_split('/[\s]+/', $out[1]);
31 return $line[$valuecol];
37 * Add any missing ZFS pool to the OMV backend
40 public static function addMissingOMVMntEnt() {
43 $cmd = "zpool list -H -o name";
44 OMVModuleZFSUtil
::exec($cmd, $out, $res);
45 foreach($out as $name) {
46 $pooluuid = OMVModuleZFSUtil
::getUUIDbyName($name);
47 if (isset($pooluuid)) {
48 $pooluuid = "UUID=" . $pooluuid;
49 $xpath = "//system/fstab/mntent[fsname=" . $pooluuid . "]";
50 $object = $xmlConfig->get($xpath);
51 if(is_null($object)) {
52 $uuid = OMVUtil
::uuid();
53 $ds = new OMVModuleZFSDataset($name);
54 $dir = $ds->getMountPoint();
57 "fsname" => $pooluuid,
60 "opts" => "rw,relatime,xattr",
64 $xmlConfig->set("//system/fstab",array("mntent" => $object));
65 $dispatcher = &OMVNotifyDispatcher
::getInstance();
66 $dispatcher->notify(OMV_NOTIFY_CREATE
,"org.openmediavault.system.fstab.mntent", $object);
74 * Get an array with all ZFS objects
76 * @return An array with all ZFS objects
78 public static function getZFSFlatArray() {
79 $prefix = "root/pool-";
81 $cmd = "zfs list -H -t all -o name,type 2>&1";
83 OMVModuleZFSUtil
::exec($cmd,$out,$res);
84 foreach ($out as $line) {
85 $parts = preg_split('/\t/',$line);
88 $subdirs = preg_split('/\//',$path);
94 if (strpos($path,'/') === false) {
95 //This is a Pool, thus create both the Pool entry and a Filesystem entry corresponding to the Pool.
96 $tmp = array('id'=>$prefix . $path,
100 'icon'=>'images/raid.png',
101 'expanded'=>$expanded,
103 array_push($objects,$tmp);
104 $tmp = array('id'=>$prefix . $path . '/' . $path,
105 'parentid'=>$prefix . $path,
107 'type'=>'Filesystem',
108 'icon'=>'images/filesystem.png',
110 'expanded'=>$expanded);
111 array_push($objects,$tmp);
113 //This is a Filesystem other than the Pool
114 preg_match('/(.*)\/(.*)$/', $path, $result);
115 $tmp = array('id'=>$prefix . $root . "/" . $path,
116 'parentid'=>$prefix . $root . "/" . $result[1],
118 'icon'=>"images/filesystem.png",
120 'expanded'=>$expanded);
121 $ds = new OMVModuleZFSDataset($path);
122 if ($ds->isClone()) {
123 //This is a cloned Filesystem
124 $tmp['type'] = "Clone";
125 $tmp['origin'] = $ds->getOrigin();
127 //This is a standard Filesystem.
128 $tmp['type']= ucfirst($type);
130 array_push($objects,$tmp);
135 preg_match('/(.*)\/(.*)$/', $path, $result);
136 $tmp = array('id'=>$prefix . $root . "/" . $path,
137 'parentid'=>$prefix . $root . "/" . $result[1],
139 'type'=>ucfirst($type),
140 'icon'=>"images/zfs_disk.png",
142 'expanded'=>$expanded);
143 array_push($objects,$tmp);
147 preg_match('/(.*)\@(.*)$/', $path, $result);
148 $subdirs = preg_split('/\//',$result[1]);
150 $tmp = array('id'=>$prefix . $root . "/" . $path,
151 'parentid'=>$prefix . $root . "/" . $result[1],
153 'type'=>ucfirst($type),
154 'icon'=>'images/zfs_snap.png',
156 'expanded'=>$expanded);
157 array_push($objects,$tmp);
168 * Create a tree structured array
170 * @param &$list The flat array to convert to a tree structure
171 * @param $parent Root node of the tree to create
172 * @return Tree structured array
175 public static function createTree(&$list, $parent){
177 foreach ($parent as $k=>$l){
178 if(isset($list[$l['id']])){
180 $l['children'] = OMVModuleZFSUtil
::createTree($list, $list[$l['id']]);
190 * Get all Datasets as objects
192 * @return An array with all the Datasets
194 public static function getAllDatasets() {
196 $cmd = "zfs list -H -t filesystem -o name 2>&1";
197 OMVModuleZFSUtil
::exec($cmd, $out, $res);
198 foreach ($out as $name) {
199 $ds = new OMVModuleZFSDataset($name);
200 array_push($datasets, $ds);
206 * Helper function to execute a command and throw an exception on error
207 * (requires stderr redirected to stdout for proper exception message).
209 * @param string $cmd Command to execute
210 * @param array &$out If provided will contain output in an array
211 * @param int &$res If provided will contain Exit status of the command
212 * @return string Last line of output when executing the command
213 * @throws OMVModuleZFSException
216 public static function exec($cmd, &$out = null, &$res = null) {
217 $tmp = OMVUtil
::exec($cmd, $out, $res);
219 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.180376 seconds and 6 git commands to generate.