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