]>
Commit | Line | Data |
---|---|---|
f891182f | 1 | <?php |
ba6cf545 | 2 | require_once("Exception.php"); |
eb034e47 | 3 | require_once("openmediavault/util.inc"); |
f891182f MR |
4 | |
5 | /** | |
6 | * XXX detailed description | |
7 | * | |
8 | * @author XXX | |
9 | * @version XXX | |
10 | * @copyright XXX | |
11 | */ | |
63617ac2 | 12 | class OMVModuleZFSDataset { |
f891182f MR |
13 | // Attributes |
14 | /** | |
a4056e15 | 15 | * Name of Dataset |
f891182f MR |
16 | * |
17 | * @var string $name | |
18 | * @access private | |
19 | */ | |
a4056e15 | 20 | private $name; |
f891182f | 21 | /** |
ba6cf545 | 22 | * Mountpoint of the Dataset |
f891182f MR |
23 | * |
24 | * @var string $mountPoint | |
25 | * @access private | |
26 | */ | |
ba6cf545 | 27 | private $mountPoint; |
f891182f MR |
28 | |
29 | /** | |
31fb83f4 | 30 | * List of properties assigned to the Dataset |
f891182f | 31 | * |
31fb83f4 | 32 | * @var array $properties |
f891182f MR |
33 | * @access private |
34 | */ | |
31fb83f4 | 35 | private $properties; |
f891182f | 36 | |
ba6cf545 NB |
37 | // Associations |
38 | // Operations | |
f891182f | 39 | |
ba6cf545 NB |
40 | /** |
41 | * Constructor | |
eb034e47 | 42 | * |
ba6cf545 | 43 | * @param string $name Name of the new Dataset |
31fb83f4 | 44 | * @param array $properties An array of properties (strings) in the form <key>=<value> to set when creating the Dataset |
0b156fc3 | 45 | * @throws OMVModuleZFSException |
ba6cf545 NB |
46 | * |
47 | */ | |
31fb83f4 | 48 | public function __construct($name, array $properties = null) { |
ba6cf545 | 49 | $cmd = "zfs create "; |
31fb83f4 NB |
50 | if (isset($properties)) { |
51 | foreach ($properties as $property) { | |
52 | $cmd .= "-o " . $property . " "; | |
0b156fc3 | 53 | } |
ba6cf545 NB |
54 | } |
55 | $cmd .= $name . " 2>&1"; | |
eb034e47 | 56 | OMVUtil::exec($cmd,$out,$res); |
31fb83f4 | 57 | if ($res) { |
ba6cf545 NB |
58 | throw new OMVModuleZFSException(implode("\n", $out)); |
59 | } | |
60 | unset($res); | |
61 | $this->name = $name; | |
31fb83f4 NB |
62 | if (isset($properties)) { |
63 | $this->properties = $properties; | |
64 | foreach ($properties as $property) { | |
65 | if (preg_match('/^mountpoint\=(.*)$/', $property, $res)) { | |
ba6cf545 NB |
66 | $this->mountPoint = $res[1]; |
67 | continue; | |
68 | } | |
69 | } | |
70 | } else { | |
31fb83f4 | 71 | $this->properties = array(); |
ba6cf545 NB |
72 | $this->mountPoint = "/" . $name; |
73 | } | |
74 | } | |
f891182f | 75 | |
ba6cf545 NB |
76 | /** |
77 | * Return name of the Dataset | |
78 | * | |
79 | * @return string $name | |
80 | * @access public | |
81 | */ | |
82 | public function getName() { | |
83 | return $this->name; | |
84 | } | |
f891182f | 85 | |
ba6cf545 NB |
86 | /** |
87 | * Get the mountpoint of the Dataset | |
88 | * | |
89 | * @return string $mountPoint | |
90 | * @access public | |
91 | */ | |
92 | public function getMountPoint() { | |
93 | return $this->mountPoint; | |
94 | } | |
95 | ||
96 | /** | |
31fb83f4 | 97 | * Get an array of properties associated with the Dataset |
ba6cf545 | 98 | * |
31fb83f4 | 99 | * @return array $properties |
ba6cf545 NB |
100 | * @access public |
101 | */ | |
31fb83f4 NB |
102 | public function getProperties() { |
103 | return $this->properties; | |
ba6cf545 NB |
104 | } |
105 | ||
106 | /** | |
0b156fc3 | 107 | * Sets a number of Dataset properties. If a property is already set it will be updated with the new value. |
ba6cf545 | 108 | * |
31fb83f4 | 109 | * @param array $properties An array of strings in format <key>=<value> |
eb034e47 | 110 | * @return void |
ba6cf545 NB |
111 | * @access public |
112 | */ | |
31fb83f4 NB |
113 | public function setProperties($properties) { |
114 | foreach ($properties as $newproperty) { | |
115 | $cmd = "zfs set " . $newproperty . " " . $this->name; | |
eb034e47 | 116 | OMVUtil::exec($cmd,$out,$res); |
31fb83f4 | 117 | if ($res) { |
0b156fc3 NB |
118 | throw new OMVModuleZFSException(implode("\n", $out)); |
119 | } | |
31fb83f4 NB |
120 | $tmp = explode("=", $newproperty); |
121 | $newpropertyk = $tmp[0]; | |
0b156fc3 | 122 | $found = false; |
31fb83f4 NB |
123 | for ($i=0; $i<count($this->properties); $i++) { |
124 | $tmp = explode("=", $this->properties[$i]); | |
125 | $oldpropertyk = $tmp[0]; | |
126 | if (strcmp($newpropertyk, $oldpropertyk) == 0) { | |
127 | $this->properties[$i] = $newproperty; | |
0b156fc3 NB |
128 | $found = true; |
129 | continue; | |
130 | } | |
131 | } | |
132 | if (!$found) { | |
31fb83f4 | 133 | array_push($this->properties, $newproperty); |
0b156fc3 NB |
134 | } |
135 | } | |
136 | } | |
137 | ||
138 | /** | |
139 | * Destroy the Dataset. | |
140 | * | |
141 | */ | |
142 | public function destroy() { | |
143 | $cmd = "zfs destroy " . $this->name; | |
eb034e47 | 144 | OMVUtil::exec($cmd,$out,$res); |
0b156fc3 NB |
145 | if ($res == 1) { |
146 | throw new OMVModuleZFSException(implode("\n", $out)); | |
147 | } | |
ba6cf545 | 148 | } |
f891182f MR |
149 | |
150 | } | |
151 | ||
152 | ?> |