]>
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 $cmd = "zfs list -H -t snapshot " .$name . " 2>&1";
45 $this->exec($cmd, $out, $res);
46 $this->updateAllProperties();
48 catch (OMVModuleZFSException
$e) {
53 * Return name of the Snapshot
55 * @return string Nameof the Snapshot
58 public function getName() {
63 * Get a single property value associated with the Snapshot
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>.
70 public function getProperty($property) {
71 return $this->properties
["$property"];
75 * Get an associative array of all properties associated with the Snapshot
77 * @return array $properties Each entry is an associative array with two elements
78 * <value> and <source>
81 public function getProperties() {
82 return $this->properties
;
86 * Sets a number of Snapshot properties. If a property is already set it will be updated with the new value.
88 * @param array $properties An associative array with properties to set
92 public function setProperties($properties) {
93 foreach ($properties as $newpropertyk => $newpropertyv) {
94 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name
. " 2>&1";
95 $this->exec($cmd,$out,$res);
96 $this->updateProperty($newpropertyk);
101 * Get single Snapshot property from commandline and update object property attribute
103 * @param string $property Name of the property to update
107 private function updateProperty($property) {
108 $cmd = "zfs get -H " . $property . " " . $this->name
. " 2>&1";
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]);
115 * Get all Snapshot properties from commandline and update object properties attribute
120 private function updateAllProperties() {
121 $cmd = "zfs get -H all " . $this->name
. " 2>&1";
122 $this->exec($cmd,$out,$res);
123 unset($this->properties
);
124 foreach ($out as $line) {
125 $tmpary = preg_split('/\t/', $line);
126 if (strlen($tmpary[2] == 0)) {
127 $this->properties
["$tmpary[1]"] = array("value" => "-", "source" => $tmpary[3]);
129 $this->properties
["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
135 * Craete a Snapshot on commandline. Optionally provide a number of properties to set.
137 * @param array $properties Properties to set when creating the dataset.
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);
149 * Destroy a Snapshot on commandline.
154 public function destroy() {
155 $cmd = "zfs destroy " . $this->name
. " 2>&1";
156 $this->exec($cmd,$out,$res);
160 * Helper function to execute a command and throw an exception on error
161 * (requires stderr redirected to stdout for proper exception message).
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
170 private function exec($cmd, &$out = null, &$res = null) {
171 $tmp = OMVUtil
::exec($cmd, $out, $res);
173 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.116694 seconds and 6 git commands to generate.