]>
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
{
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=3.0";
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",
120 enum
=> ['2.0', '2.1', '3.0'],
128 path
=> { fixed
=> 1 },
129 server
=> { fixed
=> 1 },
130 share
=> { fixed
=> 1 },
131 nodes
=> { optional
=> 1 },
132 disable
=> { optional
=> 1 },
133 maxfiles
=> { optional
=> 1 },
134 'prune-backups' => { optional
=> 1 },
135 content
=> { optional
=> 1 },
136 format
=> { optional
=> 1 },
137 username
=> { optional
=> 1 },
138 password
=> { optional
=> 1},
139 domain
=> { optional
=> 1},
140 smbversion
=> { optional
=> 1},
141 mkdir => { optional
=> 1 },
142 bwlimit
=> { optional
=> 1 },
148 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
150 $config->{path
} = "/mnt/pve/$sectionId" if $create && !$config->{path
};
152 return $class->SUPER::check_config
($sectionId, $config, $create, $skipSchemaCheck);
155 # Storage implementation
158 my ($class, $storeid, $scfg, %param) = @_;
160 if (defined($param{password
})) {
161 cifs_set_credentials
($param{password
}, $storeid);
162 if (!exists($scfg->{username
})) {
163 warn "ignoring password parameter\n";
166 cifs_delete_credentials
($storeid);
173 my ($class, $storeid, $scfg, %param) = @_;
175 return if !exists($param{password
});
177 if (defined($param{password
})) {
178 cifs_set_credentials
($param{password
}, $storeid);
179 if (!exists($scfg->{username
})) {
180 warn "ignoring password parameter\n";
183 cifs_delete_credentials
($storeid);
190 my ($class, $storeid, $scfg) = @_;
192 cifs_delete_credentials
($storeid);
198 my ($class, $storeid, $scfg, $cache) = @_;
200 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
201 if !$cache->{mountdata
};
203 my $path = $scfg->{path
};
204 my $server = $scfg->{server
};
205 my $share = $scfg->{share
};
208 if !cifs_is_mounted
($server, $share, $path, $cache->{mountdata
});
210 return $class->SUPER::status
($storeid, $scfg, $cache);
213 sub activate_storage
{
214 my ($class, $storeid, $scfg, $cache) = @_;
216 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
217 if !$cache->{mountdata
};
219 my $path = $scfg->{path
};
220 my $server = $scfg->{server
};
221 my $share = $scfg->{share
};
223 if (!cifs_is_mounted
($server, $share, $path, $cache->{mountdata
})) {
225 mkpath
$path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir});
227 die "unable to activate storage '$storeid' - " .
228 "directory '$path' does not exist\n" if ! -d
$path;
230 cifs_mount
($server, $share, $path, $storeid, $scfg->{smbversion
},
231 $scfg->{username
}, $scfg->{domain
});
234 $class->SUPER::activate_storage
($storeid, $scfg, $cache);
237 sub deactivate_storage
{
238 my ($class, $storeid, $scfg, $cache) = @_;
240 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
241 if !$cache->{mountdata
};
243 my $path = $scfg->{path
};
244 my $server = $scfg->{server
};
245 my $share = $scfg->{share
};
247 if (cifs_is_mounted
($server, $share, $path, $cache->{mountdata
})) {
248 my $cmd = ['/bin/umount', $path];
249 run_command
($cmd, errmsg
=> 'umount error');
253 sub check_connection
{
254 my ($class, $storeid, $scfg) = @_;
256 my $servicename = '//'.$scfg->{server
}.'/'.$scfg->{share
};
258 my $cmd = ['/usr/bin/smbclient', $servicename, '-d', '0', '-m'];
260 push @$cmd, $scfg->{smbversion
} ?
"smb".int($scfg->{smbversion
}) : 'smb3';
262 if (my $cred_file = get_cred_file
($storeid)) {
263 push @$cmd, '-U', $scfg->{username
}, '-A', $cred_file;
264 push @$cmd, '-W', $scfg->{domain
} if defined($scfg->{domain
});
266 push @$cmd, '-U', 'Guest','-N';
268 push @$cmd, '-c', 'echo 1 0';
271 my $out = sub { $out_str .= shift };
273 eval { run_command
($cmd, timeout
=> 10, outfunc
=> $out, errfunc
=> sub {}) };
276 die "$out_str\n" if defined($out_str) &&
277 ($out_str =~ m/NT_STATUS_ACCESS_DENIED/);
284 sub get_volume_notes
{
286 PVE
::Storage
::DirPlugin
::get_volume_notes
($class, @_);
288 sub update_volume_notes
{
290 PVE
::Storage
::DirPlugin
::update_volume_notes
($class, @_);