]>
git.datanom.net - omvzfs.git/blob - src/Dataset.php
2 require_once("Exception.php");
3 require_once("openmediavault/util.inc");
6 * XXX detailed description
12 class OMVModuleZFSDataset
{
23 * Mountpoint of the Dataset
25 * @var string $mountPoint
31 * Array with properties assigned to the Dataset
33 * @var array $properties
42 * Constructor. If the Dataset already exists in the system the object will be updated with all
43 * associated properties from commandline.
45 * @param string $name Name of the new Dataset
49 public function __construct($name) {
51 $qname = preg_quote($name, '/');
53 $this->exec($cmd, $out, $res);
54 foreach ($out as $line) {
55 if (preg_match('/^' . $qname . '\t.*$/', $line)) {
56 $this->updateAllProperties();
57 $this->mountPoint
= $this->properties
["mountpoint"]["value"];
64 * Return name of the Dataset
66 * @return string $name
69 public function getName() {
74 * Get the mountpoint of the Dataset
76 * @return string $mountPoint
79 public function getMountPoint() {
80 return $this->mountPoint
;
84 * Get a single property value associated with the Dataset
86 * @param string $property Name of the property to fetch
87 * @return array The returned array key 0=property value and key 1=property source.
90 public function getProperty($property) {
91 return $this->properties
["$property"];
95 * Get an associative array of all properties associated with the Dataset.
97 * @return array $properties Each entry is an array where key 0=property value and key
102 public function getProperties() {
103 return $this->properties
;
107 * Sets a number of Dataset properties. If a property is already set it will be updated with the new value.
109 * @param array $properties An associative array with properties to set
113 public function setProperties($properties) {
114 foreach ($properties as $newpropertyk => $newpropertyv) {
115 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name
;
116 $this->exec($cmd,$out,$res);
117 $this->updateProperty($newpropertyk);
122 * Get all Dataset properties from commandline and update object properties attribute
127 private function updateAllProperties() {
128 $cmd = "zfs get -H all " . $this->name
;
129 $this->exec($cmd,$out,$res);
130 unset($this->properties
);
131 foreach ($out as $line) {
132 $tmpary = preg_split('/\t+/', $line);
133 $this->properties
["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
138 * Get single Datset property from commandline and update object property attribute
140 * @param string $property Name of the property to update
144 private function updateProperty($property) {
145 $cmd = "zfs get -H " . $property . " " . $this->name
;
146 $this->exec($cmd,$out,$res);
147 $tmpary = preg_split('/\t+/', $out[0]);
148 $this->properties
["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
152 * Craete a Dataset on commandline. Optionally provide a number of properties to set.
154 * @param array $properties Properties to set when creating the dataset.
158 public function create(array $properties = null) {
159 $cmd = "zfs create -p " . $this->name
. " 2>&1";
160 $this->exec($cmd,$out,$res);
161 $this->updateAllProperties();
162 $this->setProperties($properties);
163 $this->mountPoint
= $this->properties
["mountpoint"]["value"];
168 * Destroy the Dataset.
173 public function destroy() {
174 $cmd = "zfs destroy " . $this->name
;
175 $this->exec($cmd,$out,$res);
181 * @param string $newname New name of the Dataset
185 public function rename($newname) {
186 $cmd = "zfs rename -p " . $this->name
. " " . $newname . " 2>&1";
187 $this->exec($cmd,$out,$res);
188 $this->name
= $newname;
192 * Clears a previously set proporty and specifies that it should be
193 * inherited from it's parent.
195 * @param string $property Name of the property to inherit.
199 public function inherit($property) {
200 $cmd = "zfs inherit " . $property . " " . $this->name
. " 2>&1";
201 $this->exec($cmd,$out,$res);
202 $this->updateProperty($property);
206 * Upgrades the Dataset to latest filesystem version
211 public function upgrade() {
212 $cmd = "zfs upgrade " . $this->name
. " 2>&1";
213 $this->exec($cmd,$out,$res);
222 public function mount() {
223 $cmd = "zfs mount " . $this->name
. " 2>&1";
224 $this->exec($cmd,$out,$res);
225 $this->updateProperty("mounted");
229 * Unmount the Dataset
234 public function unmount() {
235 $cmd = "zfs unmount " . $this->name
. " 2>&1";
236 $this->exec($cmd,$out,$res);
237 $this->updateProperty("mounted");
242 * Helper function to execute a command and throw an exception on error
243 * (requires stderr redirected to stdout for proper exception message).
245 * @param string $cmd Command to execute
246 * @param array &$out If provided will contain output in an array
247 * @param int &$res If provided will contain Exit status of the command
248 * @return string Last line of output when executing the command
249 * @throws OMVModuleZFSException
252 private function exec($cmd, &$out = null, &$res = null) {
253 $tmp = OMVUtil
::exec($cmd, $out, $res);
255 throw new OMVModuleZFSException(implode("\n", $out));
This page took 0.133895 seconds and 6 git commands to generate.