]> git.datanom.net - omvzfs.git/blob - src/Dataset.php
Extracted code from constructor to new method called create.
[omvzfs.git] / src / Dataset.php
1 <?php
2 require_once("Exception.php");
3 require_once("openmediavault/util.inc");
4
5 /**
6 * XXX detailed description
7 *
8 * @author XXX
9 * @version XXX
10 * @copyright XXX
11 */
12 class OMVModuleZFSDataset {
13 // Attributes
14 /**
15 * Name of Dataset
16 *
17 * @var string $name
18 * @access private
19 */
20 private $name;
21
22 /**
23 * Mountpoint of the Dataset
24 *
25 * @var string $mountPoint
26 * @access private
27 */
28 private $mountPoint;
29
30 /**
31 * Array with properties assigned to the Dataset
32 *
33 * @var array $properties
34 * @access private
35 */
36 private $properties;
37
38 // Associations
39 // Operations
40
41 /**
42 * Constructor. If the Dataset already exists in the system the object will be updated with all
43 * associated properties from commandline.
44 *
45 * @param string $name Name of the new Dataset
46 * @return void
47 * @access public
48 */
49 public function __construct($name) {
50 $this->name = $name;
51 $qname = preg_quote($name, '/');
52 $cmd = "zfs list -H";
53 $this->exec($cmd, $out, $res);
54 foreach ($out as $line) {
55 if (preg_match('/^' . $qname . '\t.*$/', $line)) {
56 $this->updateAllProperties();
57 $this->mountPoint = $this->properties["mountpoint"]["value"];
58 continue;
59 }
60 }
61 }
62
63 /**
64 * Return name of the Dataset
65 *
66 * @return string $name
67 * @access public
68 */
69 public function getName() {
70 return $this->name;
71 }
72
73 /**
74 * Get the mountpoint of the Dataset
75 *
76 * @return string $mountPoint
77 * @access public
78 */
79 public function getMountPoint() {
80 return $this->mountPoint;
81 }
82
83 /**
84 * Get a single property value associated with the Dataset
85 *
86 * @param string $property Name of the property to fetch
87 * @return array The returned array key 0=property value and key 1=property source.
88 * @access public
89 */
90 public function getProperty($property) {
91 return $this->properties["$property"];
92 }
93
94 /**
95 * Get an associative array of all properties associated with the Dataset.
96 *
97 * @return array $properties Each entry is an array where key 0=property value and key
98 * 1=property source.
99 *
100 * @access public
101 */
102 public function getProperties() {
103 return $this->properties;
104 }
105
106 /**
107 * Sets a number of Dataset properties. If a property is already set it will be updated with the new value.
108 *
109 * @param array $properties An associative array with properties to set
110 * @return void
111 * @access public
112 */
113 public function setProperties($properties) {
114 foreach ($properties as $newpropertyk => $newpropertyv) {
115 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name;
116 $this->exec($cmd,$out,$res);
117 $this->updateProperty($newpropertyk);
118 }
119 }
120
121 /**
122 * Get all Dataset properties from commandline and update object properties attribute
123 *
124 * @return void
125 * @access private
126 */
127 private function updateAllProperties() {
128 $cmd = "zfs get -H all " . $this->name;
129 $this->exec($cmd,$out,$res);
130 unset($this->properties);
131 foreach ($out as $line) {
132 $tmpary = preg_split('/\t+/', $line);
133 $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
134 }
135 }
136
137 /**
138 * Get single Datset property from commandline and update object property attribute
139 *
140 * @param string $property Name of the property to update
141 * @return void
142 * @access private
143 */
144 private function updateProperty($property) {
145 $cmd = "zfs get -H " . $property . " " . $this->name;
146 $this->exec($cmd,$out,$res);
147 $tmpary = preg_split('/\t+/', $out[0]);
148 $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
149 }
150
151 /**
152 * Craete a Dataset on commandline. Optionally provide a number of properties to set.
153 *
154 * @param array $properties Properties to set when creating the dataset.
155 * @return void
156 * @access public
157 */
158 public function create(array $properties = null) {
159 $cmd = "zfs create -p " . $this->name . " 2>&1";
160 $this->exec($cmd,$out,$res);
161 $this->updateAllProperties();
162 $this->setProperties($properties);
163 $this->mountPoint = $this->properties["mountpoint"]["value"];
164 }
165
166
167 /**
168 * Destroy the Dataset.
169 *
170 * @return void
171 * @access public
172 */
173 public function destroy() {
174 $cmd = "zfs destroy " . $this->name;
175 $this->exec($cmd,$out,$res);
176 }
177
178 /**
179 * Renames a Dataset
180 *
181 * @param string $newname New name of the Dataset
182 * @return void
183 * @access public
184 */
185 public function rename($newname) {
186 $cmd = "zfs rename -p " . $this->name . " " . $newname . " 2>&1";
187 $this->exec($cmd,$out,$res);
188 $this->name = $newname;
189 }
190
191 /**
192 * Clears a previously set proporty and specifies that it should be
193 * inherited from it's parent.
194 *
195 * @param string $property Name of the property to inherit.
196 * @return void
197 * @access public
198 */
199 public function inherit($property) {
200 $cmd = "zfs inherit " . $property . " " . $this->name . " 2>&1";
201 $this->exec($cmd,$out,$res);
202 $this->updateProperty($property);
203 }
204
205 /**
206 * Upgrades the Dataset to latest filesystem version
207 *
208 * @return void
209 * @access public
210 */
211 public function upgrade() {
212 $cmd = "zfs upgrade " . $this->name . " 2>&1";
213 $this->exec($cmd,$out,$res);
214 }
215
216 /**
217 * Mount the Dataset
218 *
219 * @return void
220 * @access public
221 */
222 public function mount() {
223 $cmd = "zfs mount " . $this->name . " 2>&1";
224 $this->exec($cmd,$out,$res);
225 $this->updateProperty("mounted");
226 }
227
228 /**
229 * Unmount the Dataset
230 *
231 * @return void
232 * @access public
233 */
234 public function unmount() {
235 $cmd = "zfs unmount " . $this->name . " 2>&1";
236 $this->exec($cmd,$out,$res);
237 $this->updateProperty("mounted");
238 }
239
240
241 /**
242 * Helper function to execute a command and throw an exception on error
243 * (requires stderr redirected to stdout for proper exception message).
244 *
245 * @param string $cmd Command to execute
246 * @param array &$out If provided will contain output in an array
247 * @param int &$res If provided will contain Exit status of the command
248 * @return string Last line of output when executing the command
249 * @throws OMVModuleZFSException
250 * @access private
251 */
252 private function exec($cmd, &$out = null, &$res = null) {
253 $tmp = OMVUtil::exec($cmd, $out, $res);
254 if ($res) {
255 throw new OMVModuleZFSException(implode("\n", $out));
256 }
257 return $tmp;
258 }
259
260 }
261
262 ?>
This page took 0.133895 seconds and 6 git commands to generate.