]>
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},
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 content
=> { optional
=> 1 },
136 format
=> { optional
=> 1 },
137 mkdir => { optional
=> 1 },
138 bwlimit
=> { optional
=> 1 },
144 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
146 $config->{path
} = "/mnt/pve/$sectionId" if $create && !$config->{path
};
148 return $class->SUPER::check_config
($sectionId, $config, $create, $skipSchemaCheck);
151 # Storage implementation
156 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk))$!) {
160 die "unable to parse volume filename '$name'\n";
163 my $find_free_diskname = sub {
164 my ($imgdir, $vmid, $fmt, $scfg) = @_;
168 my $dh = IO
::Dir-
>new ($imgdir);
169 @$disk_list = $dh->read() if defined($dh);
171 return PVE
::Storage
::Plugin
::get_next_vm_diskname
($disk_list, $imgdir, $vmid, $fmt, $scfg, 1);
175 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
177 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
178 $class->parse_volname($volname);
180 # Note: qcow2/qed has internal snapshot, so path is always
181 # the same (with or without snapshot => same file).
182 die "can't snapshot this image format\n"
183 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
186 if ($vtype eq 'images') {
188 my $server = &$get_active_server($scfg, 1);
189 my $glustervolume = $scfg->{volume
};
190 my $transport = $scfg->{transport
};
191 my $protocol = "gluster";
194 $protocol = "gluster+$transport";
197 $path = "$protocol://$server/$glustervolume/images/$vmid/$name";
200 my $dir = $class->get_subdir($scfg, $vtype);
201 $path = "$dir/$name";
204 return wantarray ?
($path, $vmid, $vtype) : $path;
208 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
210 die "storage definintion has no path\n" if !$scfg->{path
};
212 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
213 $class->parse_volname($volname);
215 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
217 die "this storage type does not support clone_image on snapshot\n" if $snap;
219 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
221 die "clone_image only works on base images\n" if !$isBase;
223 my $imagedir = $class->get_subdir($scfg, 'images');
224 $imagedir .= "/$vmid";
228 my $name = $find_free_diskname->($imagedir, $vmid, "qcow2", $scfg);
230 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
232 my $path = "$imagedir/$name";
234 die "disk image '$path' already exists\n" if -e
$path;
236 my $server = &$get_active_server($scfg, 1);
237 my $glustervolume = $scfg->{volume
};
238 my $volumepath = "gluster://$server/$glustervolume/images/$vmid/$name";
240 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
241 '-f', 'qcow2', $volumepath];
243 run_command
($cmd, errmsg
=> "unable to create image");
245 return "$basevmid/$basename/$vmid/$name";
249 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
251 my $imagedir = $class->get_subdir($scfg, 'images');
252 $imagedir .= "/$vmid";
256 $name = $find_free_diskname->($imagedir, $vmid, $fmt, $scfg) if !$name;
258 my (undef, $tmpfmt) = parse_name_dir
($name);
260 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
263 my $path = "$imagedir/$name";
265 die "disk image '$path' already exists\n" if -e
$path;
267 my $server = &$get_active_server($scfg, 1);
268 my $glustervolume = $scfg->{volume
};
269 my $volumepath = "gluster://$server/$glustervolume/images/$vmid/$name";
271 my $cmd = ['/usr/bin/qemu-img', 'create'];
273 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
275 push @$cmd, '-f', $fmt, $volumepath, "${size}K";
277 eval { run_command
($cmd, errmsg
=> "unable to create image"); };
284 return "$vmid/$name";
288 my ($class, $storeid, $scfg, $cache) = @_;
290 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
291 if !$cache->{mountdata
};
293 my $path = $scfg->{path
};
295 my $volume = $scfg->{volume
};
297 return undef if !glusterfs_is_mounted
($volume, $path, $cache->{mountdata
});
299 return $class->SUPER::status
($storeid, $scfg, $cache);
302 sub activate_storage
{
303 my ($class, $storeid, $scfg, $cache) = @_;
305 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
306 if !$cache->{mountdata
};
308 my $path = $scfg->{path
};
309 my $volume = $scfg->{volume
};
311 if (!glusterfs_is_mounted
($volume, $path, $cache->{mountdata
})) {
313 mkpath
$path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir});
315 die "unable to activate storage '$storeid' - " .
316 "directory '$path' does not exist\n" if ! -d
$path;
318 my $server = &$get_active_server($scfg, 1);
320 glusterfs_mount
($server, $volume, $path);
323 $class->SUPER::activate_storage
($storeid, $scfg, $cache);
326 sub deactivate_storage
{
327 my ($class, $storeid, $scfg, $cache) = @_;
329 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
330 if !$cache->{mountdata
};
332 my $path = $scfg->{path
};
333 my $volume = $scfg->{volume
};
335 if (glusterfs_is_mounted
($volume, $path, $cache->{mountdata
})) {
336 my $cmd = ['/bin/umount', $path];
337 run_command
($cmd, errmsg
=> 'umount error');
341 sub activate_volume
{
342 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
344 # do nothing by default
347 sub deactivate_volume
{
348 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
350 # do nothing by default
353 sub check_connection
{
354 my ($class, $storeid, $scfg, $cache) = @_;
356 my $server = &$get_active_server($scfg);
358 return defined($server) ?
1 : 0;