]>
git.datanom.net - omvzfs.git/blob - src/Zvol.php
2 require_once("Exception.php");
3 require_once("openmediavault/util.inc");
4 require_once("Snapshot.php");
7 * XXX detailed description
13 class OMVModuleZFSZvol
{
33 * Array with properties assigned to the Zvol
35 * @var array $properties
41 * Array with Snapshots associated to the Zvol
43 * @var array $snapshots
52 * Constructor. If the Zvol already exists in the system the object will be updated with all
53 * associated properties from commandline.
55 * @param string $name Name of the new Zvol
59 public function __construct($name) {
61 $qname = preg_quote($name, '/');
62 $cmd = "zfs list -H -t volume 2>&1";
63 $this->exec($cmd, $out, $res);
64 foreach ($out as $line) {
65 if (preg_match('/^' . $qname . '\t.*$/', $line)) {
66 $this->updateAllProperties();
67 $this->size
= $this->properties
["volsize"]["value"];
71 $qname = preg_quote($name . "@", '/');
72 $cmd = "zfs list -H -t snapshot 2>&1";
73 $this->exec($cmd, $out, $res);
74 foreach ($out as $line) {
75 if (preg_match('/^(' . $qname . '[^\s]+)\t.*$/', $line, $res)) {
76 $this->snapshots
[$res[1]] = new OMVModuleZFSSnapshot($res[1]);
82 * Return name of the Zvol
84 * @return string $name
87 public function getName() {
92 * Get a single property value associated with the Zvol
94 * @param string $property Name of the property to fetch
95 * @return array The returned array with the property. The property is an associative array with
96 * two elements, <value> and <source>.
99 public function getProperty($property) {
100 return $this->properties
["$property"];
104 * Get an associative array of all properties associated with the Zvol
106 * @return array $properties Each entry is an associative array with two elements
107 * <value> and <source>
110 public function getProperties() {
111 return $this->properties
;
115 * Get the total size of the Zvol
117 * @return string $size
120 public function getSize() {
125 * Sets a number of Zvol properties. If a property is already set it will be updated with the new value.
127 * @param array $properties An associative array with properties to set
131 public function setProperties($properties) {
132 foreach ($properties as $newpropertyk => $newpropertyv) {
133 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name
. " 2>&1";
134 $this->exec($cmd,$out,$res);
135 $this->updateProperty($newpropertyk);
140 * Get all Zvol properties from commandline and update object properties attribute
145 private function updateAllProperties() {
146 $cmd = "zfs get -H all " . $this->name
. " 2>&1";
147 $this->exec($cmd,$out,$res);
148 unset($this->properties
);
149 foreach ($out as $line) {
150 $tmpary = preg_split('/\t+/', $line);
151 $this->properties
["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
156 * Get single Datset property from commandline and update object property attribute
158 * @param string $property Name of the property to update
162 private function updateProperty($property) {
163 $cmd = "zfs get -H " . $property . " " . $this->name
. " 2>&1";
164 $this->exec($cmd,$out,$res);
165 $tmpary = preg_split('/\t+/', $out[0]);
166 $this->properties
["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
170 * Create a Zvol on commandline. Optionally provide a number of properties to set.
172 * @param string $size Size of the Zvol that should be created
173 * @param array $properties Properties to set when creatiing the dataset.
174 * @param boolean $sparse Defines if a sparse volume should be created.
178 public function create($size, array $properties = null, $sparse = null) {
179 $cmd = "zfs create -p ";
180 if ((isset($sparse)) && ($sparse == true)) {
183 $cmd .= "-V " . $size . " " . $this->name
. " 2>&1";
184 $this->exec($cmd,$out,$res);
185 $this->updateAllProperties();
186 $this->setProperties($properties);
187 $this->size
= $this->properties
["volsize"]["value"];
196 public function destroy() {
197 $cmd = "zfs destroy " . $this->name
. " 2>&1";
198 $this->exec($cmd,$out,$res);
204 * @param string $newname New name of the Dataset
208 public function rename($newname) {
209 $cmd = "zfs rename -p " . $this->name
. " " . $newname . " 2>&1";
210 $this->exec($cmd,$out,$res);
211 $this->name
= $newname;
215 * Clears a previously set proporty and specifies that it should be
216 * inherited from it's parent.
218 * @param string $property Name of the property to inherit.
222 public function inherit($property) {
223 $cmd = "zfs inherit " . $property . " " . $this->name
. " 2>&1";
224 $this->exec($cmd,$out,$res);
225 $this->updateProperty($property);
229 * Creates a Snapshot and adds it to the existing list of snapshots associated
232 * @param string $snap_name Name of the Snapshot to create.
233 * @param array $properties Optional array of properties to set on Snapshot
237 public function addSnapshot($snap_name, array $properties = null) {
238 $snap = new OMVModuleZFSSnapshot($snap_name);
239 $snap->create($properties);
240 $this->snapshots
[$snap_name] = $snap;
244 * Destroys a Snapshot on commandline and removes it from the Zvol.
246 * @param string $snap_name Name of the Snapshot to delete.
250 public function deleteSnapshot($snap_name) {
251 $this->snapshots
[$snap_name]->destroy();
252 unset($this->snapshots
[$snap_name]);
256 * Helper function to execute a command and throw an exception on error
257 * (requires stderr redirected to stdout for proper exception message).
259 * @param string $cmd Command to execute
260 * @param array &$out If provided will contain output in an array
261 * @param int &$res If provided will contain Exit status of the command
262 * @return string Last line of output when executing the command
263 * @throws OMVModuleZFSException
266 private function exec($cmd, &$out = null, &$res = null) {
267 $tmp = OMVUtil
::exec($cmd, $out, $res);
269 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.199351 seconds and 6 git commands to generate.