]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/GlusterfsPlugin.pm
Remove content type container from GlusterFS.
[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') {
a66159e3 42
be48449c
WB
43 # ping the echo port (7) without service check
44 $status = PVE::Network::tcp_ping($server, undef, 2);
a66159e3
DM
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
f4648aef 70sub glusterfs_is_mounted {
a66159e3 71 my ($volume, $mountpoint, $mountdata) = @_;
f4648aef 72
80b64788 73 $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata;
f4648aef 74
80b64788
WB
75 return $mountpoint if grep {
76 $_->[2] eq 'fuse.glusterfs' &&
aed6c85d 77 $_->[0] =~ /^\S+:\Q$volume\E$/ &&
80b64788
WB
78 $_->[1] eq $mountpoint
79 } @$mountdata;
f4648aef
AD
80 return undef;
81}
82
83sub 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
95sub type {
96 return 'glusterfs';
97}
98
99sub plugindata {
100 return {
7a047fce 101 content => [ { images => 1, vztmpl => 1, iso => 1, backup => 1},
f4648aef
AD
102 { images => 1 }],
103 format => [ { raw => 1, qcow2 => 1, vmdk => 1 } , 'raw' ],
104 };
1a3459ac 105}
f4648aef
AD
106
107sub properties {
108 return {
109 volume => {
110 description => "Glusterfs Volume.",
111 type => 'string',
112 },
a66159e3
DM
113 server2 => {
114 description => "Backup volfile server IP or DNS name.",
115 type => 'string', format => 'pve-storage-server',
116 requires => 'server',
117 },
187ca539
SM
118 transport => {
119 description => "Gluster transport: tcp or rdma",
120 type => 'string',
121 enum => ['tcp', 'rdma', 'unix'],
122 },
f4648aef
AD
123 };
124}
125
126sub options {
127 return {
128 path => { fixed => 1 },
129 server => { optional => 1 },
a66159e3 130 server2 => { optional => 1 },
f4648aef 131 volume => { fixed => 1 },
187ca539 132 transport => { optional => 1 },
f4648aef
AD
133 nodes => { optional => 1 },
134 disable => { optional => 1 },
135 maxfiles => { optional => 1 },
136 content => { optional => 1 },
137 format => { optional => 1 },
138 };
139}
140
141
142sub check_config {
143 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
144
145 $config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path};
146
147 return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck);
148}
149
150# Storage implementation
151
db7922dc
AD
152sub parse_name_dir {
153 my $name = shift;
154
155 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk))$!) {
156 return ($1, $3, $2);
157 }
158
159 die "unable to parse volume filename '$name'\n";
160}
161
162my $find_free_diskname = sub {
163 my ($imgdir, $vmid, $fmt) = @_;
164
165 my $disk_ids = {};
166 PVE::Tools::dir_glob_foreach($imgdir,
167 qr!(vm|base)-$vmid-disk-(\d+)\..*!,
168 sub {
169 my ($fn, $type, $disk) = @_;
170 $disk_ids->{$disk} = 1;
171 });
172
173 for (my $i = 1; $i < 100; $i++) {
174 if (!$disk_ids->{$i}) {
175 return "vm-$vmid-disk-$i.$fmt";
176 }
177 }
178
179 die "unable to allocate a new image name for VM $vmid in '$imgdir'\n";
180};
181
f4648aef 182sub path {
e67069eb 183 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
f4648aef 184
e67069eb
DM
185 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
186 $class->parse_volname($volname);
187
188 # Note: qcow2/qed has internal snapshot, so path is always
189 # the same (with or without snapshot => same file).
190 die "can't snapshot this image format\n"
191 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
f4648aef 192
f4648aef 193 my $path = undef;
a66159e3
DM
194 if ($vtype eq 'images') {
195
196 my $server = &$get_active_server($scfg, 1);
197 my $glustervolume = $scfg->{volume};
187ca539
SM
198 my $transport = $scfg->{transport};
199 my $protocol = "gluster";
200
201 if ($transport) {
202 $protocol = "gluster+$transport";
203 }
a66159e3 204
187ca539 205 $path = "$protocol://$server/$glustervolume/images/$vmid/$name";
a66159e3
DM
206
207 } else {
f4648aef
AD
208 my $dir = $class->get_subdir($scfg, $vtype);
209 $path = "$dir/$name";
210 }
211
f4648aef
AD
212 return wantarray ? ($path, $vmid, $vtype) : $path;
213}
214
db7922dc
AD
215sub alloc_image {
216 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
217
218 my $imagedir = $class->get_subdir($scfg, 'images');
219 $imagedir .= "/$vmid";
220
221 mkpath $imagedir;
222
223 $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name;
224
225 my (undef, $tmpfmt) = parse_name_dir($name);
226
227 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
228 if $tmpfmt ne $fmt;
229
230 my $path = "$imagedir/$name";
231
232 die "disk image '$path' already exists\n" if -e $path;
233
a66159e3 234 my $server = &$get_active_server($scfg, 1);
db7922dc
AD
235 my $glustervolume = $scfg->{volume};
236 my $volumepath = "gluster://$server/$glustervolume/images/$vmid/$name";
237
238 my $cmd = ['/usr/bin/qemu-img', 'create'];
239
240 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
241
242 push @$cmd, '-f', $fmt, $volumepath, "${size}K";
243
244 run_command($cmd, errmsg => "unable to create image");
245
246 return "$vmid/$name";
247}
248
f4648aef
AD
249sub status {
250 my ($class, $storeid, $scfg, $cache) = @_;
251
80b64788 252 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
84c8e52d 253 if !$cache->{mountdata};
f4648aef
AD
254
255 my $path = $scfg->{path};
f4648aef
AD
256
257 my $volume = $scfg->{volume};
258
a66159e3 259 return undef if !glusterfs_is_mounted($volume, $path, $cache->{mountdata});
f4648aef
AD
260
261 return $class->SUPER::status($storeid, $scfg, $cache);
262}
263
264sub activate_storage {
265 my ($class, $storeid, $scfg, $cache) = @_;
266
80b64788 267 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
84c8e52d 268 if !$cache->{mountdata};
f4648aef
AD
269
270 my $path = $scfg->{path};
f4648aef
AD
271 my $volume = $scfg->{volume};
272
a66159e3
DM
273 if (!glusterfs_is_mounted($volume, $path, $cache->{mountdata})) {
274
f4648aef
AD
275 mkpath $path;
276
277 die "unable to activate storage '$storeid' - " .
278 "directory '$path' does not exist\n" if ! -d $path;
279
a66159e3
DM
280 my $server = &$get_active_server($scfg, 1);
281
282 glusterfs_mount($server, $volume, $path);
f4648aef
AD
283 }
284
285 $class->SUPER::activate_storage($storeid, $scfg, $cache);
286}
287
288sub deactivate_storage {
289 my ($class, $storeid, $scfg, $cache) = @_;
290
80b64788 291 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
84c8e52d 292 if !$cache->{mountdata};
f4648aef
AD
293
294 my $path = $scfg->{path};
f4648aef
AD
295 my $volume = $scfg->{volume};
296
a66159e3 297 if (glusterfs_is_mounted($volume, $path, $cache->{mountdata})) {
f4648aef 298 my $cmd = ['/bin/umount', $path];
1a3459ac 299 run_command($cmd, errmsg => 'umount error');
f4648aef
AD
300 }
301}
302
303sub activate_volume {
02e797b8 304 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
f4648aef
AD
305
306 # do nothing by default
307}
308
309sub deactivate_volume {
02e797b8 310 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
f4648aef
AD
311
312 # do nothing by default
313}
314
315sub check_connection {
a66159e3 316 my ($class, $storeid, $scfg, $cache) = @_;
f4648aef 317
a66159e3 318 my $server = &$get_active_server($scfg);
f4648aef 319
a66159e3 320 return defined($server) ? 1 : 0;
f4648aef
AD
321}
322
3231;