]>
Commit | Line | Data |
---|---|---|
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 | $this->name = $name; | |
61 | $qname = preg_quote($name, '/'); | |
62 | $cmd = "zfs list -H -t volume 2>&1"; | |
63 | $this->exec($cmd, $out, $res); | |
64 | foreach ($out as $line) { | |
65 | if (preg_match('/^' . $qname . '\t.*$/', $line)) { | |
66 | $this->updateAllProperties(); | |
67 | $this->size = $this->properties["volsize"]["value"]; | |
68 | continue; | |
69 | } | |
70 | } | |
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 Zvol | |
81 | * | |
82 | * @return string $name | |
83 | * @access public | |
84 | */ | |
85 | public function getName() { | |
86 | return $this->name; | |
87 | } | |
88 | ||
89 | /** | |
90 | * Get a single property value associated with the Zvol | |
91 | * | |
92 | * @param string $property Name of the property to fetch | |
93 | * @return array The returned array with the property. The property is an associative array with | |
94 | * two elements, <value> and <source>. | |
95 | * @access public | |
96 | */ | |
97 | public function getProperty($property) { | |
98 | return $this->properties["$property"]; | |
99 | } | |
100 | ||
101 | /** | |
102 | * Get an associative array of all properties associated with the Zvol | |
103 | * | |
104 | * @return array $properties Each entry is an associative array with two elements | |
105 | * <value> and <source> | |
106 | * @access public | |
107 | */ | |
108 | public function getProperties() { | |
109 | return $this->properties; | |
110 | } | |
111 | ||
112 | /** | |
113 | * Get the total size of the Zvol | |
114 | * | |
115 | * @return string $size | |
116 | * @access public | |
117 | */ | |
118 | public function getSize() { | |
119 | return $this->size; | |
120 | } | |
121 | ||
122 | /** | |
123 | * Sets a number of Zvol properties. If a property is already set it will be updated with the new value. | |
124 | * | |
125 | * @param array $properties An associative array with properties to set | |
126 | * @return void | |
127 | * @access public | |
128 | */ | |
129 | public function setProperties($properties) { | |
130 | foreach ($properties as $newpropertyk => $newpropertyv) { | |
131 | $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name . " 2>&1"; | |
132 | $this->exec($cmd,$out,$res); | |
133 | $this->updateProperty($newpropertyk); | |
134 | } | |
135 | } | |
136 | ||
137 | /** | |
138 | * Get all Zvol properties from commandline and update object properties attribute | |
139 | * | |
140 | * @return void | |
141 | * @access private | |
142 | */ | |
143 | private function updateAllProperties() { | |
144 | $cmd = "zfs get -H all " . $this->name . " 2>&1"; | |
145 | $this->exec($cmd,$out,$res); | |
146 | unset($this->properties); | |
147 | foreach ($out as $line) { | |
148 | $tmpary = preg_split('/\t+/', $line); | |
149 | $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]); | |
150 | } | |
151 | } | |
152 | ||
153 | /** | |
154 | * Get single Datset property from commandline and update object property attribute | |
155 | * | |
156 | * @param string $property Name of the property to update | |
157 | * @return void | |
158 | * @access private | |
159 | */ | |
160 | private function updateProperty($property) { | |
161 | $cmd = "zfs get -H " . $property . " " . $this->name . " 2>&1"; | |
162 | $this->exec($cmd,$out,$res); | |
163 | $tmpary = preg_split('/\t+/', $out[0]); | |
164 | $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]); | |
165 | } | |
166 | ||
167 | /** | |
168 | * Create a Zvol on commandline. Optionally provide a number of properties to set. | |
169 | * | |
170 | * @param string $size Size of the Zvol that should be created | |
171 | * @param array $properties Properties to set when creatiing the dataset. | |
172 | * @param boolean $sparse Defines if a sparse volume should be created. | |
173 | * @return void | |
174 | * @access public | |
175 | */ | |
176 | public function create($size, array $properties = null, $sparse = null) { | |
177 | $cmd = "zfs create -p "; | |
178 | if ((isset($sparse)) && ($sparse == true)) { | |
179 | $cmd .= "-s "; | |
180 | } | |
181 | $cmd .= "-V " . $size . " " . $this->name . " 2>&1"; | |
182 | $this->exec($cmd,$out,$res); | |
183 | $this->updateAllProperties(); | |
184 | $this->setProperties($properties); | |
185 | $this->size = $this->properties["volsize"]["value"]; | |
186 | } | |
187 | ||
188 | /** | |
189 | * Destroy the Zvol. | |
190 | * | |
191 | * @return void | |
192 | * @access public | |
193 | */ | |
194 | public function destroy() { | |
195 | $cmd = "zfs destroy " . $this->name . " 2>&1"; | |
196 | $this->exec($cmd,$out,$res); | |
197 | } | |
198 | ||
199 | /** | |
200 | * Renames a Zvol | |
201 | * | |
202 | * @param string $newname New name of the Dataset | |
203 | * @return void | |
204 | * @access public | |
205 | */ | |
206 | public function rename($newname) { | |
207 | $cmd = "zfs rename -p " . $this->name . " " . $newname . " 2>&1"; | |
208 | $this->exec($cmd,$out,$res); | |
209 | $this->name = $newname; | |
210 | } | |
211 | ||
212 | /** | |
213 | * Clears a previously set proporty and specifies that it should be | |
214 | * inherited from it's parent. | |
215 | * | |
216 | * @param string $property Name of the property to inherit. | |
217 | * @return void | |
218 | * @access public | |
219 | */ | |
220 | public function inherit($property) { | |
221 | $cmd = "zfs inherit " . $property . " " . $this->name . " 2>&1"; | |
222 | $this->exec($cmd,$out,$res); | |
223 | $this->updateProperty($property); | |
224 | } | |
225 | ||
226 | /** | |
227 | * Creates a Snapshot and adds it to the existing list of snapshots associated | |
228 | * with the Zvol. | |
229 | * | |
230 | * @param string $snap_name Name of the Snapshot to create. | |
231 | * @param array $properties Optional array of properties to set on Snapshot | |
232 | * @return void | |
233 | * @access public | |
234 | */ | |
235 | public function addSnapshot($snap_name, array $properties = null) { | |
236 | $snap = new OMVModuleZFSSnapshot($snap_name); | |
237 | $snap->create($properties); | |
238 | $this->snapshots[$snap_name] = $snap; | |
239 | } | |
240 | ||
241 | /** | |
242 | * Destroys a Snapshot on commandline and removes it from the Zvol. | |
243 | * | |
244 | * @param string $snap_name Name of the Snapshot to delete. | |
245 | * @return void | |
246 | * @access public | |
247 | */ | |
248 | public function deleteSnapshot($snap_name) { | |
249 | $this->snapshots[$snap_name]->destroy(); | |
250 | unset($this->snapshots[$snap_name]); | |
251 | } | |
252 | ||
253 | /** | |
254 | * Helper function to execute a command and throw an exception on error | |
255 | * (requires stderr redirected to stdout for proper exception message). | |
256 | * | |
257 | * @param string $cmd Command to execute | |
258 | * @param array &$out If provided will contain output in an array | |
259 | * @param int &$res If provided will contain Exit status of the command | |
260 | * @return string Last line of output when executing the command | |
261 | * @throws OMVModuleZFSException | |
262 | * @access private | |
263 | */ | |
264 | private function exec($cmd, &$out = null, &$res = null) { | |
265 | $tmp = OMVUtil::exec($cmd, $out, $res); | |
266 | if ($res) { | |
267 | throw new OMVModuleZFSException(implode("\n", $out)); | |
268 | } | |
269 | return $tmp; | |
270 | } | |
271 | } | |
272 | ||
273 | ?> |