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