]> git.datanom.net - omvzfs.git/blob - src/Dataset.php
2ba1bede28e5f4af83fd2077eeb1c2632e243d38
[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 * Mountpoint of the Dataset
23 *
24 * @var string $mountPoint
25 * @access private
26 */
27 private $mountPoint;
28
29 /**
30 * Array with properties assigned to the Dataset
31 *
32 * @var array $properties
33 * @access private
34 */
35 private $properties;
36
37 // Associations
38 // Operations
39
40 /**
41 * Constructor
42 *
43 * @param string $name Name of the new Dataset
44 * @param array $properties An associative array with properties to set when creating the Dataset
45 * @throws OMVModuleZFSException
46 *
47 */
48 public function __construct($name, array $properties = null) {
49 $cmd = "zfs create -p " . $name . " 2>&1";
50 OMVUtil::exec($cmd,$out,$res);
51 if ($res) {
52 throw new OMVModuleZFSException(implode("\n", $out));
53 }
54 $this->name = $name;
55 $this->updateAllProperties();
56 $this->setProperties($properties);
57 $this->mountPoint = $this->properties["mountpoint"][0];
58 }
59
60 /**
61 * Return name of the Dataset
62 *
63 * @return string $name
64 * @access public
65 */
66 public function getName() {
67 return $this->name;
68 }
69
70 /**
71 * Get the mountpoint of the Dataset
72 *
73 * @return string $mountPoint
74 * @access public
75 */
76 public function getMountPoint() {
77 return $this->mountPoint;
78 }
79
80 /**
81 * Get a single property value associated with the Dataset
82 *
83 * @param string $property Name of the property to fetch
84 * @return array The returned array key 0=property value and key 1=property source.
85 * @access public
86 */
87 public function getProperty($property) {
88 return $this->properties["$property"];
89 }
90
91 /**
92 * Get an associative array of all properties associated with the Dataset.
93 *
94 * @return array $properties Each entry is an array where key 0=property value and key
95 * 1=property source.
96 *
97 * @access public
98 */
99 public function getProperties() {
100 return $this->properties;
101 }
102
103 /**
104 * Sets a number of Dataset properties. If a property is already set it will be updated with the new value.
105 *
106 * @param array $properties An associative array with properties to set
107 * @return void
108 * @access public
109 */
110 public function setProperties($properties) {
111 foreach ($properties as $newpropertyk => $newpropertyv) {
112 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name;
113 OMVUtil::exec($cmd,$out,$res);
114 if ($res) {
115 throw new OMVModuleZFSException(implode("\n", $out));
116 }
117 $this->updateProperty($newpropertyk);
118 }
119 }
120
121 /**
122 * Get all Dataset properties from commandline and update object properties attribute
123 *
124 * @return void
125 * @throws OMVModuleZFSException
126 * @access private
127 */
128 private function updateAllProperties() {
129 $cmd = "zfs get -H all " . $this->name;
130 OMVUtil::exec($cmd,$out,$res);
131 if ($res) {
132 throw new OMVModuleZFSException(implode("\n", $out));
133 }
134 unset($this->properties);
135 foreach ($out as $line) {
136 $tmpary = preg_split('/\t+/', $line);
137 $this->properties["$tmpary[1]"] = array($tmpary[2], $tmpary[3]);
138 }
139 }
140
141 /**
142 * Get single Datset property from commandline and update object property attribute
143 *
144 * @param string $property Name of the property to update
145 * @return void
146 * @throws OMVModuleZFSException
147 * @access private
148 */
149 private function updateProperty($property) {
150 $cmd = "zfs get -H " . $property . " " . $this->name;
151 OMVUtil::exec($cmd,$out,$res);
152 if ($res) {
153 throw new OMVModuleZFSException(implode("\n", $out));
154 }
155 $tmpary = preg_split('/\t+/', $out[0]);
156 $this->properties["$tmpary[1]"] = array($tmpary[2], $tmpary[3]);
157 }
158
159 /**
160 * Destroy the Dataset.
161 *
162 * @return void
163 * @throws OMVModuleZFSException
164 * @access public
165 */
166 public function destroy() {
167 $cmd = "zfs destroy " . $this->name;
168 OMVUtil::exec($cmd,$out,$res);
169 if ($res) {
170 throw new OMVModuleZFSException(implode("\n", $out));
171 }
172 }
173
174 /**
175 * Renames a Dataset
176 *
177 * @param string $newname New name of the Dataset
178 * @return void
179 * @throws OMVModuleZFSException
180 * @access public
181 */
182 public function rename($newname) {
183 $cmd = "zfs rename -p " . $this->name . " " . $newname . " 2>&1";
184 OMVUtil::exec($cmd,$out,$res);
185 if ($res) {
186 throw new OMVModuleZFSException(implode("\n", $out));
187 }
188 $this->name = $newname;
189 }
190
191 /**
192 * Clears a previously set proporty and specifies that it should be
193 * inherited from it's parent.
194 *
195 * @param string $property Name of the property to inherit.
196 * @return void
197 * @throws OMVModuleZFSException
198 * @access public
199 */
200 public function inherit($property) {
201 $cmd = "zfs inherit " . $property . " " . $this->name . " 2>&1";
202 OMVUtil::exec($cmd,$out,$res);
203 if ($res) {
204 throw new OMVModuleZFSException(implode("\n", $out));
205 }
206 $this->updateProperty($property);
207 }
208
209 }
210
211 ?>
This page took 0.072677 seconds and 4 git commands to generate.