]>
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 -o name -H -t snapshot " . $name . " 2>&1";
72 $this->exec($cmd, $out2, $res2);
73 foreach ($out2 as $line2) {
74 if (preg_match('/^' . preg_quote($name, '/') . '\@.*$/', $line2)) {
75 $this->snapshots
[$line2] = new OMVModuleZFSSnapshot($line2);
84 * Return name of the Dataset
86 * @return string $name
89 public function getName() {
94 * Get the mountpoint of the Dataset
96 * @return string $mountPoint
99 public function getMountPoint() {
100 return $this->mountPoint
;
104 * Get all Snapshots associated with the Dataset
106 * @return array $snapshots
109 public function getSnapshots() {
110 if (isset($this->snapshots
)) {
111 return $this->snapshots
;
118 * Get a single property value associated with the Dataset
120 * @param string $property Name of the property to fetch
121 * @return array The returned array with the property. The property is an associative array with
122 * two elements, <value> and <source>.
125 public function getProperty($property) {
126 return $this->properties
["$property"];
130 * Get an associative array of all properties associated with the Snapshot
132 * @return array $properties Each entry is an associative array with two elements
133 * <value> and <source>
136 public function getProperties() {
137 return $this->properties
;
141 * Sets a number of Dataset properties. If a property is already set it will be updated with the new value.
143 * @param array $properties An associative array with properties to set
147 public function setProperties($properties) {
148 foreach ($properties as $newpropertyk => $newpropertyv) {
149 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name
. " 2>&1";
150 $this->exec($cmd,$out,$res);
151 $this->updateProperty($newpropertyk);
156 * Get all Dataset properties from commandline and update object properties attribute
161 private function updateAllProperties() {
162 $cmd = "zfs get -H all " . $this->name
. " 2>&1";
163 $this->exec($cmd,$out,$res);
164 unset($this->properties
);
165 foreach ($out as $line) {
166 $tmpary = preg_split('/\t+/', $line);
167 $this->properties
["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
172 * Get single Datset property from commandline and update object property attribute
174 * @param string $property Name of the property to update
178 private function updateProperty($property) {
179 $cmd = "zfs get -H " . $property . " " . $this->name
. " 2>&1";
180 $this->exec($cmd,$out,$res);
181 $tmpary = preg_split('/\t+/', $out[0]);
182 $this->properties
["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
186 * Craete a Dataset on commandline.
191 private function create() {
192 $cmd = "zfs create -p " . $this->name
. " 2>&1";
193 $this->exec($cmd,$out,$res);
194 $this->updateAllProperties();
195 $this->mountPoint
= $this->properties
["mountpoint"]["value"];
199 * Destroy the Dataset.
204 public function destroy() {
205 $cmd = "zfs destroy " . $this->name
. " 2>&1";
206 $this->exec($cmd,$out,$res);
212 * @param string $newname New name of the Dataset
216 public function rename($newname) {
217 $cmd = "zfs rename -p " . $this->name
. " " . $newname . " 2>&1";
218 $this->exec($cmd,$out,$res);
219 $this->name
= $newname;
223 * Clears a previously set proporty and specifies that it should be
224 * inherited from it's parent.
226 * @param string $property Name of the property to inherit.
230 public function inherit($property) {
231 $cmd = "zfs inherit " . $property . " " . $this->name
. " 2>&1";
232 $this->exec($cmd,$out,$res);
233 $this->updateProperty($property);
237 * Upgrades the Dataset to latest filesystem version
242 public function upgrade() {
243 $cmd = "zfs upgrade " . $this->name
. " 2>&1";
244 $this->exec($cmd,$out,$res);
253 public function mount() {
254 $cmd = "zfs mount " . $this->name
. " 2>&1";
255 $this->exec($cmd,$out,$res);
256 $this->updateProperty("mounted");
260 * Unmount the Dataset
265 public function unmount() {
266 $cmd = "zfs unmount " . $this->name
. " 2>&1";
267 $this->exec($cmd,$out,$res);
268 $this->updateProperty("mounted");
272 * Creates a Snapshot and adds it to the existing list of snapshots associated
275 * @param string $snap_name Name of the Snapshot to create.
276 * @param array $properties Optional array of properties to set on Snapshot
280 public function addSnapshot($snap_name, array $properties = null) {
281 $snap = new OMVModuleZFSSnapshot($snap_name);
282 $snap->create($properties);
283 $this->snapshots
[$snap_name] = $snap;
287 * Destroys a Snapshot on commandline and removes it from the Dataset.
289 * @param string $snap_name Name of the Snapshot to delete.
293 public function deleteSnapshot($snap_name) {
294 $this->snapshots
[$snap_name]->destroy();
295 unset($this->snapshots
[$snap_name]);
299 * Helper function to execute a command and throw an exception on error
300 * (requires stderr redirected to stdout for proper exception message).
302 * @param string $cmd Command to execute
303 * @param array &$out If provided will contain output in an array
304 * @param int &$res If provided will contain Exit status of the command
305 * @return string Last line of output when executing the command
306 * @throws OMVModuleZFSException
309 private function exec($cmd, &$out = null, &$res = null) {
310 $tmp = OMVUtil
::exec($cmd, $out, $res);
312 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.101233 seconds and 6 git commands to generate.