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