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