]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/GlusterfsPlugin.pm
1 package PVE
::Storage
::GlusterfsPlugin
;
7 use PVE
::Tools
qw(run_command);
8 use PVE
::Storage
::Plugin
;
9 use PVE
::JSONSchema
qw(get_standard_option);
12 use base
qw(PVE::Storage::Plugin);
14 # Glusterfs helper functions
16 my $server_test_results = {};
18 my $get_active_server = sub {
19 my ($scfg, $return_default_if_offline) = @_;
21 my $defaultserver = $scfg->{server
} ?
$scfg->{server
} : 'localhost';
23 if ($return_default_if_offline && !defined($scfg->{server2
})) {
24 # avoid delays (there is no backup server anyways)
25 return $defaultserver;
28 my $serverlist = [ $defaultserver ];
29 push @$serverlist, $scfg->{server2
} if $scfg->{server2
};
32 foreach my $server (@$serverlist) {
33 my $stat = $server_test_results->{$server};
34 return $server if $stat && $stat->{active
} && (($ctime - $stat->{time}) <= 2);
37 foreach my $server (@$serverlist) {
40 if ($server && $server ne 'localhost' && $server ne '127.0.0.1' && $server ne '::1') {
42 my $p = Net
::Ping-
>new("tcp", 2);
43 $status = $p->ping($server);
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 read_proc_mounts
{
71 local $/; # enable slurp mode
74 if (my $fd = IO
::File-
>new("/proc/mounts", "r")) {
82 sub glusterfs_is_mounted
{
83 my ($volume, $mountpoint, $mountdata) = @_;
85 $mountdata = read_proc_mounts
() if !$mountdata;
87 if ($mountdata =~ m
|^\S
+:$volume/?\s$mountpoint\sfuse.glusterfs
|m
) {
95 my ($server, $volume, $mountpoint) = @_;
97 my $source = "$server:$volume";
99 my $cmd = ['/bin/mount', '-t', 'glusterfs', $source, $mountpoint];
101 run_command
($cmd, errmsg
=> "mount error");
112 content
=> [ { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1, backup
=> 1},
114 format
=> [ { raw
=> 1, qcow2
=> 1, vmdk
=> 1 } , 'raw' ],
121 description
=> "Glusterfs Volume.",
125 description
=> "Backup volfile server IP or DNS name.",
126 type
=> 'string', format
=> 'pve-storage-server',
127 requires
=> 'server',
130 description
=> "Gluster transport: tcp or rdma",
132 enum
=> ['tcp', 'rdma', 'unix'],
139 path
=> { fixed
=> 1 },
140 server
=> { optional
=> 1 },
141 server2
=> { optional
=> 1 },
142 volume
=> { fixed
=> 1 },
143 transport
=> { optional
=> 1 },
144 nodes
=> { optional
=> 1 },
145 disable
=> { optional
=> 1 },
146 maxfiles
=> { optional
=> 1 },
147 content
=> { optional
=> 1 },
148 format
=> { optional
=> 1 },
154 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
156 $config->{path
} = "/mnt/pve/$sectionId" if $create && !$config->{path
};
158 return $class->SUPER::check_config
($sectionId, $config, $create, $skipSchemaCheck);
161 # Storage implementation
166 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk))$!) {
170 die "unable to parse volume filename '$name'\n";
173 my $find_free_diskname = sub {
174 my ($imgdir, $vmid, $fmt) = @_;
177 PVE
::Tools
::dir_glob_foreach
($imgdir,
178 qr!(vm|base)-$vmid-disk-(\d+)\..*!,
180 my ($fn, $type, $disk) = @_;
181 $disk_ids->{$disk} = 1;
184 for (my $i = 1; $i < 100; $i++) {
185 if (!$disk_ids->{$i}) {
186 return "vm-$vmid-disk-$i.$fmt";
190 die "unable to allocate a new image name for VM $vmid in '$imgdir'\n";
194 my ($class, $scfg, $volname, $storeid) = @_;
196 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
199 if ($vtype eq 'images') {
201 my $server = &$get_active_server($scfg, 1);
202 my $glustervolume = $scfg->{volume
};
203 my $transport = $scfg->{transport
};
204 my $protocol = "gluster";
207 $protocol = "gluster+$transport";
210 $path = "$protocol://$server/$glustervolume/images/$vmid/$name";
213 my $dir = $class->get_subdir($scfg, $vtype);
214 $path = "$dir/$name";
217 return wantarray ?
($path, $vmid, $vtype) : $path;
221 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
223 my $imagedir = $class->get_subdir($scfg, 'images');
224 $imagedir .= "/$vmid";
228 $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name;
230 my (undef, $tmpfmt) = parse_name_dir
($name);
232 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
235 my $path = "$imagedir/$name";
237 die "disk image '$path' already exists\n" if -e
$path;
239 my $server = &$get_active_server($scfg, 1);
240 my $glustervolume = $scfg->{volume
};
241 my $volumepath = "gluster://$server/$glustervolume/images/$vmid/$name";
243 my $cmd = ['/usr/bin/qemu-img', 'create'];
245 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
247 push @$cmd, '-f', $fmt, $volumepath, "${size}K";
249 run_command
($cmd, errmsg
=> "unable to create image");
251 return "$vmid/$name";
255 my ($class, $storeid, $scfg, $cache) = @_;
257 $cache->{mountdata
} = read_proc_mounts
() if !$cache->{mountdata
};
259 my $path = $scfg->{path
};
261 my $volume = $scfg->{volume
};
263 return undef if !glusterfs_is_mounted
($volume, $path, $cache->{mountdata
});
265 return $class->SUPER::status
($storeid, $scfg, $cache);
268 sub activate_storage
{
269 my ($class, $storeid, $scfg, $cache) = @_;
271 $cache->{mountdata
} = read_proc_mounts
() if !$cache->{mountdata
};
273 my $path = $scfg->{path
};
274 my $volume = $scfg->{volume
};
276 if (!glusterfs_is_mounted
($volume, $path, $cache->{mountdata
})) {
280 die "unable to activate storage '$storeid' - " .
281 "directory '$path' does not exist\n" if ! -d
$path;
283 my $server = &$get_active_server($scfg, 1);
285 glusterfs_mount
($server, $volume, $path);
288 $class->SUPER::activate_storage
($storeid, $scfg, $cache);
291 sub deactivate_storage
{
292 my ($class, $storeid, $scfg, $cache) = @_;
294 $cache->{mountdata
} = read_proc_mounts
() if !$cache->{mountdata
};
296 my $path = $scfg->{path
};
297 my $volume = $scfg->{volume
};
299 if (glusterfs_is_mounted
($volume, $path, $cache->{mountdata
})) {
300 my $cmd = ['/bin/umount', $path];
301 run_command
($cmd, errmsg
=> 'umount error');
305 sub activate_volume
{
306 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
308 # do nothing by default
311 sub deactivate_volume
{
312 my ($class, $storeid, $scfg, $volname, $cache) = @_;
314 # do nothing by default
317 sub check_connection
{
318 my ($class, $storeid, $scfg, $cache) = @_;
320 my $server = &$get_active_server($scfg);
322 return defined($server) ?
1 : 0;