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