]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/CIFSPlugin.pm
fix #3097: cifs, nfs: increase connection check timeout to 10s
[pve-storage.git] / PVE / Storage / CIFSPlugin.pm
1 package PVE::Storage::CIFSPlugin;
2
3 use strict;
4 use warnings;
5 use Net::IP;
6 use PVE::Tools qw(run_command);
7 use PVE::ProcFSTools;
8 use File::Path;
9 use PVE::Storage::Plugin;
10 use PVE::JSONSchema qw(get_standard_option);
11
12 use base qw(PVE::Storage::Plugin);
13
14 # CIFS helper functions
15
16 sub cifs_is_mounted {
17 my ($server, $share, $mountpoint, $mountdata) = @_;
18
19 $server = "[$server]" if Net::IP::ip_is_ipv6($server);
20 my $source = "//${server}/$share";
21 $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata;
22
23 return $mountpoint if grep {
24 $_->[2] =~ /^cifs/ &&
25 $_->[0] =~ m|^\Q$source\E/?$| &&
26 $_->[1] eq $mountpoint
27 } @$mountdata;
28 return undef;
29 }
30
31 sub cifs_cred_file_name {
32 my ($storeid) = @_;
33 return "/etc/pve/priv/storage/${storeid}.pw";
34 }
35
36 sub cifs_delete_credentials {
37 my ($storeid) = @_;
38
39 if (my $cred_file = get_cred_file($storeid)) {
40 unlink($cred_file) or warn "removing cifs credientials '$cred_file' failed: $!\n";
41 }
42 }
43
44 sub cifs_set_credentials {
45 my ($password, $storeid) = @_;
46
47 my $cred_file = cifs_cred_file_name($storeid);
48 mkdir "/etc/pve/priv/storage";
49
50 PVE::Tools::file_set_contents($cred_file, "password=$password\n");
51
52 return $cred_file;
53 }
54
55 sub get_cred_file {
56 my ($storeid) = @_;
57
58 my $cred_file = cifs_cred_file_name($storeid);
59
60 if (-e $cred_file) {
61 return $cred_file;
62 } elsif (-e "/etc/pve/priv/${storeid}.cred") {
63 # FIXME: remove fallback with 7.0 by doing a rename on upgrade from 6.x
64 return "/etc/pve/priv/${storeid}.cred";
65 }
66 return undef;
67 }
68
69 sub cifs_mount {
70 my ($server, $share, $mountpoint, $storeid, $smbver, $user, $domain) = @_;
71
72 $server = "[$server]" if Net::IP::ip_is_ipv6($server);
73 my $source = "//${server}/$share";
74
75 my $cmd = ['/bin/mount', '-t', 'cifs', $source, $mountpoint, '-o', 'soft', '-o'];
76
77 if (my $cred_file = get_cred_file($storeid)) {
78 push @$cmd, "username=$user", '-o', "credentials=$cred_file";
79 push @$cmd, '-o', "domain=$domain" if defined($domain);
80 } else {
81 push @$cmd, 'guest,username=guest';
82 }
83
84 push @$cmd, '-o', defined($smbver) ? "vers=$smbver" : "vers=3.0";
85
86 run_command($cmd, errmsg => "mount error");
87 }
88
89 # Configuration
90
91 sub type {
92 return 'cifs';
93 }
94
95 sub plugindata {
96 return {
97 content => [ { images => 1, rootdir => 1, vztmpl => 1, iso => 1,
98 backup => 1, snippets => 1}, { images => 1 }],
99 format => [ { raw => 1, qcow2 => 1, vmdk => 1 } , 'raw' ],
100 };
101 }
102
103 sub properties {
104 return {
105 share => {
106 description => "CIFS share.",
107 type => 'string',
108 },
109 password => {
110 description => "Password for accessing the share/datastore.",
111 type => 'string',
112 maxLength => 256,
113 },
114 domain => {
115 description => "CIFS domain.",
116 type => 'string',
117 optional => 1,
118 maxLength => 256,
119 },
120 smbversion => {
121 description => "SMB protocol version",
122 type => 'string',
123 enum => ['2.0', '2.1', '3.0'],
124 optional => 1,
125 },
126 };
127 }
128
129 sub options {
130 return {
131 path => { fixed => 1 },
132 server => { fixed => 1 },
133 share => { fixed => 1 },
134 nodes => { optional => 1 },
135 disable => { optional => 1 },
136 maxfiles => { optional => 1 },
137 'prune-backups' => { optional => 1 },
138 content => { optional => 1 },
139 format => { optional => 1 },
140 username => { optional => 1 },
141 password => { optional => 1},
142 domain => { optional => 1},
143 smbversion => { optional => 1},
144 mkdir => { optional => 1 },
145 bwlimit => { optional => 1 },
146 };
147 }
148
149
150 sub check_config {
151 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
152
153 $config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path};
154
155 return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck);
156 }
157
158 # Storage implementation
159
160 sub on_add_hook {
161 my ($class, $storeid, $scfg, %param) = @_;
162
163 if (defined($param{password})) {
164 cifs_set_credentials($param{password}, $storeid);
165 if (!exists($scfg->{username})) {
166 warn "ignoring password parameter\n";
167 }
168 } else {
169 cifs_delete_credentials($storeid);
170 }
171 }
172
173 sub on_update_hook {
174 my ($class, $storeid, $scfg, %param) = @_;
175
176 return if !exists($param{password});
177
178 if (defined($param{password})) {
179 cifs_set_credentials($param{password}, $storeid);
180 if (!exists($scfg->{username})) {
181 warn "ignoring password parameter\n";
182 }
183 } else {
184 cifs_delete_credentials($storeid);
185 }
186 }
187
188 sub on_delete_hook {
189 my ($class, $storeid, $scfg) = @_;
190
191 cifs_delete_credentials($storeid);
192 }
193
194 sub status {
195 my ($class, $storeid, $scfg, $cache) = @_;
196
197 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
198 if !$cache->{mountdata};
199
200 my $path = $scfg->{path};
201 my $server = $scfg->{server};
202 my $share = $scfg->{share};
203
204 return undef
205 if !cifs_is_mounted($server, $share, $path, $cache->{mountdata});
206
207 return $class->SUPER::status($storeid, $scfg, $cache);
208 }
209
210 sub activate_storage {
211 my ($class, $storeid, $scfg, $cache) = @_;
212
213 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
214 if !$cache->{mountdata};
215
216 my $path = $scfg->{path};
217 my $server = $scfg->{server};
218 my $share = $scfg->{share};
219
220 if (!cifs_is_mounted($server, $share, $path, $cache->{mountdata})) {
221
222 mkpath $path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir});
223
224 die "unable to activate storage '$storeid' - " .
225 "directory '$path' does not exist\n" if ! -d $path;
226
227 cifs_mount($server, $share, $path, $storeid, $scfg->{smbversion},
228 $scfg->{username}, $scfg->{domain});
229 }
230
231 $class->SUPER::activate_storage($storeid, $scfg, $cache);
232 }
233
234 sub deactivate_storage {
235 my ($class, $storeid, $scfg, $cache) = @_;
236
237 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
238 if !$cache->{mountdata};
239
240 my $path = $scfg->{path};
241 my $server = $scfg->{server};
242 my $share = $scfg->{share};
243
244 if (cifs_is_mounted($server, $share, $path, $cache->{mountdata})) {
245 my $cmd = ['/bin/umount', $path];
246 run_command($cmd, errmsg => 'umount error');
247 }
248 }
249
250 sub check_connection {
251 my ($class, $storeid, $scfg) = @_;
252
253 my $servicename = '//'.$scfg->{server}.'/'.$scfg->{share};
254
255 my $cmd = ['/usr/bin/smbclient', $servicename, '-d', '0', '-m'];
256
257 push @$cmd, $scfg->{smbversion} ? "smb".int($scfg->{smbversion}) : 'smb3';
258
259 if (my $cred_file = get_cred_file($storeid)) {
260 push @$cmd, '-U', $scfg->{username}, '-A', $cred_file;
261 push @$cmd, '-W', $scfg->{domain} if defined($scfg->{domain});
262 } else {
263 push @$cmd, '-U', 'Guest','-N';
264 }
265 push @$cmd, '-c', 'echo 1 0';
266
267 my $out_str;
268 my $out = sub { $out_str .= shift };
269
270 eval { run_command($cmd, timeout => 10, outfunc => $out, errfunc => sub {}) };
271
272 if (my $err = $@) {
273 die "$out_str\n" if defined($out_str) &&
274 ($out_str =~ m/NT_STATUS_ACCESS_DENIED/);
275 return 0;
276 }
277
278 return 1;
279 }
280
281 1;