]>
Commit | Line | Data |
---|---|---|
f891182f | 1 | <?php |
ba6cf545 | 2 | require_once("Exception.php"); |
eb034e47 | 3 | require_once("openmediavault/util.inc"); |
f891182f MR |
4 | |
5 | /** | |
6 | * XXX detailed description | |
7 | * | |
8 | * @author XXX | |
9 | * @version XXX | |
10 | * @copyright XXX | |
11 | */ | |
63617ac2 | 12 | class OMVModuleZFSDataset { |
f891182f MR |
13 | // Attributes |
14 | /** | |
a4056e15 | 15 | * Name of Dataset |
f891182f MR |
16 | * |
17 | * @var string $name | |
18 | * @access private | |
19 | */ | |
a4056e15 | 20 | private $name; |
dcac32d6 NB |
21 | |
22 | /** | |
ba6cf545 | 23 | * Mountpoint of the Dataset |
f891182f MR |
24 | * |
25 | * @var string $mountPoint | |
26 | * @access private | |
27 | */ | |
ba6cf545 | 28 | private $mountPoint; |
f891182f MR |
29 | |
30 | /** | |
60a2cc94 | 31 | * Array with properties assigned to the Dataset |
f891182f | 32 | * |
31fb83f4 | 33 | * @var array $properties |
f891182f MR |
34 | * @access private |
35 | */ | |
31fb83f4 | 36 | private $properties; |
f891182f | 37 | |
ba6cf545 NB |
38 | // Associations |
39 | // Operations | |
f891182f | 40 | |
ba6cf545 | 41 | /** |
6ed033a4 NB |
42 | * Constructor. If the Dataset already exists in the system the object will be updated with all |
43 | * associated properties from commandline. | |
eb034e47 | 44 | * |
ba6cf545 | 45 | * @param string $name Name of the new Dataset |
6ed033a4 NB |
46 | * @return void |
47 | * @access public | |
ba6cf545 | 48 | */ |
6ed033a4 | 49 | public function __construct($name) { |
ba6cf545 | 50 | $this->name = $name; |
6ed033a4 NB |
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 | } | |
ba6cf545 | 61 | } |
f891182f | 62 | |
ba6cf545 NB |
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 | } | |
f891182f | 72 | |
ba6cf545 NB |
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 | /** | |
60a2cc94 NB |
84 | * Get a single property value associated with the Dataset |
85 | * | |
86 | * @param string $property Name of the property to fetch | |
9b922382 | 87 | * @return array The returned array key 0=property value and key 1=property source. |
60a2cc94 NB |
88 | * @access public |
89 | */ | |
90 | public function getProperty($property) { | |
91 | return $this->properties["$property"]; | |
92 | } | |
93 | ||
94 | /** | |
9b922382 NB |
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. | |
ba6cf545 | 99 | * |
ba6cf545 NB |
100 | * @access public |
101 | */ | |
31fb83f4 NB |
102 | public function getProperties() { |
103 | return $this->properties; | |
ba6cf545 NB |
104 | } |
105 | ||
106 | /** | |
0b156fc3 | 107 | * Sets a number of Dataset properties. If a property is already set it will be updated with the new value. |
ba6cf545 | 108 | * |
60a2cc94 | 109 | * @param array $properties An associative array with properties to set |
eb034e47 | 110 | * @return void |
ba6cf545 NB |
111 | * @access public |
112 | */ | |
31fb83f4 | 113 | public function setProperties($properties) { |
60a2cc94 NB |
114 | foreach ($properties as $newpropertyk => $newpropertyv) { |
115 | $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name; | |
b7cf97c0 | 116 | $this->exec($cmd,$out,$res); |
9b922382 | 117 | $this->updateProperty($newpropertyk); |
60a2cc94 | 118 | } |
60a2cc94 NB |
119 | } |
120 | ||
121 | /** | |
122 | * Get all Dataset properties from commandline and update object properties attribute | |
123 | * | |
c03e412f | 124 | * @return void |
60a2cc94 NB |
125 | * @access private |
126 | */ | |
127 | private function updateAllProperties() { | |
128 | $cmd = "zfs get -H all " . $this->name; | |
b7cf97c0 | 129 | $this->exec($cmd,$out,$res); |
60a2cc94 NB |
130 | unset($this->properties); |
131 | foreach ($out as $line) { | |
132 | $tmpary = preg_split('/\t+/', $line); | |
e6831d92 | 133 | $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]); |
9b922382 NB |
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 | |
c03e412f | 141 | * @return void |
9b922382 NB |
142 | * @access private |
143 | */ | |
144 | private function updateProperty($property) { | |
145 | $cmd = "zfs get -H " . $property . " " . $this->name; | |
b7cf97c0 | 146 | $this->exec($cmd,$out,$res); |
9b922382 | 147 | $tmpary = preg_split('/\t+/', $out[0]); |
e6831d92 | 148 | $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]); |
0b156fc3 NB |
149 | } |
150 | ||
6ed033a4 NB |
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 | ||
0b156fc3 NB |
167 | /** |
168 | * Destroy the Dataset. | |
169 | * | |
c03e412f | 170 | * @return void |
60a2cc94 | 171 | * @access public |
0b156fc3 NB |
172 | */ |
173 | public function destroy() { | |
174 | $cmd = "zfs destroy " . $this->name; | |
b7cf97c0 | 175 | $this->exec($cmd,$out,$res); |
ba6cf545 | 176 | } |
f891182f | 177 | |
9e15bd05 NB |
178 | /** |
179 | * Renames a Dataset | |
180 | * | |
181 | * @param string $newname New name of the Dataset | |
c03e412f | 182 | * @return void |
9e15bd05 NB |
183 | * @access public |
184 | */ | |
185 | public function rename($newname) { | |
186 | $cmd = "zfs rename -p " . $this->name . " " . $newname . " 2>&1"; | |
b7cf97c0 | 187 | $this->exec($cmd,$out,$res); |
9e15bd05 NB |
188 | $this->name = $newname; |
189 | } | |
190 | ||
c03e412f NB |
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 | |
c03e412f NB |
197 | * @access public |
198 | */ | |
199 | public function inherit($property) { | |
200 | $cmd = "zfs inherit " . $property . " " . $this->name . " 2>&1"; | |
b7cf97c0 NB |
201 | $this->exec($cmd,$out,$res); |
202 | $this->updateProperty($property); | |
203 | } | |
204 | ||
dcac32d6 NB |
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 | ||
990e1baf NB |
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 | ||
b7cf97c0 NB |
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); | |
c03e412f NB |
254 | if ($res) { |
255 | throw new OMVModuleZFSException(implode("\n", $out)); | |
256 | } | |
b7cf97c0 | 257 | return $tmp; |
c03e412f NB |
258 | } |
259 | ||
f891182f MR |
260 | } |
261 | ||
262 | ?> |