X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=PVE%2FStorage%2FGlusterfsPlugin.pm;h=2dd414d895696ce773d818aab4f2faeeb8cc0a2e;hb=32dbc619a5ab7f45a9b0bb28e28a458e4a3fa2ce;hp=36322b1784449ae4f8fe0e0df0768d5f4ef7e67f;hpb=187ca539270623ea619731306a8f046c8f9bd7e1;p=pve-storage.git diff --git a/PVE/Storage/GlusterfsPlugin.pm b/PVE/Storage/GlusterfsPlugin.pm index 36322b1..2dd414d 100644 --- a/PVE/Storage/GlusterfsPlugin.pm +++ b/PVE/Storage/GlusterfsPlugin.pm @@ -5,9 +5,10 @@ use warnings; use IO::File; use File::Path; use PVE::Tools qw(run_command); +use PVE::ProcFSTools; +use PVE::Network; use PVE::Storage::Plugin; use PVE::JSONSchema qw(get_standard_option); -use Net::Ping; use base qw(PVE::Storage::Plugin); @@ -37,10 +38,9 @@ my $get_active_server = sub { foreach my $server (@$serverlist) { my $status = 0; - if ($server && $server ne 'localhost' && $server ne '127.0.0.1') { - - my $p = Net::Ping->new("tcp", 2); - $status = $p->ping($server); + if ($server && $server ne 'localhost' && $server ne '127.0.0.1' && $server ne '::1') { + # ping the gluster daemon default port (24007) as heuristic + $status = PVE::Network::tcp_ping($server, 24007, 2); } else { @@ -66,28 +66,16 @@ my $get_active_server = sub { return undef; }; -sub read_proc_mounts { - - local $/; # enable slurp mode - - my $data = ""; - if (my $fd = IO::File->new("/proc/mounts", "r")) { - $data = <$fd>; - close ($fd); - } - - return $data; -} - sub glusterfs_is_mounted { my ($volume, $mountpoint, $mountdata) = @_; - $mountdata = read_proc_mounts() if !$mountdata; - - if ($mountdata =~ m|^\S+:$volume/?\s$mountpoint\sfuse.glusterfs|m) { - return $mountpoint; - } + $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata; + return $mountpoint if grep { + $_->[2] eq 'fuse.glusterfs' && + $_->[0] =~ /^\S+:\Q$volume\E$/ && + $_->[1] eq $mountpoint + } @$mountdata; return undef; } @@ -109,7 +97,7 @@ sub type { sub plugindata { return { - content => [ { images => 1, rootdir => 1, vztmpl => 1, iso => 1, backup => 1}, + content => [ { images => 1, vztmpl => 1, iso => 1, backup => 1, snippets => 1}, { images => 1 }], format => [ { raw => 1, qcow2 => 1, vmdk => 1 } , 'raw' ], }; @@ -141,11 +129,14 @@ sub options { server2 => { optional => 1 }, volume => { fixed => 1 }, transport => { optional => 1 }, - nodes => { optional => 1 }, + nodes => { optional => 1 }, disable => { optional => 1 }, - maxfiles => { optional => 1 }, + maxfiles => { optional => 1 }, + 'prune-backups' => { optional => 1 }, content => { optional => 1 }, format => { optional => 1 }, + mkdir => { optional => 1 }, + bwlimit => { optional => 1 }, }; } @@ -170,30 +161,16 @@ sub parse_name_dir { die "unable to parse volume filename '$name'\n"; } -my $find_free_diskname = sub { - my ($imgdir, $vmid, $fmt) = @_; - - my $disk_ids = {}; - PVE::Tools::dir_glob_foreach($imgdir, - qr!(vm|base)-$vmid-disk-(\d+)\..*!, - sub { - my ($fn, $type, $disk) = @_; - $disk_ids->{$disk} = 1; - }); - - for (my $i = 1; $i < 100; $i++) { - if (!$disk_ids->{$i}) { - return "vm-$vmid-disk-$i.$fmt"; - } - } - - die "unable to allocate a new image name for VM $vmid in '$imgdir'\n"; -}; - sub path { - my ($class, $scfg, $volname, $storeid) = @_; + my ($class, $scfg, $volname, $storeid, $snapname) = @_; + + my ($vtype, $name, $vmid, undef, undef, $isBase, $format) = + $class->parse_volname($volname); - my ($vtype, $name, $vmid) = $class->parse_volname($volname); + # Note: qcow2/qed has internal snapshot, so path is always + # the same (with or without snapshot => same file). + die "can't snapshot this image format\n" + if defined($snapname) && $format !~ m/^(qcow2|qed)$/; my $path = undef; if ($vtype eq 'images') { @@ -217,6 +194,47 @@ sub path { return wantarray ? ($path, $vmid, $vtype) : $path; } +sub clone_image { + my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_; + + die "storage definintion has no path\n" if !$scfg->{path}; + + my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) = + $class->parse_volname($volname); + + die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images'; + + die "this storage type does not support clone_image on snapshot\n" if $snap; + + die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol'; + + die "clone_image only works on base images\n" if !$isBase; + + my $imagedir = $class->get_subdir($scfg, 'images'); + $imagedir .= "/$vmid"; + + mkpath $imagedir; + + my $name = $class->find_free_diskname($imagedir, $scfg, $vmid, "qcow2", 1); + + warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n"; + + my $path = "$imagedir/$name"; + + die "disk image '$path' already exists\n" if -e $path; + + my $server = &$get_active_server($scfg, 1); + my $glustervolume = $scfg->{volume}; + my $volumepath = "gluster://$server/$glustervolume/images/$vmid/$name"; + + my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename", + '-f', 'qcow2', $volumepath]; + + run_command($cmd, errmsg => "unable to create image"); + + return "$basevmid/$basename/$vmid/$name"; +} + sub alloc_image { my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; @@ -225,7 +243,7 @@ sub alloc_image { mkpath $imagedir; - $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name; + $name = $class->find_free_diskname($imagedir, $scfg, $vmid, $fmt, 1) if !$name; my (undef, $tmpfmt) = parse_name_dir($name); @@ -246,7 +264,12 @@ sub alloc_image { push @$cmd, '-f', $fmt, $volumepath, "${size}K"; - run_command($cmd, errmsg => "unable to create image"); + eval { run_command($cmd, errmsg => "unable to create image"); }; + if ($@) { + unlink $path; + rmdir $imagedir; + die "$@"; + } return "$vmid/$name"; } @@ -254,7 +277,8 @@ sub alloc_image { sub status { my ($class, $storeid, $scfg, $cache) = @_; - $cache->{mountdata} = read_proc_mounts() if !$cache->{mountdata}; + $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts() + if !$cache->{mountdata}; my $path = $scfg->{path}; @@ -268,14 +292,15 @@ sub status { sub activate_storage { my ($class, $storeid, $scfg, $cache) = @_; - $cache->{mountdata} = read_proc_mounts() if !$cache->{mountdata}; + $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts() + if !$cache->{mountdata}; my $path = $scfg->{path}; my $volume = $scfg->{volume}; if (!glusterfs_is_mounted($volume, $path, $cache->{mountdata})) { - mkpath $path; + mkpath $path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir}); die "unable to activate storage '$storeid' - " . "directory '$path' does not exist\n" if ! -d $path; @@ -291,7 +316,8 @@ sub activate_storage { sub deactivate_storage { my ($class, $storeid, $scfg, $cache) = @_; - $cache->{mountdata} = read_proc_mounts() if !$cache->{mountdata}; + $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts() + if !$cache->{mountdata}; my $path = $scfg->{path}; my $volume = $scfg->{volume}; @@ -303,13 +329,13 @@ sub deactivate_storage { } sub activate_volume { - my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_; + my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; # do nothing by default } sub deactivate_volume { - my ($class, $storeid, $scfg, $volname, $cache) = @_; + my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; # do nothing by default }