]> git.datanom.net - omvzfs.git/blob - src/Dataset.php
d6796e59217a55127df9d9aa6fa69095219b2f0b
[omvzfs.git] / src / Dataset.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 OMVModuleZFSDataset {
13 // Attributes
14 /**
15 * Name of Dataset
16 *
17 * @var string $name
18 * @access private
19 */
20 private $name;
21
22 /**
23 * Mountpoint of the Dataset
24 *
25 * @var string $mountPoint
26 * @access private
27 */
28 private $mountPoint;
29
30 /**
31 * Array with properties assigned to the Dataset
32 *
33 * @var array $properties
34 * @access private
35 */
36 private $properties;
37
38 // Associations
39 // Operations
40
41 /**
42 * Constructor
43 *
44 * @param string $name Name of the new Dataset
45 * @param array $properties An associative array with properties to set when creating the Dataset
46 *
47 */
48 public function __construct($name, array $properties = null) {
49 $cmd = "zfs create -p " . $name . " 2>&1";
50 $this->exec($cmd,$out,$res);
51 $this->name = $name;
52 $this->updateAllProperties();
53 $this->setProperties($properties);
54 $this->mountPoint = $this->properties["mountpoint"][0];
55 }
56
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 }
66
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 /**
78 * Get a single property value associated with the Dataset
79 *
80 * @param string $property Name of the property to fetch
81 * @return array The returned array key 0=property value and key 1=property source.
82 * @access public
83 */
84 public function getProperty($property) {
85 return $this->properties["$property"];
86 }
87
88 /**
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.
93 *
94 * @access public
95 */
96 public function getProperties() {
97 return $this->properties;
98 }
99
100 /**
101 * Sets a number of Dataset properties. If a property is already set it will be updated with the new value.
102 *
103 * @param array $properties An associative array with properties to set
104 * @return void
105 * @access public
106 */
107 public function setProperties($properties) {
108 foreach ($properties as $newpropertyk => $newpropertyv) {
109 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name;
110 $this->exec($cmd,$out,$res);
111 $this->updateProperty($newpropertyk);
112 }
113 }
114
115 /**
116 * Get all Dataset properties from commandline and update object properties attribute
117 *
118 * @return void
119 * @access private
120 */
121 private function updateAllProperties() {
122 $cmd = "zfs get -H all " . $this->name;
123 $this->exec($cmd,$out,$res);
124 unset($this->properties);
125 foreach ($out as $line) {
126 $tmpary = preg_split('/\t+/', $line);
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
135 * @return void
136 * @access private
137 */
138 private function updateProperty($property) {
139 $cmd = "zfs get -H " . $property . " " . $this->name;
140 $this->exec($cmd,$out,$res);
141 $tmpary = preg_split('/\t+/', $out[0]);
142 $this->properties["$tmpary[1]"] = array($tmpary[2], $tmpary[3]);
143 }
144
145 /**
146 * Destroy the Dataset.
147 *
148 * @return void
149 * @access public
150 */
151 public function destroy() {
152 $cmd = "zfs destroy " . $this->name;
153 $this->exec($cmd,$out,$res);
154 }
155
156 /**
157 * Renames a Dataset
158 *
159 * @param string $newname New name of the Dataset
160 * @return void
161 * @access public
162 */
163 public function rename($newname) {
164 $cmd = "zfs rename -p " . $this->name . " " . $newname . " 2>&1";
165 $this->exec($cmd,$out,$res);
166 $this->name = $newname;
167 }
168
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
175 * @access public
176 */
177 public function inherit($property) {
178 $cmd = "zfs inherit " . $property . " " . $this->name . " 2>&1";
179 $this->exec($cmd,$out,$res);
180 $this->updateProperty($property);
181 }
182
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
194 /**
195 * Helper function to execute a command and throw an exception on error
196 * (requires stderr redirected to stdout for proper exception message).
197 *
198 * @param string $cmd Command to execute
199 * @param array &$out If provided will contain output in an array
200 * @param int &$res If provided will contain Exit status of the command
201 * @return string Last line of output when executing the command
202 * @throws OMVModuleZFSException
203 * @access private
204 */
205 private function exec($cmd, &$out = null, &$res = null) {
206 $tmp = OMVUtil::exec($cmd, $out, $res);
207 if ($res) {
208 throw new OMVModuleZFSException(implode("\n", $out));
209 }
210 return $tmp;
211 }
212
213 }
214
215 ?>
This page took 0.074441 seconds and 4 git commands to generate.