]>
git.datanom.net - omvzfs.git/blob - src/Zvol.php
2 require_once("Exception.php");
3 require_once("openmediavault/util.inc");
6 * XXX detailed description
12 class OMVModuleZFSZvol
{
32 * Array with properties assigned to the Zvol
34 * @var array $properties
43 * Constructor. If the Zvol already exists in the system the object will be updated with all
44 * associated properties from commandline.
46 * @param string $name Name of the new Zvol
50 public function __construct($name) {
52 $qname = preg_quote($name, '/');
53 $cmd = "zfs list -H -t volume 2>&1";
54 $this->exec($cmd, $out, $res);
55 foreach ($out as $line) {
56 if (preg_match('/^' . $qname . '\t.*$/', $line)) {
57 $this->updateAllProperties();
58 $this->size
= $this->properties
["volsize"]["value"];
65 * Return name of the Zvol
67 * @return string $name
70 public function getName() {
75 * Get a single property value associated with the Zvol
77 * @param string $property Name of the property to fetch
78 * @return array The returned array with the property. The property is an associative array with
79 * two elements, <value> and <source>.
82 public function getProperty($property) {
83 return $this->properties
["$property"];
87 * Get an associative array of all properties associated with the Zvol
89 * @return array $properties Each entry is an associative array with two elements
90 * <value> and <source>
93 public function getProperties() {
94 return $this->properties
;
98 * Get the total size of the Zvol
100 * @return string $size
103 public function getSize() {
108 * Sets a number of Zvol properties. If a property is already set it will be updated with the new value.
110 * @param array $properties An associative array with properties to set
114 public function setProperties($properties) {
115 foreach ($properties as $newpropertyk => $newpropertyv) {
116 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name
. " 2>&1";
117 $this->exec($cmd,$out,$res);
118 $this->updateProperty($newpropertyk);
123 * Get all Zvol properties from commandline and update object properties attribute
128 private function updateAllProperties() {
129 $cmd = "zfs get -H all " . $this->name
. " 2>&1";
130 $this->exec($cmd,$out,$res);
131 unset($this->properties
);
132 foreach ($out as $line) {
133 $tmpary = preg_split('/\t+/', $line);
134 $this->properties
["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
139 * Get single Datset property from commandline and update object property attribute
141 * @param string $property Name of the property to update
145 private function updateProperty($property) {
146 $cmd = "zfs get -H " . $property . " " . $this->name
. " 2>&1";
147 $this->exec($cmd,$out,$res);
148 $tmpary = preg_split('/\t+/', $out[0]);
149 $this->properties
["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
153 * Create a Zvol on commandline. Optionally provide a number of properties to set.
155 * @param string $size Size of the Zvol that should be created
156 * @param array $properties Properties to set when creatiing the dataset.
157 * @param boolean $sparse Defines if a sparse volume should be created.
161 public function create($size, array $properties = null, $sparse = null) {
162 $cmd = "zfs create -p ";
163 if ((isset($sparse)) && ($sparse == true)) {
166 $cmd .= "-V " . $size . " " . $this->name
. " 2>&1";
167 $this->exec($cmd,$out,$res);
168 $this->updateAllProperties();
169 $this->setProperties($properties);
170 $this->size
= $this->properties
["volsize"]["value"];
179 public function destroy() {
180 $cmd = "zfs destroy " . $this->name
. " 2>&1";
181 $this->exec($cmd,$out,$res);
187 * @param string $newname New name of the Dataset
191 public function rename($newname) {
192 $cmd = "zfs rename -p " . $this->name
. " " . $newname . " 2>&1";
193 $this->exec($cmd,$out,$res);
194 $this->name
= $newname;
198 * Clears a previously set proporty and specifies that it should be
199 * inherited from it's parent.
201 * @param string $property Name of the property to inherit.
205 public function inherit($property) {
206 $cmd = "zfs inherit " . $property . " " . $this->name
. " 2>&1";
207 $this->exec($cmd,$out,$res);
208 $this->updateProperty($property);
212 * Helper function to execute a command and throw an exception on error
213 * (requires stderr redirected to stdout for proper exception message).
215 * @param string $cmd Command to execute
216 * @param array &$out If provided will contain output in an array
217 * @param int &$res If provided will contain Exit status of the command
218 * @return string Last line of output when executing the command
219 * @throws OMVModuleZFSException
222 private function exec($cmd, &$out = null, &$res = null) {
223 $tmp = OMVUtil
::exec($cmd, $out, $res);
225 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.126302 seconds and 6 git commands to generate.