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