]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/GlusterfsPlugin.pm
path: corretly implement path to snapshots
[pve-storage.git] / PVE / Storage / GlusterfsPlugin.pm
index af859343c8c1a44ffcbb1319df1f2b04749d0d37..7a8c82fbee81a9f9b2153bb18847038855675b2d 100644 (file)
@@ -13,6 +13,59 @@ use base qw(PVE::Storage::Plugin);
 
 # Glusterfs helper functions
 
+my $server_test_results = {};
+
+my $get_active_server = sub {
+    my ($scfg, $return_default_if_offline) = @_;
+
+    my $defaultserver = $scfg->{server} ? $scfg->{server} : 'localhost';
+
+    if ($return_default_if_offline && !defined($scfg->{server2})) {
+       # avoid delays (there is no backup server anyways)
+       return $defaultserver;
+    }
+
+    my $serverlist = [ $defaultserver ];
+    push @$serverlist, $scfg->{server2} if $scfg->{server2};
+
+    my $ctime = time();
+    foreach my $server (@$serverlist) {
+       my $stat = $server_test_results->{$server};
+       return $server if $stat && $stat->{active} && (($ctime - $stat->{time}) <= 2); 
+    }
+
+    foreach my $server (@$serverlist) {
+       my $status = 0;
+
+       if ($server && $server ne 'localhost' && $server ne '127.0.0.1' && $server ne '::1') {
+
+           my $p = Net::Ping->new("tcp", 2);
+           $status = $p->ping($server);
+
+       } else {
+
+           my $parser = sub {
+               my $line = shift;
+
+               if ($line =~ m/Status: Started$/) {
+                   $status = 1;
+               }
+           };
+
+           my $cmd = ['/usr/sbin/gluster', 'volume', 'info', $scfg->{volume}];
+
+           run_command($cmd, errmsg => "glusterfs error", errfunc => sub {}, outfunc => $parser);
+       }
+
+       $server_test_results->{$server} = { time => time(), active => $status };
+       return $server if $status;
+    }
+
+    return $defaultserver if $return_default_if_offline;
+
+    return undef;
+};
+
 sub read_proc_mounts {
 
     local $/; # enable slurp mode
@@ -27,13 +80,11 @@ sub read_proc_mounts {
 }
 
 sub glusterfs_is_mounted {
-    my ($server, $volume, $mountpoint, $mountdata) = @_;
-
-    my $source = "$server:$volume";
+    my ($volume, $mountpoint, $mountdata) = @_;
 
     $mountdata = read_proc_mounts() if !$mountdata;
 
-    if ($mountdata =~ m|^$source/?\s$mountpoint\sfuse.glusterfs|m) {
+    if ($mountdata =~ m|^\S+:$volume/?\s$mountpoint\sfuse.glusterfs|m) {
        return $mountpoint;
     }
 
@@ -70,6 +121,16 @@ sub properties {
            description => "Glusterfs Volume.",
            type => 'string',
        },
+       server2 => {
+           description => "Backup volfile server IP or DNS name.",
+           type => 'string', format => 'pve-storage-server',
+           requires => 'server',
+       },
+       transport => {
+           description => "Gluster transport: tcp or rdma",
+           type => 'string',
+           enum => ['tcp', 'rdma', 'unix'],
+       },
     };
 }
 
@@ -77,7 +138,9 @@ sub options {
     return {
        path => { fixed => 1 },
        server => { optional => 1 },
+       server2 => { optional => 1 },
        volume => { fixed => 1 },
+       transport => { optional => 1 },
         nodes => { optional => 1 },
        disable => { optional => 1 },
         maxfiles => { optional => 1 },
@@ -97,37 +160,113 @@ sub check_config {
 
 # Storage implementation
 
+sub parse_name_dir {
+    my $name = shift;
+
+    if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk))$!) {
+        return ($1, $3, $2);
+    }
+
+    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) = $class->parse_volname($volname);
+    my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
+       $class->parse_volname($volname);
 
-    my $server = $scfg->{server} ? $scfg->{server} : 'localhost';
-    my $glustervolume = $scfg->{volume};
+    # 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'){
-       $path = "gluster://$server/$glustervolume/images/$vmid/$name";
-    }else{
+    if ($vtype eq 'images') {
+
+       my $server = &$get_active_server($scfg, 1);
+       my $glustervolume = $scfg->{volume};
+       my $transport = $scfg->{transport};
+       my $protocol = "gluster";
+
+       if ($transport) {
+           $protocol = "gluster+$transport";
+       }
+
+       $path = "$protocol://$server/$glustervolume/images/$vmid/$name";
+
+    } else {
        my $dir = $class->get_subdir($scfg, $vtype);
        $path = "$dir/$name";
     }
 
-
     return wantarray ? ($path, $vmid, $vtype) : $path;
 }
 
