]> git.datanom.net - omvzfs.git/blame - src/Dataset.php
Introduce new helper function to further cleanup code
[omvzfs.git] / src / Dataset.php
CommitLineData
f891182f 1<?php
ba6cf545 2require_once("Exception.php");
eb034e47 3require_once("openmediavault/util.inc");
f891182f
MR
4
5/**
6 * XXX detailed description
7 *
8 * @author XXX
9 * @version XXX
10 * @copyright XXX
11 */
63617ac2 12class OMVModuleZFSDataset {
f891182f
MR
13 // Attributes
14 /**
a4056e15 15 * Name of Dataset
f891182f
MR
16 *
17 * @var string $name
18 * @access private
19 */
a4056e15 20 private $name;
f891182f 21 /**
ba6cf545 22 * Mountpoint of the Dataset
f891182f
MR
23 *
24 * @var string $mountPoint
25 * @access private
26 */
ba6cf545 27 private $mountPoint;
f891182f
MR
28
29 /**
60a2cc94 30 * Array with properties assigned to the Dataset
f891182f 31 *
31fb83f4 32 * @var array $properties
f891182f
MR
33 * @access private
34 */
31fb83f4 35 private $properties;
f891182f 36
ba6cf545
NB
37 // Associations
38 // Operations
f891182f 39
ba6cf545
NB
40 /**
41 * Constructor
eb034e47 42 *
ba6cf545 43 * @param string $name Name of the new Dataset
60a2cc94 44 * @param array $properties An associative array with properties to set when creating the Dataset
ba6cf545
NB
45 *
46 */
31fb83f4 47 public function __construct($name, array $properties = null) {
9e15bd05 48 $cmd = "zfs create -p " . $name . " 2>&1";
b7cf97c0 49 $this->exec($cmd,$out,$res);
ba6cf545 50 $this->name = $name;
9b922382 51 $this->updateAllProperties();
60a2cc94 52 $this->setProperties($properties);
9b922382 53 $this->mountPoint = $this->properties["mountpoint"][0];
ba6cf545 54 }
f891182f 55
ba6cf545
NB
56 /**
57 * Return name of the Dataset
58 *
59 * @return string $name
60 * @access public
61 */
62 public function getName() {
63 return $this->name;
64 }
f891182f 65
ba6cf545
NB
66 /**
67 * Get the mountpoint of the Dataset
68 *
69 * @return string $mountPoint
70 * @access public
71 */
72 public function getMountPoint() {
73 return $this->mountPoint;
74 }
75
76 /**
60a2cc94
NB
77 * Get a single property value associated with the Dataset
78 *
79 * @param string $property Name of the property to fetch
9b922382 80 * @return array The returned array key 0=property value and key 1=property source.
60a2cc94
NB
81 * @access public
82 */
83 public function getProperty($property) {
84 return $this->properties["$property"];
85 }
86
87 /**
9b922382
NB
88 * Get an associative array of all properties associated with the Dataset.
89 *
90 * @return array $properties Each entry is an array where key 0=property value and key
91 * 1=property source.
ba6cf545 92 *
ba6cf545
NB
93 * @access public
94 */
31fb83f4
NB
95 public function getProperties() {
96 return $this->properties;
ba6cf545
NB
97 }
98
99 /**
0b156fc3 100 * Sets a number of Dataset properties. If a property is already set it will be updated with the new value.
ba6cf545 101 *
60a2cc94 102 * @param array $properties An associative array with properties to set
eb034e47 103 * @return void
ba6cf545
NB
104 * @access public
105 */
31fb83f4 106 public function setProperties($properties) {
60a2cc94
NB
107 foreach ($properties as $newpropertyk => $newpropertyv) {
108 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name;
b7cf97c0 109 $this->exec($cmd,$out,$res);
9b922382 110 $this->updateProperty($newpropertyk);
60a2cc94 111 }
60a2cc94
NB
112 }
113
114 /**
115 * Get all Dataset properties from commandline and update object properties attribute
116 *
c03e412f 117 * @return void
60a2cc94
NB
118 * @access private
119 */
120 private function updateAllProperties() {
121 $cmd = "zfs get -H all " . $this->name;
b7cf97c0 122 $this->exec($cmd,$out,$res);
60a2cc94
NB
123 unset($this->properties);
124 foreach ($out as $line) {
125 $tmpary = preg_split('/\t+/', $line);
9b922382
NB
126 $this->properties["$tmpary[1]"] = array($tmpary[2], $tmpary[3]);
127 }
128 }
129
130 /**
131 * Get single Datset property from commandline and update object property attribute
132 *
133 * @param string $property Name of the property to update
c03e412f 134 * @return void
9b922382
NB
135 * @access private
136 */
137 private function updateProperty($property) {
138 $cmd = "zfs get -H " . $property . " " . $this->name;
b7cf97c0 139 $this->exec($cmd,$out,$res);
9b922382
NB
140 $tmpary = preg_split('/\t+/', $out[0]);
141 $this->properties["$tmpary[1]"] = array($tmpary[2], $tmpary[3]);
0b156fc3
NB
142 }
143
144 /**
145 * Destroy the Dataset.
146 *
c03e412f 147 * @return void
60a2cc94 148 * @access public
0b156fc3
NB
149 */
150 public function destroy() {
151 $cmd = "zfs destroy " . $this->name;
b7cf97c0 152 $this->exec($cmd,$out,$res);
ba6cf545 153 }
f891182f 154
9e15bd05
NB
155 /**
156 * Renames a Dataset
157 *
158 * @param string $newname New name of the Dataset
c03e412f 159 * @return void
9e15bd05
NB
160 * @access public
161 */
162 public function rename($newname) {
163 $cmd = "zfs rename -p " . $this->name . " " . $newname . " 2>&1";
b7cf97c0 164 $this->exec($cmd,$out,$res);
9e15bd05
NB
165 $this->name = $newname;
166 }
167
c03e412f
NB
168 /**
169 * Clears a previously set proporty and specifies that it should be
170 * inherited from it's parent.
171 *
172 * @param string $property Name of the property to inherit.
173 * @return void
c03e412f
NB
174 * @access public
175 */
176 public function inherit($property) {
177 $cmd = "zfs inherit " . $property . " " . $this->name . " 2>&1";
b7cf97c0
NB
178 $this->exec($cmd,$out,$res);
179 $this->updateProperty($property);
180 }
181
182 /**
183 * Helper function to execute a command and throw an exception on error
184 * (requires stderr redirected to stdout for proper exception message).
185 *
186 * @param string $cmd Command to execute
187 * @param array &$out If provided will contain output in an array
188 * @param int &$res If provided will contain Exit status of the command
189 * @return string Last line of output when executing the command
190 * @throws OMVModuleZFSException
191 * @access private
192 */
193 private function exec($cmd, &$out = null, &$res = null) {
194 $tmp = OMVUtil::exec($cmd, $out, $res);
c03e412f
NB
195 if ($res) {
196 throw new OMVModuleZFSException(implode("\n", $out));
197 }
b7cf97c0 198 return $tmp;
c03e412f
NB
199 }
200
f891182f
MR
201}
202
203?>
This page took 0.093922 seconds and 5 git commands to generate.