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