]>
Commit | Line | Data |
---|---|---|
f891182f MR |
1 | <?php |
2 | ||
3 | /** | |
4 | * XXX detailed description | |
5 | * | |
6 | * @author XXX | |
7 | * @version XXX | |
8 | * @copyright XXX | |
9 | */ | |
63617ac2 | 10 | class OMVModuleZFSZvol { |
4e7d4caf NB |
11 | // Attributes |
12 | ||
13 | /** | |
14 | * Name of Zvol | |
15 | * | |
16 | * @var string $name | |
17 | * @access private | |
18 | */ | |
19 | private $name; | |
f891182f | 20 | |
4e7d4caf NB |
21 | /** |
22 | * Size of Zvol | |
23 | * | |
24 | * @var int $size | |
25 | * @access private | |
26 | */ | |
27 | private $size; | |
f891182f | 28 | |
4e7d4caf NB |
29 | /** |
30 | * Mountpoint of the Zvol | |
31 | * | |
32 | * @var string $mountPoint | |
33 | * @access private | |
34 | */ | |
35 | private $mountPoint; | |
f891182f | 36 | |
4e7d4caf NB |
37 | /** |
38 | * Array with properties assigned to the Zvol | |
39 | * | |
40 | * @var array $properties | |
41 | * @access private | |
42 | */ | |
43 | private $properties; | |
f891182f | 44 | |
4e7d4caf NB |
45 | // Associations |
46 | // Operations | |
112892fb NB |
47 | |
48 | /** | |
49 | * Constructor. If the Zvol already exists in the system the object will be updated with all | |
50 | * associated properties from commandline. | |
51 | * | |
52 | * @param string $name Name of the new Zvol | |
53 | * @return void | |
54 | * @access public | |
55 | */ | |
56 | public function __construct($name) { | |
57 | $this->name = $name; | |
58 | $qname = preg_quote($name, '/'); | |
59 | $cmd = "zfs list -H -t volume"; | |
60 | $this->exec($cmd, $out, $res); | |
61 | foreach ($out as $line) { | |
62 | if (preg_match('/^' . $qname . '\t.*$/', $line)) { | |
63 | $this->updateAllProperties(); | |
64 | $this->mountPoint = $this->properties["mountpoint"]["value"]; | |
65 | $this->size = $this->properties["size"]["value"]; | |
66 | continue; | |
67 | } | |
68 | } | |
69 | } | |
70 | ||
4e7d4caf | 71 | /** |
91f56fbc NB |
72 | * Return name of the Zvol |
73 | * | |
74 | * @return string $name | |
4e7d4caf NB |
75 | * @access public |
76 | */ | |
77 | public function getName() { | |
91f56fbc | 78 | return $this->name; |
4e7d4caf | 79 | } |
f891182f | 80 | |
4e7d4caf | 81 | /** |
91f56fbc NB |
82 | * Get the mountpoint of the Zvol |
83 | * | |
84 | * @return string $mountPoint | |
4e7d4caf NB |
85 | * @access public |
86 | */ | |
91f56fbc NB |
87 | public function getMountPoint() { |
88 | return $this->mountPoint; | |
4e7d4caf | 89 | } |
f891182f | 90 | |
4e7d4caf | 91 | /** |
91f56fbc NB |
92 | * Get a single property value associated with the Zvol |
93 | * | |
94 | * @param string $property Name of the property to fetch | |
95 | * @return array The returned array with the property. The property is an associative array with | |
96 | * two elements, <value> and <source>. | |
4e7d4caf NB |
97 | * @access public |
98 | */ | |
91f56fbc NB |
99 | public function getProperty($property) { |
100 | return $this->properties["$property"]; | |
101 | } | |
102 | ||
103 | /** | |
104 | * Get an associative array of all properties associated with the Zvol | |
105 | * | |
106 | * @return array $properties Each entry is an associative array with two elements | |
107 | * <value> and <source> | |
108 | * @access public | |
109 | */ | |
110 | public function getProperties() { | |
111 | return $this->properties; | |
4e7d4caf | 112 | } |
f891182f | 113 | |
91f56fbc | 114 | |
4e7d4caf NB |
115 | /** |
116 | * XXX | |
117 | * | |
91f56fbc | 118 | * @return int XXX |
4e7d4caf NB |
119 | * @access public |
120 | */ | |
91f56fbc | 121 | public function getSize() { |
4e7d4caf NB |
122 | trigger_error('Not Implemented!', E_USER_WARNING); |
123 | } | |
f891182f | 124 | |
4e7d4caf NB |
125 | /** |
126 | * XXX | |
127 | * | |
128 | * @param $list<Feature> XXX | |
129 | * @return void XXX | |
130 | * @access public | |
131 | */ | |
132 | public function setProperties($properties) { | |
133 | trigger_error('Not Implemented!', E_USER_WARNING); | |
134 | } | |
135 | ||
112892fb NB |
136 | /** |
137 | * Get all Zvol properties from commandline and update object properties attribute | |
138 | * | |
139 | * @return void | |
140 | * @access private | |
141 | */ | |
142 | private function updateAllProperties() { | |
143 | $cmd = "zfs get -H all " . $this->name; | |
144 | $this->exec($cmd,$out,$res); | |
145 | unset($this->properties); | |
146 | foreach ($out as $line) { | |
147 | $tmpary = preg_split('/\t+/', $line); | |
148 | $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]); | |
149 | } | |
150 | } | |
151 | ||
4e7d4caf NB |
152 | /** |
153 | * Helper function to execute a command and throw an exception on error | |
154 | * (requires stderr redirected to stdout for proper exception message). | |
155 | * | |
156 | * @param string $cmd Command to execute | |
157 | * @param array &$out If provided will contain output in an array | |
158 | * @param int &$res If provided will contain Exit status of the command | |
159 | * @return string Last line of output when executing the command | |
160 | * @throws OMVModuleZFSException | |
161 | * @access private | |
162 | */ | |
163 | private function exec($cmd, &$out = null, &$res = null) { | |
164 | $tmp = OMVUtil::exec($cmd, $out, $res); | |
165 | if ($res) { | |
166 | throw new OMVModuleZFSException(implode("\n", $out)); | |
167 | } | |
168 | return $tmp; | |
169 | } | |
f891182f MR |
170 | |
171 | } | |
172 | ||
173 | ?> |