]>
git.proxmox.com Git - pve-storage.git/blob - src/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, $options) = $scfg->@{'path', 'server', 'share', 'options'};
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";
88 push @$cmd, '-o', $options if $options;
90 run_command
($cmd, errmsg
=> "mount error");
101 content
=> [ { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1,
102 backup
=> 1, snippets
=> 1}, { images
=> 1 }],
103 format
=> [ { raw
=> 1, qcow2
=> 1, vmdk
=> 1 } , 'raw' ],
110 description
=> "CIFS share.",
114 description
=> "Password for accessing the share/datastore.",
119 description
=> "CIFS domain.",
125 description
=> "SMB protocol version. 'default' if not set, negotiates the highest SMB2+"
126 ." version supported by both the client and server.",
128 default => 'default',
129 enum
=> ['default', '2.0', '2.1', '3', '3.0', '3.11'],
137 path
=> { fixed
=> 1 },
138 'content-dirs' => { optional
=> 1 },
139 server
=> { fixed
=> 1 },
140 share
=> { fixed
=> 1 },
141 subdir
=> { optional
=> 1 },
142 nodes
=> { optional
=> 1 },
143 disable
=> { optional
=> 1 },
144 maxfiles
=> { optional
=> 1 },
145 'prune-backups' => { optional
=> 1 },
146 'max-protected-backups' => { optional
=> 1 },
147 content
=> { optional
=> 1 },
148 format
=> { optional
=> 1 },
149 username
=> { optional
=> 1 },
150 password
=> { optional
=> 1},
151 domain
=> { optional
=> 1},
152 smbversion
=> { optional
=> 1},
153 mkdir => { optional
=> 1 },
154 'create-base-path' => { optional
=> 1 },
155 'create-subdirs' => { optional
=> 1 },
156 bwlimit
=> { optional
=> 1 },
157 preallocation
=> { optional
=> 1 },
158 options
=> { optional
=> 1 },
164 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
166 $config->{path
} = "/mnt/pve/$sectionId" if $create && !$config->{path
};
168 return $class->SUPER::check_config
($sectionId, $config, $create, $skipSchemaCheck);
171 # Storage implementation
174 my ($class, $storeid, $scfg, %sensitive) = @_;
176 if (defined($sensitive{password
})) {
177 cifs_set_credentials
($sensitive{password
}, $storeid);
178 if (!exists($scfg->{username
})) {
179 warn "storage $storeid: ignoring password parameter, no user set\n";
182 cifs_delete_credentials
($storeid);
189 my ($class, $storeid, $scfg, %sensitive) = @_;
191 return if !exists($sensitive{password
});
193 if (defined($sensitive{password
})) {
194 cifs_set_credentials
($sensitive{password
}, $storeid);
195 if (!exists($scfg->{username
})) {
196 warn "storage $storeid: ignoring password parameter, no user set\n";
199 cifs_delete_credentials
($storeid);
206 my ($class, $storeid, $scfg) = @_;
208 cifs_delete_credentials
($storeid);
214 my ($class, $storeid, $scfg, $cache) = @_;
216 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
217 if !$cache->{mountdata
};
220 if !cifs_is_mounted
($scfg, $cache->{mountdata
});
222 return $class->SUPER::status
($storeid, $scfg, $cache);
225 sub activate_storage
{
226 my ($class, $storeid, $scfg, $cache) = @_;
228 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
229 if !$cache->{mountdata
};
231 my $path = $scfg->{path
};
233 if (!cifs_is_mounted
($scfg, $cache->{mountdata
})) {
235 $class->config_aware_base_mkdir($scfg, $path);
237 die "unable to activate storage '$storeid' - " .
238 "directory '$path' does not exist\n" if ! -d
$path;
240 cifs_mount
($scfg, $storeid, $scfg->{smbversion
},
241 $scfg->{username
}, $scfg->{domain
});
244 $class->SUPER::activate_storage
($storeid, $scfg, $cache);
247 sub deactivate_storage
{
248 my ($class, $storeid, $scfg, $cache) = @_;
250 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
251 if !$cache->{mountdata
};
253 my $path = $scfg->{path
};
255 if (cifs_is_mounted
($scfg, $cache->{mountdata
})) {
256 my $cmd = ['/bin/umount', $path];
257 run_command
($cmd, errmsg
=> 'umount error');
261 sub check_connection
{
262 my ($class, $storeid, $scfg) = @_;
264 my $servicename = '//'.$scfg->{server
}.'/'.$scfg->{share
};
266 my $cmd = ['/usr/bin/smbclient', $servicename, '-d', '0'];
268 if (defined($scfg->{smbversion
}) && $scfg->{smbversion
} ne 'default') {
269 # max-protocol version, so basically only relevant for smb2 vs smb3
270 push @$cmd, '-m', "smb" . int($scfg->{smbversion
});
273 if (my $cred_file = get_cred_file
($storeid)) {
274 push @$cmd, '-U', $scfg->{username
}, '-A', $cred_file;
275 push @$cmd, '-W', $scfg->{domain
} if $scfg->{domain
};
277 push @$cmd, '-U', 'Guest','-N';
279 push @$cmd, '-c', 'echo 1 0';
282 my $out = sub { $out_str .= shift };
284 eval { run_command
($cmd, timeout
=> 10, outfunc
=> $out, errfunc
=> sub {}) };
287 die "$out_str\n" if defined($out_str) &&
288 ($out_str =~ m/NT_STATUS_(ACCESS_DENIED|INVALID_PARAMETER|LOGON_FAILURE)/);
295 # FIXME remove on the next APIAGE reset.
296 # Deprecated, use get_volume_attribute instead.
297 sub get_volume_notes
{
299 PVE
::Storage
::DirPlugin
::get_volume_notes
($class, @_);
302 # FIXME remove on the next APIAGE reset.
303 # Deprecated, use update_volume_attribute instead.
304 sub update_volume_notes
{
306 PVE
::Storage
::DirPlugin
::update_volume_notes
($class, @_);
309 sub get_volume_attribute
{
310 return PVE
::Storage
::DirPlugin
::get_volume_attribute
(@_);
313 sub update_volume_attribute
{
314 return PVE
::Storage
::DirPlugin
::update_volume_attribute
(@_);