]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/CIFSPlugin.pm
c5f38942979aafeb4dcfb28bbd892c83910e6c20
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
{
33 return "/etc/pve/priv/storage/${storeid}.pw";
36 sub cifs_delete_credentials
{
39 if (my $cred_file = get_cred_file
($storeid)) {
40 unlink($cred_file) or warn "removing cifs credientials '$cred_file' failed: $!\n";
44 sub cifs_set_credentials
{
45 my ($password, $storeid) = @_;
47 my $cred_file = cifs_cred_file_name
($storeid);
48 mkdir "/etc/pve/priv/storage";
50 PVE
::Tools
::file_set_contents
($cred_file, "password=$password\n");
58 my $cred_file = cifs_cred_file_name
($storeid);
67 my ($server, $share, $mountpoint, $storeid, $smbver, $user, $domain) = @_;
69 $server = "[$server]" if Net
::IP
::ip_is_ipv6
($server);
70 my $source = "//${server}/$share";
72 my $cmd = ['/bin/mount', '-t', 'cifs', $source, $mountpoint, '-o', 'soft', '-o'];
74 if (my $cred_file = get_cred_file
($storeid)) {
75 push @$cmd, "username=$user", '-o', "credentials=$cred_file";
76 push @$cmd, '-o', "domain=$domain" if defined($domain);
78 push @$cmd, 'guest,username=guest';
81 push @$cmd, '-o', defined($smbver) ?
"vers=$smbver" : "vers=default";
83 run_command
($cmd, errmsg
=> "mount error");
94 content
=> [ { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1,
95 backup
=> 1, snippets
=> 1}, { images
=> 1 }],
96 format
=> [ { raw
=> 1, qcow2
=> 1, vmdk
=> 1 } , 'raw' ],
103 description
=> "CIFS share.",
107 description
=> "Password for accessing the share/datastore.",
112 description
=> "CIFS domain.",
118 description
=> "SMB protocol version. 'default' if not set, negotiates the highest SMB2+"
119 ." version supported by both the client and server.",
121 default => 'default',
122 enum
=> ['default', '2.0', '2.1', '3', '3.0', '3.11'],
130 path
=> { fixed
=> 1 },
131 server
=> { fixed
=> 1 },
132 share
=> { fixed
=> 1 },
133 nodes
=> { optional
=> 1 },
134 disable
=> { optional
=> 1 },
135 maxfiles
=> { optional
=> 1 },
136 'prune-backups' => { optional
=> 1 },
137 content
=> { optional
=> 1 },
138 format
=> { optional
=> 1 },
139 username
=> { optional
=> 1 },
140 password
=> { optional
=> 1},
141 domain
=> { optional
=> 1},
142 smbversion
=> { optional
=> 1},
143 mkdir => { optional
=> 1 },
144 bwlimit
=> { optional
=> 1 },
150 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
152 $config->{path
} = "/mnt/pve/$sectionId" if $create && !$config->{path
};
154 return $class->SUPER::check_config
($sectionId, $config, $create, $skipSchemaCheck);
157 # Storage implementation
160 my ($class, $storeid, $scfg, %sensitive) = @_;
162 if (defined($sensitive{password
})) {
163 cifs_set_credentials
($sensitive{password
}, $storeid);
164 if (!exists($scfg->{username
})) {
165 warn "storage $storeid: ignoring password parameter, no user set\n";
168 cifs_delete_credentials
($storeid);
175 my ($class, $storeid, $scfg, %sensitive) = @_;
177 return if !exists($sensitive{password
});
179 if (defined($sensitive{password
})) {
180 cifs_set_credentials
($sensitive{password
}, $storeid);
181 if (!exists($scfg->{username
})) {
182 warn "storage $storeid: ignoring password parameter, no user set\n";
185 cifs_delete_credentials
($storeid);
192 my ($class, $storeid, $scfg) = @_;
194 cifs_delete_credentials
($storeid);
200 my ($class, $storeid, $scfg, $cache) = @_;
202 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
203 if !$cache->{mountdata
};
205 my $path = $scfg->{path
};
206 my $server = $scfg->{server
};
207 my $share = $scfg->{share
};
210 if !cifs_is_mounted
($server, $share, $path, $cache->{mountdata
});
212 return $class->SUPER::status
($storeid, $scfg, $cache);
215 sub activate_storage
{
216 my ($class, $storeid, $scfg, $cache) = @_;
218 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
219 if !$cache->{mountdata
};
221 my $path = $scfg->{path
};
222 my $server = $scfg->{server
};
223 my $share = $scfg->{share
};
225 if (!cifs_is_mounted
($server, $share, $path, $cache->{mountdata
})) {
227 mkpath
$path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir});
229 die "unable to activate storage '$storeid' - " .
230 "directory '$path' does not exist\n" if ! -d
$path;
232 cifs_mount
($server, $share, $path, $storeid, $scfg->{smbversion
},
233 $scfg->{username
}, $scfg->{domain
});
236 $class->SUPER::activate_storage
($storeid, $scfg, $cache);
239 sub deactivate_storage
{
240 my ($class, $storeid, $scfg, $cache) = @_;
242 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
243 if !$cache->{mountdata
};
245 my $path = $scfg->{path
};
246 my $server = $scfg->{server
};
247 my $share = $scfg->{share
};
249 if (cifs_is_mounted
($server, $share, $path, $cache->{mountdata
})) {
250 my $cmd = ['/bin/umount', $path];
251 run_command
($cmd, errmsg
=> 'umount error');
255 sub check_connection
{
256 my ($class, $storeid, $scfg) = @_;
258 my $servicename = '//'.$scfg->{server
}.'/'.$scfg->{share
};
260 my $cmd = ['/usr/bin/smbclient', $servicename, '-d', '0'];
262 if (defined($scfg->{smbversion
}) && $scfg->{smbversion
} ne 'default') {
263 # max-protocol version, so basically only relevant for smb2 vs smb3
264 push @$cmd, '-m', "smb" . int($scfg->{smbversion
});
267 if (my $cred_file = get_cred_file
($storeid)) {
268 push @$cmd, '-U', $scfg->{username
}, '-A', $cred_file;
269 push @$cmd, '-W', $scfg->{domain
} if defined($scfg->{domain
});
271 push @$cmd, '-U', 'Guest','-N';
273 push @$cmd, '-c', 'echo 1 0';
276 my $out = sub { $out_str .= shift };
278 eval { run_command
($cmd, timeout
=> 10, outfunc
=> $out, errfunc
=> sub {}) };
281 die "$out_str\n" if defined($out_str) &&
282 ($out_str =~ m/NT_STATUS_ACCESS_DENIED/);
289 sub get_volume_notes
{
291 PVE
::Storage
::DirPlugin
::get_volume_notes
($class, @_);
293 sub update_volume_notes
{
295 PVE
::Storage
::DirPlugin
::update_volume_notes
($class, @_);