]>
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) {
43 $qname = preg_quote($name, '/');
44 $cmd = "zfs list -H -t snapshot 2>&1";
45 $this->exec($cmd, $out, $res);
46 foreach ($out as $line) {
47 if (preg_match('/^' . $qname . '\t.*$/', $line)) {
48 $this->updateAllProperties();
55 * Return name of the Snapshot
57 * @return string Nameof the Snapshot
60 public function getName() {
65 * Get a single property value associated with the Snapshot
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>.
72 public function getProperty($property) {
73 return $this->properties
["$property"];
77 * Get an associative array of all properties associated with the Snapshot
79 * @return array $properties Each entry is an associative array with two elements
80 * <value> and <source>
83 public function getProperties() {
84 return $this->properties
;
88 * Sets a number of Snapshot properties. If a property is already set it will be updated with the new value.
90 * @param array $properties An associative array with properties to set
94 public function setProperties($properties) {
95 foreach ($properties as $newpropertyk => $newpropertyv) {
96 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name
. " 2>&1";
97 $this->exec($cmd,$out,$res);
98 $this->updateProperty($newpropertyk);
103 * Get single Snapshot property from commandline and update object property attribute
105 * @param string $property Name of the property to update
109 private function updateProperty($property) {
110 $cmd = "zfs get -H " . $property . " " . $this->name
. " 2>&1";
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]);
117 * Get all Snapshot properties from commandline and update object properties attribute
122 private function updateAllProperties() {
123 $cmd = "zfs get -H all " . $this->name
. " 2>&1";
124 $this->exec($cmd,$out,$res);
125 unset($this->properties
);
126 foreach ($out as $line) {
127 $tmpary = preg_split('/\t/', $line);
128 if (strlen($tmpary[2] == 0)) {
129 $this->properties
["$tmpary[1]"] = array("value" => "-", "source" => $tmpary[3]);
131 $this->properties
["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
137 * Craete a Snapshot on commandline. Optionally provide a number of properties to set.
139 * @param array $properties Properties to set when creating the dataset.
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);
151 * Destroy a Snapshot on commandline.
156 public function destroy() {
157 $cmd = "zfs destroy " . $this->name
. " 2>&1";
158 $this->exec($cmd,$out,$res);
162 * Helper function to execute a command and throw an exception on error
163 * (requires stderr redirected to stdout for proper exception message).
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
172 private function exec($cmd, &$out = null, &$res = null) {
173 $tmp = OMVUtil
::exec($cmd, $out, $res);
175 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.269715 seconds and 6 git commands to generate.