]>
git.datanom.net - omvzfs.git/blob - src/Dataset.php
2 require_once("Exception.php");
3 require_once("openmediavault/util.inc");
4 require_once("Snapshot.php");
7 * XXX detailed description
13 class OMVModuleZFSDataset
{
24 * Mountpoint of the Dataset
26 * @var string $mountPoint
32 * Array with properties assigned to the Dataset
34 * @var array $properties
40 * Array with Snapshots associated to the Dataset
42 * @var array $snapshots
51 * Constructor. If the Dataset already exists in the system the object will be updated with all
52 * associated properties from commandline.
54 * @param string $name Name of the new Dataset
58 public function __construct($name) {
61 $cmd = "zfs list -H -t filesystem " . $name . " 2>&1";
63 $this->exec($cmd, $out, $res);
64 $this->updateAllProperties();
65 $this->mountPoint
= $this->properties
["mountpoint"]["value"];
67 catch (OMVModuleZFSException
$e) {
71 $cmd = "zfs list -r -d 1 -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);
82 * Return name of the Dataset
84 * @return string $name
87 public function getName() {
92 * Get the mountpoint of the Dataset
94 * @return string $mountPoint
97 public function getMountPoint() {
98 return $this->mountPoint
;
102 * Get all Snapshots associated with the Dataset
104 * @return array $snapshots
107 public function getSnapshots() {
108 if (isset($this->snapshots
)) {
109 return $this->snapshots
;
116 * Get a single property value associated with the Dataset
118 * @param string $property Name of the property to fetch
119 * @return array The returned array with the property. The property is an associative array with
120 * two elements, <value> and <source>.
123 public function getProperty($property) {
124 return $this->properties
["$property"];
128 * Get an associative array of all properties associated with the Snapshot
130 * @return array $properties Each entry is an associative array with two elements
131 * <value> and <source>
134 public function getProperties() {
135 return $this->properties
;
139 * Sets a number of Dataset 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 Dataset 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 * Craete a Dataset on commandline.
189 private function create() {
190 $cmd = "zfs create -p " . $this->name
. " 2>&1";
191 $this->exec($cmd,$out,$res);
192 $this->updateAllProperties();
193 $this->mountPoint
= $this->properties
["mountpoint"]["value"];
197 * Destroy the Dataset.
202 public function destroy() {
203 $cmd = "zfs destroy " . $this->name
. " 2>&1";
204 $this->exec($cmd,$out,$res);
210 * @param string $newname New name of the Dataset
214 public function rename($newname) {
215 $cmd = "zfs rename -p " . $this->name
. " " . $newname . " 2>&1";
216 $this->exec($cmd,$out,$res);
217 $this->name
= $newname;
221 * Clears a previously set proporty and specifies that it should be
222 * inherited from it's parent.
224 * @param string $property Name of the property to inherit.
228 public function inherit($property) {
229 $cmd = "zfs inherit " . $property . " " . $this->name
. " 2>&1";
230 $this->exec($cmd,$out,$res);
231 $this->updateProperty($property);
235 * Upgrades the Dataset to latest filesystem version
240 public function upgrade() {
241 $cmd = "zfs upgrade " . $this->name
. " 2>&1";
242 $this->exec($cmd,$out,$res);
251 public function mount() {
252 $cmd = "zfs mount " . $this->name
. " 2>&1";
253 $this->exec($cmd,$out,$res);
254 $this->updateProperty("mounted");
258 * Unmount the Dataset
263 public function unmount() {
264 $cmd = "zfs unmount " . $this->name
. " 2>&1";
265 $this->exec($cmd,$out,$res);
266 $this->updateProperty("mounted");
270 * Creates a Snapshot and adds it to the existing list of snapshots associated
273 * @param string $snap_name Name of the Snapshot to create.
274 * @param array $properties Optional array of properties to set on Snapshot
278 public function addSnapshot($snap_name, array $properties = null) {
279 $snap = new OMVModuleZFSSnapshot($snap_name);
280 $snap->create($properties);
281 $this->snapshots
[$snap_name] = $snap;
285 * Destroys a Snapshot on commandline and removes it from the Dataset.
287 * @param string $snap_name Name of the Snapshot to delete.
291 public function deleteSnapshot($snap_name) {
292 $this->snapshots
[$snap_name]->destroy();
293 unset($this->snapshots
[$snap_name]);
297 * Check if the Dataset is a clone or not.
302 public function isClone() {
303 $origin = $this->getProperty("origin");
304 if (strlen($origin["value"]) > 0) {
312 * Get the origin of the Dataset if it's a clone.
314 * @return string The name of the origin if it exists. Otherwise an empty string.
317 public function getOrigin() {
318 if ($this->isClone()) {
319 $origin = $this->getProperty("origin");
320 return $origin['value'];
327 * Promotes the Dataset if it's a clone.
332 public function promote() {
333 if ($this->isClone()) {
334 $cmd = "zfs promote " . $this->name
. " 2>&1";
335 $this->exec($cmd,$out,$res);
340 * Helper function to execute a command and throw an exception on error
341 * (requires stderr redirected to stdout for proper exception message).
343 * @param string $cmd Command to execute
344 * @param array &$out If provided will contain output in an array
345 * @param int &$res If provided will contain Exit status of the command
346 * @return string Last line of output when executing the command
347 * @throws OMVModuleZFSException
350 private function exec($cmd, &$out = null, &$res = null) {
351 $tmp = OMVUtil
::exec($cmd, $out, $res);
353 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.135525 seconds and 6 git commands to generate.