]>
Commit | Line | Data |
---|---|---|
f891182f | 1 | <?php |
b9872428 NB |
2 | require_once("Exception.php"); |
3 | require_once("openmediavault/util.inc"); | |
f891182f MR |
4 | |
5 | /** | |
6 | * XXX detailed description | |
7 | * | |
8 | * @author XXX | |
9 | * @version XXX | |
10 | * @copyright XXX | |
11 | */ | |
63617ac2 | 12 | class 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) { | |
42 | $this->name = $name; | |
43 | $qname = preg_quote($name, '/'); | |
670691f3 | 44 | $cmd = "zfs list -H -t snapshot 2>&1"; |
b9872428 NB |
45 | $this->exec($cmd, $out, $res); |
46 | foreach ($out as $line) { | |
47 | if (preg_match('/^' . $qname . '\t.*$/', $line)) { | |
b9872428 NB |
48 | $this->updateAllProperties(); |
49 | continue; | |
50 | } | |
51 | } | |
52 | } | |
f891182f | 53 | |
b9872428 NB |
54 | /** |
55 | * Return name of the Snapshot | |
56 | * | |
57 | * @return string Nameof the Snapshot | |
58 | * @access public | |
59 | */ | |
60 | public function getName() { | |
61 | return $this->name; | |
62 | } | |
f891182f | 63 | |
b9872428 | 64 | /** |
ff78c9eb NB |
65 | * Get a single property value associated with the Snapshot |
66 | * | |
67 | * @param string $property Name of the property to fetch | |
68 | * @return array The returned array with the property. The property is an associative array with | |
69 | * two elements, <value> and <source>. | |
b9872428 NB |
70 | * @access public |
71 | */ | |
ff78c9eb NB |
72 | public function getProperty($property) { |
73 | return $this->properties["$property"]; | |
b9872428 | 74 | } |
f891182f | 75 | |
b9872428 | 76 | /** |
ff78c9eb NB |
77 | * Get an associative array of all properties associated with the Snapshot |
78 | * | |
79 | * @return array $properties Each entry is an associative array with two elements | |
80 | * <value> and <source> | |
81 | * @access public | |
82 | */ | |
83 | public function getProperties() { | |
84 | return $this->properties; | |
85 | } | |
86 | ||
87 | /** | |
88 | * Sets a number of Snapshot properties. If a property is already set it will be updated with the new value. | |
89 | * | |
90 | * @param array $properties An associative array with properties to set | |
91 | * @return void | |
b9872428 NB |
92 | * @access public |
93 | */ | |
ff78c9eb NB |
94 | public function setProperties($properties) { |
95 | foreach ($properties as $newpropertyk => $newpropertyv) { | |
670691f3 | 96 | $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name . " 2>&1"; |
ff78c9eb NB |
97 | $this->exec($cmd,$out,$res); |
98 | $this->updateProperty($newpropertyk); | |
99 | } | |
100 | } | |
101 | ||
102 | /** | |
103 | * Get single Snapshot property from commandline and update object property attribute | |
104 | * | |
105 | * @param string $property Name of the property to update | |
106 | * @return void | |
107 | * @access private | |
108 | */ | |
109 | private function updateProperty($property) { | |
670691f3 | 110 | $cmd = "zfs get -H " . $property . " " . $this->name . " 2>&1"; |
ff78c9eb NB |
111 | $this->exec($cmd,$out,$res); |
112 | $tmpary = preg_split('/\t+/', $out[0]); | |
113 | $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]); | |
b9872428 NB |
114 | } |
115 | ||
116 | /** | |
117 | * Get all Snapshot properties from commandline and update object properties attribute | |
118 | * | |
119 | * @return void | |
120 | * @access private | |
121 | */ | |
122 | private function updateAllProperties() { | |
670691f3 | 123 | $cmd = "zfs get -H all " . $this->name . " 2>&1"; |
b9872428 NB |
124 | $this->exec($cmd,$out,$res); |
125 | unset($this->properties); | |
126 | foreach ($out as $line) { | |
ddcd4558 NB |
127 | $tmpary = preg_split('/\t/', $line); |
128 | if (strlen($tmpary[2] == 0)) { | |
129 | $this->properties["$tmpary[1]"] = array("value" => "-", "source" => $tmpary[3]); | |
130 | } else { | |
131 | $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]); | |
132 | } | |
b9872428 NB |
133 | } |
134 | } | |
f891182f | 135 | |
ff78c9eb NB |
136 | /** |
137 | * Craete a Snapshot on commandline. Optionally provide a number of properties to set. | |
138 | * | |
139 | * @param array $properties Properties to set when creating the dataset. | |
140 | * @return void | |
141 | * @access public | |
142 | */ | |
143 | public function create(array $properties = null) { | |
144 | $cmd = "zfs snapshot " . $this->name . " 2>&1"; | |
145 | $this->exec($cmd,$out,$res); | |
146 | $this->updateAllProperties(); | |
147 | $this->setProperties($properties); | |
148 | } | |
149 | ||
ddcd4558 NB |
150 | /** |
151 | * Destroy a Snapshot on commandline. | |
152 | * | |
153 | * @return void | |
154 | * @access public | |
155 | */ | |
156 | public function destroy() { | |
157 | $cmd = "zfs destroy " . $this->name . " 2>&1"; | |
158 | $this->exec($cmd,$out,$res); | |
159 | } | |
160 | ||
b9872428 NB |
161 | /** |
162 | * Helper function to execute a command and throw an exception on error | |
163 | * (requires stderr redirected to stdout for proper exception message). | |
164 | * | |
165 | * @param string $cmd Command to execute | |
166 | * @param array &$out If provided will contain output in an array | |
167 | * @param int &$res If provided will contain Exit status of the command | |
168 | * @return string Last line of output when executing the command | |
169 | * @throws OMVModuleZFSException | |
170 | * @access private | |
171 | */ | |
172 | private function exec($cmd, &$out = null, &$res = null) { | |
173 | $tmp = OMVUtil::exec($cmd, $out, $res); | |
174 | if ($res) { | |
175 | throw new OMVModuleZFSException(implode("\n", $out)); | |
176 | } | |
177 | return $tmp; | |
178 | } | |
f891182f MR |
179 | } |
180 | ||
181 | ?> |