+sub alloc_image {
+    my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
+
+    my $imagedir = $class->get_subdir($scfg, 'images');
+    $imagedir .= "/$vmid";
+
+    mkpath $imagedir;
+
+    $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name;
+
+    my (undef, $tmpfmt) = parse_name_dir($name);
+
+    die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
+        if $tmpfmt ne $fmt;
+
+    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'];
+
+    push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
+
+    push @$cmd, '-f', $fmt, $volumepath, "${size}K";
+
+    run_command($cmd, errmsg => "unable to create image");
+
+    return "$vmid/$name";
+}
+
 sub status {
     my ($class, $storeid, $scfg, $cache) = @_;
 
     $cache->{mountdata} = read_proc_mounts() if !$cache->{mountdata};
 
     my $path = $scfg->{path};
-    my $server = $scfg->{server} ? $scfg->{server} : 'localhost';
 
     my $volume = $scfg->{volume};
 
-    return undef if !glusterfs_is_mounted($server, $volume, $path, $cache->{mountdata});
+    return undef if !glusterfs_is_mounted($volume, $path, $cache->{mountdata});
 
     return $class->SUPER::status($storeid, $scfg, $cache);
 }
@@ -138,17 +277,18 @@ sub activate_storage {
     $cache->{mountdata} = read_proc_mounts() if !$cache->{mountdata};
 
     my $path = $scfg->{path};
-    my $server = $scfg->{server} ? $scfg->{server} : 'localhost';
     my $volume = $scfg->{volume};
 
-    if (!glusterfs_is_mounted($server, $volume, $path, $cache->{mountdata})) {
-
+    if (!glusterfs_is_mounted($volume, $path, $cache->{mountdata})) {
+       
        mkpath $path;
 
        die "unable to activate storage '$storeid' - " .
            "directory '$path' does not exist\n" if ! -d $path;
 
-       glusterfs_mount($server, $volume, $path, $scfg->{options});
+       my $server = &$get_active_server($scfg, 1);
+
+       glusterfs_mount($server, $volume, $path);
     }
 
     $class->SUPER::activate_storage($storeid, $scfg, $cache);
@@ -160,10 +300,9 @@ sub deactivate_storage {
     $cache->{mountdata} = read_proc_mounts() if !$cache->{mountdata};
 
     my $path = $scfg->{path};
-    my $server = $scfg->{server} ? $scfg->{server} : 'localhost';
     my $volume = $scfg->{volume};
 
-    if (glusterfs_is_mounted($server, $volume, $path, $cache->{mountdata})) {
+    if (glusterfs_is_mounted($volume, $path, $cache->{mountdata})) {
        my $cmd = ['/bin/umount', $path];
        run_command($cmd, errmsg => 'umount error');
     }
@@ -182,33 +321,11 @@ sub deactivate_volume {
 }
 
 sub check_connection {
-    my ($class, $storeid, $scfg) = @_;
-
-    my $server = $scfg->{server} ? $scfg->{server} : 'localhost';
-    my $volume = $scfg->{volume};
-
-    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);
-
-    }else{
-
-       my $parser = sub {
-           my $line = shift;
-
-           if ($line =~ m/Status: Started$/) {
-               $status = 1;
-           }
-       };
-
-       my $cmd = ['/usr/sbin/gluster', 'volume', 'info', $volume];
+    my ($class, $storeid, $scfg, $cache) = @_;
 
-       run_command($cmd, errmsg => "glusterfs error", errfunc => sub {}, outfunc => $parser);
-    }
+    my $server = &$get_active_server($scfg);
 
-    return $status;
+    return defined($server) ? 1 : 0;
 }
 
 1;