]> git.datanom.net - omvzfs.git/blob - src/Snapshot.php
Minor change to clarify shared folder creation.
[omvzfs.git] / src / Snapshot.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 OMVModuleZFSSnapshot {
13 // Attributes
14 /**
15 * Name of the Snapshot
16 *
17 * @var string $name
18 * @access private
19 */
20 private $name;
21
22 /**
23 * Properties associated with the Snaphost
24 *
25 * @var array $properties
26 * @access private
27 */
28 private $properties;
29
30 // Associations
31 // Operations
32
33 /**
34 * Constructor. If the Snapshot already exists in the system the object will be updated with all
35 * associated properties from commandline.
36 *
37 * @param string $name Name of the new Snapshot
38 * @return void
39 * @access public
40 */
41 public function __construct($name) {
42 $snap_exists = true;
43 $this->name = $name;
44 $cmd = "zfs list -H -t snapshot " .$name . " 2>&1";
45 try {
46 $this->exec($cmd, $out, $res);
47 $this->updateAllProperties();
48 }
49 catch (OMVModuleZFSException $e) {
50 $snap_exists = false;
51 }
52 if (!$snap_exists) {
53 $this->create();
54 }
55 }
56
57 /**
58 * Return name of the Snapshot
59 *
60 * @return string Nameof the Snapshot
61 * @access public
62 */
63 public function getName() {
64 return $this->name;
65 }
66
67 /**
68 * Get a single property value associated with the Snapshot
69 *
70 * @param string $property Name of the property to fetch
71 * @return array The returned array with the property. The property is an associative array with
72 * two elements, <value> and <source>.
73 * @access public
74 */
75 public function getProperty($property) {
76 return $this->properties["$property"];
77 }
78
79 /**
80 * Get an associative array of all properties associated with the Snapshot
81 *
82 * @return array $properties Each entry is an associative array with two elements
83 * <value> and <source>
84 * @access public
85 */
86 public function getProperties() {
87 return $this->properties;
88 }
89
90 /**
91 * Sets a number of Snapshot properties. If a property is already set it will be updated with the new value.
92 *
93 * @param array $properties An associative array with properties to set
94 * @return void
95 * @access public
96 */
97 public function setProperties($properties) {
98 foreach ($properties as $newpropertyk => $newpropertyv) {
99 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name . " 2>&1";
100 $this->exec($cmd,$out,$res);
101 $this->updateProperty($newpropertyk);
102 }
103 }
104
105 /**
106 * Get single Snapshot property from commandline and update object property attribute
107 *
108 * @param string $property Name of the property to update
109 * @return void
110 * @access private
111 */
112 private function updateProperty($property) {
113 $cmd = "zfs get -H " . $property . " " . $this->name . " 2>&1";
114 $this->exec($cmd,$out,$res);
115 $tmpary = preg_split('/\t+/', $out[0]);
116 $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
117 }
118
119 /**
120 * Get all Snapshot properties from commandline and update object properties attribute
121 *
122 * @return void
123 * @access private
124 */
125 private function updateAllProperties() {
126 $cmd = "zfs get -H all " . $this->name . " 2>&1";
127 $this->exec($cmd,$out,$res);
128 unset($this->properties);
129 foreach ($out as $line) {
130 $tmpary = preg_split('/\t/', $line);
131 if (strlen($tmpary[2] == 0)) {
132 $this->properties["$tmpary[1]"] = array("value" => "-", "source" => $tmpary[3]);
133 } else {
134 $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
135 }
136 }
137 }
138
139 /**
140 * Create a Snapshot on commandline.
141 *
142 * @return void
143 * @access private
144 */
145 private function create() {
146 $cmd = "zfs snapshot " . $this->name . " 2>&1";
147 $this->exec($cmd,$out,$res);
148 $this->updateAllProperties();
149 }
150
151 /**
152 * Destroy a Snapshot on commandline.
153 *
154 * @return void
155 * @access public
156 */
157 public function destroy() {
158 $cmd = "zfs destroy " . $this->name . " 2>&1";
159 $this->exec($cmd,$out,$res);
160 }
161
162 /**
163 * Rollback a Snapshot on commandline.
164 *
165 * @return void
166 * @access public
167 */
168 public function rollback() {
169 $cmd = "zfs rollback " . $this->name . " 2>&1";
170 $this->exec($cmd,$out,$res);
171 }
172
173 /**
174 * Clones a Snapshot on commandline.
175 *
176 * @param string $newname
177 * @return void
178 * @access public
179 */
180 public function clonesnap($newname) {
181 $cmd = "zfs clone -p " . $this->name . " " . $newname . " 2>&1";
182 $this->exec($cmd,$out,$res);
183 }
184
185 /**
186 * Helper function to execute a command and throw an exception on error
187 * (requires stderr redirected to stdout for proper exception message).
188 *
189 * @param string $cmd Command to execute
190 * @param array &$out If provided will contain output in an array
191 * @param int &$res If provided will contain Exit status of the command
192 * @return string Last line of output when executing the command
193 * @throws OMVModuleZFSException
194 * @access private
195 */
196 private function exec($cmd, &$out = null, &$res = null) {
197 $tmp = OMVUtil::exec($cmd, $out, $res);
198 if ($res) {
199 throw new OMVModuleZFSException(implode("\n", $out));
200 }
201 return $tmp;
202 }
203 }
204
205 ?>
This page took 0.076856 seconds and 6 git commands to generate.