]> git.datanom.net - omvzfs.git/blob - src/Dataset.php
e1611416a73d8fd00102d3f958881eeae2878118
[omvzfs.git] / src / Dataset.php
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 OMVModuleZFSDataset {
14 // Attributes
15 /**
16 * Name of Dataset
17 *
18 * @var string $name
19 * @access private
20 */
21 private $name;
22
23 /**
24 * Mountpoint of the Dataset
25 *
26 * @var string $mountPoint
27 * @access private
28 */
29 private $mountPoint;
30
31 /**
32 * Array with properties assigned to the Dataset
33 *
34 * @var array $properties
35 * @access private
36 */
37 private $properties;
38
39 /**
40 * Array with Snapshots associated to the Dataset
41 *
42 * @var array $snapshots
43 * @access private
44 */
45 private $snapshots;
46
47 // Associations
48 // Operations
49
50 /**
51 * Constructor. If the Dataset already exists in the system the object will be updated with all
52 * associated properties from commandline.
53 *
54 * @param string $name Name of the new Dataset
55 * @return void
56 * @access public
57 */
58 public function __construct($name) {
59 $this->name = $name;
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 }
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)) {
75 $this->snapshots[] = new OMVModuleZFSSnapshot($res[1]);
76 }
77 }
78 }
79
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 }
89
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
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
110 /**
111 * Get a single property value associated with the Dataset
112 *
113 * @param string $property Name of the property to fetch
114 * @return array The returned array key 0=property value and key 1=property source.
115 * @access public
116 */
117 public function getProperty($property) {
118 return $this->properties["$property"];
119 }
120
121 /**
122 * Get an associative array of all properties associated with the Dataset.
123 *
124 * @return array $properties Each entry is an array where key 0=property value and key
125 * 1=property source.
126 *
127 * @access public
128 */
129 public function getProperties() {
130 return $this->properties;
131 }
132
133 /**
134 * Sets a number of Dataset properties. If a property is already set it will be updated with the new value.
135 *
136 * @param array $properties An associative array with properties to set
137 * @return void
138 * @access public
139 */
140 public function setProperties($properties) {
141 foreach ($properties as $newpropertyk => $newpropertyv) {
142 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name;
143 $this->exec($cmd,$out,$res);
144 $this->updateProperty($newpropertyk);
145 }
146 }
147
148 /**
149 * Get all Dataset properties from commandline and update object properties attribute
150 *
151 * @return void
152 * @access private
153 */
154 private function updateAllProperties() {
155 $cmd = "zfs get -H all " . $this->name;
156 $this->exec($cmd,$out,$res);
157 unset($this->properties);
158 foreach ($out as $line) {
159 $tmpary = preg_split('/\t+/', $line);
160 $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
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
168 * @return void
169 * @access private
170 */
171 private function updateProperty($property) {
172 $cmd = "zfs get -H " . $property . " " . $this->name;
173 $this->exec($cmd,$out,$res);
174 $tmpary = preg_split('/\t+/', $out[0]);
175 $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
176 }
177
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
193
194 /**
195 * Destroy the Dataset.
196 *
197 * @return void
198 * @access public
199 */
200 public function destroy() {
201 $cmd = "zfs destroy " . $this->name;
202 $this->exec($cmd,$out,$res);
203 }
204
205 /**
206 * Renames a Dataset
207 *
208 * @param string $newname New name of the Dataset
209 * @return void
210 * @access public
211 */
212 public function rename($newname) {
213 $cmd = "zfs rename -p " . $this->name . " " . $newname . " 2>&1";
214 $this->exec($cmd,$out,$res);
215 $this->name = $newname;
216 }
217
218 /**
219 * Clears a previously set proporty and specifies that it should be
220 * inherited from it's parent.
221 *
222 * @param string $property Name of the property to inherit.
223 * @return void
224 * @access public
225 */
226 public function inherit($property) {
227 $cmd = "zfs inherit " . $property . " " . $this->name . " 2>&1";
228 $this->exec($cmd,$out,$res);
229 $this->updateProperty($property);
230 }
231
232 /**
233 * Upgrades the Dataset to latest filesystem version
234 *
235 * @return void
236 * @access public
237 */
238 public function upgrade() {
239 $cmd = "zfs upgrade " . $this->name . " 2>&1";
240 $this->exec($cmd,$out,$res);
241 }
242
243 /**
244 * Mount the Dataset
245 *
246 * @return void
247 * @access public
248 */
249 public function mount() {
250 $cmd = "zfs mount " . $this->name . " 2>&1";
251 $this->exec($cmd,$out,$res);
252 $this->updateProperty("mounted");
253 }
254
255 /**
256 * Unmount the Dataset
257 *
258 * @return void
259 * @access public
260 */
261 public function unmount() {
262 $cmd = "zfs unmount " . $this->name . " 2>&1";
263 $this->exec($cmd,$out,$res);
264 $this->updateProperty("mounted");
265 }
266
267
268 /**
269 * Helper function to execute a command and throw an exception on error
270 * (requires stderr redirected to stdout for proper exception message).
271 *
272 * @param string $cmd Command to execute
273 * @param array &$out If provided will contain output in an array
274 * @param int &$res If provided will contain Exit status of the command
275 * @return string Last line of output when executing the command
276 * @throws OMVModuleZFSException
277 * @access private
278 */
279 private function exec($cmd, &$out = null, &$res = null) {
280 $tmp = OMVUtil::exec($cmd, $out, $res);
281 if ($res) {
282 throw new OMVModuleZFSException(implode("\n", $out));
283 }
284 return $tmp;
285 }
286
287 }
288
289 ?>
This page took 0.093938 seconds and 4 git commands to generate.