]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/GlusterfsPlugin.pm
makefile: convert to use simple parenthesis
[pve-storage.git] / PVE / Storage / GlusterfsPlugin.pm
CommitLineData
f4648aef
AD
1package PVE::Storage::GlusterfsPlugin;
2
3use strict;
4use warnings;
5use IO::File;
6use File::Path;
7use PVE::Tools qw(run_command);
84c8e52d 8use PVE::ProcFSTools;
be48449c 9use PVE::Network;
f4648aef
AD
10use PVE::Storage::Plugin;
11use PVE::JSONSchema qw(get_standard_option);
f4648aef
AD
12
13use base qw(PVE::Storage::Plugin);
14
15# Glusterfs helper functions
16
a66159e3
DM
17my $server_test_results = {};
18
19my $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 69sub 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
82sub 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
94sub type {
95 return 'glusterfs';
96}
97
98sub plugindata {
99 return {
b9364dc6 100 content => [ { images => 1, vztmpl => 1, iso => 1, backup => 1, snippets => 1},
f4648aef
AD
101 { images => 1 }],
102 format => [ { raw => 1, qcow2 => 1, vmdk => 1 } , 'raw' ],
103 };
1a3459ac 104}
f4648aef
AD
105
106sub 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
125sub 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 },
3353698f 132 nodes => { optional => 1 },
f4648aef 133 disable => { optional => 1 },
3353698f
FE
134 maxfiles => { optional => 1 },
135 'prune-backups' => { optional => 1 },
8009417d 136 'max-protected-backups' => { optional => 1 },
f4648aef
AD
137 content => { optional => 1 },
138 format => { optional => 1 },
c7616abc 139 mkdir => { optional => 1 },
9edb99a5 140 bwlimit => { optional => 1 },
95ff5dbd 141 preallocation => { optional => 1 },
f4648aef
AD
142 };
143}
144
145
146sub check_config {
147 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
148
149 $config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path};
150
151 return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck);
152}
153
154# Storage implementation
155
db7922dc
AD
156sub parse_name_dir {
157 my $name = shift;
158
159 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk))$!) {
160 return ($1, $3, $2);
161 }
162
163 die "unable to parse volume filename '$name'\n";
164}
165
f4648aef 166sub path {
e67069eb 167 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
f4648aef 168
e67069eb
DM
169 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
170 $class->parse_volname($volname);
171
172 # Note: qcow2/qed has internal snapshot, so path is always
173 # the same (with or without snapshot => same file).
174 die "can't snapshot this image format\n"
175 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
f4648aef 176
f4648aef 177 my $path = undef;
a66159e3
DM
178 if ($vtype eq 'images') {
179
180 my $server = &$get_active_server($scfg, 1);
181 my $glustervolume = $scfg->{volume};
187ca539
SM
182 my $transport = $scfg->{transport};
183 my $protocol = "gluster";
184
185 if ($transport) {
186 $protocol = "gluster+$transport";
187 }
a66159e3 188
187ca539 189 $path = "$protocol://$server/$glustervolume/images/$vmid/$name";
a66159e3
DM
190
191 } else {
f4648aef
AD
192 my $dir = $class->get_subdir($scfg, $vtype);
193 $path = "$dir/$name";
194 }
195
f4648aef
AD
196 return wantarray ? ($path, $vmid, $vtype) : $path;
197}
198
2ff3b506
DC
199sub clone_image {
200 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
201
ffc31266 202 die "storage definition has no path\n" if !$scfg->{path};
2ff3b506
DC
203
204 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
205 $class->parse_volname($volname);
206
207 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
208
209 die "this storage type does not support clone_image on snapshot\n" if $snap;
210
211 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
212
213 die "clone_image only works on base images\n" if !$isBase;
214
215 my $imagedir = $class->get_subdir($scfg, 'images');
216 $imagedir .= "/$vmid";
217
218 mkpath $imagedir;
219
d7f6f85e 220 my $name = $class->find_free_diskname($storeid, $scfg, $vmid, "qcow2", 1);
2ff3b506
DC
221
222 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
223
224 my $path = "$imagedir/$name";
225
226 die "disk image '$path' already exists\n" if -e $path;
227
228 my $server = &$get_active_server($scfg, 1);
229 my $glustervolume = $scfg->{volume};
230 my $volumepath = "gluster://$server/$glustervolume/images/$vmid/$name";
231
232 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
9177cc2e 233 '-F', $format, '-f', 'qcow2', $volumepath];
2ff3b506
DC
234
235 run_command($cmd, errmsg => "unable to create image");
236
237 return "$basevmid/$basename/$vmid/$name";
238}
239
db7922dc
AD
240sub alloc_image {
241 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
242
243 my $imagedir = $class->get_subdir($scfg, 'images');
244 $imagedir .= "/$vmid";
245
246 mkpath $imagedir;
247
d7f6f85e 248 $name = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt, 1) if !$name;
db7922dc
AD
249
250 my (undef, $tmpfmt) = parse_name_dir($name);
251
252 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
253 if $tmpfmt ne $fmt;
254
255 my $path = "$imagedir/$name";
256
257 die "disk image '$path' already exists\n" if -e $path;
258
a66159e3 259 my $server = &$get_active_server($scfg, 1);
db7922dc
AD
260 my $glustervolume = $scfg->{volume};
261 my $volumepath = "gluster://$server/$glustervolume/images/$vmid/$name";
262
263 my $cmd = ['/usr/bin/qemu-img', 'create'];
264
95ff5dbd
LS
265 my $prealloc_opt = PVE::Storage::Plugin::preallocation_cmd_option($scfg, $fmt);
266 push @$cmd, '-o', $prealloc_opt if defined($prealloc_opt);
db7922dc
AD
267
268 push @$cmd, '-f', $fmt, $volumepath, "${size}K";
269
a8ec2f02
CE
270 eval { run_command($cmd, errmsg => "unable to create image"); };
271 if ($@) {
272 unlink $path;
273 rmdir $imagedir;
274 die "$@";
275 }
db7922dc
AD
276
277 return "$vmid/$name";
278}
279
f4648aef
AD
280sub status {
281 my ($class, $storeid, $scfg, $cache) = @_;
282
80b64788 283 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
84c8e52d 284 if !$cache->{mountdata};
f4648aef
AD
285
286 my $path = $scfg->{path};
f4648aef
AD
287
288 my $volume = $scfg->{volume};
289
a66159e3 290 return undef if !glusterfs_is_mounted($volume, $path, $cache->{mountdata});
f4648aef
AD
291
292 return $class->SUPER::status($storeid, $scfg, $cache);
293}
294
295sub activate_storage {
296 my ($class, $storeid, $scfg, $cache) = @_;
297
80b64788 298 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
84c8e52d 299 if !$cache->{mountdata};
f4648aef
AD
300
301 my $path = $scfg->{path};
f4648aef
AD
302 my $volume = $scfg->{volume};
303
a66159e3
DM
304 if (!glusterfs_is_mounted($volume, $path, $cache->{mountdata})) {
305
c7616abc 306 mkpath $path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir});
f4648aef
AD
307
308 die "unable to activate storage '$storeid' - " .
309 "directory '$path' does not exist\n" if ! -d $path;
310
a66159e3
DM
311 my $server = &$get_active_server($scfg, 1);
312
313 glusterfs_mount($server, $volume, $path);
f4648aef
AD
314 }
315
316 $class->SUPER::activate_storage($storeid, $scfg, $cache);
317}
318
319sub deactivate_storage {
320 my ($class, $storeid, $scfg, $cache) = @_;
321
80b64788 322 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
84c8e52d 323 if !$cache->{mountdata};
f4648aef
AD
324
325 my $path = $scfg->{path};
f4648aef
AD
326 my $volume = $scfg->{volume};
327
a66159e3 328 if (glusterfs_is_mounted($volume, $path, $cache->{mountdata})) {
f4648aef 329 my $cmd = ['/bin/umount', $path];
1a3459ac 330 run_command($cmd, errmsg => 'umount error');
f4648aef
AD
331 }
332}
333
334sub activate_volume {
02e797b8 335 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
f4648aef
AD
336
337 # do nothing by default
338}
339
340sub deactivate_volume {
02e797b8 341 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
f4648aef
AD
342
343 # do nothing by default
344}
345
346sub check_connection {
a66159e3 347 my ($class, $storeid, $scfg, $cache) = @_;
f4648aef 348
a66159e3 349 my $server = &$get_active_server($scfg);
f4648aef 350
a66159e3 351 return defined($server) ? 1 : 0;
f4648aef
AD
352}
353
3541;