]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | require_once("Exception.php"); | |
3 | require_once("openmediavault/util.inc"); | |
4 | ||
5 | /** | |
6 | * XXX detailed description | |
7 | * | |
8 | * @author XXX | |
9 | * @version XXX | |
10 | * @copyright XXX | |
11 | */ | |
12 | class OMVModuleZFSDataset { | |
13 | // Attributes | |
14 | /** | |
15 | * Name of Dataset | |
16 | * | |
17 | * @var string $name | |
18 | * @access private | |
19 | */ | |
20 | private $name; | |
21 | /** | |
22 | * Mountpoint of the Dataset | |
23 | * | |
24 | * @var string $mountPoint | |
25 | * @access private | |
26 | */ | |
27 | private $mountPoint; | |
28 | ||
29 | /** | |
30 | * Array with properties assigned to the Dataset | |
31 | * | |
32 | * @var array $properties | |
33 | * @access private | |
34 | */ | |
35 | private $properties; | |
36 | ||
37 | // Associations | |
38 | // Operations | |
39 | ||
40 | /** | |
41 | * Constructor | |
42 | * | |
43 | * @param string $name Name of the new Dataset | |
44 | * @param array $properties An associative array with properties to set when creating the Dataset | |
45 | * @throws OMVModuleZFSException | |
46 | * | |
47 | */ | |
48 | public function __construct($name, array $properties = null) { | |
49 | $cmd = "zfs create -p " . $name . " 2>&1"; | |
50 | OMVUtil::exec($cmd,$out,$res); | |
51 | if ($res) { | |
52 | throw new OMVModuleZFSException(implode("\n", $out)); | |
53 | } | |
54 | $this->name = $name; | |
55 | $this->updateAllProperties(); | |
56 | $this->setProperties($properties); | |
57 | $this->mountPoint = $this->properties["mountpoint"][0]; | |
58 | } | |
59 | ||
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 | } | |
69 | ||
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 | /** | |
81 | * Get a single property value associated with the Dataset | |
82 | * | |
83 | * @param string $property Name of the property to fetch | |
84 | * @return array The returned array key 0=property value and key 1=property source. | |
85 | * @access public | |
86 | */ | |
87 | public function getProperty($property) { | |
88 | return $this->properties["$property"]; | |
89 | } | |
90 | ||
91 | /** | |
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. | |
96 | * | |
97 | * @access public | |
98 | */ | |
99 | public function getProperties() { | |
100 | return $this->properties; | |
101 | } | |
102 | ||
103 | /** | |
104 | * Sets a number of Dataset properties. If a property is already set it will be updated with the new value. | |
105 | * | |
106 | * @param array $properties An associative array with properties to set | |
107 | * @return void | |
108 | * @access public | |
109 | */ | |
110 | public function setProperties($properties) { | |
111 | foreach ($properties as $newpropertyk => $newpropertyv) { | |
112 | $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name; | |
113 | OMVUtil::exec($cmd,$out,$res); | |
114 | if ($res) { | |
115 | throw new OMVModuleZFSException(implode("\n", $out)); | |
116 | } | |
117 | $this->updateProperty($newpropertyk); | |
118 | } | |
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); | |
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)); | |
152 | } | |
153 | $tmpary = preg_split('/\t+/', $out[0]); | |
154 | $this->properties["$tmpary[1]"] = array($tmpary[2], $tmpary[3]); | |
155 | } | |
156 | ||
157 | /** | |
158 | * Destroy the Dataset. | |
159 | * | |
160 | * @throws OMVModuleZFSException | |
161 | * @access public | |
162 | */ | |
163 | public function destroy() { | |
164 | $cmd = "zfs destroy " . $this->name; | |
165 | OMVUtil::exec($cmd,$out,$res); | |
166 | if ($res) { | |
167 | throw new OMVModuleZFSException(implode("\n", $out)); | |
168 | } | |
169 | } | |
170 | ||
171 | /** | |
172 | * Renames a Dataset | |
173 | * | |
174 | * @param string $newname New name of the Dataset | |
175 | * @throws OMVModuleZFSException | |
176 | * @access public | |
177 | */ | |
178 | public function rename($newname) { | |
179 | $cmd = "zfs rename -p " . $this->name . " " . $newname . " 2>&1"; | |
180 | OMVUtil::exec($cmd,$out,$res); | |
181 | if ($res) { | |
182 | throw new OMVModuleZFSException(implode("\n", $out)); | |
183 | } | |
184 | $this->name = $newname; | |
185 | } | |
186 | ||
187 | } | |
188 | ||
189 | ?> |