]>
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 $this->snapshots
[$line2] = new OMVModuleZFSSnapshot($line2);
80 * Return name of the Dataset
82 * @return string $name
85 public function getName() {
90 * Get the mountpoint of the Dataset
92 * @return string $mountPoint
95 public function getMountPoint() {
96 return $this->mountPoint
;
100 * Get all Snapshots associated with the Dataset
102 * @return array $snapshots
105 public function getSnapshots() {
106 return $this->snapshots
;
110 * Get a single property value associated with the Dataset
112 * @param string $property Name of the property to fetch
113 * @return array The returned array with the property. The property is an associative array with
114 * two elements, <value> and <source>.
117 public function getProperty($property) {
118 return $this->properties
["$property"];
122 * Get an associative array of all properties associated with the Snapshot
124 * @return array $properties Each entry is an associative array with two elements
125 * <value> and <source>
128 public function getProperties() {
129 return $this->properties
;
133 * Sets a number of Dataset properties. If a property is already set it will be updated with the new value.
135 * @param array $properties An associative array with properties to set
139 public function setProperties($properties) {
140 foreach ($properties as $newpropertyk => $newpropertyv) {
141 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name
. " 2>&1";
142 $this->exec($cmd,$out,$res);
143 $this->updateProperty($newpropertyk);
148 * Get all Dataset properties from commandline and update object properties attribute
153 private function updateAllProperties() {
154 $cmd = "zfs get -H all " . $this->name
. " 2>&1";
155 $this->exec($cmd,$out,$res);
156 unset($this->properties
);
157 foreach ($out as $line) {
158 $tmpary = preg_split('/\t+/', $line);
159 $this->properties
["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
164 * Get single Datset property from commandline and update object property attribute
166 * @param string $property Name of the property to update
170 private function updateProperty($property) {
171 $cmd = "zfs get -H " . $property . " " . $this->name
. " 2>&1";
172 $this->exec($cmd,$out,$res);
173 $tmpary = preg_split('/\t+/', $out[0]);
174 $this->properties
["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
178 * Craete a Dataset on commandline. Optionally provide a number of properties to set.
180 * @param array $properties Properties to set when creating the dataset.
184 public function create(array $properties = null) {
185 $cmd = "zfs create -p " . $this->name
. " 2>&1";
186 $this->exec($cmd,$out,$res);
187 $this->updateAllProperties();
188 $this->setProperties($properties);
189 $this->mountPoint
= $this->properties
["mountpoint"]["value"];
193 * Destroy the Dataset.
198 public function destroy() {
199 $cmd = "zfs destroy " . $this->name
. " 2>&1";
200 $this->exec($cmd,$out,$res);
206 * @param string $newname New name of the Dataset
210 public function rename($newname) {
211 $cmd = "zfs rename -p " . $this->name
. " " . $newname . " 2>&1";
212 $this->exec($cmd,$out,$res);
213 $this->name
= $newname;
217 * Clears a previously set proporty and specifies that it should be
218 * inherited from it's parent.
220 * @param string $property Name of the property to inherit.
224 public function inherit($property) {
225 $cmd = "zfs inherit " . $property . " " . $this->name
. " 2>&1";
226 $this->exec($cmd,$out,$res);
227 $this->updateProperty($property);
231 * Upgrades the Dataset to latest filesystem version
236 public function upgrade() {
237 $cmd = "zfs upgrade " . $this->name
. " 2>&1";
238 $this->exec($cmd,$out,$res);
247 public function mount() {
248 $cmd = "zfs mount " . $this->name
. " 2>&1";
249 $this->exec($cmd,$out,$res);
250 $this->updateProperty("mounted");
254 * Unmount the Dataset
259 public function unmount() {
260 $cmd = "zfs unmount " . $this->name
. " 2>&1";
261 $this->exec($cmd,$out,$res);
262 $this->updateProperty("mounted");
266 * Creates a Snapshot and adds it to the existing list of snapshots associated
269 * @param string $snap_name Name of the Snapshot to create.
270 * @param array $properties Optional array of properties to set on Snapshot
274 public function addSnapshot($snap_name, array $properties = null) {
275 $snap = new OMVModuleZFSSnapshot($snap_name);
276 $snap->create($properties);
277 $this->snapshots
[$snap_name] = $snap;
281 * Destroys a Snapshot on commandline and removes it from the Dataset.
283 * @param string $snap_name Name of the Snapshot to delete.
287 public function deleteSnapshot($snap_name) {
288 $this->snapshots
[$snap_name]->destroy();
289 unset($this->snapshots
[$snap_name]);
293 * Helper function to execute a command and throw an exception on error
294 * (requires stderr redirected to stdout for proper exception message).
296 * @param string $cmd Command to execute
297 * @param array &$out If provided will contain output in an array
298 * @param int &$res If provided will contain Exit status of the command
299 * @return string Last line of output when executing the command
300 * @throws OMVModuleZFSException
303 private function exec($cmd, &$out = null, &$res = null) {
304 $tmp = OMVUtil
::exec($cmd, $out, $res);
306 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.101678 seconds and 6 git commands to generate.