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