]> git.datanom.net - omvzfs.git/blame - src/Dataset.php
Added inherit method to Dataset
[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;
f891182f 21 /**
ba6cf545 22 * Mountpoint of the Dataset
f891182f
MR
23 *
24 * @var string $mountPoint
25 * @access private
26 */
ba6cf545 27 private $mountPoint;
f891182f
MR
28
29 /**
60a2cc94 30 * Array with properties assigned to the Dataset
f891182f 31 *
31fb83f4 32 * @var array $properties
f891182f
MR
33 * @access private
34 */
31fb83f4 35 private $properties;
f891182f 36
ba6cf545
NB
37 // Associations
38 // Operations
f891182f 39
ba6cf545
NB
40 /**
41 * Constructor
eb034e47 42 *
ba6cf545 43 * @param string $name Name of the new Dataset
60a2cc94 44 * @param array $properties An associative array with properties to set when creating the Dataset
0b156fc3 45 * @throws OMVModuleZFSException
ba6cf545
NB
46 *
47 */
31fb83f4 48 public function __construct($name, array $properties = null) {
9e15bd05 49 $cmd = "zfs create -p " . $name . " 2>&1";
eb034e47 50 OMVUtil::exec($cmd,$out,$res);
31fb83f4 51 if ($res) {
ba6cf545
NB
52 throw new OMVModuleZFSException(implode("\n", $out));
53 }
ba6cf545 54 $this->name = $name;
9b922382 55 $this->updateAllProperties();
60a2cc94 56 $this->setProperties($properties);
9b922382 57 $this->mountPoint = $this->properties["mountpoint"][0];
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;
eb034e47 113 OMVUtil::exec($cmd,$out,$res);
31fb83f4 114 if ($res) {
0b156fc3
NB
115 throw new OMVModuleZFSException(implode("\n", $out));
116 }
9b922382 117 $this->updateProperty($newpropertyk);
60a2cc94 118 }
60a2cc94
NB
119 }
120
121 /**
122 * Get all Dataset properties from commandline and update object properties attribute
123 *
c03e412f 124 * @return void
60a2cc94
NB
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);
9b922382
NB
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
c03e412f 145 * @return void
9b922382
NB
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));
0b156fc3 154 }
9b922382
NB
155 $tmpary = preg_split('/\t+/', $out[0]);
156 $this->properties["$tmpary[1]"] = array($tmpary[2], $tmpary[3]);
0b156fc3
NB
157 }
158
159 /**
160 * Destroy the Dataset.
161 *
c03e412f 162 * @return void
60a2cc94
NB
163 * @throws OMVModuleZFSException
164 * @access public
0b156fc3
NB
165 */
166 public function destroy() {
167 $cmd = "zfs destroy " . $this->name;
eb034e47 168 OMVUtil::exec($cmd,$out,$res);
60a2cc94 169 if ($res) {
0b156fc3
NB
170 throw new OMVModuleZFSException(implode("\n", $out));
171 }
ba6cf545 172 }
f891182f 173
9e15bd05
NB
174 /**
175 * Renames a Dataset
176 *
177 * @param string $newname New name of the Dataset
c03e412f 178 * @return void
9e15bd05
NB
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
c03e412f
NB
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
f891182f
MR
209}
210
211?>
This page took 0.072283 seconds and 5 git commands to generate.