]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/CIFSPlugin.pm
1 package PVE
::Storage
::CIFSPlugin
;
6 use PVE
::Tools
qw(run_command);
9 use PVE
::Storage
::Plugin
;
10 use PVE
::JSONSchema
qw(get_standard_option);
12 use base
qw(PVE::Storage::Plugin);
14 # CIFS helper functions
17 my ($server, $share, $mountpoint, $mountdata) = @_;
19 $server = "[$server]" if Net
::IP
::ip_is_ipv6
($server);
20 my $source = "//${server}/$share";
21 $mountdata = PVE
::ProcFSTools
::parse_proc_mounts
() if !$mountdata;
23 return $mountpoint if grep {
25 $_->[0] =~ m
|^\Q
$source\E/?$| &&
26 $_->[1] eq $mountpoint
31 sub cifs_cred_file_name
{
34 return "/etc/pve/priv/${storeid}.cred";
37 sub cifs_set_credentials
{
38 my ($password, $storeid) = @_;
40 my $cred_file = cifs_cred_file_name
($storeid);
42 PVE
::Tools
::file_set_contents
($cred_file, "password=$password\n");
50 my $cred_file = cifs_cred_file_name
($storeid);
52 return -e
$cred_file ?
$cred_file : undef;
56 my ($server, $share, $mountpoint, $storeid, $smbver, $user, $domain) = @_;
58 $server = "[$server]" if Net
::IP
::ip_is_ipv6
($server);
59 my $source = "//${server}/$share";
61 my $cmd = ['/bin/mount', '-t', 'cifs', $source, $mountpoint, '-o', 'soft', '-o'];
63 if (my $cred_file = get_cred_file
($storeid)) {
64 push @$cmd, "username=$user", '-o', "credentials=$cred_file";
65 push @$cmd, '-o', "domain=$domain" if defined($domain);
67 push @$cmd, 'guest,username=guest';
70 push @$cmd, '-o', defined($smbver) ?
"vers=$smbver" : "vers=3.0";
72 run_command
($cmd, errmsg
=> "mount error");
83 content
=> [ { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1,
84 backup
=> 1, snippets
=> 1}, { images
=> 1 }],
85 format
=> [ { raw
=> 1, qcow2
=> 1, vmdk
=> 1 } , 'raw' ],
92 description
=> "CIFS share.",
96 description
=> "Password for CIFS share.",
101 description
=> "CIFS domain.",
107 description
=> "SMB protocol version",
109 enum
=> ['2.0', '2.1', '3.0'],
117 path
=> { fixed
=> 1 },
118 server
=> { fixed
=> 1 },
119 share
=> { fixed
=> 1 },
120 nodes
=> { optional
=> 1 },
121 disable
=> { optional
=> 1 },
122 maxfiles
=> { optional
=> 1 },
123 content
=> { optional
=> 1 },
124 format
=> { optional
=> 1 },
125 username
=> { optional
=> 1 },
126 password
=> { optional
=> 1},
127 domain
=> { optional
=> 1},
128 smbversion
=> { optional
=> 1},
129 mkdir => { optional
=> 1 },
130 bwlimit
=> { optional
=> 1 },
136 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
138 $config->{path
} = "/mnt/pve/$sectionId" if $create && !$config->{path
};
140 return $class->SUPER::check_config
($sectionId, $config, $create, $skipSchemaCheck);
143 # Storage implementation
146 my ($class, $storeid, $scfg, %param) = @_;
148 if (my $password = $param{password
}) {
149 cifs_set_credentials
($password, $storeid);
154 my ($class, $storeid, $scfg) = @_;
156 my $cred_file = cifs_cred_file_name
($storeid);
158 unlink($cred_file) or warn "removing cifs credientials '$cred_file' failed: $!\n";
163 my ($class, $storeid, $scfg, $cache) = @_;
165 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
166 if !$cache->{mountdata
};
168 my $path = $scfg->{path
};
169 my $server = $scfg->{server
};
170 my $share = $scfg->{share
};
173 if !cifs_is_mounted
($server, $share, $path, $cache->{mountdata
});
175 return $class->SUPER::status
($storeid, $scfg, $cache);
178 sub activate_storage
{
179 my ($class, $storeid, $scfg, $cache) = @_;
181 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
182 if !$cache->{mountdata
};
184 my $path = $scfg->{path
};
185 my $server = $scfg->{server
};
186 my $share = $scfg->{share
};
188 if (!cifs_is_mounted
($server, $share, $path, $cache->{mountdata
})) {
190 mkpath
$path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir});
192 die "unable to activate storage '$storeid' - " .
193 "directory '$path' does not exist\n" if ! -d
$path;
195 cifs_mount
($server, $share, $path, $storeid, $scfg->{smbversion
},
196 $scfg->{username
}, $scfg->{domain
});
199 $class->SUPER::activate_storage
($storeid, $scfg, $cache);
202 sub deactivate_storage
{
203 my ($class, $storeid, $scfg, $cache) = @_;
205 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
206 if !$cache->{mountdata
};
208 my $path = $scfg->{path
};
209 my $server = $scfg->{server
};
210 my $share = $scfg->{share
};
212 if (cifs_is_mounted
($server, $share, $path, $cache->{mountdata
})) {
213 my $cmd = ['/bin/umount', $path];
214 run_command
($cmd, errmsg
=> 'umount error');
218 sub check_connection
{
219 my ($class, $storeid, $scfg) = @_;
221 my $servicename = '//'.$scfg->{server
}.'/'.$scfg->{share
};
223 my $cmd = ['/usr/bin/smbclient', $servicename, '-d', '0', '-m'];
225 push @$cmd, $scfg->{smbversion
} ?
"smb".int($scfg->{smbversion
}) : 'smb3';
227 if (my $cred_file = get_cred_file
($storeid)) {
228 push @$cmd, '-U', $scfg->{username
}, '-A', $cred_file;
229 push @$cmd, '-W', $scfg->{domain
} if defined($scfg->{domain
});
231 push @$cmd, '-U', 'Guest','-N';
234 push @$cmd, '-c', 'echo 1 0';
238 run_command
($cmd, timeout
=> 2, outfunc
=> sub {$out_str .= shift;},
243 die "$out_str\n" if defined($out_str) &&
244 ($out_str =~ m/NT_STATUS_ACCESS_DENIED/);