]>
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') {
43 # ping the echo port (7) without service check
44 $status = PVE
::Network
::tcp_ping
($server, undef, 2);
51 if ($line =~ m/Status: Started$/) {
56 my $cmd = ['/usr/sbin/gluster', 'volume', 'info', $scfg->{volume
}];
58 run_command
($cmd, errmsg
=> "glusterfs error", errfunc
=> sub {}, outfunc
=> $parser);
61 $server_test_results->{$server} = { time => time(), active
=> $status };
62 return $server if $status;
65 return $defaultserver if $return_default_if_offline;
70 sub glusterfs_is_mounted
{
71 my ($volume, $mountpoint, $mountdata) = @_;
73 $mountdata = PVE
::ProcFSTools
::parse_proc_mounts
() if !$mountdata;
75 return $mountpoint if grep {
76 $_->[2] eq 'fuse.glusterfs' &&
78 $_->[1] eq $mountpoint
84 my ($server, $volume, $mountpoint) = @_;
86 my $source = "$server:$volume";
88 my $cmd = ['/bin/mount', '-t', 'glusterfs', $source, $mountpoint];
90 run_command
($cmd, errmsg
=> "mount error");
101 content
=> [ { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1, backup
=> 1},
103 format
=> [ { raw
=> 1, qcow2
=> 1, vmdk
=> 1 } , 'raw' ],
110 description
=> "Glusterfs Volume.",
114 description
=> "Backup volfile server IP or DNS name.",
115 type
=> 'string', format
=> 'pve-storage-server',
116 requires
=> 'server',
119 description
=> "Gluster transport: tcp or rdma",
121 enum
=> ['tcp', 'rdma', 'unix'],
128 path
=> { fixed
=> 1 },
129 server
=> { optional
=> 1 },
130 server2
=> { optional
=> 1 },
131 volume
=> { fixed
=> 1 },
132 transport
=> { optional
=> 1 },
133 nodes
=> { optional
=> 1 },
134 disable
=> { optional
=> 1 },
135 maxfiles
=> { optional
=> 1 },
136 content
=> { optional
=> 1 },
137 format
=> { optional
=> 1 },
143 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
145 $config->{path
} = "/mnt/pve/$sectionId" if $create && !$config->{path
};
147 return $class->SUPER::check_config
($sectionId, $config, $create, $skipSchemaCheck);
150 # Storage implementation
155 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk))$!) {
159 die "unable to parse volume filename '$name'\n";
162 my $find_free_diskname = sub {
163 my ($imgdir, $vmid, $fmt) = @_;
166 PVE
::Tools
::dir_glob_foreach
($imgdir,
167 qr!(vm|base)-$vmid-disk-(\d+)\..*!,
169 my ($fn, $type, $disk) = @_;
170 $disk_ids->{$disk} = 1;
173 for (my $i = 1; $i < 100; $i++) {
174 if (!$disk_ids->{$i}) {
175 return "vm-$vmid-disk-$i.$fmt";
179 die "unable to allocate a new image name for VM $vmid in '$imgdir'\n";
183 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
185 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
186 $class->parse_volname($volname);
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)$/;
194 if ($vtype eq 'images') {
196 my $server = &$get_active_server($scfg, 1);
197 my $glustervolume = $scfg->{volume
};
198 my $transport = $scfg->{transport
};
199 my $protocol = "gluster";
202 $protocol = "gluster+$transport";
205 $path = "$protocol://$server/$glustervolume/images/$vmid/$name";
208 my $dir = $class->get_subdir($scfg, $vtype);
209 $path = "$dir/$name";
212 return wantarray ?
($path, $vmid, $vtype) : $path;
216 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
218 my $imagedir = $class->get_subdir($scfg, 'images');
219 $imagedir .= "/$vmid";
223 $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name;
225 my (undef, $tmpfmt) = parse_name_dir
($name);
227 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
230 my $path = "$imagedir/$name";
232 die "disk image '$path' already exists\n" if -e
$path;
234 my $server = &$get_active_server($scfg, 1);
235 my $glustervolume = $scfg->{volume
};
236 my $volumepath = "gluster://$server/$glustervolume/images/$vmid/$name";
238 my $cmd = ['/usr/bin/qemu-img', 'create'];
240 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
242 push @$cmd, '-f', $fmt, $volumepath, "${size}K";
244 run_command
($cmd, errmsg
=> "unable to create image");
246 return "$vmid/$name";
250 my ($class, $storeid, $scfg, $cache) = @_;
252 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
253 if !$cache->{mountdata
};
255 my $path = $scfg->{path
};
257 my $volume = $scfg->{volume
};
259 return undef if !glusterfs_is_mounted
($volume, $path, $cache->{mountdata
});
261 return $class->SUPER::status
($storeid, $scfg, $cache);
264 sub activate_storage
{
265 my ($class, $storeid, $scfg, $cache) = @_;
267 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
268 if !$cache->{mountdata
};
270 my $path = $scfg->{path
};
271 my $volume = $scfg->{volume
};
273 if (!glusterfs_is_mounted
($volume, $path, $cache->{mountdata
})) {
277 die "unable to activate storage '$storeid' - " .
278 "directory '$path' does not exist\n" if ! -d
$path;
280 my $server = &$get_active_server($scfg, 1);
282 glusterfs_mount
($server, $volume, $path);
285 $class->SUPER::activate_storage
($storeid, $scfg, $cache);
288 sub deactivate_storage
{
289 my ($class, $storeid, $scfg, $cache) = @_;
291 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
292 if !$cache->{mountdata
};
294 my $path = $scfg->{path
};
295 my $volume = $scfg->{volume
};
297 if (glusterfs_is_mounted
($volume, $path, $cache->{mountdata
})) {
298 my $cmd = ['/bin/umount', $path];
299 run_command
($cmd, errmsg
=> 'umount error');
303 sub activate_volume
{
304 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
306 # do nothing by default
309 sub deactivate_volume
{
310 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
312 # do nothing by default
315 sub check_connection
{
316 my ($class, $storeid, $scfg, $cache) = @_;
318 my $server = &$get_active_server($scfg);
320 return defined($server) ?
1 : 0;