]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/GlusterfsPlugin.pm
export: add missing format query call parameter
[pve-storage.git] / 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
43 # ping the echo port (7) without service check
44 $status = PVE::Network::tcp_ping($server, undef, 2);
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
70 sub glusterfs_is_mounted {
71 my ($volume, $mountpoint, $mountdata) = @_;
72
73 $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata;
74
75 return $mountpoint if grep {
76 $_->[2] eq 'fuse.glusterfs' &&
77 $_->[0] =~ /^\S+:\Q$volume\E$/ &&
78 $_->[1] eq $mountpoint
79 } @$mountdata;
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 {
101 content => [ { images => 1, vztmpl => 1, iso => 1, backup => 1},
102 { images => 1 }],
103 format => [ { raw => 1, qcow2 => 1, vmdk => 1 } , 'raw' ],
104 };
105 }
106
107 sub properties {
108 return {
109 volume => {
110 description => "Glusterfs Volume.",
111 type => 'string',
112 },
113 server2 => {
114 description => "Backup volfile server IP or DNS name.",
115 type => 'string', format => 'pve-storage-server',
116 requires => 'server',
117 },
118 transport => {
119 description => "Gluster transport: tcp or rdma",
120 type => 'string',
121 enum => ['tcp', 'rdma', 'unix'],
122 },
123 };
124 }
125
126 sub options {
127 return {
128 path => { fixed => 1 },
129 server => { optional => 1 },
130 server2 => { optional => 1 },
131 volume => { fixed => 1 },
132 transport => { optional => 1 },
133 nodes => { optional => 1 },
134 disable => { optional => 1 },
135 maxfiles => { optional => 1 },
136 content => { optional => 1 },
137 format => { optional => 1 },
138 mkdir => { optional => 1 },
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
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 {
164 my ($imgdir, $vmid, $fmt) = @_;
165
166 my $disk_ids = {};
167 PVE::Tools::dir_glob_foreach($imgdir,
168 qr!(vm|base)-$vmid-disk-(\d+)\..*!,
169 sub {
170 my ($fn, $type, $disk) = @_;
171 $disk_ids->{$disk} = 1;
172 });
173
174 for (my $i = 1; $i < 100; $i++) {
175 if (!$disk_ids->{$i}) {
176 return "vm-$vmid-disk-$i.$fmt";
177 }
178 }
179
180 die "unable to allocate a new image name for VM $vmid in '$imgdir'\n";
181 };
182
183 sub path {
184 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
185
186 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
187 $class->parse_volname($volname);
188
189 # Note: qcow2/qed has internal snapshot, so path is always
190 # the same (with or without snapshot => same file).
191 die "can't snapshot this image format\n"
192 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
193
194 my $path = undef;
195 if ($vtype eq 'images') {
196
197 my $server = &$get_active_server($scfg, 1);
198 my $glustervolume = $scfg->{volume};
199 my $transport = $scfg->{transport};
200 my $protocol = "gluster";
201
202 if ($transport) {
203 $protocol = "gluster+$transport";
204 }
205
206 $path = "$protocol://$server/$glustervolume/images/$vmid/$name";
207
208 } else {
209 my $dir = $class->get_subdir($scfg, $vtype);
210 $path = "$dir/$name";
211 }
212
213 return wantarray ? ($path, $vmid, $vtype) : $path;
214 }
215
216 sub clone_image {
217 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
218
219 die "storage definintion has no path\n" if !$scfg->{path};
220
221 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
222 $class->parse_volname($volname);
223
224 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
225
226 die "this storage type does not support clone_image on snapshot\n" if $snap;
227
228 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
229
230 die "clone_image only works on base images\n" if !$isBase;
231
232 my $imagedir = $class->get_subdir($scfg, 'images');
233 $imagedir .= "/$vmid";
234
235 mkpath $imagedir;
236
237 my $name = &$find_free_diskname($imagedir, $vmid, "qcow2");
238
239 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
240
241 my $path = "$imagedir/$name";
242
243 die "disk image '$path' already exists\n" if -e $path;
244
245 my $server = &$get_active_server($scfg, 1);
246 my $glustervolume = $scfg->{volume};
247 my $volumepath = "gluster://$server/$glustervolume/images/$vmid/$name";
248
249 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
250 '-f', 'qcow2', $volumepath];
251
252 run_command($cmd, errmsg => "unable to create image");
253
254 return "$basevmid/$basename/$vmid/$name";
255 }
256
257 sub alloc_image {
258 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
259
260 my $imagedir = $class->get_subdir($scfg, 'images');
261 $imagedir .= "/$vmid";
262
263 mkpath $imagedir;
264
265 $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name;
266
267 my (undef, $tmpfmt) = parse_name_dir($name);
268
269 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
270 if $tmpfmt ne $fmt;
271
272 my $path = "$imagedir/$name";
273
274 die "disk image '$path' already exists\n" if -e $path;
275
276 my $server = &$get_active_server($scfg, 1);
277 my $glustervolume = $scfg->{volume};
278 my $volumepath = "gluster://$server/$glustervolume/images/$vmid/$name";
279
280 my $cmd = ['/usr/bin/qemu-img', 'create'];
281
282 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
283
284 push @$cmd, '-f', $fmt, $volumepath, "${size}K";
285
286 run_command($cmd, errmsg => "unable to create image");
287
288 return "$vmid/$name";
289 }
290
291 sub status {
292 my ($class, $storeid, $scfg, $cache) = @_;
293
294 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
295 if !$cache->{mountdata};
296
297 my $path = $scfg->{path};
298
299 my $volume = $scfg->{volume};
300
301 return undef if !glusterfs_is_mounted($volume, $path, $cache->{mountdata});
302
303 return $class->SUPER::status($storeid, $scfg, $cache);
304 }
305
306 sub activate_storage {
307 my ($class, $storeid, $scfg, $cache) = @_;
308
309 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
310 if !$cache->{mountdata};
311
312 my $path = $scfg->{path};
313 my $volume = $scfg->{volume};
314
315 if (!glusterfs_is_mounted($volume, $path, $cache->{mountdata})) {
316
317 mkpath $path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir});
318
319 die "unable to activate storage '$storeid' - " .
320 "directory '$path' does not exist\n" if ! -d $path;
321
322 my $server = &$get_active_server($scfg, 1);
323
324 glusterfs_mount($server, $volume, $path);
325 }
326
327 $class->SUPER::activate_storage($storeid, $scfg, $cache);
328 }
329
330 sub deactivate_storage {
331 my ($class, $storeid, $scfg, $cache) = @_;
332
333 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
334 if !$cache->{mountdata};
335
336 my $path = $scfg->{path};
337 my $volume = $scfg->{volume};
338
339 if (glusterfs_is_mounted($volume, $path, $cache->{mountdata})) {
340 my $cmd = ['/bin/umount', $path];
341 run_command($cmd, errmsg => 'umount error');
342 }
343 }
344
345 sub activate_volume {
346 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
347
348 # do nothing by default
349 }
350
351 sub deactivate_volume {
352 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
353
354 # do nothing by default
355 }
356
357 sub check_connection {
358 my ($class, $storeid, $scfg, $cache) = @_;
359
360 my $server = &$get_active_server($scfg);
361
362 return defined($server) ? 1 : 0;
363 }
364
365 1;