]> git.datanom.net - omvzfs.git/blame - src/Dataset.php
Added property source
[omvzfs.git] / src / Dataset.php
CommitLineData
f891182f 1<?php
ba6cf545 2require_once("Exception.php");
eb034e47 3require_once("openmediavault/util.inc");
f891182f
MR
4
5/**
6 * XXX detailed description
7 *
8 * @author XXX
9 * @version XXX
10 * @copyright XXX
11 */
63617ac2 12class OMVModuleZFSDataset {
f891182f
MR
13 // Attributes
14 /**
a4056e15 15 * Name of Dataset
f891182f
MR
16 *
17 * @var string $name
18 * @access private
19 */
a4056e15 20 private $name;
f891182f 21 /**
ba6cf545 22 * Mountpoint of the Dataset
f891182f
MR
23 *
24 * @var string $mountPoint
25 * @access private
26 */
ba6cf545 27 private $mountPoint;
f891182f
MR
28
29 /**
60a2cc94 30 * Array with properties assigned to the Dataset
f891182f 31 *
31fb83f4 32 * @var array $properties
f891182f
MR
33 * @access private
34 */
31fb83f4 35 private $properties;
f891182f 36
ba6cf545
NB
37 // Associations
38 // Operations
f891182f 39
ba6cf545
NB
40 /**
41 * Constructor
eb034e47 42 *
ba6cf545 43 * @param string $name Name of the new Dataset
60a2cc94 44 * @param array $properties An associative array with properties to set when creating the Dataset
0b156fc3 45 * @throws OMVModuleZFSException
ba6cf545
NB
46 *
47 */
31fb83f4 48 public function __construct($name, array $properties = null) {
60a2cc94 49 $cmd = "zfs create " . $name . " 2>&1";
eb034e47 50 OMVUtil::exec($cmd,$out,$res);
31fb83f4 51 if ($res) {
ba6cf545
NB
52 throw new OMVModuleZFSException(implode("\n", $out));
53 }
ba6cf545 54 $this->name = $name;
9b922382 55 $this->updateAllProperties();
60a2cc94 56 $this->setProperties($properties);
9b922382 57 $this->mountPoint = $this->properties["mountpoint"][0];
ba6cf545 58 }
f891182f 59
ba6cf545
NB
60 /**
61 * Return name of the Dataset
62 *
63 * @return string $name
64 * @access public
65 */
66 public function getName() {
67 return $this->name;
68 }
f891182f 69
ba6cf545
NB
70 /**
71 * Get the mountpoint of the Dataset
72 *
73 * @return string $mountPoint
74 * @access public
75 */
76 public function getMountPoint() {
77 return $this->mountPoint;
78 }
79
80 /**
60a2cc94
NB
81 * Get a single property value associated with the Dataset
82 *
83 * @param string $property Name of the property to fetch
9b922382 84 * @return array The returned array key 0=property value and key 1=property source.
60a2cc94
NB
85 * @access public
86 */
87 public function getProperty($property) {
88 return $this->properties["$property"];
89 }
90
91 /**
9b922382
NB
92 * Get an associative array of all properties associated with the Dataset.
93 *
94 * @return array $properties Each entry is an array where key 0=property value and key
95 * 1=property source.
ba6cf545 96 *
ba6cf545
NB
97 * @access public
98 */
31fb83f4
NB
99 public function getProperties() {
100 return $this->properties;
ba6cf545
NB
101 }
102
103 /**
0b156fc3 104 * Sets a number of Dataset properties. If a property is already set it will be updated with the new value.
ba6cf545 105 *
60a2cc94 106 * @param array $properties An associative array with properties to set
eb034e47 107 * @return void
ba6cf545
NB
108 * @access public
109 */
31fb83f4 110 public function setProperties($properties) {
60a2cc94
NB
111 foreach ($properties as $newpropertyk => $newpropertyv) {
112 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name;
eb034e47 113 OMVUtil::exec($cmd,$out,$res);
31fb83f4 114 if ($res) {
0b156fc3
NB
115 throw new OMVModuleZFSException(implode("\n", $out));
116 }
9b922382 117 $this->updateProperty($newpropertyk);
60a2cc94 118 }
60a2cc94
NB
119 }
120
121 /**
122 * Get all Dataset properties from commandline and update object properties attribute
123 *
124 * @throws OMVModuleZFSException
125 * @access private
126 */
127 private function updateAllProperties() {
128 $cmd = "zfs get -H all " . $this->name;
129 OMVUtil::exec($cmd,$out,$res);
130 if ($res) {
131 throw new OMVModuleZFSException(implode("\n", $out));
132 }
133 unset($this->properties);
134 foreach ($out as $line) {
135 $tmpary = preg_split('/\t+/', $line);
9b922382
NB
136 $this->properties["$tmpary[1]"] = array($tmpary[2], $tmpary[3]);
137 }
138 }
139
140 /**
141 * Get single Datset property from commandline and update object property attribute
142 *
143 * @param string $property Name of the property to update
144 * @throws OMVModuleZFSException
145 * @access private
146 */
147 private function updateProperty($property) {
148 $cmd = "zfs get -H " . $property . " " . $this->name;
149 OMVUtil::exec($cmd,$out,$res);
150 if ($res) {
151 throw new OMVModuleZFSException(implode("\n", $out));
0b156fc3 152 }
9b922382
NB
153 $tmpary = preg_split('/\t+/', $out[0]);
154 $this->properties["$tmpary[1]"] = array($tmpary[2], $tmpary[3]);
0b156fc3
NB
155 }
156
157 /**
158 * Destroy the Dataset.
159 *
60a2cc94
NB
160 * @throws OMVModuleZFSException
161 * @access public
0b156fc3
NB
162 */
163 public function destroy() {
164 $cmd = "zfs destroy " . $this->name;
eb034e47 165 OMVUtil::exec($cmd,$out,$res);
60a2cc94 166 if ($res) {
0b156fc3
NB
167 throw new OMVModuleZFSException(implode("\n", $out));
168 }
ba6cf545 169 }
f891182f
MR
170
171}
172
173?>
This page took 0.056403 seconds and 5 git commands to generate.