]>
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 NB |
70 | if ($ds_exists) { |
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 | } | |
946a8c30 | 76 | } |
ba6cf545 | 77 | } |
f891182f | 78 | |
ba6cf545 NB |
79 | /** |
80 | * Return name of the Dataset | |
81 | * | |
82 | * @return string $name | |
83 | * @access public | |
84 | */ | |
85 | public function getName() { | |
86 | return $this->name; | |
87 | } | |
f891182f | 88 | |
ba6cf545 NB |
89 | /** |
90 | * Get the mountpoint of the Dataset | |
91 | * | |
92 | * @return string $mountPoint | |
93 | * @access public | |
94 | */ | |
95 | public function getMountPoint() { | |
96 | return $this->mountPoint; | |
97 | } | |
98 | ||
946a8c30 NB |
99 | /** |
100 | * Get all Snapshots associated with the Dataset | |
101 | * | |
102 | * @return array $snapshots | |
103 | * @access public | |
104 | */ | |
105 | public function getSnapshots() { | |
106 | return $this->snapshots; | |
107 | } | |
108 | ||
ba6cf545 | 109 | /** |
60a2cc94 | 110 | * Get a single property value associated with the Dataset |
43a6a77c | 111 | * |
60a2cc94 | 112 | * @param string $property Name of the property to fetch |
43a6a77c NB |
113 | * @return array The returned array with the property. The property is an associative array with |
114 | * two elements, <value> and <source>. | |
60a2cc94 NB |
115 | * @access public |
116 | */ | |
117 | public function getProperty($property) { | |
118 | return $this->properties["$property"]; | |
119 | } | |
120 | ||
121 | /** | |
43a6a77c NB |
122 | * Get an associative array of all properties associated with the Snapshot |
123 | * | |
124 | * @return array $properties Each entry is an associative array with two elements | |
125 | * <value> and <source> | |
ba6cf545 NB |
126 | * @access public |
127 | */ | |
31fb83f4 NB |
128 | public function getProperties() { |
129 | return $this->properties; | |
ba6cf545 NB |
130 | } |
131 | ||
132 | /** | |
0b156fc3 | 133 | * Sets a number of Dataset properties. If a property is already set it will be updated with the new value. |
ba6cf545 | 134 | * |
60a2cc94 | 135 | * @param array $properties An associative array with properties to set |
eb034e47 | 136 | * @return void |
ba6cf545 NB |
137 | * @access public |
138 | */ | |
31fb83f4 | 139 | public function setProperties($properties) { |
60a2cc94 | 140 | foreach ($properties as $newpropertyk => $newpropertyv) { |
8b1436f9 | 141 | $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name . " 2>&1"; |
b7cf97c0 | 142 | $this->exec($cmd,$out,$res); |
9b922382 | 143 | $this->updateProperty($newpropertyk); |
60a2cc94 | 144 | } |
60a2cc94 NB |
145 | } |
146 | ||
147 | /** | |
148 | * Get all Dataset properties from commandline and update object properties attribute | |
149 | * | |
c03e412f | 150 | * @return void |
60a2cc94 NB |
151 | * @access private |
152 | */ | |
153 | private function updateAllProperties() { | |
8b1436f9 | 154 | $cmd = "zfs get -H all " . $this->name . " 2>&1"; |
b7cf97c0 | 155 | $this->exec($cmd,$out,$res); |
60a2cc94 NB |
156 | unset($this->properties); |
157 | foreach ($out as $line) { | |
158 | $tmpary = preg_split('/\t+/', $line); | |
e6831d92 | 159 | $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]); |
9b922382 NB |
160 | } |
161 | } | |
162 | ||
163 | /** | |
164 | * Get single Datset property from commandline and update object property attribute | |
165 | * | |
166 | * @param string $property Name of the property to update | |
c03e412f | 167 | * @return void |
9b922382 NB |
168 | * @access private |
169 | */ | |
170 | private function updateProperty($property) { | |
8b1436f9 | 171 | $cmd = "zfs get -H " . $property . " " . $this->name . " 2>&1"; |
b7cf97c0 | 172 | $this->exec($cmd,$out,$res); |
9b922382 | 173 | $tmpary = preg_split('/\t+/', $out[0]); |
e6831d92 | 174 | $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]); |
0b156fc3 NB |
175 | } |
176 | ||
6ed033a4 NB |
177 | /** |
178 | * Craete a Dataset on commandline. Optionally provide a number of properties to set. | |
179 | * | |
180 | * @param array $properties Properties to set when creating the dataset. | |
181 | * @return void | |
182 | * @access public | |
183 | */ | |
184 | public function create(array $properties = null) { | |
185 | $cmd = "zfs create -p " . $this->name . " 2>&1"; | |
186 | $this->exec($cmd,$out,$res); | |
187 | $this->updateAllProperties(); | |
188 | $this->setProperties($properties); | |
189 | $this->mountPoint = $this->properties["mountpoint"]["value"]; | |
190 | } | |
191 | ||
0b156fc3 NB |
192 | /** |
193 | * Destroy the Dataset. | |
194 | * | |
c03e412f | 195 | * @return void |
60a2cc94 | 196 | * @access public |
0b156fc3 NB |
197 | */ |
198 | public function destroy() { | |
8b1436f9 | 199 | $cmd = "zfs destroy " . $this->name . " 2>&1"; |
b7cf97c0 | 200 | $this->exec($cmd,$out,$res); |
ba6cf545 | 201 | } |
f891182f | 202 | |
9e15bd05 NB |
203 | /** |
204 | * Renames a Dataset | |
205 | * | |
206 | * @param string $newname New name of the Dataset | |
c03e412f | 207 | * @return void |
9e15bd05 NB |
208 | * @access public |
209 | */ | |
210 | public function rename($newname) { | |
211 | $cmd = "zfs rename -p " . $this->name . " " . $newname . " 2>&1"; | |
b7cf97c0 | 212 | $this->exec($cmd,$out,$res); |
9e15bd05 NB |
213 | $this->name = $newname; |
214 | } | |
215 | ||
c03e412f NB |
216 | /** |
217 | * Clears a previously set proporty and specifies that it should be | |
218 | * inherited from it's parent. | |
219 | * | |
220 | * @param string $property Name of the property to inherit. | |
221 | * @return void | |
c03e412f NB |
222 | * @access public |
223 | */ | |
224 | public function inherit($property) { | |
225 | $cmd = "zfs inherit " . $property . " " . $this->name . " 2>&1"; | |
b7cf97c0 NB |
226 | $this->exec($cmd,$out,$res); |
227 | $this->updateProperty($property); | |
228 | } | |
229 | ||
dcac32d6 NB |
230 | /** |
231 | * Upgrades the Dataset to latest filesystem version | |
232 | * | |
233 | * @return void | |
234 | * @access public | |
235 | */ | |
236 | public function upgrade() { | |
237 | $cmd = "zfs upgrade " . $this->name . " 2>&1"; | |
238 | $this->exec($cmd,$out,$res); | |
239 | } | |
240 | ||
990e1baf NB |
241 | /** |
242 | * Mount the Dataset | |
243 | * | |
244 | * @return void | |
245 | * @access public | |
246 | */ | |
247 | public function mount() { | |
248 | $cmd = "zfs mount " . $this->name . " 2>&1"; | |
249 | $this->exec($cmd,$out,$res); | |
250 | $this->updateProperty("mounted"); | |
251 | } | |
252 | ||
253 | /** | |
254 | * Unmount the Dataset | |
255 | * | |
256 | * @return void | |
257 | * @access public | |
258 | */ | |
259 | public function unmount() { | |
260 | $cmd = "zfs unmount " . $this->name . " 2>&1"; | |
261 | $this->exec($cmd,$out,$res); | |
262 | $this->updateProperty("mounted"); | |
263 | } | |
264 | ||
ddcd4558 NB |
265 | /** |
266 | * Creates a Snapshot and adds it to the existing list of snapshots associated | |
267 | * with the Dataset. | |
268 | * | |
269 | * @param string $snap_name Name of the Snapshot to create. | |
270 | * @param array $properties Optional array of properties to set on Snapshot | |
271 | * @return void | |
272 | * @access public | |
273 | */ | |
274 | public function addSnapshot($snap_name, array $properties = null) { | |
275 | $snap = new OMVModuleZFSSnapshot($snap_name); | |
276 | $snap->create($properties); | |
277 | $this->snapshots[$snap_name] = $snap; | |
278 | } | |
279 | ||
280 | /** | |
281 | * Destroys a Snapshot on commandline and removes it from the Dataset. | |
282 | * | |
283 | * @param string $snap_name Name of the Snapshot to delete. | |
284 | * @return void | |
285 | * @access public | |
286 | */ | |
287 | public function deleteSnapshot($snap_name) { | |
288 | $this->snapshots[$snap_name]->destroy(); | |
289 | unset($this->snapshots[$snap_name]); | |
290 | } | |
990e1baf | 291 | |
b7cf97c0 NB |
292 | /** |
293 | * Helper function to execute a command and throw an exception on error | |
294 | * (requires stderr redirected to stdout for proper exception message). | |
295 | * | |
296 | * @param string $cmd Command to execute | |
297 | * @param array &$out If provided will contain output in an array | |
298 | * @param int &$res If provided will contain Exit status of the command | |
299 | * @return string Last line of output when executing the command | |
300 | * @throws OMVModuleZFSException | |
301 | * @access private | |
302 | */ | |
303 | private function exec($cmd, &$out = null, &$res = null) { | |
304 | $tmp = OMVUtil::exec($cmd, $out, $res); | |
c03e412f NB |
305 | if ($res) { |
306 | throw new OMVModuleZFSException(implode("\n", $out)); | |
307 | } | |
b7cf97c0 | 308 | return $tmp; |
c03e412f NB |
309 | } |
310 | ||
f891182f MR |
311 | } |
312 | ||
313 | ?> |