]>
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) { | |
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 | ||
b9872428 NB |
162 | /** |
163 | * Helper function to execute a command and throw an exception on error | |
164 | * (requires stderr redirected to stdout for proper exception message). | |
165 | * | |
166 | * @param string $cmd Command to execute | |
167 | * @param array &$out If provided will contain output in an array | |
168 | * @param int &$res If provided will contain Exit status of the command | |
169 | * @return string Last line of output when executing the command | |
170 | * @throws OMVModuleZFSException | |
171 | * @access private | |
172 | */ | |
173 | private function exec($cmd, &$out = null, &$res = null) { | |
174 | $tmp = OMVUtil::exec($cmd, $out, $res); | |
175 | if ($res) { | |
176 | throw new OMVModuleZFSException(implode("\n", $out)); | |
177 | } | |
178 | return $tmp; | |
179 | } | |
f891182f MR |
180 | } |
181 | ||
182 | ?> |