]> git.datanom.net - omvzfs.git/blame_incremental - src/Zvol.php
Added columns to overview and implemented object details window.
[omvzfs.git] / src / Zvol.php
... / ...
CommitLineData
1<?php
2require_once("Exception.php");
3require_once("openmediavault/util.inc");
4require_once("Snapshot.php");
5
6/**
7 * XXX detailed description
8 *
9 * @author XXX
10 * @version XXX
11 * @copyright XXX
12 */
13class 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 * Helper function to execute a command and throw an exception on error
271 * (requires stderr redirected to stdout for proper exception message).
272 *
273 * @param string $cmd Command to execute
274 * @param array &$out If provided will contain output in an array
275 * @param int &$res If provided will contain Exit status of the command
276 * @return string Last line of output when executing the command
277 * @throws OMVModuleZFSException
278 * @access private
279 */
280 private function exec($cmd, &$out = null, &$res = null) {
281 $tmp = OMVUtil::exec($cmd, $out, $res);
282 if ($res) {
283 throw new OMVModuleZFSException(implode("\n", $out));
284 }
285 return $tmp;
286 }
287}
288
289?>
This page took 0.032865 seconds and 5 git commands to generate.