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