]> git.datanom.net - omvzfs.git/blame - src/Dataset.php
Added upgrade method to Dataset class
[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
ba6cf545
NB
46 *
47 */
31fb83f4 48 public function __construct($name, array $properties = null) {
9e15bd05 49 $cmd = "zfs create -p " . $name . " 2>&1";
b7cf97c0 50 $this->exec($cmd,$out,$res);
ba6cf545 51 $this->name = $name;
9b922382 52 $this->updateAllProperties();
60a2cc94 53 $this->setProperties($properties);
9b922382 54 $this->mountPoint = $this->properties["mountpoint"][0];
ba6cf545 55 }
f891182f 56
ba6cf545
NB
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 }
f891182f 66
ba6cf545
NB
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 /**
60a2cc94
NB
78 * Get a single property value associated with the Dataset
79 *
80 * @param string $property Name of the property to fetch
9b922382 81 * @return array The returned array key 0=property value and key 1=property source.
60a2cc94
NB
82 * @access public
83 */
84 public function getProperty($property) {
85 return $this->properties["$property"];
86 }
87
88 /**
9b922382
NB
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.
ba6cf545 93 *
ba6cf545
NB
94 * @access public
95 */
31fb83f4
NB
96 public function getProperties() {
97 return $this->properties;
ba6cf545
NB
98 }
99
100 /**
0b156fc3 101 * Sets a number of Dataset properties. If a property is already set it will be updated with the new value.
ba6cf545 102 *
60a2cc94 103 * @param array $properties An associative array with properties to set
eb034e47 104 * @return void
ba6cf545
NB
105 * @access public
106 */
31fb83f4 107 public function setProperties($properties) {
60a2cc94
NB
108 foreach ($properties as $newpropertyk => $newpropertyv) {
109 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name;
b7cf97c0 110 $this->exec($cmd,$out,$res);
9b922382 111 $this->updateProperty($newpropertyk);
60a2cc94 112 }
60a2cc94
NB
113 }
114
115 /**
116 * Get all Dataset properties from commandline and update object properties attribute
117 *
c03e412f 118 * @return void
60a2cc94
NB
119 * @access private
120 */
121 private function updateAllProperties() {
122 $cmd = "zfs get -H all " . $this->name;
b7cf97c0 123 $this->exec($cmd,$out,$res);
60a2cc94
NB
124 unset($this->properties);
125 foreach ($out as $line) {
126 $tmpary = preg_split('/\t+/', $line);
9b922382
NB
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
c03e412f 135 * @return void
9b922382
NB
136 * @access private
137 */
138 private function updateProperty($property) {
139 $cmd = "zfs get -H " . $property . " " . $this->name;
b7cf97c0 140 $this->exec($cmd,$out,$res);
9b922382
NB
141 $tmpary = preg_split('/\t+/', $out[0]);
142 $this->properties["$tmpary[1]"] = array($tmpary[2], $tmpary[3]);
0b156fc3
NB
143 }
144
145 /**
146 * Destroy the Dataset.
147 *
c03e412f 148 * @return void
60a2cc94 149 * @access public
0b156fc3
NB
150 */
151 public function destroy() {
152 $cmd = "zfs destroy " . $this->name;
b7cf97c0 153 $this->exec($cmd,$out,$res);
ba6cf545 154 }
f891182f 155
9e15bd05
NB
156 /**
157 * Renames a Dataset
158 *
159 * @param string $newname New name of the Dataset
c03e412f 160 * @return void
9e15bd05
NB
161 * @access public
162 */
163 public function rename($newname) {
164 $cmd = "zfs rename -p " . $this->name . " " . $newname . " 2>&1";
b7cf97c0 165 $this->exec($cmd,$out,$res);
9e15bd05
NB
166 $this->name = $newname;
167 }
168
c03e412f
NB
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
c03e412f
NB
175 * @access public
176 */
177 public function inherit($property) {
178 $cmd = "zfs inherit " . $property . " " . $this->name . " 2>&1";
b7cf97c0
NB
179 $this->exec($cmd,$out,$res);
180 $this->updateProperty($property);
181 }
182
dcac32d6
NB
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
b7cf97c0
NB
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);
c03e412f
NB
207 if ($res) {
208 throw new OMVModuleZFSException(implode("\n", $out));
209 }
b7cf97c0 210 return $tmp;
c03e412f
NB
211 }
212
f891182f
MR
213}
214
215?>
This page took 0.097303 seconds and 5 git commands to generate.