]> git.datanom.net - omvzfs.git/blob - src/Zvol.php
Added destroy method to Zvol class.
[omvzfs.git] / src / Zvol.php
1 <?php
2 require_once("Exception.php");
3 require_once("openmediavault/util.inc");
4
5 /**
6 * XXX detailed description
7 *
8 * @author XXX
9 * @version XXX
10 * @copyright XXX
11 */
12 class OMVModuleZFSZvol {
13 // Attributes
14
15 /**
16 * Name of Zvol
17 *
18 * @var string $name
19 * @access private
20 */
21 private $name;
22
23 /**
24 * Size of Zvol
25 *
26 * @var string $size
27 * @access private
28 */
29 private $size;
30
31 /**
32 * Array with properties assigned to the Zvol
33 *
34 * @var array $properties
35 * @access private
36 */
37 private $properties;
38
39 // Associations
40 // Operations
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, '/');
53 $cmd = "zfs list -H -t volume";
54 $this->exec($cmd, $out, $res);
55 foreach ($out as $line) {
56 if (preg_match('/^' . $qname . '\t.*$/', $line)) {
57 $this->updateAllProperties();
58 $this->size = $this->properties["volsize"]["value"];
59 continue;
60 }
61 }
62 }
63
64 /**
65 * Return name of the Zvol
66 *
67 * @return string $name
68 * @access public
69 */
70 public function getName() {
71 return $this->name;
72 }
73
74 /**
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>.
80 * @access public
81 */
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;
95 }
96
97 /**
98 * Get the total size of the Zvol
99 *
100 * @return string $size
101 * @access public
102 */
103 public function getSize() {
104 return $this->size;
105 }
106
107 /**
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
112 * @access public
113 */
114 public function setProperties($properties) {
115 foreach ($properties as $newpropertyk => $newpropertyv) {
116 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name;
117 $this->exec($cmd,$out,$res);
118 $this->updateProperty($newpropertyk);
119 }
120 }
121
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() {
129 $cmd = "zfs get -H all " . $this->name;
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
138 /**
139 * Create a Zvol on commandline. Optionally provide a number of properties to set.
140 *
141 * @param string $size Size of the Zvol that should be created
142 * @param array $properties Properties to set when creatiing the dataset.
143 * @param boolean $sparse Defines if a sparse volume should be created.
144 * @return void
145 * @access public
146 */
147 public function create($size, array $properties = null, $sparse = null) {
148 $cmd = "zfs create -p ";
149 if (isset($sparse) && $sparse == true) {
150 $cmd .= "-s ";
151 }
152 $cmd .= "-V " . $size . " " . $this->name . " 2>&1";
153 $this->exec($cmd,$out,$res);
154 $this->updateAllProperties();
155 $this->setProperties($properties);
156 $this->size = $this->properties["volsize"]["value"];
157 }
158
159 /**
160 * Destroy the Zvol.
161 *
162 * @return void
163 * @access public
164 */
165 public function destroy() {
166 $cmd = "zfs destroy " . $this->name;
167 $this->exec($cmd,$out,$res);
168 }
169
170 /**
171 * Helper function to execute a command and throw an exception on error
172 * (requires stderr redirected to stdout for proper exception message).
173 *
174 * @param string $cmd Command to execute
175 * @param array &$out If provided will contain output in an array
176 * @param int &$res If provided will contain Exit status of the command
177 * @return string Last line of output when executing the command
178 * @throws OMVModuleZFSException
179 * @access private
180 */
181 private function exec($cmd, &$out = null, &$res = null) {
182 $tmp = OMVUtil::exec($cmd, $out, $res);
183 if ($res) {
184 throw new OMVModuleZFSException(implode("\n", $out));
185 }
186 return $tmp;
187 }
188 }
189
190 ?>
This page took 0.074013 seconds and 6 git commands to generate.