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