]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/GlusterfsPlugin.pm
move read_proc_mounts from NFSPlugin to Tools.
[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);
be48449c 8use PVE::Network;
f4648aef
AD
9use PVE::Storage::Plugin;
10use PVE::JSONSchema qw(get_standard_option);
f4648aef
AD
11
12use base qw(PVE::Storage::Plugin);
13
14# Glusterfs helper functions
15
a66159e3
DM
16my $server_test_results = {};
17
18my $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
65644196 40 if ($server && $server ne 'localhost' && $server ne '127.0.0.1' && $server ne '::1') {
a66159e3 41
be48449c
WB
42 # ping the echo port (7) without service check
43 $status = PVE::Network::tcp_ping($server, undef, 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 read_proc_mounts {
1a3459ac 70
f4648aef 71 local $/; # enable slurp mode
1a3459ac 72
f4648aef
AD
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
82sub glusterfs_is_mounted {
a66159e3 83 my ($volume, $mountpoint, $mountdata) = @_;
f4648aef
AD
84
85 $mountdata = read_proc_mounts() if !$mountdata;
86
a66159e3 87 if ($mountdata =~ m|^\S+:$volume/?\s$mountpoint\sfuse.glusterfs|m) {
f4648aef 88 return $mountpoint;
1a3459ac 89 }
f4648aef
AD
90
91 return undef;
92}
93
94sub 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
106sub type {
107 return 'glusterfs';
108}
109
110sub 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 };
1a3459ac 116}
f4648aef
AD
117
118sub properties {
119 return {
120 volume => {
121 description => "Glusterfs Volume.",
122 type => 'string',
123 },
a66159e3
DM
124 server2 => {
125 description => "Backup volfile server IP or DNS name.",
126 type => 'string', format => 'pve-storage-server',
127 requires => 'server',
128 },
187ca539
SM
129 transport => {
130 description => "Gluster transport: tcp or rdma",
131 type => 'string',
132 enum => ['tcp', 'rdma', 'unix'],
133 },
f4648aef
AD
134 };
135}
136
137sub options {
138 return {
139 path => { fixed => 1 },
140 server => { optional => 1 },
a66159e3 141 server2 => { optional => 1 },
f4648aef 142 volume => { fixed => 1 },
187ca539 143 transport => { optional => 1 },
f4648aef
AD
144 nodes => { optional => 1 },
145 disable => { optional => 1 },
146 maxfiles => { optional => 1 },
147 content => { optional => 1 },
148 format => { optional => 1 },
149 };
150}
151
152
153sub 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
db7922dc
AD
163sub 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
173my $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
f4648aef 193sub path {
e67069eb 194 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
f4648aef 195
e67069eb
DM
196 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
197 $class->parse_volname($volname);
198
199 # Note: qcow2/qed has internal snapshot, so path is always
200 # the same (with or without snapshot => same file).
201 die "can't snapshot this image format\n"
202 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
f4648aef 203
f4648aef 204 my $path = undef;
a66159e3
DM
205 if ($vtype eq 'images') {
206
207 my $server = &$get_active_server($scfg, 1);
208 my $glustervolume = $scfg->{volume};
187ca539
SM
209 my $transport = $scfg->{transport};
210 my $protocol = "gluster";
211
212 if ($transport) {
213 $protocol = "gluster+$transport";
214 }
a66159e3 215
187ca539 216 $path = "$protocol://$server/$glustervolume/images/$vmid/$name";
a66159e3
DM
217
218 } else {
f4648aef
AD
219 my $dir = $class->get_subdir($scfg, $vtype);
220 $path = "$dir/$name";
221 }
222
f4648aef
AD
223 return wantarray ? ($path, $vmid, $vtype) : $path;
224}
225
db7922dc
AD
226sub alloc_image {
227 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
228
229 my $imagedir = $class->get_subdir($scfg, 'images');
230 $imagedir .= "/$vmid";
231
232 mkpath $imagedir;
233
234 $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name;
235
236 my (undef, $tmpfmt) = parse_name_dir($name);
237
238 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
239 if $tmpfmt ne $fmt;
240
241 my $path = "$imagedir/$name";
242
243 die "disk image '$path' already exists\n" if -e $path;
244
a66159e3 245 my $server = &$get_active_server($scfg, 1);
db7922dc
AD
246 my $glustervolume = $scfg->{volume};
247 my $volumepath = "gluster://$server/$glustervolume/images/$vmid/$name";
248
249 my $cmd = ['/usr/bin/qemu-img', 'create'];
250
251 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
252
253 push @$cmd, '-f', $fmt, $volumepath, "${size}K";
254
255 run_command($cmd, errmsg => "unable to create image");
256
257 return "$vmid/$name";
258}
259
f4648aef
AD
260sub status {
261 my ($class, $storeid, $scfg, $cache) = @_;
262
263 $cache->{mountdata} = read_proc_mounts() if !$cache->{mountdata};
264
265 my $path = $scfg->{path};
f4648aef
AD
266
267 my $volume = $scfg->{volume};
268
a66159e3 269 return undef if !glusterfs_is_mounted($volume, $path, $cache->{mountdata});
f4648aef
AD
270
271 return $class->SUPER::status($storeid, $scfg, $cache);
272}
273
274sub activate_storage {
275 my ($class, $storeid, $scfg, $cache) = @_;
276
277 $cache->{mountdata} = read_proc_mounts() if !$cache->{mountdata};
278
279 my $path = $scfg->{path};
f4648aef
AD
280 my $volume = $scfg->{volume};
281
a66159e3
DM
282 if (!glusterfs_is_mounted($volume, $path, $cache->{mountdata})) {
283
f4648aef
AD
284 mkpath $path;
285
286 die "unable to activate storage '$storeid' - " .
287 "directory '$path' does not exist\n" if ! -d $path;
288
a66159e3
DM
289 my $server = &$get_active_server($scfg, 1);
290
291 glusterfs_mount($server, $volume, $path);
f4648aef
AD
292 }
293
294 $class->SUPER::activate_storage($storeid, $scfg, $cache);
295}
296
297sub deactivate_storage {
298 my ($class, $storeid, $scfg, $cache) = @_;
299
300 $cache->{mountdata} = read_proc_mounts() if !$cache->{mountdata};
301
302 my $path = $scfg->{path};
f4648aef
AD
303 my $volume = $scfg->{volume};
304
a66159e3 305 if (glusterfs_is_mounted($volume, $path, $cache->{mountdata})) {
f4648aef 306 my $cmd = ['/bin/umount', $path];
1a3459ac 307 run_command($cmd, errmsg => 'umount error');
f4648aef
AD
308 }
309}
310
311sub activate_volume {
312 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
313
314 # do nothing by default
315}
316
317sub deactivate_volume {
318 my ($class, $storeid, $scfg, $volname, $cache) = @_;
319
320 # do nothing by default
321}
322
323sub check_connection {
a66159e3 324 my ($class, $storeid, $scfg, $cache) = @_;
f4648aef 325
a66159e3 326 my $server = &$get_active_server($scfg);
f4648aef 327
a66159e3 328 return defined($server) ? 1 : 0;
f4648aef
AD
329}
330
3311;