]>
Commit | Line | Data |
---|---|---|
f4648aef AD |
1 | package PVE::Storage::GlusterfsPlugin; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use IO::File; | |
6 | use File::Path; | |
7 | use PVE::Tools qw(run_command); | |
84c8e52d | 8 | use PVE::ProcFSTools; |
be48449c | 9 | use PVE::Network; |
f4648aef AD |
10 | use PVE::Storage::Plugin; |
11 | use PVE::JSONSchema qw(get_standard_option); | |
f4648aef AD |
12 | |
13 | use base qw(PVE::Storage::Plugin); | |
14 | ||
15 | # Glusterfs helper functions | |
16 | ||
a66159e3 DM |
17 | my $server_test_results = {}; |
18 | ||
19 | my $get_active_server = sub { | |
20 | my ($scfg, $return_default_if_offline) = @_; | |
21 | ||
22 | my $defaultserver = $scfg->{server} ? $scfg->{server} : 'localhost'; | |
23 | ||
24 | if ($return_default_if_offline && !defined($scfg->{server2})) { | |
25 | # avoid delays (there is no backup server anyways) | |
26 | return $defaultserver; | |
27 | } | |
28 | ||
29 | my $serverlist = [ $defaultserver ]; | |
30 | push @$serverlist, $scfg->{server2} if $scfg->{server2}; | |
31 | ||
32 | my $ctime = time(); | |
33 | foreach my $server (@$serverlist) { | |
34 | my $stat = $server_test_results->{$server}; | |
35 | return $server if $stat && $stat->{active} && (($ctime - $stat->{time}) <= 2); | |
36 | } | |
37 | ||
38 | foreach my $server (@$serverlist) { | |
39 | my $status = 0; | |
40 | ||
65644196 | 41 | if ($server && $server ne 'localhost' && $server ne '127.0.0.1' && $server ne '::1') { |
a66159e3 | 42 | |
be48449c WB |
43 | # ping the echo port (7) without service check |
44 | $status = PVE::Network::tcp_ping($server, undef, 2); | |
a66159e3 DM |
45 | |
46 | } else { | |
47 | ||
48 | my $parser = sub { | |
49 | my $line = shift; | |
50 | ||
51 | if ($line =~ m/Status: Started$/) { | |
52 | $status = 1; | |
53 | } | |
54 | }; | |
55 | ||
56 | my $cmd = ['/usr/sbin/gluster', 'volume', 'info', $scfg->{volume}]; | |
57 | ||
58 | run_command($cmd, errmsg => "glusterfs error", errfunc => sub {}, outfunc => $parser); | |
59 | } | |
60 | ||
61 | $server_test_results->{$server} = { time => time(), active => $status }; | |
62 | return $server if $status; | |
63 | } | |
64 | ||
65 | return $defaultserver if $return_default_if_offline; | |
66 | ||
67 | return undef; | |
68 | }; | |
69 | ||
f4648aef | 70 | sub glusterfs_is_mounted { |
a66159e3 | 71 | my ($volume, $mountpoint, $mountdata) = @_; |
f4648aef | 72 | |
80b64788 | 73 | $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata; |
f4648aef | 74 | |
80b64788 WB |
75 | return $mountpoint if grep { |
76 | $_->[2] eq 'fuse.glusterfs' && | |
aed6c85d | 77 | $_->[0] =~ /^\S+:\Q$volume\E$/ && |
80b64788 WB |
78 | $_->[1] eq $mountpoint |
79 | } @$mountdata; | |
f4648aef AD |
80 | return undef; |
81 | } | |
82 | ||
83 | sub glusterfs_mount { | |
84 | my ($server, $volume, $mountpoint) = @_; | |
85 | ||
86 | my $source = "$server:$volume"; | |
87 | ||
88 | my $cmd = ['/bin/mount', '-t', 'glusterfs', $source, $mountpoint]; | |
89 | ||
90 | run_command($cmd, errmsg => "mount error"); | |
91 | } | |
92 | ||
93 | # Configuration | |
94 | ||
95 | sub type { | |
96 | return 'glusterfs'; | |
97 | } | |
98 | ||
99 | sub plugindata { | |
100 | return { | |
7a047fce | 101 | content => [ { images => 1, vztmpl => 1, iso => 1, backup => 1}, |
f4648aef AD |
102 | { images => 1 }], |
103 | format => [ { raw => 1, qcow2 => 1, vmdk => 1 } , 'raw' ], | |
104 | }; | |
1a3459ac | 105 | } |
f4648aef AD |
106 | |
107 | sub properties { | |
108 | return { | |
109 | volume => { | |
110 | description => "Glusterfs Volume.", | |
111 | type => 'string', | |
112 | }, | |
a66159e3 DM |
113 | server2 => { |
114 | description => "Backup volfile server IP or DNS name.", | |
115 | type => 'string', format => 'pve-storage-server', | |
116 | requires => 'server', | |
117 | }, | |
187ca539 SM |
118 | transport => { |
119 | description => "Gluster transport: tcp or rdma", | |
120 | type => 'string', | |
121 | enum => ['tcp', 'rdma', 'unix'], | |
122 | }, | |
f4648aef AD |
123 | }; |
124 | } | |
125 | ||
126 | sub options { | |
127 | return { | |
128 | path => { fixed => 1 }, | |
129 | server => { optional => 1 }, | |
a66159e3 | 130 | server2 => { optional => 1 }, |
f4648aef | 131 | volume => { fixed => 1 }, |
187ca539 | 132 | transport => { optional => 1 }, |
f4648aef AD |
133 | nodes => { optional => 1 }, |
134 | disable => { optional => 1 }, | |
135 | maxfiles => { optional => 1 }, | |
136 | content => { optional => 1 }, | |
137 | format => { optional => 1 }, | |
c7616abc | 138 | mkdir => { optional => 1 }, |
9edb99a5 | 139 | bwlimit => { optional => 1 }, |
f4648aef AD |
140 | }; |
141 | } | |
142 | ||
143 | ||
144 | sub check_config { | |
145 | my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_; | |
146 | ||
147 | $config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path}; | |
148 | ||
149 | return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck); | |
150 | } | |
151 | ||
152 | # Storage implementation | |
153 | ||
db7922dc AD |
154 | sub parse_name_dir { |
155 | my $name = shift; | |
156 | ||
157 | if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk))$!) { | |
158 | return ($1, $3, $2); | |
159 | } | |
160 | ||
161 | die "unable to parse volume filename '$name'\n"; | |
162 | } | |
163 | ||
164 | my $find_free_diskname = sub { | |
165 | my ($imgdir, $vmid, $fmt) = @_; | |
166 | ||
167 | my $disk_ids = {}; | |
168 | PVE::Tools::dir_glob_foreach($imgdir, | |
169 | qr!(vm|base)-$vmid-disk-(\d+)\..*!, | |
170 | sub { | |
171 | my ($fn, $type, $disk) = @_; | |
172 | $disk_ids->{$disk} = 1; | |
173 | }); | |
174 | ||
175 | for (my $i = 1; $i < 100; $i++) { | |
176 | if (!$disk_ids->{$i}) { | |
177 | return "vm-$vmid-disk-$i.$fmt"; | |
178 | } | |
179 | } | |
180 | ||
181 | die "unable to allocate a new image name for VM $vmid in '$imgdir'\n"; | |
182 | }; | |
183 | ||
f4648aef | 184 | sub path { |
e67069eb | 185 | my ($class, $scfg, $volname, $storeid, $snapname) = @_; |
f4648aef | 186 | |
e67069eb DM |
187 | my ($vtype, $name, $vmid, undef, undef, $isBase, $format) = |
188 | $class->parse_volname($volname); | |
189 | ||
190 | # Note: qcow2/qed has internal snapshot, so path is always | |
191 | # the same (with or without snapshot => same file). | |
192 | die "can't snapshot this image format\n" | |
193 | if defined($snapname) && $format !~ m/^(qcow2|qed)$/; | |
f4648aef | 194 | |
f4648aef | 195 | my $path = undef; |
a66159e3 DM |
196 | if ($vtype eq 'images') { |
197 | ||
198 | my $server = &$get_active_server($scfg, 1); | |
199 | my $glustervolume = $scfg->{volume}; | |
187ca539 SM |
200 | my $transport = $scfg->{transport}; |
201 | my $protocol = "gluster"; | |
202 | ||
203 | if ($transport) { | |
204 | $protocol = "gluster+$transport"; | |
205 | } | |
a66159e3 | 206 | |
187ca539 | 207 | $path = "$protocol://$server/$glustervolume/images/$vmid/$name"; |
a66159e3 DM |
208 | |
209 | } else { | |
f4648aef AD |
210 | my $dir = $class->get_subdir($scfg, $vtype); |
211 | $path = "$dir/$name"; | |
212 | } | |
213 | ||
f4648aef AD |
214 | return wantarray ? ($path, $vmid, $vtype) : $path; |
215 | } | |
216 | ||
2ff3b506 DC |
217 | sub clone_image { |
218 | my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_; | |
219 | ||
220 | die "storage definintion has no path\n" if !$scfg->{path}; | |
221 | ||
222 | my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) = | |
223 | $class->parse_volname($volname); | |
224 | ||
225 | die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images'; | |
226 | ||
227 | die "this storage type does not support clone_image on snapshot\n" if $snap; | |
228 | ||
229 | die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol'; | |
230 | ||
231 | die "clone_image only works on base images\n" if !$isBase; | |
232 | ||
233 | my $imagedir = $class->get_subdir($scfg, 'images'); | |
234 | $imagedir .= "/$vmid"; | |
235 | ||
236 | mkpath $imagedir; | |
237 | ||
238 | my $name = &$find_free_diskname($imagedir, $vmid, "qcow2"); | |
239 | ||
240 | warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n"; | |
241 | ||
242 | my $path = "$imagedir/$name"; | |
243 | ||
244 | die "disk image '$path' already exists\n" if -e $path; | |
245 | ||
246 | my $server = &$get_active_server($scfg, 1); | |
247 | my $glustervolume = $scfg->{volume}; | |
248 | my $volumepath = "gluster://$server/$glustervolume/images/$vmid/$name"; | |
249 | ||
250 | my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename", | |
251 | '-f', 'qcow2', $volumepath]; | |
252 | ||
253 | run_command($cmd, errmsg => "unable to create image"); | |
254 | ||
255 | return "$basevmid/$basename/$vmid/$name"; | |
256 | } | |
257 | ||
db7922dc AD |
258 | sub alloc_image { |
259 | my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; | |
260 | ||
261 | my $imagedir = $class->get_subdir($scfg, 'images'); | |
262 | $imagedir .= "/$vmid"; | |
263 | ||
264 | mkpath $imagedir; | |
265 | ||
266 | $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name; | |
267 | ||
268 | my (undef, $tmpfmt) = parse_name_dir($name); | |
269 | ||
270 | die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n" | |
271 | if $tmpfmt ne $fmt; | |
272 | ||
273 | my $path = "$imagedir/$name"; | |
274 | ||
275 | die "disk image '$path' already exists\n" if -e $path; | |
276 | ||
a66159e3 | 277 | my $server = &$get_active_server($scfg, 1); |
db7922dc AD |
278 | my $glustervolume = $scfg->{volume}; |
279 | my $volumepath = "gluster://$server/$glustervolume/images/$vmid/$name"; | |
280 | ||
281 | my $cmd = ['/usr/bin/qemu-img', 'create']; | |
282 | ||
283 | push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2'; | |
284 | ||
285 | push @$cmd, '-f', $fmt, $volumepath, "${size}K"; | |
286 | ||
287 | run_command($cmd, errmsg => "unable to create image"); | |
288 | ||
289 | return "$vmid/$name"; | |
290 | } | |
291 | ||
f4648aef AD |
292 | sub status { |
293 | my ($class, $storeid, $scfg, $cache) = @_; | |
294 | ||
80b64788 | 295 | $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts() |
84c8e52d | 296 | if !$cache->{mountdata}; |
f4648aef AD |
297 | |
298 | my $path = $scfg->{path}; | |
f4648aef AD |
299 | |
300 | my $volume = $scfg->{volume}; | |
301 | ||
a66159e3 | 302 | return undef if !glusterfs_is_mounted($volume, $path, $cache->{mountdata}); |
f4648aef AD |
303 | |
304 | return $class->SUPER::status($storeid, $scfg, $cache); | |
305 | } | |
306 | ||
307 | sub activate_storage { | |
308 | my ($class, $storeid, $scfg, $cache) = @_; | |
309 | ||
80b64788 | 310 | $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts() |
84c8e52d | 311 | if !$cache->{mountdata}; |
f4648aef AD |
312 | |
313 | my $path = $scfg->{path}; | |
f4648aef AD |
314 | my $volume = $scfg->{volume}; |
315 | ||
a66159e3 DM |
316 | if (!glusterfs_is_mounted($volume, $path, $cache->{mountdata})) { |
317 | ||
c7616abc | 318 | mkpath $path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir}); |
f4648aef AD |
319 | |
320 | die "unable to activate storage '$storeid' - " . | |
321 | "directory '$path' does not exist\n" if ! -d $path; | |
322 | ||
a66159e3 DM |
323 | my $server = &$get_active_server($scfg, 1); |
324 | ||
325 | glusterfs_mount($server, $volume, $path); | |
f4648aef AD |
326 | } |
327 | ||
328 | $class->SUPER::activate_storage($storeid, $scfg, $cache); | |
329 | } | |
330 | ||
331 | sub deactivate_storage { | |
332 | my ($class, $storeid, $scfg, $cache) = @_; | |
333 | ||
80b64788 | 334 | $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts() |
84c8e52d | 335 | if !$cache->{mountdata}; |
f4648aef AD |
336 | |
337 | my $path = $scfg->{path}; | |
f4648aef AD |
338 | my $volume = $scfg->{volume}; |
339 | ||
a66159e3 | 340 | if (glusterfs_is_mounted($volume, $path, $cache->{mountdata})) { |
f4648aef | 341 | my $cmd = ['/bin/umount', $path]; |
1a3459ac | 342 | run_command($cmd, errmsg => 'umount error'); |
f4648aef AD |
343 | } |
344 | } | |
345 | ||
346 | sub activate_volume { | |
02e797b8 | 347 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; |
f4648aef AD |
348 | |
349 | # do nothing by default | |
350 | } | |
351 | ||
352 | sub deactivate_volume { | |
02e797b8 | 353 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; |
f4648aef AD |
354 | |
355 | # do nothing by default | |
356 | } | |
357 | ||
358 | sub check_connection { | |
a66159e3 | 359 | my ($class, $storeid, $scfg, $cache) = @_; |
f4648aef | 360 | |
a66159e3 | 361 | my $server = &$get_active_server($scfg); |
f4648aef | 362 | |
a66159e3 | 363 | return defined($server) ? 1 : 0; |
f4648aef AD |
364 | } |
365 | ||
366 | 1; |