]> git.datanom.net - omvzfs.git/blame - src/Dataset.php
Bugfix in Dataset constructor.
[omvzfs.git] / src / Dataset.php
CommitLineData
f891182f 1<?php
ba6cf545 2require_once("Exception.php");
eb034e47 3require_once("openmediavault/util.inc");
946a8c30 4require_once("Snapshot.php");
f891182f
MR
5
6/**
7 * XXX detailed description
8 *
9 * @author XXX
10 * @version XXX
11 * @copyright XXX
12 */
63617ac2 13class OMVModuleZFSDataset {
f891182f
MR
14 // Attributes
15 /**
a4056e15 16 * Name of Dataset
f891182f
MR
17 *
18 * @var string $name
19 * @access private
20 */
a4056e15 21 private $name;
dcac32d6
NB
22
23 /**
ba6cf545 24 * Mountpoint of the Dataset
f891182f
MR
25 *
26 * @var string $mountPoint
27 * @access private
28 */
ba6cf545 29 private $mountPoint;
f891182f
MR
30
31 /**
60a2cc94 32 * Array with properties assigned to the Dataset
f891182f 33 *
31fb83f4 34 * @var array $properties
f891182f
MR
35 * @access private
36 */
31fb83f4 37 private $properties;
f891182f 38
946a8c30
NB
39 /**
40 * Array with Snapshots associated to the Dataset
41 *
42 * @var array $snapshots
43 * @access private
44 */
45 private $snapshots;
46
ba6cf545
NB
47 // Associations
48 // Operations
f891182f 49
ba6cf545 50 /**
6ed033a4
NB
51 * Constructor. If the Dataset already exists in the system the object will be updated with all
52 * associated properties from commandline.
eb034e47 53 *
ba6cf545 54 * @param string $name Name of the new Dataset
6ed033a4
NB
55 * @return void
56 * @access public
ba6cf545 57 */
6ed033a4 58 public function __construct($name) {
e419cf47 59 $ds_exists = true;
ba6cf545 60 $this->name = $name;
e419cf47
NB
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;
6ed033a4 69 }
e419cf47
NB
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) {
4b7ce266
NB
74 if (preg_match('/^' . preg_quote($name, '/') . '\@.*$/', $line2)) {
75 $this->snapshots[$line2] = new OMVModuleZFSSnapshot($line2);
76 }
e419cf47 77 }
4b7ce266
NB
78 } else {
79 $this->create();
946a8c30 80 }
ba6cf545 81 }
f891182f 82
ba6cf545
NB
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 }
f891182f 92
ba6cf545
NB
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
946a8c30
NB
103 /**
104 * Get all Snapshots associated with the Dataset
105 *
106 * @return array $snapshots
107 * @access public
108 */
109 public function getSnapshots() {
4b7ce266
NB
110 if (isset($this->snapshots)) {
111 return $this->snapshots;
112 } else {
113 return array();
114 }
946a8c30
NB
115 }
116
ba6cf545 117 /**
60a2cc94 118 * Get a single property value associated with the Dataset
43a6a77c 119 *
60a2cc94 120 * @param string $property Name of the property to fetch
43a6a77c
NB
121 * @return array The returned array with the property. The property is an associative array with
122 * two elements, <value> and <source>.
60a2cc94
NB
123 * @access public
124 */
125 public function getProperty($property) {
126 return $this->properties["$property"];
127 }
128
129 /**
43a6a77c
NB
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>
ba6cf545
NB
134 * @access public
135 */
31fb83f4
NB
136 public function getProperties() {
137 return $this->properties;
ba6cf545
NB
138 }
139
140 /**
0b156fc3 141 * Sets a number of Dataset properties. If a property is already set it will be updated with the new value.
ba6cf545 142 *
60a2cc94 143 * @param array $properties An associative array with properties to set
eb034e47 144 * @return void
ba6cf545
NB
145 * @access public
146 */
31fb83f4 147 public function setProperties($properties) {
60a2cc94 148 foreach ($properties as $newpropertyk => $newpropertyv) {
8b1436f9 149 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name . " 2>&1";
b7cf97c0 150 $this->exec($cmd,$out,$res);
9b922382 151 $this->updateProperty($newpropertyk);
60a2cc94 152 }
60a2cc94
NB
153 }
154
155 /**
156 * Get all Dataset properties from commandline and update object properties attribute
157 *
c03e412f 158 * @return void
60a2cc94
NB
159 * @access private
160 */
161 private function updateAllProperties() {
8b1436f9 162 $cmd = "zfs get -H all " . $this->name . " 2>&1";
b7cf97c0 163 $this->exec($cmd,$out,$res);
60a2cc94
NB
164 unset($this->properties);
165 foreach ($out as $line) {
166 $tmpary = preg_split('/\t+/', $line);
e6831d92 167 $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
9b922382
NB
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
c03e412f 175 * @return void
9b922382
NB
176 * @access private
177 */
178 private function updateProperty($property) {
8b1436f9 179 $cmd = "zfs get -H " . $property . " " . $this->name . " 2>&1";
b7cf97c0 180 $this->exec($cmd,$out,$res);
9b922382 181 $tmpary = preg_split('/\t+/', $out[0]);
e6831d92 182 $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
0b156fc3
NB
183 }
184
6ed033a4 185 /**
4b7ce266 186 * Craete a Dataset on commandline.
6ed033a4 187 *
6ed033a4 188 * @return void
4b7ce266 189 * @access private
6ed033a4 190 */
4b7ce266 191 private function create() {
6ed033a4
NB
192 $cmd = "zfs create -p " . $this->name . " 2>&1";
193 $this->exec($cmd,$out,$res);
194 $this->updateAllProperties();
6ed033a4
NB
195 $this->mountPoint = $this->properties["mountpoint"]["value"];
196 }
197
0b156fc3
NB
198 /**
199 * Destroy the Dataset.
200 *
c03e412f 201 * @return void
60a2cc94 202 * @access public
0b156fc3
NB
203 */
204 public function destroy() {
8b1436f9 205 $cmd = "zfs destroy " . $this->name . " 2>&1";
b7cf97c0 206 $this->exec($cmd,$out,$res);
ba6cf545 207 }
f891182f 208
9e15bd05
NB
209 /**
210 * Renames a Dataset
211 *
212 * @param string $newname New name of the Dataset
c03e412f 213 * @return void
9e15bd05
NB
214 * @access public
215 */
216 public function rename($newname) {
217 $cmd = "zfs rename -p " . $this->name . " " . $newname . " 2>&1";
b7cf97c0 218 $this->exec($cmd,$out,$res);
9e15bd05
NB
219 $this->name = $newname;
220 }
221
c03e412f
NB
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
c03e412f
NB
228 * @access public
229 */
230 public function inherit($property) {
231 $cmd = "zfs inherit " . $property . " " . $this->name . " 2>&1";
b7cf97c0
NB
232 $this->exec($cmd,$out,$res);
233 $this->updateProperty($property);
234 }
235
dcac32d6
NB
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
990e1baf
NB
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
ddcd4558
NB
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 }
990e1baf 297
b7cf97c0
NB
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);
c03e412f
NB
311 if ($res) {
312 throw new OMVModuleZFSException(implode("\n", $out));
313 }
b7cf97c0 314 return $tmp;
c03e412f
NB
315 }
316
f891182f
MR
317}
318
319?>
This page took 0.117333 seconds and 5 git commands to generate.