]>
git.datanom.net - omvzfs.git/blob - src/Snapshot.php
2 require_once("Exception.php");
3 require_once("openmediavault/util.inc");
6 * XXX detailed description
12 class OMVModuleZFSSnapshot
{
15 * Name of the Snapshot
23 * Properties associated with the Snaphost
25 * @var array $properties
34 * Constructor. If the Snapshot already exists in the system the object will be updated with all
35 * associated properties from commandline.
37 * @param string $name Name of the new Snapshot
41 public function __construct($name) {
44 $cmd = "zfs list -H -t snapshot " .$name . " 2>&1";
46 $this->exec($cmd, $out, $res);
47 $this->updateAllProperties();
49 catch (OMVModuleZFSException
$e) {
58 * Return name of the Snapshot
60 * @return string Nameof the Snapshot
63 public function getName() {
68 * Get a single property value associated with the Snapshot
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>.
75 public function getProperty($property) {
76 return $this->properties
["$property"];
80 * Get an associative array of all properties associated with the Snapshot
82 * @return array $properties Each entry is an associative array with two elements
83 * <value> and <source>
86 public function getProperties() {
87 return $this->properties
;
91 * Sets a number of Snapshot properties. If a property is already set it will be updated with the new value.
93 * @param array $properties An associative array with properties to set
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);
106 * Get single Snapshot property from commandline and update object property attribute
108 * @param string $property Name of the property to update
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]);
120 * Get all Snapshot properties from commandline and update object properties attribute
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]);
134 $this->properties
["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
140 * Create a Snapshot on commandline.
145 private function create() {
146 $cmd = "zfs snapshot " . $this->name
. " 2>&1";
147 $this->exec($cmd,$out,$res);
148 $this->updateAllProperties();
152 * Destroy a Snapshot on commandline.
157 public function destroy() {
158 $cmd = "zfs destroy " . $this->name
. " 2>&1";
159 $this->exec($cmd,$out,$res);
163 * Rollback a Snapshot on commandline.
168 public function rollback() {
169 $cmd = "zfs rollback " . $this->name
. " 2>&1";
170 $this->exec($cmd,$out,$res);
174 * Clones a Snapshot on commandline.
176 * @param string $newname
180 public function clonesnap($newname) {
181 $cmd = "zfs clone -p " . $this->name
. " " . $newname . " 2>&1";
182 $this->exec($cmd,$out,$res);
186 * Helper function to execute a command and throw an exception on error
187 * (requires stderr redirected to stdout for proper exception message).
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
196 private function exec($cmd, &$out = null, &$res = null) {
197 $tmp = OMVUtil
::exec($cmd, $out, $res);
199 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.132401 seconds and 6 git commands to generate.