]> git.datanom.net - omvzfs.git/blob - src/Zvol.php
Support for cloned filesystems and volumes.
[omvzfs.git] / src / Zvol.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 OMVModuleZFSZvol {
14 // Attributes
15
16 /**
17 * Name of Zvol
18 *
19 * @var string $name
20 * @access private
21 */
22 private $name;
23
24 /**
25 * Size of Zvol
26 *
27 * @var string $size
28 * @access private
29 */
30 private $size;
31
32 /**
33 * Array with properties assigned to the Zvol
34 *
35 * @var array $properties
36 * @access private
37 */
38 private $properties;
39
40 /**
41 * Array with Snapshots associated to the Zvol
42 *
43 * @var array $snapshots
44 * @access private
45 */
46 private $snapshots;
47
48 // Associations
49 // Operations
50
51 /**
52 * Constructor. If the Zvol already exists in the system the object will be updated with all
53 * associated properties from commandline.
54 *
55 * @param string $name Name of the new Zvol
56 * @return void
57 * @access public
58 */
59 public function __construct($name) {
60 $zvol_exists = true;
61 $this->name = $name;
62 $cmd = "zfs list -H -t volume " . $name . " 2>&1";
63 try {
64 $this->exec($cmd, $out, $res);
65 $this->updateAllProperties();
66 $this->size = $this->properties["volsize"]["value"];
67 }
68 catch (OMVModuleZFSException $e) {
69 $zvol_exists = false;
70 }
71 if ($zvol_exists) {
72 $cmd = "zfs list -r -d 1 -o name -H -t snapshot " . $name . " 2>&1";
73 $this->exec($cmd, $out2, $res2);
74 foreach ($out2 as $line2) {
75 $this->snapshots[$line2] = new OMVModuleZFSSnapshot($line2);
76 }
77 }
78
79 }
80
81 /**
82 * Return name of the Zvol
83 *
84 * @return string $name
85 * @access public
86 */
87 public function getName() {
88 return $this->name;
89 }
90
91 /**
92 * Get a single property value associated with the Zvol
93 *
94 * @param string $property Name of the property to fetch
95 * @return array The returned array with the property. The property is an associative array with
96 * two elements, <value> and <source>.
97 * @access public
98 */
99 public function getProperty($property) {
100 return $this->properties["$property"];
101 }
102
103 /**
104 * Get an associative array of all properties associated with the Zvol
105 *
106 * @return array $properties Each entry is an associative array with two elements
107 * <value> and <source>
108 * @access public
109 */
110 public function getProperties() {
111 return $this->properties;
112 }
113
114 /**
115 * Get the total size of the Zvol
116 *
117 * @return string $size
118 * @access public
119 */
120 public function getSize() {
121 return $this->size;
122 }
123
124 /**
125 * Get all Snapshots associated with the Zvol
126 *
127 * @return array $snapshots
128 * @access public
129 */
130 public function getSnapshots() {
131 if (isset($this->snapshots)) {
132 return $this->snapshots;
133 } else {
134 return array();
135 }
136 }
137
138 /**
139 * Sets a number of Zvol 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 Zvol 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 * Create a Zvol on commandline. Optionally provide a number of properties to set.
185 *
186 * @param string $size Size of the Zvol that should be created
187 * @param array $properties Properties to set when creatiing the dataset.
188 * @param boolean $sparse Defines if a sparse volume should be created.
189 * @return void
190 * @access public
191 */
192 public function create($size, array $properties = null, $sparse = null) {
193 $cmd = "zfs create -p ";
194 if ((isset($sparse)) && ($sparse == true)) {
195 $cmd .= "-s ";
196 }
197 $cmd .= "-V " . $size . " " . $this->name . " 2>&1";
198 $this->exec($cmd,$out,$res);
199 $this->updateAllProperties();
200 $this->setProperties($properties);
201 $this->size = $this->properties["volsize"]["value"];
202 }
203
204 /**
205 * Destroy the Zvol.
206 *
207 * @return void
208 * @access public
209 */
210 public function destroy() {
211 $cmd = "zfs destroy " . $this->name . " 2>&1";
212 $this->exec($cmd,$out,$res);
213 }
214
215 /**
216 * Renames a Zvol
217 *
218 * @param string $newname New name of the Dataset
219 * @return void
220 * @access public
221 */
222 public function rename($newname) {
223 $cmd = "zfs rename -p " . $this->name . " " . $newname . " 2>&1";
224 $this->exec($cmd,$out,$res);
225 $this->name = $newname;
226 }
227
228 /**
229 * Clears a previously set proporty and specifies that it should be
230 * inherited from it's parent.
231 *
232 * @param string $property Name of the property to inherit.
233 * @return void
234 * @access public
235 */
236 public function inherit($property) {
237 $cmd = "zfs inherit " . $property . " " . $this->name . " 2>&1";
238 $this->exec($cmd,$out,$res);
239 $this->updateProperty($property);
240 }
241
242 /**
243 * Creates a Snapshot and adds it to the existing list of snapshots associated
244 * with the Zvol.
245 *
246 * @param string $snap_name Name of the Snapshot to create.
247 * @param array $properties Optional array of properties to set on Snapshot
248 * @return void
249 * @access public
250 */
251 public function addSnapshot($snap_name, array $properties = null) {
252 $snap = new OMVModuleZFSSnapshot($snap_name);
253 $snap->create($properties);
254 $this->snapshots[$snap_name] = $snap;
255 }
256
257 /**
258 * Destroys a Snapshot on commandline and removes it from the Zvol.
259 *
260 * @param string $snap_name Name of the Snapshot to delete.
261 * @return void
262 * @access public
263 */
264 public function deleteSnapshot($snap_name) {
265 $this->snapshots[$snap_name]->destroy();
266 unset($this->snapshots[$snap_name]);
267 }
268
269 /**
270 * Check if the Volume is a clone or not.
271 *
272 * @return bool
273 * @access public
274 */
275 public function isClone() {
276 $origin = $this->getProperty("origin");
277 if (strlen($origin["value"]) > 0) {
278 return true;
279 } else {
280 return false;
281 }
282 }
283
284 /**
285 * Get the origin of the Volume if it's a clone.
286 *
287 * @return string The name of the origin if it exists. Otherwise an empty string.
288 * @access public
289 */
290 public function getOrigin() {
291 if ($this->isClone()) {
292 $origin = $this->getProperty("origin");
293 return $origin['value'];
294 } else {
295 return "";
296 }
297 }
298
299 /**
300 * Promotes the Volume if it's a clone.
301 *
302 * @return void
303 * @access public
304 */
305 public function promote() {
306 if ($this->isClone()) {
307 $cmd = "zfs promote " . $this->name . " 2>&1";
308 $this->exec($cmd,$out,$res);
309 }
310 }
311
312 /**
313 * Helper function to execute a command and throw an exception on error
314 * (requires stderr redirected to stdout for proper exception message).
315 *
316 * @param string $cmd Command to execute
317 * @param array &$out If provided will contain output in an array
318 * @param int &$res If provided will contain Exit status of the command
319 * @return string Last line of output when executing the command
320 * @throws OMVModuleZFSException
321 * @access private
322 */
323 private function exec($cmd, &$out = null, &$res = null) {
324 $tmp = OMVUtil::exec($cmd, $out, $res);
325 if ($res) {
326 throw new OMVModuleZFSException(implode("\n", $out));
327 }
328 return $tmp;
329 }
330 }
331
332 ?>
This page took 0.087735 seconds and 6 git commands to generate.