]> git.datanom.net - omvzfs.git/blame - src/Dataset.php
Made it optional to create Dataset on commandline in constructor. Changed properties...
[omvzfs.git] / src / Dataset.php
CommitLineData
f891182f 1<?php
ba6cf545 2require_once("Exception.php");
eb034e47 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 OMVModuleZFSDataset {
f891182f
MR
13 // Attributes
14 /**
a4056e15 15 * Name of Dataset
f891182f
MR
16 *
17 * @var string $name
18 * @access private
19 */
a4056e15 20 private $name;
dcac32d6
NB
21
22 /**
ba6cf545 23 * Mountpoint of the Dataset
f891182f
MR
24 *
25 * @var string $mountPoint
26 * @access private
27 */
ba6cf545 28 private $mountPoint;
f891182f
MR
29
30 /**
60a2cc94 31 * Array with properties assigned to the Dataset
f891182f 32 *
31fb83f4 33 * @var array $properties
f891182f
MR
34 * @access private
35 */
31fb83f4 36 private $properties;
f891182f 37
ba6cf545
NB
38 // Associations
39 // Operations
f891182f 40
ba6cf545
NB
41 /**
42 * Constructor
eb034e47 43 *
ba6cf545 44 * @param string $name Name of the new Dataset
60a2cc94 45 * @param array $properties An associative array with properties to set when creating the Dataset
e6831d92 46 * @param bool $create Should the Dataset be created on commandline? Defaults to false.
ba6cf545
NB
47 *
48 */
e6831d92
NB
49 public function __construct($name, array $properties = null, $create = false) {
50 if ($create) {
51 $cmd = "zfs create -p " . $name . " 2>&1";
52 $this->exec($cmd,$out,$res);
53 }
ba6cf545 54 $this->name = $name;
9b922382 55 $this->updateAllProperties();
60a2cc94 56 $this->setProperties($properties);
e6831d92 57 $this->mountPoint = $this->properties["mountpoint"]["value"];
ba6cf545 58 }
f891182f 59
ba6cf545
NB
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 }
f891182f 69
ba6cf545
NB
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 /**
60a2cc94
NB
81 * Get a single property value associated with the Dataset
82 *
83 * @param string $property Name of the property to fetch
9b922382 84 * @return array The returned array key 0=property value and key 1=property source.
60a2cc94
NB
85 * @access public
86 */
87 public function getProperty($property) {
88 return $this->properties["$property"];
89 }
90
91 /**
9b922382
NB
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.
ba6cf545 96 *
ba6cf545
NB
97 * @access public
98 */
31fb83f4
NB
99 public function getProperties() {
100 return $this->properties;
ba6cf545
NB
101 }
102
103 /**
0b156fc3 104 * Sets a number of Dataset properties. If a property is already set it will be updated with the new value.
ba6cf545 105 *
60a2cc94 106 * @param array $properties An associative array with properties to set
eb034e47 107 * @return void
ba6cf545
NB
108 * @access public
109 */
31fb83f4 110 public function setProperties($properties) {
60a2cc94
NB
111 foreach ($properties as $newpropertyk => $newpropertyv) {
112 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name;
b7cf97c0 113 $this->exec($cmd,$out,$res);
9b922382 114 $this->updateProperty($newpropertyk);
60a2cc94 115 }
60a2cc94
NB
116 }
117
118 /**
119 * Get all Dataset properties from commandline and update object properties attribute
120 *
c03e412f 121 * @return void
60a2cc94
NB
122 * @access private
123 */
124 private function updateAllProperties() {
125 $cmd = "zfs get -H all " . $this->name;
b7cf97c0 126 $this->exec($cmd,$out,$res);
60a2cc94
NB
127 unset($this->properties);
128 foreach ($out as $line) {
129 $tmpary = preg_split('/\t+/', $line);
e6831d92 130 $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
9b922382
NB
131 }
132 }
133
134 /**
135 * Get single Datset property from commandline and update object property attribute
136 *
137 * @param string $property Name of the property to update
c03e412f 138 * @return void
9b922382
NB
139 * @access private
140 */
141 private function updateProperty($property) {
142 $cmd = "zfs get -H " . $property . " " . $this->name;
b7cf97c0 143 $this->exec($cmd,$out,$res);
9b922382 144 $tmpary = preg_split('/\t+/', $out[0]);
e6831d92 145 $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
0b156fc3
NB
146 }
147
148 /**
149 * Destroy the Dataset.
150 *
c03e412f 151 * @return void
60a2cc94 152 * @access public
0b156fc3
NB
153 */
154 public function destroy() {
155 $cmd = "zfs destroy " . $this->name;
b7cf97c0 156 $this->exec($cmd,$out,$res);
ba6cf545 157 }
f891182f 158
9e15bd05
NB
159 /**
160 * Renames a Dataset
161 *
162 * @param string $newname New name of the Dataset
c03e412f 163 * @return void
9e15bd05
NB
164 * @access public
165 */
166 public function rename($newname) {
167 $cmd = "zfs rename -p " . $this->name . " " . $newname . " 2>&1";
b7cf97c0 168 $this->exec($cmd,$out,$res);
9e15bd05
NB
169 $this->name = $newname;
170 }
171
c03e412f
NB
172 /**
173 * Clears a previously set proporty and specifies that it should be
174 * inherited from it's parent.
175 *
176 * @param string $property Name of the property to inherit.
177 * @return void
c03e412f
NB
178 * @access public
179 */
180 public function inherit($property) {
181 $cmd = "zfs inherit " . $property . " " . $this->name . " 2>&1";
b7cf97c0
NB
182 $this->exec($cmd,$out,$res);
183 $this->updateProperty($property);
184 }
185
dcac32d6
NB
186 /**
187 * Upgrades the Dataset to latest filesystem version
188 *
189 * @return void
190 * @access public
191 */
192 public function upgrade() {
193 $cmd = "zfs upgrade " . $this->name . " 2>&1";
194 $this->exec($cmd,$out,$res);
195 }
196
990e1baf
NB
197 /**
198 * Mount the Dataset
199 *
200 * @return void
201 * @access public
202 */
203 public function mount() {
204 $cmd = "zfs mount " . $this->name . " 2>&1";
205 $this->exec($cmd,$out,$res);
206 $this->updateProperty("mounted");
207 }
208
209 /**
210 * Unmount the Dataset
211 *
212 * @return void
213 * @access public
214 */
215 public function unmount() {
216 $cmd = "zfs unmount " . $this->name . " 2>&1";
217 $this->exec($cmd,$out,$res);
218 $this->updateProperty("mounted");
219 }
220
221
b7cf97c0
NB
222 /**
223 * Helper function to execute a command and throw an exception on error
224 * (requires stderr redirected to stdout for proper exception message).
225 *
226 * @param string $cmd Command to execute
227 * @param array &$out If provided will contain output in an array
228 * @param int &$res If provided will contain Exit status of the command
229 * @return string Last line of output when executing the command
230 * @throws OMVModuleZFSException
231 * @access private
232 */
233 private function exec($cmd, &$out = null, &$res = null) {
234 $tmp = OMVUtil::exec($cmd, $out, $res);
c03e412f
NB
235 if ($res) {
236 throw new OMVModuleZFSException(implode("\n", $out));
237 }
b7cf97c0 238 return $tmp;
c03e412f
NB
239 }
240
f891182f
MR
241}
242
243?>
This page took 0.066881 seconds and 5 git commands to generate.