]>
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 $cmd = "zfs list -r -o name -H -t snapshot " . $name . " 2>&1";
71 $this->exec($cmd, $out2, $res2);
72 foreach ($out2 as $line2) {
73 $this->snapshots
[$line2] = new OMVModuleZFSSnapshot($line2);
78 * Return name of the Dataset
80 * @return string $name
83 public function getName() {
88 * Get the mountpoint of the Dataset
90 * @return string $mountPoint
93 public function getMountPoint() {
94 return $this->mountPoint
;
98 * Get all Snapshots associated with the Dataset
100 * @return array $snapshots
103 public function getSnapshots() {
104 return $this->snapshots
;
108 * Get a single property value associated with the Dataset
110 * @param string $property Name of the property to fetch
111 * @return array The returned array with the property. The property is an associative array with
112 * two elements, <value> and <source>.
115 public function getProperty($property) {
116 return $this->properties
["$property"];
120 * Get an associative array of all properties associated with the Snapshot
122 * @return array $properties Each entry is an associative array with two elements
123 * <value> and <source>
126 public function getProperties() {
127 return $this->properties
;
131 * Sets a number of Dataset properties. If a property is already set it will be updated with the new value.
133 * @param array $properties An associative array with properties to set
137 public function setProperties($properties) {
138 foreach ($properties as $newpropertyk => $newpropertyv) {
139 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name
. " 2>&1";
140 $this->exec($cmd,$out,$res);
141 $this->updateProperty($newpropertyk);
146 * Get all Dataset properties from commandline and update object properties attribute
151 private function updateAllProperties() {
152 $cmd = "zfs get -H all " . $this->name
. " 2>&1";
153 $this->exec($cmd,$out,$res);
154 unset($this->properties
);
155 foreach ($out as $line) {
156 $tmpary = preg_split('/\t+/', $line);
157 $this->properties
["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
162 * Get single Datset property from commandline and update object property attribute
164 * @param string $property Name of the property to update
168 private function updateProperty($property) {
169 $cmd = "zfs get -H " . $property . " " . $this->name
. " 2>&1";
170 $this->exec($cmd,$out,$res);
171 $tmpary = preg_split('/\t+/', $out[0]);
172 $this->properties
["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
176 * Craete a Dataset on commandline. Optionally provide a number of properties to set.
178 * @param array $properties Properties to set when creating the dataset.
182 public function create(array $properties = null) {
183 $cmd = "zfs create -p " . $this->name
. " 2>&1";
184 $this->exec($cmd,$out,$res);
185 $this->updateAllProperties();
186 $this->setProperties($properties);
187 $this->mountPoint
= $this->properties
["mountpoint"]["value"];
191 * Destroy the Dataset.
196 public function destroy() {
197 $cmd = "zfs destroy " . $this->name
. " 2>&1";
198 $this->exec($cmd,$out,$res);
204 * @param string $newname New name of the Dataset
208 public function rename($newname) {
209 $cmd = "zfs rename -p " . $this->name
. " " . $newname . " 2>&1";
210 $this->exec($cmd,$out,$res);
211 $this->name
= $newname;
215 * Clears a previously set proporty and specifies that it should be
216 * inherited from it's parent.
218 * @param string $property Name of the property to inherit.
222 public function inherit($property) {
223 $cmd = "zfs inherit " . $property . " " . $this->name
. " 2>&1";
224 $this->exec($cmd,$out,$res);
225 $this->updateProperty($property);
229 * Upgrades the Dataset to latest filesystem version
234 public function upgrade() {
235 $cmd = "zfs upgrade " . $this->name
. " 2>&1";
236 $this->exec($cmd,$out,$res);
245 public function mount() {
246 $cmd = "zfs mount " . $this->name
. " 2>&1";
247 $this->exec($cmd,$out,$res);
248 $this->updateProperty("mounted");
252 * Unmount the Dataset
257 public function unmount() {
258 $cmd = "zfs unmount " . $this->name
. " 2>&1";
259 $this->exec($cmd,$out,$res);
260 $this->updateProperty("mounted");
264 * Creates a Snapshot and adds it to the existing list of snapshots associated
267 * @param string $snap_name Name of the Snapshot to create.
268 * @param array $properties Optional array of properties to set on Snapshot
272 public function addSnapshot($snap_name, array $properties = null) {
273 $snap = new OMVModuleZFSSnapshot($snap_name);
274 $snap->create($properties);
275 $this->snapshots
[$snap_name] = $snap;
279 * Destroys a Snapshot on commandline and removes it from the Dataset.
281 * @param string $snap_name Name of the Snapshot to delete.
285 public function deleteSnapshot($snap_name) {
286 $this->snapshots
[$snap_name]->destroy();
287 unset($this->snapshots
[$snap_name]);
291 * Helper function to execute a command and throw an exception on error
292 * (requires stderr redirected to stdout for proper exception message).
294 * @param string $cmd Command to execute
295 * @param array &$out If provided will contain output in an array
296 * @param int &$res If provided will contain Exit status of the command
297 * @return string Last line of output when executing the command
298 * @throws OMVModuleZFSException
301 private function exec($cmd, &$out = null, &$res = null) {
302 $tmp = OMVUtil
::exec($cmd, $out, $res);
304 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.267223 seconds and 6 git commands to generate.