]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/CIFSPlugin.pm
drbd: comment that the builtin plugin is depreacated
[pve-storage.git] / PVE / Storage / CIFSPlugin.pm
index 5cf8509ac666d28462732d47fdd391f05fe711eb..be06cc761e4e7244358ce72379ef50d7e2a93edd 100644 (file)
@@ -28,13 +28,40 @@ sub cifs_is_mounted {
     return undef;
 }
 
+sub cifs_cred_file_name {
+    my ($storeid) = @_;
+    return "/etc/pve/priv/storage/${storeid}.pw";
+}
+
+sub cifs_delete_credentials {
+    my ($storeid) = @_;
+
+    if (my $cred_file = get_cred_file($storeid)) {
+       unlink($cred_file) or warn "removing cifs credientials '$cred_file' failed: $!\n";
+    }
+}
+
+sub cifs_set_credentials {
+    my ($password, $storeid) = @_;
+
+    my $cred_file = cifs_cred_file_name($storeid);
+    mkdir "/etc/pve/priv/storage";
+
+    PVE::Tools::file_set_contents($cred_file, "password=$password\n");
+
+    return $cred_file;
+}
+
 sub get_cred_file {
     my ($storeid) = @_;
 
-    my $cred_file = '/etc/pve/priv/'.$storeid.'.cred';
+    my $cred_file = cifs_cred_file_name($storeid);
 
     if (-e $cred_file) {
        return $cred_file;
+    } elsif (-e "/etc/pve/priv/${storeid}.cred") {
+       # FIXME: remove fallback with 7.0 by doing a rename on upgrade from 6.x
+       return "/etc/pve/priv/${storeid}.cred";
     }
     return undef;
 }
@@ -68,7 +95,7 @@ sub type {
 sub plugindata {
     return {
        content => [ { images => 1, rootdir => 1, vztmpl => 1, iso => 1,
-                  backup => 1}, { images => 1 }],
+                  backup => 1, snippets => 1}, { images => 1 }],
        format => [ { raw => 1, qcow2 => 1, vmdk => 1 } , 'raw' ],
     };
 }
@@ -80,7 +107,7 @@ sub properties {
            type => 'string',
        },
        password => {
-           description => "Password for CIFS share.",
+           description => "Password for accessing the share/datastore.",
            type => 'string',
            maxLength => 256,
        },
@@ -91,8 +118,9 @@ sub properties {
            maxLength => 256,
        },
        smbversion => {
-           description => "",
+           description => "SMB protocol version",
            type => 'string',
+           enum => ['2.0', '2.1', '3.0'],
            optional => 1,
        },
     };
@@ -106,12 +134,15 @@ sub options {
        nodes => { optional => 1 },
        disable => { optional => 1 },
        maxfiles => { optional => 1 },
+       'prune-backups' => { optional => 1 },
        content => { optional => 1 },
        format => { optional => 1 },
        username => { optional => 1 },
        password => { optional => 1},
        domain => { optional => 1},
        smbversion => { optional => 1},
+       mkdir => { optional => 1 },
+       bwlimit => { optional => 1 },
     };
 }
 
@@ -126,6 +157,46 @@ sub check_config {
 
 # Storage implementation
 
+sub on_add_hook {
+    my ($class, $storeid, $scfg, %param) = @_;
+
+    if (defined($param{password})) {
+       cifs_set_credentials($param{password}, $storeid);
+       if (!exists($scfg->{username})) {
+           warn "ignoring password parameter\n";
+       }
+    } else {
+       cifs_delete_credentials($storeid);
+    }
+
+    return;
+}
+
+sub on_update_hook {
+    my ($class, $storeid, $scfg, %param) = @_;
+
+    return if !exists($param{password});
+
+    if (defined($param{password})) {
+       cifs_set_credentials($param{password}, $storeid);
+       if (!exists($scfg->{username})) {
+           warn "ignoring password parameter\n";
+       }
+    } else {
+       cifs_delete_credentials($storeid);
+    }
+
+    return;
+}
+
+sub on_delete_hook {
+    my ($class, $storeid, $scfg) = @_;
+
+    cifs_delete_credentials($storeid);
+
+    return;
+}
+
 sub status {
     my ($class, $storeid, $scfg, $cache) = @_;
 
@@ -154,7 +225,7 @@ sub activate_storage {
 
     if (!cifs_is_mounted($server, $share, $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;
@@ -185,9 +256,9 @@ sub deactivate_storage {
 sub check_connection {
     my ($class, $storeid, $scfg) = @_;
 
-    my $server = $scfg->{server};
+    my $servicename = '//'.$scfg->{server}.'/'.$scfg->{share};
 
-    my $cmd = ['/usr/bin/smbclient', '-L', $server, '-d', '0', '-m'];
+    my $cmd = ['/usr/bin/smbclient', $servicename, '-d', '0', '-m'];
 
     push @$cmd, $scfg->{smbversion} ? "smb".int($scfg->{smbversion}) : 'smb3';
 
@@ -197,19 +268,29 @@ sub check_connection {
     } else {
        push @$cmd, '-U', 'Guest','-N';
     }
+    push @$cmd, '-c', 'echo 1 0';
 
     my $out_str;
-    eval {
-       run_command($cmd, timeout => 2, outfunc => sub {$out_str .= shift;},
-                   errfunc => sub {});
-    };
+    my $out = sub { $out_str .= shift };
+
+    eval { run_command($cmd, timeout => 10, outfunc => $out, errfunc => sub {}) };
 
     if (my $err = $@) {
-       die "$out_str\n" if ($out_str =~ m/NT_STATUS_ACCESS_DENIED/);
+       die "$out_str\n" if defined($out_str) &&
+           ($out_str =~ m/NT_STATUS_ACCESS_DENIED/);
        return 0;
     }
 
     return 1;
 }
 
+sub get_volume_notes {
+    my $class = shift;
+    PVE::Storage::DirPlugin::get_volume_notes($class, @_);
+}
+sub update_volume_notes {
+    my $class = shift;
+    PVE::Storage::DirPlugin::update_volume_notes($class, @_);
+}
+
 1;