]>
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 $cmd = "zfs list -r -o name -H -t snapshot " . $name . " 2>&1";
72 $this->exec($cmd, $out2, $res2);
73 foreach ($out2 as $line2) {
74 $this->snapshots
[$line2] = new OMVModuleZFSSnapshot($line2);
80 * Return name of the Zvol
82 * @return string $name
85 public function getName() {
90 * Get a single property value associated with the Zvol
92 * @param string $property Name of the property to fetch
93 * @return array The returned array with the property. The property is an associative array with
94 * two elements, <value> and <source>.
97 public function getProperty($property) {
98 return $this->properties
["$property"];
102 * Get an associative array of all properties associated with the Zvol
104 * @return array $properties Each entry is an associative array with two elements
105 * <value> and <source>
108 public function getProperties() {
109 return $this->properties
;
113 * Get the total size of the Zvol
115 * @return string $size
118 public function getSize() {
123 * Sets a number of Zvol properties. If a property is already set it will be updated with the new value.
125 * @param array $properties An associative array with properties to set
129 public function setProperties($properties) {
130 foreach ($properties as $newpropertyk => $newpropertyv) {
131 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name
. " 2>&1";
132 $this->exec($cmd,$out,$res);
133 $this->updateProperty($newpropertyk);
138 * Get all Zvol properties from commandline and update object properties attribute
143 private function updateAllProperties() {
144 $cmd = "zfs get -H all " . $this->name
. " 2>&1";
145 $this->exec($cmd,$out,$res);
146 unset($this->properties
);
147 foreach ($out as $line) {
148 $tmpary = preg_split('/\t+/', $line);
149 $this->properties
["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
154 * Get single Datset property from commandline and update object property attribute
156 * @param string $property Name of the property to update
160 private function updateProperty($property) {
161 $cmd = "zfs get -H " . $property . " " . $this->name
. " 2>&1";
162 $this->exec($cmd,$out,$res);
163 $tmpary = preg_split('/\t+/', $out[0]);
164 $this->properties
["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
168 * Create a Zvol on commandline. Optionally provide a number of properties to set.
170 * @param string $size Size of the Zvol that should be created
171 * @param array $properties Properties to set when creatiing the dataset.
172 * @param boolean $sparse Defines if a sparse volume should be created.
176 public function create($size, array $properties = null, $sparse = null) {
177 $cmd = "zfs create -p ";
178 if ((isset($sparse)) && ($sparse == true)) {
181 $cmd .= "-V " . $size . " " . $this->name
. " 2>&1";
182 $this->exec($cmd,$out,$res);
183 $this->updateAllProperties();
184 $this->setProperties($properties);
185 $this->size
= $this->properties
["volsize"]["value"];
194 public function destroy() {
195 $cmd = "zfs destroy " . $this->name
. " 2>&1";
196 $this->exec($cmd,$out,$res);
202 * @param string $newname New name of the Dataset
206 public function rename($newname) {
207 $cmd = "zfs rename -p " . $this->name
. " " . $newname . " 2>&1";
208 $this->exec($cmd,$out,$res);
209 $this->name
= $newname;
213 * Clears a previously set proporty and specifies that it should be
214 * inherited from it's parent.
216 * @param string $property Name of the property to inherit.
220 public function inherit($property) {
221 $cmd = "zfs inherit " . $property . " " . $this->name
. " 2>&1";
222 $this->exec($cmd,$out,$res);
223 $this->updateProperty($property);
227 * Creates a Snapshot and adds it to the existing list of snapshots associated
230 * @param string $snap_name Name of the Snapshot to create.
231 * @param array $properties Optional array of properties to set on Snapshot
235 public function addSnapshot($snap_name, array $properties = null) {
236 $snap = new OMVModuleZFSSnapshot($snap_name);
237 $snap->create($properties);
238 $this->snapshots
[$snap_name] = $snap;
242 * Destroys a Snapshot on commandline and removes it from the Zvol.
244 * @param string $snap_name Name of the Snapshot to delete.
248 public function deleteSnapshot($snap_name) {
249 $this->snapshots
[$snap_name]->destroy();
250 unset($this->snapshots
[$snap_name]);
254 * Helper function to execute a command and throw an exception on error
255 * (requires stderr redirected to stdout for proper exception message).
257 * @param string $cmd Command to execute
258 * @param array &$out If provided will contain output in an array
259 * @param int &$res If provided will contain Exit status of the command
260 * @return string Last line of output when executing the command
261 * @throws OMVModuleZFSException
264 private function exec($cmd, &$out = null, &$res = null) {
265 $tmp = OMVUtil
::exec($cmd, $out, $res);
267 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.176569 seconds and 6 git commands to generate.