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