]>
Commit | Line | Data |
---|---|---|
f891182f | 1 | <?php |
ba6cf545 | 2 | require_once("Exception.php"); |
eb034e47 | 3 | require_once("openmediavault/util.inc"); |
946a8c30 | 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 OMVModuleZFSDataset { |
f891182f MR |
14 | // Attributes |
15 | /** | |
a4056e15 | 16 | * Name of Dataset |
f891182f MR |
17 | * |
18 | * @var string $name | |
19 | * @access private | |
20 | */ | |
a4056e15 | 21 | private $name; |
dcac32d6 NB |
22 | |
23 | /** | |
ba6cf545 | 24 | * Mountpoint of the Dataset |
f891182f MR |
25 | * |
26 | * @var string $mountPoint | |
27 | * @access private | |
28 | */ | |
ba6cf545 | 29 | private $mountPoint; |
f891182f MR |
30 | |
31 | /** | |
60a2cc94 | 32 | * Array with properties assigned to the Dataset |
f891182f | 33 | * |
31fb83f4 | 34 | * @var array $properties |
f891182f MR |
35 | * @access private |
36 | */ | |
31fb83f4 | 37 | private $properties; |
f891182f | 38 | |
946a8c30 NB |
39 | /** |
40 | * Array with Snapshots associated to the Dataset | |
41 | * | |
42 | * @var array $snapshots | |
43 | * @access private | |
44 | */ | |
45 | private $snapshots; | |
46 | ||
ba6cf545 NB |
47 | // Associations |
48 | // Operations | |
f891182f | 49 | |
ba6cf545 | 50 | /** |
6ed033a4 NB |
51 | * Constructor. If the Dataset already exists in the system the object will be updated with all |
52 | * associated properties from commandline. | |
eb034e47 | 53 | * |
ba6cf545 | 54 | * @param string $name Name of the new Dataset |
6ed033a4 NB |
55 | * @return void |
56 | * @access public | |
ba6cf545 | 57 | */ |
6ed033a4 | 58 | public function __construct($name) { |
e419cf47 | 59 | $ds_exists = true; |
ba6cf545 | 60 | $this->name = $name; |
e419cf47 NB |
61 | $cmd = "zfs list -H -t filesystem " . $name . " 2>&1"; |
62 | try { | |
63 | $this->exec($cmd, $out, $res); | |
64 | $this->updateAllProperties(); | |
65 | $this->mountPoint = $this->properties["mountpoint"]["value"]; | |
66 | } | |
67 | catch (OMVModuleZFSException $e) { | |
68 | $ds_exists = false; | |
6ed033a4 | 69 | } |
e419cf47 | 70 | if ($ds_exists) { |
67b3738d | 71 | $cmd = "zfs list -r -d 1 -o name -H -t snapshot " . $name . " 2>&1"; |
e419cf47 NB |
72 | $this->exec($cmd, $out2, $res2); |
73 | foreach ($out2 as $line2) { | |
4b7ce266 | 74 | $this->snapshots[$line2] = new OMVModuleZFSSnapshot($line2); |
e419cf47 | 75 | } |
4b7ce266 NB |
76 | } else { |
77 | $this->create(); | |
946a8c30 | 78 | } |
ba6cf545 | 79 | } |
f891182f | 80 | |
ba6cf545 NB |
81 | /** |
82 | * Return name of the Dataset | |
83 | * | |
84 | * @return string $name | |
85 | * @access public | |
86 | */ | |
87 | public function getName() { | |
88 | return $this->name; | |
89 | } | |
f891182f | 90 | |
ba6cf545 NB |
91 | /** |
92 | * Get the mountpoint of the Dataset | |
93 | * | |
94 | * @return string $mountPoint | |
95 | * @access public | |
96 | */ | |
97 | public function getMountPoint() { | |
98 | return $this->mountPoint; | |
99 | } | |
100 | ||
946a8c30 NB |
101 | /** |
102 | * Get all Snapshots associated with the Dataset | |
103 | * | |
104 | * @return array $snapshots | |
105 | * @access public | |
106 | */ | |
107 | public function getSnapshots() { | |
4b7ce266 NB |
108 | if (isset($this->snapshots)) { |
109 | return $this->snapshots; | |
110 | } else { | |
111 | return array(); | |
112 | } | |
946a8c30 NB |
113 | } |
114 | ||
ba6cf545 | 115 | /** |
60a2cc94 | 116 | * Get a single property value associated with the Dataset |
43a6a77c | 117 | * |
60a2cc94 | 118 | * @param string $property Name of the property to fetch |
43a6a77c NB |
119 | * @return array The returned array with the property. The property is an associative array with |
120 | * two elements, <value> and <source>. | |
60a2cc94 NB |
121 | * @access public |
122 | */ | |
123 | public function getProperty($property) { | |
124 | return $this->properties["$property"]; | |
125 | } | |
126 | ||
127 | /** | |
43a6a77c NB |
128 | * Get an associative array of all properties associated with the Snapshot |
129 | * | |
130 | * @return array $properties Each entry is an associative array with two elements | |
131 | * <value> and <source> | |
ba6cf545 NB |
132 | * @access public |
133 | */ | |
31fb83f4 NB |
134 | public function getProperties() { |
135 | return $this->properties; | |
ba6cf545 NB |
136 | } |
137 | ||
138 | /** | |
0b156fc3 | 139 | * Sets a number of Dataset properties. If a property is already set it will be updated with the new value. |
ba6cf545 | 140 | * |
60a2cc94 | 141 | * @param array $properties An associative array with properties to set |
eb034e47 | 142 | * @return void |
ba6cf545 NB |
143 | * @access public |
144 | */ | |
31fb83f4 | 145 | public function setProperties($properties) { |
60a2cc94 | 146 | foreach ($properties as $newpropertyk => $newpropertyv) { |
8b1436f9 | 147 | $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name . " 2>&1"; |
b7cf97c0 | 148 | $this->exec($cmd,$out,$res); |
9b922382 | 149 | $this->updateProperty($newpropertyk); |
60a2cc94 | 150 | } |
60a2cc94 NB |
151 | } |
152 | ||
153 | /** | |
154 | * Get all Dataset properties from commandline and update object properties attribute | |
155 | * | |
c03e412f | 156 | * @return void |
60a2cc94 NB |
157 | * @access private |
158 | */ | |
159 | private function updateAllProperties() { | |
8b1436f9 | 160 | $cmd = "zfs get -H all " . $this->name . " 2>&1"; |
b7cf97c0 | 161 | $this->exec($cmd,$out,$res); |
60a2cc94 NB |
162 | unset($this->properties); |
163 | foreach ($out as $line) { | |
164 | $tmpary = preg_split('/\t+/', $line); | |
e6831d92 | 165 | $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]); |
9b922382 NB |
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 | |
c03e412f | 173 | * @return void |
9b922382 NB |
174 | * @access private |
175 | */ | |
176 | private function updateProperty($property) { | |
8b1436f9 | 177 | $cmd = "zfs get -H " . $property . " " . $this->name . " 2>&1"; |
b7cf97c0 | 178 | $this->exec($cmd,$out,$res); |
9b922382 | 179 | $tmpary = preg_split('/\t+/', $out[0]); |
e6831d92 | 180 | $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]); |
0b156fc3 NB |
181 | } |
182 | ||
6ed033a4 | 183 | /** |
4b7ce266 | 184 | * Craete a Dataset on commandline. |
6ed033a4 | 185 | * |
6ed033a4 | 186 | * @return void |
4b7ce266 | 187 | * @access private |
6ed033a4 | 188 | */ |
4b7ce266 | 189 | private function create() { |
6ed033a4 NB |
190 | $cmd = "zfs create -p " . $this->name . " 2>&1"; |
191 | $this->exec($cmd,$out,$res); | |
192 | $this->updateAllProperties(); | |
6ed033a4 NB |
193 | $this->mountPoint = $this->properties["mountpoint"]["value"]; |
194 | } | |
195 | ||
0b156fc3 NB |
196 | /** |
197 | * Destroy the Dataset. | |
198 | * | |
c03e412f | 199 | * @return void |
60a2cc94 | 200 | * @access public |
0b156fc3 NB |
201 | */ |
202 | public function destroy() { | |
8b1436f9 | 203 | $cmd = "zfs destroy " . $this->name . " 2>&1"; |
b7cf97c0 | 204 | $this->exec($cmd,$out,$res); |
ba6cf545 | 205 | } |
f891182f | 206 | |
9e15bd05 NB |
207 | /** |
208 | * Renames a Dataset | |
209 | * | |
210 | * @param string $newname New name of the Dataset | |
c03e412f | 211 | * @return void |
9e15bd05 NB |
212 | * @access public |
213 | */ | |
214 | public function rename($newname) { | |
215 | $cmd = "zfs rename -p " . $this->name . " " . $newname . " 2>&1"; | |
b7cf97c0 | 216 | $this->exec($cmd,$out,$res); |
9e15bd05 NB |
217 | $this->name = $newname; |
218 | } | |
219 | ||
c03e412f NB |
220 | /** |
221 | * Clears a previously set proporty and specifies that it should be | |
222 | * inherited from it's parent. | |
223 | * | |
224 | * @param string $property Name of the property to inherit. | |
225 | * @return void | |
c03e412f NB |
226 | * @access public |
227 | */ | |
228 | public function inherit($property) { | |
229 | $cmd = "zfs inherit " . $property . " " . $this->name . " 2>&1"; | |
b7cf97c0 NB |
230 | $this->exec($cmd,$out,$res); |
231 | $this->updateProperty($property); | |
232 | } | |
233 | ||
dcac32d6 NB |
234 | /** |
235 | * Upgrades the Dataset to latest filesystem version | |
236 | * | |
237 | * @return void | |
238 | * @access public | |
239 | */ | |
240 | public function upgrade() { | |
241 | $cmd = "zfs upgrade " . $this->name . " 2>&1"; | |
242 | $this->exec($cmd,$out,$res); | |
243 | } | |
244 | ||
990e1baf NB |
245 | /** |
246 | * Mount the Dataset | |
247 | * | |
248 | * @return void | |
249 | * @access public | |
250 | */ | |
251 | public function mount() { | |
252 | $cmd = "zfs mount " . $this->name . " 2>&1"; | |
253 | $this->exec($cmd,$out,$res); | |
254 | $this->updateProperty("mounted"); | |
255 | } | |
256 | ||
257 | /** | |
258 | * Unmount the Dataset | |
259 | * | |
260 | * @return void | |
261 | * @access public | |
262 | */ | |
263 | public function unmount() { | |
264 | $cmd = "zfs unmount " . $this->name . " 2>&1"; | |
265 | $this->exec($cmd,$out,$res); | |
266 | $this->updateProperty("mounted"); | |
267 | } | |
268 | ||
ddcd4558 NB |
269 | /** |
270 | * Creates a Snapshot and adds it to the existing list of snapshots associated | |
271 | * with the Dataset. | |
272 | * | |
273 | * @param string $snap_name Name of the Snapshot to create. | |
274 | * @param array $properties Optional array of properties to set on Snapshot | |
275 | * @return void | |
276 | * @access public | |
277 | */ | |
278 | public function addSnapshot($snap_name, array $properties = null) { | |
279 | $snap = new OMVModuleZFSSnapshot($snap_name); | |
280 | $snap->create($properties); | |
281 | $this->snapshots[$snap_name] = $snap; | |
282 | } | |
283 | ||
284 | /** | |
285 | * Destroys a Snapshot on commandline and removes it from the Dataset. | |
286 | * | |
287 | * @param string $snap_name Name of the Snapshot to delete. | |
288 | * @return void | |
289 | * @access public | |
290 | */ | |
291 | public function deleteSnapshot($snap_name) { | |
292 | $this->snapshots[$snap_name]->destroy(); | |
293 | unset($this->snapshots[$snap_name]); | |
294 | } | |
990e1baf | 295 | |
e078be88 NB |
296 | /** |
297 | * Check if the Dataset is a clone or not. | |
298 | * | |
299 | * @return bool | |
300 | * @access public | |
301 | */ | |
302 | public function isClone() { | |
303 | $origin = $this->getProperty("origin"); | |
304 | if (strlen($origin) > 0) { | |
305 | return true; | |
306 | } else { | |
307 | return false; | |
308 | } | |
309 | } | |
310 | ||
311 | /** | |
312 | * Get the origin of the Dataset if it's a clone. | |
313 | * | |
314 | * @return string The name of the origin if it exists. Otherwise an empty string. | |
315 | * @access public | |
316 | */ | |
317 | public function getOrigin() { | |
318 | if ($this->isClone()) { | |
319 | return $this->getProperty("origin"); | |
320 | } else { | |
321 | return ""; | |
322 | } | |
323 | } | |
324 | ||
b7cf97c0 NB |
325 | /** |
326 | * Helper function to execute a command and throw an exception on error | |
327 | * (requires stderr redirected to stdout for proper exception message). | |
328 | * | |
329 | * @param string $cmd Command to execute | |
330 | * @param array &$out If provided will contain output in an array | |
331 | * @param int &$res If provided will contain Exit status of the command | |
332 | * @return string Last line of output when executing the command | |
333 | * @throws OMVModuleZFSException | |
334 | * @access private | |
335 | */ | |
336 | private function exec($cmd, &$out = null, &$res = null) { | |
337 | $tmp = OMVUtil::exec($cmd, $out, $res); | |
c03e412f NB |
338 | if ($res) { |
339 | throw new OMVModuleZFSException(implode("\n", $out)); | |
340 | } | |
b7cf97c0 | 341 | return $tmp; |
c03e412f NB |
342 | } |
343 | ||
f891182f MR |
344 | } |
345 | ||
346 | ?> |