]>
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";
50 $object = $xmlConfig->get($xpath);
52 foreach ($object as $obj) {
53 if (strcmp($pooluuid, $obj['fsname']) === 0) {
59 $uuid = OMVUtil
::uuid();
60 $ds = new OMVModuleZFSDataset($name);
61 $dir = $ds->getMountPoint();
64 "fsname" => $pooluuid,
67 "opts" => "rw,relatime,xattr",
71 $xmlConfig->set("//system/fstab",array("mntent" => $object));
72 $dispatcher = &OMVNotifyDispatcher
::getInstance();
73 $dispatcher->notify(OMV_NOTIFY_CREATE
,"org.openmediavault.system.fstab.mntent", $object);
81 * Get an array with all ZFS objects
83 * @return An array with all ZFS objects
85 public static function getZFSFlatArray() {
86 $prefix = "root/pool-";
88 $cmd = "zfs list -H -t all -o name,type 2>&1";
90 OMVModuleZFSUtil
::exec($cmd,$out,$res);
91 foreach ($out as $line) {
92 $parts = preg_split('/\t/',$line);
95 $subdirs = preg_split('/\//',$path);
101 if (strpos($path,'/') === false) {
102 //This is a Pool, thus create both the Pool entry and a Filesystem entry corresponding to the Pool.
103 $tmp = array('id'=>$prefix . $path,
107 'icon'=>'images/raid.png',
108 'expanded'=>$expanded,
110 array_push($objects,$tmp);
111 $tmp = array('id'=>$prefix . $path . '/' . $path,
112 'parentid'=>$prefix . $path,
114 'type'=>'Filesystem',
115 'icon'=>'images/filesystem.png',
117 'expanded'=>$expanded);
118 array_push($objects,$tmp);
120 //This is a Filesystem other than the Pool
121 preg_match('/(.*)\/(.*)$/', $path, $result);
122 $tmp = array('id'=>$prefix . $root . "/" . $path,
123 'parentid'=>$prefix . $root . "/" . $result[1],
125 'icon'=>"images/filesystem.png",
127 'expanded'=>$expanded);
128 $ds = new OMVModuleZFSDataset($path);
129 if ($ds->isClone()) {
130 //This is a cloned Filesystem
131 $tmp['type'] = "Clone";
132 $tmp['origin'] = $ds->getOrigin();
134 //This is a standard Filesystem.
135 $tmp['type']= ucfirst($type);
137 array_push($objects,$tmp);
142 preg_match('/(.*)\/(.*)$/', $path, $result);
143 $tmp = array('id'=>$prefix . $root . "/" . $path,
144 'parentid'=>$prefix . $root . "/" . $result[1],
146 'type'=>ucfirst($type),
147 'icon'=>"images/zfs_disk.png",
149 'expanded'=>$expanded);
150 array_push($objects,$tmp);
154 preg_match('/(.*)\@(.*)$/', $path, $result);
155 $subdirs = preg_split('/\//',$result[1]);
157 $tmp = array('id'=>$prefix . $root . "/" . $path,
158 'parentid'=>$prefix . $root . "/" . $result[1],
160 'type'=>ucfirst($type),
161 'icon'=>'images/zfs_snap.png',
163 'expanded'=>$expanded);
164 array_push($objects,$tmp);
175 * Create a tree structured array
177 * @param &$list The flat array to convert to a tree structure
178 * @param $parent Root node of the tree to create
179 * @return Tree structured array
182 public static function createTree(&$list, $parent){
184 foreach ($parent as $k=>$l){
185 if(isset($list[$l['id']])){
187 $l['children'] = OMVModuleZFSUtil
::createTree($list, $list[$l['id']]);
197 * Get all Datasets as objects
199 * @return An array with all the Datasets
201 public static function getAllDatasets() {
203 $cmd = "zfs list -H -t filesystem -o name 2>&1";
204 OMVModuleZFSUtil
::exec($cmd, $out, $res);
205 foreach ($out as $name) {
206 $ds = new OMVModuleZFSDataset($name);
207 array_push($datasets, $ds);
213 * Helper function to execute a command and throw an exception on error
214 * (requires stderr redirected to stdout for proper exception message).
216 * @param string $cmd Command to execute
217 * @param array &$out If provided will contain output in an array
218 * @param int &$res If provided will contain Exit status of the command
219 * @return string Last line of output when executing the command
220 * @throws OMVModuleZFSException
223 public static function exec($cmd, &$out = null, &$res = null) {
224 $tmp = OMVUtil
::exec($cmd, $out, $res);
226 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.111036 seconds and 6 git commands to generate.