]>
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) {
62 $cmd = "zfs list -H -t volume " . $name . " 2>&1";
64 $this->exec($cmd, $out, $res);
65 $this->updateAllProperties();
66 $this->size
= $this->properties
["volsize"]["value"];
68 catch (OMVModuleZFSException
$e) {
72 $cmd = "zfs list -r -d 1 -o name -H -t snapshot " . $name . " 2>&1";
73 $this->exec($cmd, $out2, $res2);
74 foreach ($out2 as $line2) {
75 $this->snapshots
[$line2] = new OMVModuleZFSSnapshot($line2);
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 * Get all Snapshots associated with the Zvol
127 * @return array $snapshots
130 public function getSnapshots() {
131 if (isset($this->snapshots
)) {
132 return $this->snapshots
;
139 * Sets a number of Zvol properties. If a property is already set it will be updated with the new value.
141 * @param array $properties An associative array with properties to set
145 public function setProperties($properties) {
146 foreach ($properties as $newpropertyk => $newpropertyv) {
147 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name
. " 2>&1";
148 $this->exec($cmd,$out,$res);
149 $this->updateProperty($newpropertyk);
154 * Get all Zvol properties from commandline and update object properties attribute
159 private function updateAllProperties() {
160 $cmd = "zfs get -H all " . $this->name
. " 2>&1";
161 $this->exec($cmd,$out,$res);
162 unset($this->properties
);
163 foreach ($out as $line) {
164 $tmpary = preg_split('/\t+/', $line);
165 $this->properties
["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
170 * Get single Datset property from commandline and update object property attribute
172 * @param string $property Name of the property to update
176 private function updateProperty($property) {
177 $cmd = "zfs get -H " . $property . " " . $this->name
. " 2>&1";
178 $this->exec($cmd,$out,$res);
179 $tmpary = preg_split('/\t+/', $out[0]);
180 $this->properties
["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
184 * Create a Zvol on commandline. Optionally provide a number of properties to set.
186 * @param string $size Size of the Zvol that should be created
187 * @param array $properties Properties to set when creatiing the dataset.
188 * @param boolean $sparse Defines if a sparse volume should be created.
192 public function create($size, array $properties = null, $sparse = null) {
193 $cmd = "zfs create -p ";
194 if ((isset($sparse)) && ($sparse == true)) {
197 $cmd .= "-V " . $size . " " . $this->name
. " 2>&1";
198 $this->exec($cmd,$out,$res);
199 $this->updateAllProperties();
200 $this->setProperties($properties);
201 $this->size
= $this->properties
["volsize"]["value"];
210 public function destroy() {
211 $cmd = "zfs destroy " . $this->name
. " 2>&1";
212 $this->exec($cmd,$out,$res);
218 * @param string $newname New name of the Dataset
222 public function rename($newname) {
223 $cmd = "zfs rename -p " . $this->name
. " " . $newname . " 2>&1";
224 $this->exec($cmd,$out,$res);
225 $this->name
= $newname;
229 * Clears a previously set proporty and specifies that it should be
230 * inherited from it's parent.
232 * @param string $property Name of the property to inherit.
236 public function inherit($property) {
237 $cmd = "zfs inherit " . $property . " " . $this->name
. " 2>&1";
238 $this->exec($cmd,$out,$res);
239 $this->updateProperty($property);
243 * Creates a Snapshot and adds it to the existing list of snapshots associated
246 * @param string $snap_name Name of the Snapshot to create.
247 * @param array $properties Optional array of properties to set on Snapshot
251 public function addSnapshot($snap_name, array $properties = null) {
252 $snap = new OMVModuleZFSSnapshot($snap_name);
253 $snap->create($properties);
254 $this->snapshots
[$snap_name] = $snap;
258 * Destroys a Snapshot on commandline and removes it from the Zvol.
260 * @param string $snap_name Name of the Snapshot to delete.
264 public function deleteSnapshot($snap_name) {
265 $this->snapshots
[$snap_name]->destroy();
266 unset($this->snapshots
[$snap_name]);
270 * Helper function to execute a command and throw an exception on error
271 * (requires stderr redirected to stdout for proper exception message).
273 * @param string $cmd Command to execute
274 * @param array &$out If provided will contain output in an array
275 * @param int &$res If provided will contain Exit status of the command
276 * @return string Last line of output when executing the command
277 * @throws OMVModuleZFSException
280 private function exec($cmd, &$out = null, &$res = null) {
281 $tmp = OMVUtil
::exec($cmd, $out, $res);
283 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.119292 seconds and 6 git commands to generate.