]>
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
16 sub cifs_is_mounted
: prototype($$) {
17 my ($scfg, $mountdata) = @_;
19 my ($mountpoint, $server, $share) = $scfg->@{'path', 'server', 'share'};
20 my $subdir = $scfg->{subdir
} // '';
22 $server = "[$server]" if Net
::IP
::ip_is_ipv6
($server);
23 my $source = "//${server}/$share$subdir";
24 $mountdata = PVE
::ProcFSTools
::parse_proc_mounts
() if !$mountdata;
26 return $mountpoint if grep {
28 $_->[0] =~ m
|^\Q
$source\E/?$| &&
29 $_->[1] eq $mountpoint
34 sub cifs_cred_file_name
{
36 return "/etc/pve/priv/storage/${storeid}.pw";
39 sub cifs_delete_credentials
{
42 if (my $cred_file = get_cred_file
($storeid)) {
43 unlink($cred_file) or warn "removing cifs credientials '$cred_file' failed: $!\n";
47 sub cifs_set_credentials
{
48 my ($password, $storeid) = @_;
50 my $cred_file = cifs_cred_file_name
($storeid);
51 mkdir "/etc/pve/priv/storage";
53 PVE
::Tools
::file_set_contents
($cred_file, "password=$password\n");
61 my $cred_file = cifs_cred_file_name
($storeid);
69 sub cifs_mount
: prototype($$$$$) {
70 my ($scfg, $storeid, $smbver, $user, $domain) = @_;
72 my ($mountpoint, $server, $share) = $scfg->@{'path', 'server', 'share'};
73 my $subdir = $scfg->{subdir
} // '';
75 $server = "[$server]" if Net
::IP
::ip_is_ipv6
($server);
76 my $source = "//${server}/$share$subdir";
78 my $cmd = ['/bin/mount', '-t', 'cifs', $source, $mountpoint, '-o', 'soft', '-o'];
80 if (my $cred_file = get_cred_file
($storeid)) {
81 push @$cmd, "username=$user", '-o', "credentials=$cred_file";
82 push @$cmd, '-o', "domain=$domain" if defined($domain);
84 push @$cmd, 'guest,username=guest';
87 push @$cmd, '-o', defined($smbver) ?
"vers=$smbver" : "vers=default";
89 run_command
($cmd, errmsg
=> "mount error");
100 content
=> [ { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1,
101 backup
=> 1, snippets
=> 1}, { images
=> 1 }],
102 format
=> [ { raw
=> 1, qcow2
=> 1, vmdk
=> 1 } , 'raw' ],
109 description
=> "CIFS share.",
113 description
=> "Password for accessing the share/datastore.",
118 description
=> "CIFS domain.",
124 description
=> "SMB protocol version. 'default' if not set, negotiates the highest SMB2+"
125 ." version supported by both the client and server.",
127 default => 'default',
128 enum
=> ['default', '2.0', '2.1', '3', '3.0', '3.11'],
136 path
=> { fixed
=> 1 },
137 'content-dirs' => { optional
=> 1 },
138 server
=> { fixed
=> 1 },
139 share
=> { fixed
=> 1 },
140 subdir
=> { optional
=> 1 },
141 nodes
=> { optional
=> 1 },
142 disable
=> { optional
=> 1 },
143 maxfiles
=> { optional
=> 1 },
144 'prune-backups' => { optional
=> 1 },
145 'max-protected-backups' => { optional
=> 1 },
146 content
=> { optional
=> 1 },
147 format
=> { optional
=> 1 },
148 username
=> { optional
=> 1 },
149 password
=> { optional
=> 1},
150 domain
=> { optional
=> 1},
151 smbversion
=> { optional
=> 1},
152 mkdir => { optional
=> 1 },
153 bwlimit
=> { optional
=> 1 },
154 preallocation
=> { optional
=> 1 },
160 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
162 $config->{path
} = "/mnt/pve/$sectionId" if $create && !$config->{path
};
164 return $class->SUPER::check_config
($sectionId, $config, $create, $skipSchemaCheck);
167 # Storage implementation
170 my ($class, $storeid, $scfg, %sensitive) = @_;
172 if (defined($sensitive{password
})) {
173 cifs_set_credentials
($sensitive{password
}, $storeid);
174 if (!exists($scfg->{username
})) {
175 warn "storage $storeid: ignoring password parameter, no user set\n";
178 cifs_delete_credentials
($storeid);
185 my ($class, $storeid, $scfg, %sensitive) = @_;
187 return if !exists($sensitive{password
});
189 if (defined($sensitive{password
})) {
190 cifs_set_credentials
($sensitive{password
}, $storeid);
191 if (!exists($scfg->{username
})) {
192 warn "storage $storeid: ignoring password parameter, no user set\n";
195 cifs_delete_credentials
($storeid);
202 my ($class, $storeid, $scfg) = @_;
204 cifs_delete_credentials
($storeid);
210 my ($class, $storeid, $scfg, $cache) = @_;
212 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
213 if !$cache->{mountdata
};
216 if !cifs_is_mounted
($scfg, $cache->{mountdata
});
218 return $class->SUPER::status
($storeid, $scfg, $cache);
221 sub activate_storage
{
222 my ($class, $storeid, $scfg, $cache) = @_;
224 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
225 if !$cache->{mountdata
};
227 my $path = $scfg->{path
};
229 if (!cifs_is_mounted
($scfg, $cache->{mountdata
})) {
231 mkpath
$path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir});
233 die "unable to activate storage '$storeid' - " .
234 "directory '$path' does not exist\n" if ! -d
$path;
236 cifs_mount
($scfg, $storeid, $scfg->{smbversion
},
237 $scfg->{username
}, $scfg->{domain
});
240 $class->SUPER::activate_storage
($storeid, $scfg, $cache);
243 sub deactivate_storage
{
244 my ($class, $storeid, $scfg, $cache) = @_;
246 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
247 if !$cache->{mountdata
};
249 my $path = $scfg->{path
};
251 if (cifs_is_mounted
($scfg, $cache->{mountdata
})) {
252 my $cmd = ['/bin/umount', $path];
253 run_command
($cmd, errmsg
=> 'umount error');
257 sub check_connection
{
258 my ($class, $storeid, $scfg) = @_;
260 my $servicename = '//'.$scfg->{server
}.'/'.$scfg->{share
};
262 my $cmd = ['/usr/bin/smbclient', $servicename, '-d', '0'];
264 if (defined($scfg->{smbversion
}) && $scfg->{smbversion
} ne 'default') {
265 # max-protocol version, so basically only relevant for smb2 vs smb3
266 push @$cmd, '-m', "smb" . int($scfg->{smbversion
});
269 if (my $cred_file = get_cred_file
($storeid)) {
270 push @$cmd, '-U', $scfg->{username
}, '-A', $cred_file;
271 push @$cmd, '-W', $scfg->{domain
} if defined($scfg->{domain
});
273 push @$cmd, '-U', 'Guest','-N';
275 push @$cmd, '-c', 'echo 1 0';
278 my $out = sub { $out_str .= shift };
280 eval { run_command
($cmd, timeout
=> 10, outfunc
=> $out, errfunc
=> sub {}) };
283 die "$out_str\n" if defined($out_str) &&
284 ($out_str =~ m/NT_STATUS_(ACCESS_DENIED|LOGON_FAILURE)/);
291 # FIXME remove on the next APIAGE reset.
292 # Deprecated, use get_volume_attribute instead.
293 sub get_volume_notes
{
295 PVE
::Storage
::DirPlugin
::get_volume_notes
($class, @_);
298 # FIXME remove on the next APIAGE reset.
299 # Deprecated, use update_volume_attribute instead.
300 sub update_volume_notes
{
302 PVE
::Storage
::DirPlugin
::update_volume_notes
($class, @_);
305 sub get_volume_attribute
{
306 return PVE
::Storage
::DirPlugin
::get_volume_attribute
(@_);
309 sub update_volume_attribute
{
310 return PVE
::Storage
::DirPlugin
::update_volume_attribute
(@_);