]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/GlusterfsPlugin.pm
include ::1 in localhost check in get_active_server
[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::Storage::Plugin;
9 use PVE::JSONSchema qw(get_standard_option);
10 use Net::Ping;
11
12 use base qw(PVE::Storage::Plugin);
13
14 # Glusterfs helper functions
15
16 my $server_test_results = {};
17
18 my $get_active_server = sub {
19 my ($scfg, $return_default_if_offline) = @_;
20
21 my $defaultserver = $scfg->{server} ? $scfg->{server} : 'localhost';
22
23 if ($return_default_if_offline && !defined($scfg->{server2})) {
24 # avoid delays (there is no backup server anyways)
25 return $defaultserver;
26 }
27
28 my $serverlist = [ $defaultserver ];
29 push @$serverlist, $scfg->{server2} if $scfg->{server2};
30
31 my $ctime = time();
32 foreach my $server (@$serverlist) {
33 my $stat = $server_test_results->{$server};
34 return $server if $stat && $stat->{active} && (($ctime - $stat->{time}) <= 2);
35 }
36
37 foreach my $server (@$serverlist) {
38 my $status = 0;
39
40 if ($server && $server ne 'localhost' && $server ne '127.0.0.1' && $server ne '::1') {
41
42 my $p = Net::Ping->new("tcp", 2);
43 $status = $p->ping($server);
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 read_proc_mounts {
70
71 local $/; # enable slurp mode
72
73 my $data = "";
74 if (my $fd = IO::File->new("/proc/mounts", "r")) {
75 $data = <$fd>;
76 close ($fd);
77 }
78
79 return $data;
80 }
81
82 sub glusterfs_is_mounted {
83 my ($volume, $mountpoint, $mountdata) = @_;
84
85 $mountdata = read_proc_mounts() if !$mountdata;
86
87 if ($mountdata =~ m|^\S+:$volume/?\s$mountpoint\sfuse.glusterfs|m) {
88 return $mountpoint;
89 }
90
91 return undef;
92 }
93
94 sub glusterfs_mount {
95 my ($server, $volume, $mountpoint) = @_;
96
97 my $source = "$server:$volume";
98
99 my $cmd = ['/bin/mount', '-t', 'glusterfs', $source, $mountpoint];
100
101 run_command($cmd, errmsg => "mount error");
102 }
103
104 # Configuration
105
106 sub type {
107 return 'glusterfs';
108 }
109
110 sub plugindata {
111 return {
112 content => [ { images => 1, rootdir => 1, vztmpl => 1, iso => 1, backup => 1},
113 { images => 1 }],
114 format => [ { raw => 1, qcow2 => 1, vmdk => 1 } , 'raw' ],
115 };
116 }
117
118 sub properties {
119 return {
120 volume => {
121 description => "Glusterfs Volume.",
122 type => 'string',
123 },
124 server2 => {
125 description => "Backup volfile server IP or DNS name.",
126 type => 'string', format => 'pve-storage-server',
127 requires => 'server',
128 },
129 transport => {
130 description => "Gluster transport: tcp or rdma",
131 type => 'string',
132 enum => ['tcp', 'rdma', 'unix'],
133 },
134 };
135 }
136
137 sub options {
138 return {
139 path => { fixed => 1 },
140 server => { optional => 1 },
141 server2 => { optional => 1 },
142 volume => { fixed => 1 },
143 transport => { optional => 1 },
144 nodes => { optional => 1 },
145 disable => { optional => 1 },
146 maxfiles => { optional => 1 },
147 content => { optional => 1 },
148 format => { optional => 1 },
149 };
150 }
151
152
153 sub check_config {
154 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
155
156 $config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path};
157
158 return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck);
159 }
160
161 # Storage implementation
162
163 sub parse_name_dir {
164 my $name = shift;
165
166 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk))$!) {
167 return ($1, $3, $2);
168 }
169
170 die "unable to parse volume filename '$name'\n";
171 }
172
173 my $find_free_diskname = sub {
174 my ($imgdir, $vmid, $fmt) = @_;
175
176 my $disk_ids = {};
177 PVE::Tools::dir_glob_foreach($imgdir,
178 qr!(vm|base)-$vmid-disk-(\d+)\..*!,
179 sub {
180 my ($fn, $type, $disk) = @_;
181 $disk_ids->{$disk} = 1;
182 });
183
184 for (my $i = 1; $i < 100; $i++) {
185 if (!$disk_ids->{$i}) {
186 return "vm-$vmid-disk-$i.$fmt";
187 }
188 }
189
190 die "unable to allocate a new image name for VM $vmid in '$imgdir'\n";
191 };
192
193 sub path {
194 my ($class, $scfg, $volname, $storeid) = @_;
195
196 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
197
198 my $path = undef;
199 if ($vtype eq 'images') {
200
201 my $server = &$get_active_server($scfg, 1);
202 my $glustervolume = $scfg->{volume};
203 my $transport = $scfg->{transport};
204 my $protocol = "gluster";
205
206 if ($transport) {
207 $protocol = "gluster+$transport";
208 }
209
210 $path = "$protocol://$server/$glustervolume/images/$vmid/$name";
211
212 } else {
213 my $dir = $class->get_subdir($scfg, $vtype);
214 $path = "$dir/$name";
215 }
216
217 return wantarray ? ($path, $vmid, $vtype) : $path;
218 }
219
220 sub alloc_image {
221 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
222
223 my $imagedir = $class->get_subdir($scfg, 'images');
224 $imagedir .= "/$vmid";
225
226 mkpath $imagedir;
227
228 $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name;
229
230 my (undef, $tmpfmt) = parse_name_dir($name);
231
232 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
233 if $tmpfmt ne $fmt;
234
235 my $path = "$imagedir/$name";
236
237 die "disk image '$path' already exists\n" if -e $path;
238
239 my $server = &$get_active_server($scfg, 1);
240 my $glustervolume = $scfg->{volume};
241 my $volumepath = "gluster://$server/$glustervolume/images/$vmid/$name";
242
243 my $cmd = ['/usr/bin/qemu-img', 'create'];
244
245 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
246
247 push @$cmd, '-f', $fmt, $volumepath, "${size}K";
248
249 run_command($cmd, errmsg => "unable to create image");
250
251 return "$vmid/$name";
252 }
253
254 sub status {
255 my ($class, $storeid, $scfg, $cache) = @_;
256
257 $cache->{mountdata} = read_proc_mounts() if !$cache->{mountdata};
258
259 my $path = $scfg->{path};
260
261 my $volume = $scfg->{volume};
262
263 return undef if !glusterfs_is_mounted($volume, $path, $cache->{mountdata});
264
265 return $class->SUPER::status($storeid, $scfg, $cache);
266 }
267
268 sub activate_storage {
269 my ($class, $storeid, $scfg, $cache) = @_;
270
271 $cache->{mountdata} = read_proc_mounts() if !$cache->{mountdata};
272
273 my $path = $scfg->{path};
274 my $volume = $scfg->{volume};
275
276 if (!glusterfs_is_mounted($volume, $path, $cache->{mountdata})) {
277
278 mkpath $path;
279
280 die "unable to activate storage '$storeid' - " .
281 "directory '$path' does not exist\n" if ! -d $path;
282
283 my $server = &$get_active_server($scfg, 1);
284
285 glusterfs_mount($server, $volume, $path);
286 }
287
288 $class->SUPER::activate_storage($storeid, $scfg, $cache);
289 }
290
291 sub deactivate_storage {
292 my ($class, $storeid, $scfg, $cache) = @_;
293
294 $cache->{mountdata} = read_proc_mounts() if !$cache->{mountdata};
295
296 my $path = $scfg->{path};
297 my $volume = $scfg->{volume};
298
299 if (glusterfs_is_mounted($volume, $path, $cache->{mountdata})) {
300 my $cmd = ['/bin/umount', $path];
301 run_command($cmd, errmsg => 'umount error');
302 }
303 }
304
305 sub activate_volume {
306 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
307
308 # do nothing by default
309 }
310
311 sub deactivate_volume {
312 my ($class, $storeid, $scfg, $volname, $cache) = @_;
313
314 # do nothing by default
315 }
316
317 sub check_connection {
318 my ($class, $storeid, $scfg, $cache) = @_;
319
320 my $server = &$get_active_server($scfg);
321
322 return defined($server) ? 1 : 0;
323 }
324
325 1;