]> git.datanom.net - omvzfs.git/blame - src/Snapshot.php
Initial implementation of GUI for adding ZFS pools plus minor bugfixes.
[omvzfs.git] / src / Snapshot.php
CommitLineData
f891182f 1<?php
b9872428
NB
2require_once("Exception.php");
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 OMVModuleZFSSnapshot {
f891182f
MR
13 // Attributes
14 /**
b9872428 15 * Name of the Snapshot
f891182f
MR
16 *
17 * @var string $name
18 * @access private
19 */
b9872428 20 private $name;
f891182f
MR
21
22 /**
b9872428 23 * Properties associated with the Snaphost
f891182f 24 *
b9872428 25 * @var array $properties
f891182f
MR
26 * @access private
27 */
b9872428 28 private $properties;
f891182f
MR
29
30 // Associations
31 // Operations
f891182f 32
b9872428
NB
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) {
4b7ce266 42 $snap_exists = true;
b9872428 43 $this->name = $name;
e419cf47
NB
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) {
4b7ce266
NB
50 $snap_exists = false;
51 }
52 if (!$snap_exists) {
53 $this->create();
b9872428
NB
54 }
55 }
f891182f 56
b9872428
NB
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 }
f891182f 66
b9872428 67 /**
ff78c9eb
NB
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>.
b9872428
NB
73 * @access public
74 */
ff78c9eb
NB
75 public function getProperty($property) {
76 return $this->properties["$property"];
b9872428 77 }
f891182f 78
b9872428 79 /**
ff78c9eb
NB
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
b9872428
NB
95 * @access public
96 */
ff78c9eb
NB
97 public function setProperties($properties) {
98 foreach ($properties as $newpropertyk => $newpropertyv) {
670691f3 99 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name . " 2>&1";
ff78c9eb
NB
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) {
670691f3 113 $cmd = "zfs get -H " . $property . " " . $this->name . " 2>&1";
ff78c9eb
NB
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]);
b9872428
NB
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() {
670691f3 126 $cmd = "zfs get -H all " . $this->name . " 2>&1";
b9872428
NB
127 $this->exec($cmd,$out,$res);
128 unset($this->properties);
129 foreach ($out as $line) {
ddcd4558
NB
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 }
b9872428
NB
136 }
137 }
f891182f 138
ff78c9eb 139 /**
4b7ce266 140 * Create a Snapshot on commandline.
ff78c9eb 141 *
ff78c9eb 142 * @return void
4b7ce266 143 * @access private
ff78c9eb 144 */
4b7ce266 145 private function create() {
ff78c9eb
NB
146 $cmd = "zfs snapshot " . $this->name . " 2>&1";
147 $this->exec($cmd,$out,$res);
148 $this->updateAllProperties();
ff78c9eb
NB
149 }
150
ddcd4558
NB
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
cf576dd1
NB
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
b9872428
NB
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 }
f891182f
MR
203}
204
205?>
This page took 0.077308 seconds and 5 git commands to generate.