]> git.proxmox.com Git - pve-storage.git/blob - src/PVE/Storage/GlusterfsPlugin.pm
bump version to 8.1.5
[pve-storage.git] / src / PVE / Storage / GlusterfsPlugin.pm
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);
8 use PVE::ProcFSTools;
9 use PVE::Network;
10 use PVE::Storage::Plugin;
11 use PVE::JSONSchema qw(get_standard_option);
12
13 use base qw(PVE::Storage::Plugin);
14
15 # Glusterfs helper functions
16
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
41 if ($server && $server ne 'localhost' && $server ne '127.0.0.1' && $server ne '::1') {
42 # ping the gluster daemon default port (24007) as heuristic
43 $status = PVE::Network::tcp_ping($server, 24007, 2);
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
69 sub glusterfs_is_mounted {
70 my ($volume, $mountpoint, $mountdata) = @_;
71
72 $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata;
73
74 return $mountpoint if grep {
75 $_->[2] eq 'fuse.glusterfs' &&
76 $_->[0] =~ /^\S+:\Q$volume\E$/ &&
77 $_->[1] eq $mountpoint
78 } @$mountdata;
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 {
100 content => [ { images => 1, vztmpl => 1, iso => 1, backup => 1, snippets => 1},
101 { images => 1 }],
102 format => [ { raw => 1, qcow2 => 1, vmdk => 1 } , 'raw' ],
103 };
104 }
105
106 sub properties {
107 return {
108 volume => {
109 description => "Glusterfs Volume.",
110 type => 'string',
111 },
112 server2 => {
113 description => "Backup volfile server IP or DNS name.",
114 type => 'string', format => 'pve-storage-server',
115 requires => 'server',
116 },
117 transport => {
118 description => "Gluster transport: tcp or rdma",
119 type => 'string',
120 enum => ['tcp', 'rdma', 'unix'],
121 },
122 };
123 }
124
125 sub options {
126 return {
127 path => { fixed => 1 },
128 server => { optional => 1 },
129 server2 => { optional => 1 },
130 volume => { fixed => 1 },
131 transport => { optional => 1 },
132 nodes => { optional => 1 },
133 disable => { optional => 1 },
134 maxfiles => { optional => 1 },
135 'prune-backups' => { optional => 1 },
136 'max-protected-backups' => { optional => 1 },
137 content => { optional => 1 },
138 format => { optional => 1 },
139 mkdir => { optional => 1 },
140 'create-base-path' => { optional => 1 },
141 'create-subdirs' => { optional => 1 },
142 bwlimit => { optional => 1 },
143 preallocation => { optional => 1 },
144 };
145 }
146
147
148 sub check_config {
149 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
150
151 $config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path};
152
153 return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck);
154 }
155
156 # Storage implementation
157
158 sub parse_name_dir {
159 my $name = shift;
160
161 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk))$!) {
162 return ($1, $3, $2);
163 }
164
165 die "unable to parse volume filename '$name'\n";
166 }
167
168 sub path {
169 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
170
171 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
172 $class->parse_volname($volname);
173
174 # Note: qcow2/qed has internal snapshot, so path is always
175 # the same (with or without snapshot => same file).
176 die "can't snapshot this image format\n"
177 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
178
179 my $path = undef;
180 if ($vtype eq 'images') {
181
182 my $server = &$get_active_server($scfg, 1);
183 my $glustervolume = $scfg->{volume};
184 my $transport = $scfg->{transport};
185 my $protocol = "gluster";
186
187 if ($transport) {
188 $protocol = "gluster+$transport";
189 }
190
191 $path = "$protocol://$server/$glustervolume/images/$vmid/$name";
192
193 } else {
194 my $dir = $class->get_subdir($scfg, $vtype);
195 $path = "$dir/$name";
196 }
197
198 return wantarray ? ($path, $vmid, $vtype) : $path;
199 }
200
201 sub clone_image {
202 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
203
204 die "storage definition has no path\n" if !$scfg->{path};
205
206 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
207 $class->parse_volname($volname);
208
209 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
210
211 die "this storage type does not support clone_image on snapshot\n" if $snap;
212
213 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
214
215 die "clone_image only works on base images\n" if !$isBase;
216
217 my $imagedir = $class->get_subdir($scfg, 'images');
218 $imagedir .= "/$vmid";
219
220 mkpath $imagedir;
221
222 my $name = $class->find_free_diskname($storeid, $scfg, $vmid, "qcow2", 1);
223
224 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
225
226 my $path = "$imagedir/$name";
227
228 die "disk image '$path' already exists\n" if -e $path;
229
230 my $server = &$get_active_server($scfg, 1);
231 my $glustervolume = $scfg->{volume};
232 my $volumepath = "gluster://$server/$glustervolume/images/$vmid/$name";
233
234 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
235 '-F', $format, '-f', 'qcow2', $volumepath];
236
237 run_command($cmd, errmsg => "unable to create image");
238
239 return "$basevmid/$basename/$vmid/$name";
240 }
241
242 sub alloc_image {
243 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
244
245 my $imagedir = $class->get_subdir($scfg, 'images');
246 $imagedir .= "/$vmid";
247
248 mkpath $imagedir;
249
250 $name = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt, 1) if !$name;
251
252 my (undef, $tmpfmt) = parse_name_dir($name);
253
254 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
255 if $tmpfmt ne $fmt;
256
257 my $path = "$imagedir/$name";
258
259 die "disk image '$path' already exists\n" if -e $path;
260
261 my $server = &$get_active_server($scfg, 1);
262 my $glustervolume = $scfg->{volume};
263 my $volumepath = "gluster://$server/$glustervolume/images/$vmid/$name";
264
265 my $cmd = ['/usr/bin/qemu-img', 'create'];
266
267 my $prealloc_opt = PVE::Storage::Plugin::preallocation_cmd_option($scfg, $fmt);
268 push @$cmd, '-o', $prealloc_opt if defined($prealloc_opt);
269
270 push @$cmd, '-f', $fmt, $volumepath, "${size}K";
271
272 eval { run_command($cmd, errmsg => "unable to create image"); };
273 if ($@) {
274 unlink $path;
275 rmdir $imagedir;
276 die "$@";
277 }
278
279 return "$vmid/$name";
280 }
281
282 sub status {
283 my ($class, $storeid, $scfg, $cache) = @_;
284
285 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
286 if !$cache->{mountdata};
287
288 my $path = $scfg->{path};
289
290 my $volume = $scfg->{volume};
291
292 return undef if !glusterfs_is_mounted($volume, $path, $cache->{mountdata});
293
294 return $class->SUPER::status($storeid, $scfg, $cache);
295 }
296
297 sub activate_storage {
298 my ($class, $storeid, $scfg, $cache) = @_;
299
300 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
301 if !$cache->{mountdata};
302
303 my $path = $scfg->{path};
304 my $volume = $scfg->{volume};
305
306 if (!glusterfs_is_mounted($volume, $path, $cache->{mountdata})) {
307 $class->config_aware_base_mkdir($scfg, $path);
308
309 die "unable to activate storage '$storeid' - " .
310 "directory '$path' does not exist\n" if ! -d $path;
311
312 my $server = &$get_active_server($scfg, 1);
313
314 glusterfs_mount($server, $volume, $path);
315 }
316
317 $class->SUPER::activate_storage($storeid, $scfg, $cache);
318 }
319
320 sub deactivate_storage {
321 my ($class, $storeid, $scfg, $cache) = @_;
322
323 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
324 if !$cache->{mountdata};
325
326 my $path = $scfg->{path};
327 my $volume = $scfg->{volume};
328
329 if (glusterfs_is_mounted($volume, $path, $cache->{mountdata})) {
330 my $cmd = ['/bin/umount', $path];
331 run_command($cmd, errmsg => 'umount error');
332 }
333 }
334
335 sub activate_volume {
336 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
337
338 # do nothing by default
339 }
340
341 sub deactivate_volume {
342 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
343
344 # do nothing by default
345 }
346
347 sub check_connection {
348 my ($class, $storeid, $scfg, $cache) = @_;
349
350 my $server = &$get_active_server($scfg);
351
352 return defined($server) ? 1 : 0;
353 }
354
355 1;