]> git.datanom.net - omvzfs.git/blob - src/Dataset.php
Bugfix in Dataset constructor.
[omvzfs.git] / src / Dataset.php
1 <?php
2 require_once("Exception.php");
3 require_once("openmediavault/util.inc");
4 require_once("Snapshot.php");
5
6 /**
7 * XXX detailed description
8 *
9 * @author XXX
10 * @version XXX
11 * @copyright XXX
12 */
13 class OMVModuleZFSDataset {
14 // Attributes
15 /**
16 * Name of Dataset
17 *
18 * @var string $name
19 * @access private
20 */
21 private $name;
22
23 /**
24 * Mountpoint of the Dataset
25 *
26 * @var string $mountPoint
27 * @access private
28 */
29 private $mountPoint;
30
31 /**
32 * Array with properties assigned to the Dataset
33 *
34 * @var array $properties
35 * @access private
36 */
37 private $properties;
38
39 /**
40 * Array with Snapshots associated to the Dataset
41 *
42 * @var array $snapshots
43 * @access private
44 */
45 private $snapshots;
46
47 // Associations
48 // Operations
49
50 /**
51 * Constructor. If the Dataset already exists in the system the object will be updated with all
52 * associated properties from commandline.
53 *
54 * @param string $name Name of the new Dataset
55 * @return void
56 * @access public
57 */
58 public function __construct($name) {
59 $ds_exists = true;
60 $this->name = $name;
61 $cmd = "zfs list -H -t filesystem " . $name . " 2>&1";
62 try {
63 $this->exec($cmd, $out, $res);
64 $this->updateAllProperties();
65 $this->mountPoint = $this->properties["mountpoint"]["value"];
66 }
67 catch (OMVModuleZFSException $e) {
68 $ds_exists = false;
69 }
70 if ($ds_exists) {
71 $cmd = "zfs list -r -o name -H -t snapshot " . $name . " 2>&1";
72 $this->exec($cmd, $out2, $res2);
73 foreach ($out2 as $line2) {
74 if (preg_match('/^' . preg_quote($name, '/') . '\@.*$/', $line2)) {
75 $this->snapshots[$line2] = new OMVModuleZFSSnapshot($line2);
76 }
77 }
78 } else {
79 $this->create();
80 }
81 }
82
83 /**
84 * Return name of the Dataset
85 *
86 * @return string $name
87 * @access public
88 */
89 public function getName() {
90 return $this->name;
91 }
92
93 /**
94 * Get the mountpoint of the Dataset
95 *
96 * @return string $mountPoint
97 * @access public
98 */
99 public function getMountPoint() {
100 return $this->mountPoint;
101 }
102
103 /**
104 * Get all Snapshots associated with the Dataset
105 *
106 * @return array $snapshots
107 * @access public
108 */
109 public function getSnapshots() {
110 if (isset($this->snapshots)) {
111 return $this->snapshots;
112 } else {
113 return array();
114 }
115 }
116
117 /**
118 * Get a single property value associated with the Dataset
119 *
120 * @param string $property Name of the property to fetch
121 * @return array The returned array with the property. The property is an associative array with
122 * two elements, <value> and <source>.
123 * @access public
124 */
125 public function getProperty($property) {
126 return $this->properties["$property"];
127 }
128
129 /**
130 * Get an associative array of all properties associated with the Snapshot
131 *
132 * @return array $properties Each entry is an associative array with two elements
133 * <value> and <source>
134 * @access public
135 */
136 public function getProperties() {
137 return $this->properties;
138 }
139
140 /**
141 * Sets a number of Dataset properties. If a property is already set it will be updated with the new value.
142 *
143 * @param array $properties An associative array with properties to set
144 * @return void
145 * @access public
146 */
147 public function setProperties($properties) {
148 foreach ($properties as $newpropertyk => $newpropertyv) {
149 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name . " 2>&1";
150 $this->exec($cmd,$out,$res);
151 $this->updateProperty($newpropertyk);
152 }
153 }
154
155 /**
156 * Get all Dataset properties from commandline and update object properties attribute
157 *
158 * @return void
159 * @access private
160 */
161 private function updateAllProperties() {
162 $cmd = "zfs get -H all " . $this->name . " 2>&1";
163 $this->exec($cmd,$out,$res);
164 unset($this->properties);
165 foreach ($out as $line) {
166 $tmpary = preg_split('/\t+/', $line);
167 $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
168 }
169 }
170
171 /**
172 * Get single Datset property from commandline and update object property attribute
173 *
174 * @param string $property Name of the property to update
175 * @return void
176 * @access private
177 */
178 private function updateProperty($property) {
179 $cmd = "zfs get -H " . $property . " " . $this->name . " 2>&1";
180 $this->exec($cmd,$out,$res);
181 $tmpary = preg_split('/\t+/', $out[0]);
182 $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
183 }
184
185 /**
186 * Craete a Dataset on commandline.
187 *
188 * @return void
189 * @access private
190 */
191 private function create() {
192 $cmd = "zfs create -p " . $this->name . " 2>&1";
193 $this->exec($cmd,$out,$res);
194 $this->updateAllProperties();
195 $this->mountPoint = $this->properties["mountpoint"]["value"];
196 }
197
198 /**
199 * Destroy the Dataset.
200 *
201 * @return void
202 * @access public
203 */
204 public function destroy() {
205 $cmd = "zfs destroy " . $this->name . " 2>&1";
206 $this->exec($cmd,$out,$res);
207 }
208
209 /**
210 * Renames a Dataset
211 *
212 * @param string $newname New name of the Dataset
213 * @return void
214 * @access public
215 */
216 public function rename($newname) {
217 $cmd = "zfs rename -p " . $this->name . " " . $newname . " 2>&1";
218 $this->exec($cmd,$out,$res);
219 $this->name = $newname;
220 }
221
222 /**
223 * Clears a previously set proporty and specifies that it should be
224 * inherited from it's parent.
225 *
226 * @param string $property Name of the property to inherit.
227 * @return void
228 * @access public
229 */
230 public function inherit($property) {
231 $cmd = "zfs inherit " . $property . " " . $this->name . " 2>&1";
232 $this->exec($cmd,$out,$res);
233 $this->updateProperty($property);
234 }
235
236 /**
237 * Upgrades the Dataset to latest filesystem version
238 *
239 * @return void
240 * @access public
241 */
242 public function upgrade() {
243 $cmd = "zfs upgrade " . $this->name . " 2>&1";
244 $this->exec($cmd,$out,$res);
245 }
246
247 /**
248 * Mount the Dataset
249 *
250 * @return void
251 * @access public
252 */
253 public function mount() {
254 $cmd = "zfs mount " . $this->name . " 2>&1";
255 $this->exec($cmd,$out,$res);
256 $this->updateProperty("mounted");
257 }
258
259 /**
260 * Unmount the Dataset
261 *
262 * @return void
263 * @access public
264 */
265 public function unmount() {
266 $cmd = "zfs unmount " . $this->name . " 2>&1";
267 $this->exec($cmd,$out,$res);
268 $this->updateProperty("mounted");
269 }
270
271 /**
272 * Creates a Snapshot and adds it to the existing list of snapshots associated
273 * with the Dataset.
274 *
275 * @param string $snap_name Name of the Snapshot to create.
276 * @param array $properties Optional array of properties to set on Snapshot
277 * @return void
278 * @access public
279 */
280 public function addSnapshot($snap_name, array $properties = null) {
281 $snap = new OMVModuleZFSSnapshot($snap_name);
282 $snap->create($properties);
283 $this->snapshots[$snap_name] = $snap;
284 }
285
286 /**
287 * Destroys a Snapshot on commandline and removes it from the Dataset.
288 *
289 * @param string $snap_name Name of the Snapshot to delete.
290 * @return void
291 * @access public
292 */
293 public function deleteSnapshot($snap_name) {
294 $this->snapshots[$snap_name]->destroy();
295 unset($this->snapshots[$snap_name]);
296 }
297
298 /**
299 * Helper function to execute a command and throw an exception on error
300 * (requires stderr redirected to stdout for proper exception message).
301 *
302 * @param string $cmd Command to execute
303 * @param array &$out If provided will contain output in an array
304 * @param int &$res If provided will contain Exit status of the command
305 * @return string Last line of output when executing the command
306 * @throws OMVModuleZFSException
307 * @access private
308 */
309 private function exec($cmd, &$out = null, &$res = null) {
310 $tmp = OMVUtil::exec($cmd, $out, $res);
311 if ($res) {
312 throw new OMVModuleZFSException(implode("\n", $out));
313 }
314 return $tmp;
315 }
316
317 }
318
319 ?>
This page took 0.101233 seconds and 6 git commands to generate.