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