]>
Commit | Line | Data |
---|---|---|
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 | |
43 | * | |
44 | * @param string $name Name of the new Dataset | |
45 | * @param array $properties An associative array with properties to set when creating the Dataset | |
46 | * @param bool $create Should the Dataset be created on commandline? Defaults to false. | |
47 | * | |
48 | */ | |
49 | public function __construct($name, array $properties = null, $create = false) { | |
50 | if ($create) { | |
51 | $cmd = "zfs create -p " . $name . " 2>&1"; | |
52 | $this->exec($cmd,$out,$res); | |
53 | } | |
54 | $this->name = $name; | |
55 | $this->updateAllProperties(); | |
56 | $this->setProperties($properties); | |
57 | $this->mountPoint = $this->properties["mountpoint"]["value"]; | |
58 | } | |
59 | ||
60 | /** | |
61 | * Return name of the Dataset | |
62 | * | |
63 | * @return string $name | |
64 | * @access public | |
65 | */ | |
66 | public function getName() { | |
67 | return $this->name; | |
68 | } | |
69 | ||
70 | /** | |
71 | * Get the mountpoint of the Dataset | |
72 | * | |
73 | * @return string $mountPoint | |
74 | * @access public | |
75 | */ | |
76 | public function getMountPoint() { | |
77 | return $this->mountPoint; | |
78 | } | |
79 | ||
80 | /** | |
81 | * Get a single property value associated with the Dataset | |
82 | * | |
83 | * @param string $property Name of the property to fetch | |
84 | * @return array The returned array key 0=property value and key 1=property source. | |
85 | * @access public | |
86 | */ | |
87 | public function getProperty($property) { | |
88 | return $this->properties["$property"]; | |
89 | } | |
90 | ||
91 | /** | |
92 | * Get an associative array of all properties associated with the Dataset. | |
93 | * | |
94 | * @return array $properties Each entry is an array where key 0=property value and key | |
95 | * 1=property source. | |
96 | * | |
97 | * @access public | |
98 | */ | |
99 | public function getProperties() { | |
100 | return $this->properties; | |
101 | } | |
102 | ||
103 | /** | |
104 | * Sets a number of Dataset properties. If a property is already set it will be updated with the new value. | |
105 | * | |
106 | * @param array $properties An associative array with properties to set | |
107 | * @return void | |
108 | * @access public | |
109 | */ | |
110 | public function setProperties($properties) { | |
111 | foreach ($properties as $newpropertyk => $newpropertyv) { | |
112 | $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name; | |
113 | $this->exec($cmd,$out,$res); | |
114 | $this->updateProperty($newpropertyk); | |
115 | } | |
116 | } | |
117 | ||
118 | /** | |
119 | * Get all Dataset properties from commandline and update object properties attribute | |
120 | * | |
121 | * @return void | |
122 | * @access private | |
123 | */ | |
124 | private function updateAllProperties() { | |
125 | $cmd = "zfs get -H all " . $this->name; | |
126 | $this->exec($cmd,$out,$res); | |
127 | unset($this->properties); | |
128 | foreach ($out as $line) { | |
129 | $tmpary = preg_split('/\t+/', $line); | |
130 | $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]); | |
131 | } | |
132 | } | |
133 | ||
134 | /** | |
135 | * Get single Datset property from commandline and update object property attribute | |
136 | * | |
137 | * @param string $property Name of the property to update | |
138 | * @return void | |
139 | * @access private | |
140 | */ | |
141 | private function updateProperty($property) { | |
142 | $cmd = "zfs get -H " . $property . " " . $this->name; | |
143 | $this->exec($cmd,$out,$res); | |
144 | $tmpary = preg_split('/\t+/', $out[0]); | |
145 | $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]); | |
146 | } | |
147 | ||
148 | /** | |
149 | * Destroy the Dataset. | |
150 | * | |
151 | * @return void | |
152 | * @access public | |
153 | */ | |
154 | public function destroy() { | |
155 | $cmd = "zfs destroy " . $this->name; | |
156 | $this->exec($cmd,$out,$res); | |
157 | } | |
158 | ||
159 | /** | |
160 | * Renames a Dataset | |
161 | * | |
162 | * @param string $newname New name of the Dataset | |
163 | * @return void | |
164 | * @access public | |
165 | */ | |
166 | public function rename($newname) { | |
167 | $cmd = "zfs rename -p " . $this->name . " " . $newname . " 2>&1"; | |
168 | $this->exec($cmd,$out,$res); | |
169 | $this->name = $newname; | |
170 | } | |
171 | ||
172 | /** | |
173 | * Clears a previously set proporty and specifies that it should be | |
174 | * inherited from it's parent. | |
175 | * | |
176 | * @param string $property Name of the property to inherit. | |
177 | * @return void | |
178 | * @access public | |
179 | */ | |
180 | public function inherit($property) { | |
181 | $cmd = "zfs inherit " . $property . " " . $this->name . " 2>&1"; | |
182 | $this->exec($cmd,$out,$res); | |
183 | $this->updateProperty($property); | |
184 | } | |
185 | ||
186 | /** | |
187 | * Upgrades the Dataset to latest filesystem version | |
188 | * | |
189 | * @return void | |
190 | * @access public | |
191 | */ | |
192 | public function upgrade() { | |
193 | $cmd = "zfs upgrade " . $this->name . " 2>&1"; | |
194 | $this->exec($cmd,$out,$res); | |
195 | } | |
196 | ||
197 | /** | |
198 | * Mount the Dataset | |
199 | * | |
200 | * @return void | |
201 | * @access public | |
202 | */ | |
203 | public function mount() { | |
204 | $cmd = "zfs mount " . $this->name . " 2>&1"; | |
205 | $this->exec($cmd,$out,$res); | |
206 | $this->updateProperty("mounted"); | |
207 | } | |
208 | ||
209 | /** | |
210 | * Unmount the Dataset | |
211 | * | |
212 | * @return void | |
213 | * @access public | |
214 | */ | |
215 | public function unmount() { | |
216 | $cmd = "zfs unmount " . $this->name . " 2>&1"; | |
217 | $this->exec($cmd,$out,$res); | |
218 | $this->updateProperty("mounted"); | |
219 | } | |
220 | ||
221 | ||
222 | /** | |
223 | * Helper function to execute a command and throw an exception on error | |
224 | * (requires stderr redirected to stdout for proper exception message). | |
225 | * | |
226 | * @param string $cmd Command to execute | |
227 | * @param array &$out If provided will contain output in an array | |
228 | * @param int &$res If provided will contain Exit status of the command | |
229 | * @return string Last line of output when executing the command | |
230 | * @throws OMVModuleZFSException | |
231 | * @access private | |
232 | */ | |
233 | private function exec($cmd, &$out = null, &$res = null) { | |
234 | $tmp = OMVUtil::exec($cmd, $out, $res); | |
235 | if ($res) { | |
236 | throw new OMVModuleZFSException(implode("\n", $out)); | |
237 | } | |
238 | return $tmp; | |
239 | } | |
240 | ||
241 | } | |
242 | ||
243 | ?> |