]> git.datanom.net - omvzfs.git/blob - src/Dataset.php
Fix some bugs. Implement methods needed by zfs.inc. Zpool.php completed
[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 -d 1 -o name -H -t snapshot " . $name . " 2>&1";
72 $this->exec($cmd, $out2, $res2);
73 foreach ($out2 as $line2) {
74 $this->snapshots[$line2] = new OMVModuleZFSSnapshot($line2);
75 }
76 } else {
77 $this->create();
78 }
79 }
80
81 /**
82 * Return name of the Dataset
83 *
84 * @return string $name
85 * @access public
86 */
87 public function getName() {
88 return $this->name;
89 }
90
91 /**
92 * Get the mountpoint of the Dataset
93 *
94 * @return string $mountPoint
95 * @access public
96 */
97 public function getMountPoint() {
98 return $this->mountPoint;
99 }
100
101 /**
102 * Get all Snapshots associated with the Dataset
103 *
104 * @return array $snapshots
105 * @access public
106 */
107 public function getSnapshots() {
108 if (isset($this->snapshots)) {
109 return $this->snapshots;
110 } else {
111 return array();
112 }
113 }
114
115 /**
116 * Get a single property value associated with the Dataset
117 *
118 * @param string $property Name of the property to fetch
119 * @return array The returned array with the property. The property is an associative array with
120 * two elements, <value> and <source>.
121 * @access public
122 */
123 public function getProperty($property) {
124 return $this->properties["$property"];
125 }
126
127 /**
128 * Get an associative array of all properties associated with the Snapshot
129 *
130 * @return array $properties Each entry is an associative array with two elements
131 * <value> and <source>
132 * @access public
133 */
134 public function getProperties() {
135 return $this->properties;
136 }
137
138 /**
139 * Sets a number of Dataset properties. If a property is already set it will be updated with the new value.
140 *
141 * @param array $properties An associative array with properties to set
142 * @return void
143 * @access public
144 */
145 public function setProperties($properties) {
146 foreach ($properties as $newpropertyk => $newpropertyv) {
147 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name . " 2>&1";
148 $this->exec($cmd,$out,$res);
149 $this->updateProperty($newpropertyk);
150 }
151 }
152
153 /**
154 * Get all Dataset properties from commandline and update object properties attribute
155 *
156 * @return void
157 * @access private
158 */
159 private function updateAllProperties() {
160 $cmd = "zfs get -H all " . $this->name . " 2>&1";
161 $this->exec($cmd,$out,$res);
162 unset($this->properties);
163 foreach ($out as $line) {
164 $tmpary = preg_split('/\t+/', $line);
165 $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
166 }
167 }
168
169 /**
170 * Get single Datset property from commandline and update object property attribute
171 *
172 * @param string $property Name of the property to update
173 * @return void
174 * @access private
175 */
176 private function updateProperty($property) {
177 $cmd = "zfs get -H " . $property . " " . $this->name . " 2>&1";
178 $this->exec($cmd,$out,$res);
179 $tmpary = preg_split('/\t+/', $out[0]);
180 $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
181 }
182
183 /**
184 * Craete a Dataset on commandline.
185 *
186 * @return void
187 * @access private
188 */
189 private function create() {
190 $cmd = "zfs create -p " . $this->name . " 2>&1";
191 $this->exec($cmd,$out,$res);
192 $this->updateAllProperties();
193 $this->mountPoint = $this->properties["mountpoint"]["value"];
194 }
195
196 /**
197 * Destroy the Dataset.
198 *
199 * @return void
200 * @access public
201 */
202 public function destroy() {
203 $cmd = "zfs destroy " . $this->name . " 2>&1";
204 $this->exec($cmd,$out,$res);
205 }
206
207 /**
208 * Renames a Dataset
209 *
210 * @param string $newname New name of the Dataset
211 * @return void
212 * @access public
213 */
214 public function rename($newname) {
215 $cmd = "zfs rename -p " . $this->name . " " . $newname . " 2>&1";
216 $this->exec($cmd,$out,$res);
217 $this->name = $newname;
218 }
219
220 /**
221 * Clears a previously set proporty and specifies that it should be
222 * inherited from it's parent.
223 *
224 * @param string $property Name of the property to inherit.
225 * @return void
226 * @access public
227 */
228 public function inherit($property) {
229 $cmd = "zfs inherit " . $property . " " . $this->name . " 2>&1";
230 $this->exec($cmd,$out,$res);
231 $this->updateProperty($property);
232 }
233
234 /**
235 * Upgrades the Dataset to latest filesystem version
236 *
237 * @return void
238 * @access public
239 */
240 public function upgrade() {
241 $cmd = "zfs upgrade " . $this->name . " 2>&1";
242 $this->exec($cmd,$out,$res);
243 }
244
245 /**
246 * Mount the Dataset
247 *
248 * @return void
249 * @access public
250 */
251 public function mount() {
252 $cmd = "zfs mount " . $this->name . " 2>&1";
253 $this->exec($cmd,$out,$res);
254 $this->updateProperty("mounted");
255 }
256
257 /**
258 * Unmount the Dataset
259 *
260 * @return void
261 * @access public
262 */
263 public function unmount() {
264 $cmd = "zfs unmount " . $this->name . " 2>&1";
265 $this->exec($cmd,$out,$res);
266 $this->updateProperty("mounted");
267 }
268
269 /**
270 * Creates a Snapshot and adds it to the existing list of snapshots associated
271 * with the Dataset.
272 *
273 * @param string $snap_name Name of the Snapshot to create.
274 * @param array $properties Optional array of properties to set on Snapshot
275 * @return void
276 * @access public
277 */
278 public function addSnapshot($snap_name, array $properties = null) {
279 $snap = new OMVModuleZFSSnapshot($snap_name);
280 $snap->create($properties);
281 $this->snapshots[$snap_name] = $snap;
282 }
283
284 /**
285 * Destroys a Snapshot on commandline and removes it from the Dataset.
286 *
287 * @param string $snap_name Name of the Snapshot to delete.
288 * @return void
289 * @access public
290 */
291 public function deleteSnapshot($snap_name) {
292 $this->snapshots[$snap_name]->destroy();
293 unset($this->snapshots[$snap_name]);
294 }
295
296 /**
297 * Check if the Dataset is a clone or not.
298 *
299 * @return bool
300 * @access public
301 */
302 public function isClone() {
303 $origin = $this->getProperty("origin");
304 if (strlen($origin["value"]) > 0) {
305 return true;
306 } else {
307 return false;
308 }
309 }
310
311 /**
312 * Get the origin of the Dataset if it's a clone.
313 *
314 * @return string The name of the origin if it exists. Otherwise an empty string.
315 * @access public
316 */
317 public function getOrigin() {
318 if ($this->isClone()) {
319 $origin = $this->getProperty("origin");
320 return $origin['value'];
321 } else {
322 return "";
323 }
324 }
325
326 /**
327 * Promotes the Dataset if it's a clone.
328 *
329 * @return void
330 * @access public
331 */
332 public function promote() {
333 if ($this->isClone()) {
334 $cmd = "zfs promote " . $this->name . " 2>&1";
335 $this->exec($cmd,$out,$res);
336 }
337 }
338
339 /**
340 * Helper function to execute a command and throw an exception on error
341 * (requires stderr redirected to stdout for proper exception message).
342 *
343 * @param string $cmd Command to execute
344 * @param array &$out If provided will contain output in an array
345 * @param int &$res If provided will contain Exit status of the command
346 * @return string Last line of output when executing the command
347 * @throws OMVModuleZFSException
348 * @access private
349 */
350 private function exec($cmd, &$out = null, &$res = null) {
351 $tmp = OMVUtil::exec($cmd, $out, $res);
352 if ($res) {
353 throw new OMVModuleZFSException(implode("\n", $out));
354 }
355 return $tmp;
356 }
357
358 }
359
360 ?>
This page took 0.130014 seconds and 6 git commands to generate.