]>
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) {
60 $qname = preg_quote($name, '/');
61 $cmd = "zfs list -H 2>&1";
62 $this->exec($cmd, $out, $res);
63 foreach ($out as $line) {
64 if (preg_match('/^' . $qname . '\t.*$/', $line)) {
65 $this->updateAllProperties();
66 $this->mountPoint
= $this->properties
["mountpoint"]["value"];
70 $qname = preg_quote($name . "@", '/');
71 $cmd = "zfs list -H -t snapshot 2>&1";
72 $this->exec($cmd, $out, $res);
73 foreach ($out as $line) {
74 if (preg_match('/^(' . $qname . '[^\s]+)\t.*$/', $line, $res)) {
75 $this->snapshots
[$res[1]] = new OMVModuleZFSSnapshot($res[1]);
81 * Return name of the Dataset
83 * @return string $name
86 public function getName() {
91 * Get the mountpoint of the Dataset
93 * @return string $mountPoint
96 public function getMountPoint() {
97 return $this->mountPoint
;
101 * Get all Snapshots associated with the Dataset
103 * @return array $snapshots
106 public function getSnapshots() {
107 return $this->snapshots
;
111 * Get a single property value associated with the Dataset
113 * @param string $property Name of the property to fetch
114 * @return array The returned array with the property. The property is an associative array with
115 * two elements, <value> and <source>.
118 public function getProperty($property) {
119 return $this->properties
["$property"];
123 * Get an associative array of all properties associated with the Snapshot
125 * @return array $properties Each entry is an associative array with two elements
126 * <value> and <source>
129 public function getProperties() {
130 return $this->properties
;
134 * Sets a number of Dataset properties. If a property is already set it will be updated with the new value.
136 * @param array $properties An associative array with properties to set
140 public function setProperties($properties) {
141 foreach ($properties as $newpropertyk => $newpropertyv) {
142 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name
. " 2>&1";
143 $this->exec($cmd,$out,$res);
144 $this->updateProperty($newpropertyk);
149 * Get all Dataset properties from commandline and update object properties attribute
154 private function updateAllProperties() {
155 $cmd = "zfs get -H all " . $this->name
. " 2>&1";
156 $this->exec($cmd,$out,$res);
157 unset($this->properties
);
158 foreach ($out as $line) {
159 $tmpary = preg_split('/\t+/', $line);
160 $this->properties
["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
165 * Get single Datset property from commandline and update object property attribute
167 * @param string $property Name of the property to update
171 private function updateProperty($property) {
172 $cmd = "zfs get -H " . $property . " " . $this->name
. " 2>&1";
173 $this->exec($cmd,$out,$res);
174 $tmpary = preg_split('/\t+/', $out[0]);
175 $this->properties
["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
179 * Craete a Dataset on commandline. Optionally provide a number of properties to set.
181 * @param array $properties Properties to set when creating the dataset.
185 public function create(array $properties = null) {
186 $cmd = "zfs create -p " . $this->name
. " 2>&1";
187 $this->exec($cmd,$out,$res);
188 $this->updateAllProperties();
189 $this->setProperties($properties);
190 $this->mountPoint
= $this->properties
["mountpoint"]["value"];
194 * Destroy the Dataset.
199 public function destroy() {
200 $cmd = "zfs destroy " . $this->name
. " 2>&1";
201 $this->exec($cmd,$out,$res);
207 * @param string $newname New name of the Dataset
211 public function rename($newname) {
212 $cmd = "zfs rename -p " . $this->name
. " " . $newname . " 2>&1";
213 $this->exec($cmd,$out,$res);
214 $this->name
= $newname;
218 * Clears a previously set proporty and specifies that it should be
219 * inherited from it's parent.
221 * @param string $property Name of the property to inherit.
225 public function inherit($property) {
226 $cmd = "zfs inherit " . $property . " " . $this->name
. " 2>&1";
227 $this->exec($cmd,$out,$res);
228 $this->updateProperty($property);
232 * Upgrades the Dataset to latest filesystem version
237 public function upgrade() {
238 $cmd = "zfs upgrade " . $this->name
. " 2>&1";
239 $this->exec($cmd,$out,$res);
248 public function mount() {
249 $cmd = "zfs mount " . $this->name
. " 2>&1";
250 $this->exec($cmd,$out,$res);
251 $this->updateProperty("mounted");
255 * Unmount the Dataset
260 public function unmount() {
261 $cmd = "zfs unmount " . $this->name
. " 2>&1";
262 $this->exec($cmd,$out,$res);
263 $this->updateProperty("mounted");
267 * Creates a Snapshot and adds it to the existing list of snapshots associated
270 * @param string $snap_name Name of the Snapshot to create.
271 * @param array $properties Optional array of properties to set on Snapshot
275 public function addSnapshot($snap_name, array $properties = null) {
276 $snap = new OMVModuleZFSSnapshot($snap_name);
277 $snap->create($properties);
278 $this->snapshots
[$snap_name] = $snap;
282 * Destroys a Snapshot on commandline and removes it from the Dataset.
284 * @param string $snap_name Name of the Snapshot to delete.
288 public function deleteSnapshot($snap_name) {
289 $this->snapshots
[$snap_name]->destroy();
290 unset($this->snapshots
[$snap_name]);
294 * Helper function to execute a command and throw an exception on error
295 * (requires stderr redirected to stdout for proper exception message).
297 * @param string $cmd Command to execute
298 * @param array &$out If provided will contain output in an array
299 * @param int &$res If provided will contain Exit status of the command
300 * @return string Last line of output when executing the command
301 * @throws OMVModuleZFSException
304 private function exec($cmd, &$out = null, &$res = null) {
305 $tmp = OMVUtil
::exec($cmd, $out, $res);
307 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.120086 seconds and 6 git commands to generate.