]>
Commit | Line | Data |
---|---|---|
f891182f | 1 | <?php |
cc6e6edb NB |
2 | require_once("Exception.php"); |
3 | require_once("openmediavault/util.inc"); | |
f1fe98e6 | 4 | require_once("Snapshot.php"); |
f891182f MR |
5 | |
6 | /** | |
7 | * XXX detailed description | |
8 | * | |
9 | * @author XXX | |
10 | * @version XXX | |
11 | * @copyright XXX | |
12 | */ | |
63617ac2 | 13 | class OMVModuleZFSZvol { |
4e7d4caf NB |
14 | // Attributes |
15 | ||
16 | /** | |
17 | * Name of Zvol | |
18 | * | |
19 | * @var string $name | |
20 | * @access private | |
21 | */ | |
22 | private $name; | |
f891182f | 23 | |
4e7d4caf NB |
24 | /** |
25 | * Size of Zvol | |
26 | * | |
e6fdbbae | 27 | * @var string $size |
4e7d4caf NB |
28 | * @access private |
29 | */ | |
30 | private $size; | |
f891182f | 31 | |
4e7d4caf NB |
32 | /** |
33 | * Array with properties assigned to the Zvol | |
34 | * | |
35 | * @var array $properties | |
36 | * @access private | |
37 | */ | |
38 | private $properties; | |
f891182f | 39 | |
f1fe98e6 NB |
40 | /** |
41 | * Array with Snapshots associated to the Zvol | |
42 | * | |
43 | * @var array $snapshots | |
44 | * @access private | |
45 | */ | |
46 | private $snapshots; | |
47 | ||
4e7d4caf NB |
48 | // Associations |
49 | // Operations | |
112892fb NB |
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) { | |
e419cf47 | 60 | $zvol_exists = true; |
112892fb | 61 | $this->name = $name; |
e419cf47 NB |
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; | |
112892fb | 70 | } |
e419cf47 | 71 | if ($zvol_exists) { |
67b3738d | 72 | $cmd = "zfs list -r -d 1 -o name -H -t snapshot " . $name . " 2>&1"; |
e419cf47 NB |
73 | $this->exec($cmd, $out2, $res2); |
74 | foreach ($out2 as $line2) { | |
75 | $this->snapshots[$line2] = new OMVModuleZFSSnapshot($line2); | |
76 | } | |
f1fe98e6 | 77 | } |
a50e3caf | 78 | |
112892fb NB |
79 | } |
80 | ||
4e7d4caf | 81 | /** |
91f56fbc NB |
82 | * Return name of the Zvol |
83 | * | |
84 | * @return string $name | |
4e7d4caf NB |
85 | * @access public |
86 | */ | |
87 | public function getName() { | |
91f56fbc | 88 | return $this->name; |
4e7d4caf | 89 | } |
f891182f | 90 | |
4e7d4caf | 91 | /** |
91f56fbc NB |
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>. | |
4e7d4caf NB |
97 | * @access public |
98 | */ | |
91f56fbc NB |
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; | |
4e7d4caf | 112 | } |
f891182f | 113 | |
4e7d4caf | 114 | /** |
e6fdbbae | 115 | * Get the total size of the Zvol |
4e7d4caf | 116 | * |
e6fdbbae | 117 | * @return string $size |
4e7d4caf NB |
118 | * @access public |
119 | */ | |
91f56fbc | 120 | public function getSize() { |
e6fdbbae | 121 | return $this->size; |
4e7d4caf | 122 | } |
f891182f | 123 | |
8c2944ec NB |
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 | ||
4e7d4caf | 138 | /** |
e6fdbbae NB |
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 | |
4e7d4caf NB |
143 | * @access public |
144 | */ | |
145 | public function setProperties($properties) { | |
e6fdbbae | 146 | foreach ($properties as $newpropertyk => $newpropertyv) { |
2777aa01 | 147 | $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name . " 2>&1"; |
e6fdbbae NB |
148 | $this->exec($cmd,$out,$res); |
149 | $this->updateProperty($newpropertyk); | |
150 | } | |
4e7d4caf NB |
151 | } |
152 | ||
112892fb NB |
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() { | |
2777aa01 | 160 | $cmd = "zfs get -H all " . $this->name . " 2>&1"; |
112892fb NB |
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 | ||
6e6b3eb9 NB |
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) { | |
2777aa01 | 177 | $cmd = "zfs get -H " . $property . " " . $this->name . " 2>&1"; |
6e6b3eb9 NB |
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 | ||
cc6e6edb NB |
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 | |
e6fdbbae NB |
187 | * @param array $properties Properties to set when creatiing the dataset. |
188 | * @param boolean $sparse Defines if a sparse volume should be created. | |
cc6e6edb NB |
189 | * @return void |
190 | * @access public | |
191 | */ | |
e6fdbbae | 192 | public function create($size, array $properties = null, $sparse = null) { |
cc6e6edb | 193 | $cmd = "zfs create -p "; |
8bda0ab8 | 194 | if ((isset($sparse)) && ($sparse == true)) { |
cc6e6edb NB |
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 | ||
f6d97ba0 NB |
204 | /** |
205 | * Destroy the Zvol. | |
206 | * | |
207 | * @return void | |
208 | * @access public | |
209 | */ | |
210 | public function destroy() { | |
2777aa01 | 211 | $cmd = "zfs destroy " . $this->name . " 2>&1"; |
f6d97ba0 NB |
212 | $this->exec($cmd,$out,$res); |
213 | } | |
214 | ||
8bda0ab8 NB |
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 | ||
6e6b3eb9 NB |
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 | ||
f1fe98e6 NB |
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 | ||
4e7d4caf NB |
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 | } | |
f891182f MR |
287 | } |
288 | ||
289 | ?> |