]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/GlusterfsPlugin.pm
1 package PVE
::Storage
::GlusterfsPlugin
;
7 use PVE
::Tools
qw(run_command);
10 use PVE
::Storage
::Plugin
;
11 use PVE
::JSONSchema
qw(get_standard_option);
13 use base
qw(PVE::Storage::Plugin);
15 # Glusterfs helper functions
17 my $server_test_results = {};
19 my $get_active_server = sub {
20 my ($scfg, $return_default_if_offline) = @_;
22 my $defaultserver = $scfg->{server
} ?
$scfg->{server
} : 'localhost';
24 if ($return_default_if_offline && !defined($scfg->{server2
})) {
25 # avoid delays (there is no backup server anyways)
26 return $defaultserver;
29 my $serverlist = [ $defaultserver ];
30 push @$serverlist, $scfg->{server2
} if $scfg->{server2
};
33 foreach my $server (@$serverlist) {
34 my $stat = $server_test_results->{$server};
35 return $server if $stat && $stat->{active
} && (($ctime - $stat->{time}) <= 2);
38 foreach my $server (@$serverlist) {
41 if ($server && $server ne 'localhost' && $server ne '127.0.0.1' && $server ne '::1') {
42 # ping the gluster daemon default port (24007) as heuristic
43 $status = PVE
::Network
::tcp_ping
($server, 24007, 2);
50 if ($line =~ m/Status: Started$/) {
55 my $cmd = ['/usr/sbin/gluster', 'volume', 'info', $scfg->{volume
}];
57 run_command
($cmd, errmsg
=> "glusterfs error", errfunc
=> sub {}, outfunc
=> $parser);
60 $server_test_results->{$server} = { time => time(), active
=> $status };
61 return $server if $status;
64 return $defaultserver if $return_default_if_offline;
69 sub glusterfs_is_mounted
{
70 my ($volume, $mountpoint, $mountdata) = @_;
72 $mountdata = PVE
::ProcFSTools
::parse_proc_mounts
() if !$mountdata;
74 return $mountpoint if grep {
75 $_->[2] eq 'fuse.glusterfs' &&
76 $_->[0] =~ /^\S+:\Q$volume\E$/ &&
77 $_->[1] eq $mountpoint
83 my ($server, $volume, $mountpoint) = @_;
85 my $source = "$server:$volume";
87 my $cmd = ['/bin/mount', '-t', 'glusterfs', $source, $mountpoint];
89 run_command
($cmd, errmsg
=> "mount error");
100 content
=> [ { images
=> 1, vztmpl
=> 1, iso
=> 1, backup
=> 1, snippets
=> 1},
102 format
=> [ { raw
=> 1, qcow2
=> 1, vmdk
=> 1 } , 'raw' ],
109 description
=> "Glusterfs Volume.",
113 description
=> "Backup volfile server IP or DNS name.",
114 type
=> 'string', format
=> 'pve-storage-server',
115 requires
=> 'server',
118 description
=> "Gluster transport: tcp or rdma",
120 enum
=> ['tcp', 'rdma', 'unix'],
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 'prune-backups' => { optional
=> 1 },
136 content
=> { optional
=> 1 },
137 format
=> { optional
=> 1 },
138 mkdir => { optional
=> 1 },
139 bwlimit
=> { optional
=> 1 },
140 preallocation
=> { optional
=> 1 },
146 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
148 $config->{path
} = "/mnt/pve/$sectionId" if $create && !$config->{path
};
150 return $class->SUPER::check_config
($sectionId, $config, $create, $skipSchemaCheck);
153 # Storage implementation
158 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk))$!) {
162 die "unable to parse volume filename '$name'\n";
166 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
168 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
169 $class->parse_volname($volname);
171 # Note: qcow2/qed has internal snapshot, so path is always
172 # the same (with or without snapshot => same file).
173 die "can't snapshot this image format\n"
174 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
177 if ($vtype eq 'images') {
179 my $server = &$get_active_server($scfg, 1);
180 my $glustervolume = $scfg->{volume
};
181 my $transport = $scfg->{transport
};
182 my $protocol = "gluster";
185 $protocol = "gluster+$transport";
188 $path = "$protocol://$server/$glustervolume/images/$vmid/$name";
191 my $dir = $class->get_subdir($scfg, $vtype);
192 $path = "$dir/$name";
195 return wantarray ?
($path, $vmid, $vtype) : $path;
199 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
201 die "storage definition has no path\n" if !$scfg->{path
};
203 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
204 $class->parse_volname($volname);
206 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
208 die "this storage type does not support clone_image on snapshot\n" if $snap;
210 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
212 die "clone_image only works on base images\n" if !$isBase;
214 my $imagedir = $class->get_subdir($scfg, 'images');
215 $imagedir .= "/$vmid";
219 my $name = $class->find_free_diskname($storeid, $scfg, $vmid, "qcow2", 1);
221 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
223 my $path = "$imagedir/$name";
225 die "disk image '$path' already exists\n" if -e
$path;
227 my $server = &$get_active_server($scfg, 1);
228 my $glustervolume = $scfg->{volume
};
229 my $volumepath = "gluster://$server/$glustervolume/images/$vmid/$name";
231 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
232 '-F', $format, '-f', 'qcow2', $volumepath];
234 run_command
($cmd, errmsg
=> "unable to create image");
236 return "$basevmid/$basename/$vmid/$name";
240 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
242 my $imagedir = $class->get_subdir($scfg, 'images');
243 $imagedir .= "/$vmid";
247 $name = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt, 1) if !$name;
249 my (undef, $tmpfmt) = parse_name_dir
($name);
251 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
254 my $path = "$imagedir/$name";
256 die "disk image '$path' already exists\n" if -e
$path;
258 my $server = &$get_active_server($scfg, 1);
259 my $glustervolume = $scfg->{volume
};
260 my $volumepath = "gluster://$server/$glustervolume/images/$vmid/$name";
262 my $cmd = ['/usr/bin/qemu-img', 'create'];
264 my $prealloc_opt = PVE
::Storage
::Plugin
::preallocation_cmd_option
($scfg, $fmt);
265 push @$cmd, '-o', $prealloc_opt if defined($prealloc_opt);
267 push @$cmd, '-f', $fmt, $volumepath, "${size}K";
269 eval { run_command
($cmd, errmsg
=> "unable to create image"); };
276 return "$vmid/$name";
280 my ($class, $storeid, $scfg, $cache) = @_;
282 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
283 if !$cache->{mountdata
};
285 my $path = $scfg->{path
};
287 my $volume = $scfg->{volume
};
289 return undef if !glusterfs_is_mounted
($volume, $path, $cache->{mountdata
});
291 return $class->SUPER::status
($storeid, $scfg, $cache);
294 sub activate_storage
{
295 my ($class, $storeid, $scfg, $cache) = @_;
297 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
298 if !$cache->{mountdata
};
300 my $path = $scfg->{path
};
301 my $volume = $scfg->{volume
};
303 if (!glusterfs_is_mounted
($volume, $path, $cache->{mountdata
})) {
305 mkpath
$path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir});
307 die "unable to activate storage '$storeid' - " .
308 "directory '$path' does not exist\n" if ! -d
$path;
310 my $server = &$get_active_server($scfg, 1);
312 glusterfs_mount
($server, $volume, $path);
315 $class->SUPER::activate_storage
($storeid, $scfg, $cache);
318 sub deactivate_storage
{
319 my ($class, $storeid, $scfg, $cache) = @_;
321 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
322 if !$cache->{mountdata
};
324 my $path = $scfg->{path
};
325 my $volume = $scfg->{volume
};
327 if (glusterfs_is_mounted
($volume, $path, $cache->{mountdata
})) {
328 my $cmd = ['/bin/umount', $path];
329 run_command
($cmd, errmsg
=> 'umount error');
333 sub activate_volume
{
334 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
336 # do nothing by default
339 sub deactivate_volume
{
340 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
342 # do nothing by default
345 sub check_connection
{
346 my ($class, $storeid, $scfg, $cache) = @_;
348 my $server = &$get_active_server($scfg);
350 return defined($server) ?
1 : 0;