]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/GlusterfsPlugin.pm
glusterfs: new option server2 to specify backup volfile 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') {
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 };
130 }
131
132 sub options {
133 return {
134 path => { fixed => 1 },
135 server => { optional => 1 },
136 server2 => { optional => 1 },
137 volume => { fixed => 1 },
138 nodes => { optional => 1 },
139 disable => { optional => 1 },
140 maxfiles => { optional => 1 },
141 content => { optional => 1 },
142 format => { optional => 1 },
143 };
144 }
145
146
147 sub check_config {
148 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
149
150 $config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path};
151
152 return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck);
153 }
154
155 # Storage implementation
156
157 sub parse_name_dir {
158 my $name = shift;
159
160 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk))$!) {
161 return ($1, $3, $2);
162 }
163
164 die "unable to parse volume filename '$name'\n";
165 }
166
167 my $find_free_diskname = sub {
168 my ($imgdir, $vmid, $fmt) = @_;
169
170 my $disk_ids = {};
171 PVE::Tools::dir_glob_foreach($imgdir,
172 qr!(vm|base)-$vmid-disk-(\d+)\..*!,
173 sub {
174 my ($fn, $type, $disk) = @_;
175 $disk_ids->{$disk} = 1;
176 });
177
178 for (my $i = 1; $i < 100; $i++) {
179 if (!$disk_ids->{$i}) {
180 return "vm-$vmid-disk-$i.$fmt";
181 }
182 }
183
184 die "unable to allocate a new image name for VM $vmid in '$imgdir'\n";
185 };
186
187 sub path {
188 my ($class, $scfg, $volname, $storeid) = @_;
189
190 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
191
192 my $path = undef;
193 if ($vtype eq 'images') {
194
195 my $server = &$get_active_server($scfg, 1);
196 my $glustervolume = $scfg->{volume};
197
198 $path = "gluster://$server/$glustervolume/images/$vmid/$name";
199
200 } else {
201 my $dir = $class->get_subdir($scfg, $vtype);
202 $path = "$dir/$name";
203 }
204
205 return wantarray ? ($path, $vmid, $vtype) : $path;
206 }
207
208 sub alloc_image {
209 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
210
211 my $imagedir = $class->get_subdir($scfg, 'images');
212 $imagedir .= "/$vmid";
213
214 mkpath $imagedir;
215
216 $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name;
217
218 my (undef, $tmpfmt) = parse_name_dir($name);
219
220 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
221 if $tmpfmt ne $fmt;
222
223 my $path = "$imagedir/$name";
224
225 die "disk image '$path' already exists\n" if -e $path;
226
227 my $server = &$get_active_server($scfg, 1);
228 my $glustervolume = $scfg->{volume};
229 my $volumepath = "gluster://$server/$glustervolume/images/$vmid/$name";
230
231 my $cmd = ['/usr/bin/qemu-img', 'create'];
232
233 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
234
235 push @$cmd, '-f', $fmt, $volumepath, "${size}K";
236
237 run_command($cmd, errmsg => "unable to create image");
238
239 return "$vmid/$name";
240 }
241
242 sub status {
243 my ($class, $storeid, $scfg, $cache) = @_;
244
245 $cache->{mountdata} = read_proc_mounts() if !$cache->{mountdata};
246
247 my $path = $scfg->{path};
248
249 my $volume = $scfg->{volume};
250
251 return undef if !glusterfs_is_mounted($volume, $path, $cache->{mountdata});
252
253 return $class->SUPER::status($storeid, $scfg, $cache);
254 }
255
256 sub activate_storage {
257 my ($class, $storeid, $scfg, $cache) = @_;
258
259 $cache->{mountdata} = read_proc_mounts() if !$cache->{mountdata};
260
261 my $path = $scfg->{path};
262 my $volume = $scfg->{volume};
263
264 if (!glusterfs_is_mounted($volume, $path, $cache->{mountdata})) {
265
266 mkpath $path;
267
268 die "unable to activate storage '$storeid' - " .
269 "directory '$path' does not exist\n" if ! -d $path;
270
271 my $server = &$get_active_server($scfg, 1);
272
273 glusterfs_mount($server, $volume, $path);
274 }
275
276 $class->SUPER::activate_storage($storeid, $scfg, $cache);
277 }
278
279 sub deactivate_storage {
280 my ($class, $storeid, $scfg, $cache) = @_;
281
282 $cache->{mountdata} = read_proc_mounts() if !$cache->{mountdata};
283
284 my $path = $scfg->{path};
285 my $volume = $scfg->{volume};
286
287 if (glusterfs_is_mounted($volume, $path, $cache->{mountdata})) {
288 my $cmd = ['/bin/umount', $path];
289 run_command($cmd, errmsg => 'umount error');
290 }
291 }
292
293 sub activate_volume {
294 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
295
296 # do nothing by default
297 }
298
299 sub deactivate_volume {
300 my ($class, $storeid, $scfg, $volname, $cache) = @_;
301
302 # do nothing by default
303 }
304
305 sub check_connection {
306 my ($class, $storeid, $scfg, $cache) = @_;
307
308 my $server = &$get_active_server($scfg);
309
310 return defined($server) ? 1 : 0;
311 }
312
313 1;