]>
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') {
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',
134 path
=> { fixed
=> 1 },
135 server
=> { optional
=> 1 },
136 server2
=> { optional
=> 1 },
137 volume
=> { fixed
=> 1 },
138 nodes
=> { optional
=> 1 },
139 disable
=> { optional
=> 1 },
140 maxfiles
=> { optional
=> 1 },
141 content
=> { optional
=> 1 },
142 format
=> { optional
=> 1 },
148 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
150 $config->{path
} = "/mnt/pve/$sectionId" if $create && !$config->{path
};
152 return $class->SUPER::check_config
($sectionId, $config, $create, $skipSchemaCheck);
155 # Storage implementation
160 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk))$!) {
164 die "unable to parse volume filename '$name'\n";
167 my $find_free_diskname = sub {
168 my ($imgdir, $vmid, $fmt) = @_;
171 PVE
::Tools
::dir_glob_foreach
($imgdir,
172 qr!(vm|base)-$vmid-disk-(\d+)\..*!,
174 my ($fn, $type, $disk) = @_;
175 $disk_ids->{$disk} = 1;
178 for (my $i = 1; $i < 100; $i++) {
179 if (!$disk_ids->{$i}) {
180 return "vm-$vmid-disk-$i.$fmt";
184 die "unable to allocate a new image name for VM $vmid in '$imgdir'\n";
188 my ($class, $scfg, $volname, $storeid) = @_;
190 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
193 if ($vtype eq 'images') {
195 my $server = &$get_active_server($scfg, 1);
196 my $glustervolume = $scfg->{volume
};
198 $path = "gluster://$server/$glustervolume/images/$vmid/$name";
201 my $dir = $class->get_subdir($scfg, $vtype);
202 $path = "$dir/$name";
205 return wantarray ?
($path, $vmid, $vtype) : $path;
209 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
211 my $imagedir = $class->get_subdir($scfg, 'images');
212 $imagedir .= "/$vmid";
216 $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name;
218 my (undef, $tmpfmt) = parse_name_dir
($name);
220 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\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'];
233 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
235 push @$cmd, '-f', $fmt, $volumepath, "${size}K";
237 run_command
($cmd, errmsg
=> "unable to create image");
239 return "$vmid/$name";
243 my ($class, $storeid, $scfg, $cache) = @_;
245 $cache->{mountdata
} = read_proc_mounts
() if !$cache->{mountdata
};
247 my $path = $scfg->{path
};
249 my $volume = $scfg->{volume
};
251 return undef if !glusterfs_is_mounted
($volume, $path, $cache->{mountdata
});
253 return $class->SUPER::status
($storeid, $scfg, $cache);
256 sub activate_storage
{
257 my ($class, $storeid, $scfg, $cache) = @_;
259 $cache->{mountdata
} = read_proc_mounts
() if !$cache->{mountdata
};
261 my $path = $scfg->{path
};
262 my $volume = $scfg->{volume
};
264 if (!glusterfs_is_mounted
($volume, $path, $cache->{mountdata
})) {
268 die "unable to activate storage '$storeid' - " .
269 "directory '$path' does not exist\n" if ! -d
$path;
271 my $server = &$get_active_server($scfg, 1);
273 glusterfs_mount
($server, $volume, $path);
276 $class->SUPER::activate_storage
($storeid, $scfg, $cache);
279 sub deactivate_storage
{
280 my ($class, $storeid, $scfg, $cache) = @_;
282 $cache->{mountdata
} = read_proc_mounts
() if !$cache->{mountdata
};
284 my $path = $scfg->{path
};
285 my $volume = $scfg->{volume
};
287 if (glusterfs_is_mounted
($volume, $path, $cache->{mountdata
})) {
288 my $cmd = ['/bin/umount', $path];
289 run_command
($cmd, errmsg
=> 'umount error');
293 sub activate_volume
{
294 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
296 # do nothing by default
299 sub deactivate_volume
{
300 my ($class, $storeid, $scfg, $volname, $cache) = @_;
302 # do nothing by default
305 sub check_connection
{
306 my ($class, $storeid, $scfg, $cache) = @_;
308 my $server = &$get_active_server($scfg);
310 return defined($server) ?
1 : 0;