]>
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);
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";
70 my ($server, $share, $mountpoint, $storeid, $smbver, $user, $domain) = @_;
72 $server = "[$server]" if Net
::IP
::ip_is_ipv6
($server);
73 my $source = "//${server}/$share";
75 my $cmd = ['/bin/mount', '-t', 'cifs', $source, $mountpoint, '-o', 'soft', '-o'];
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);
81 push @$cmd, 'guest,username=guest';
84 push @$cmd, '-o', defined($smbver) ?
"vers=$smbver" : "vers=3.0";
86 run_command
($cmd, errmsg
=> "mount error");
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' ],
106 description
=> "CIFS share.",
110 description
=> "Password for accessing the share/datastore.",
115 description
=> "CIFS domain.",
121 description
=> "SMB protocol version",
123 enum
=> ['2.0', '2.1', '3.0'],
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 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, %param) = @_;
162 if (defined($param{password
})) {
163 cifs_set_credentials
($param{password
}, $storeid);
164 if (!exists($scfg->{username
})) {
165 warn "ignoring password parameter\n";
168 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);
188 my ($class, $storeid, $scfg) = @_;
190 cifs_delete_credentials
($storeid);
194 my ($class, $storeid, $scfg, $cache) = @_;
196 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
197 if !$cache->{mountdata
};
199 my $path = $scfg->{path
};
200 my $server = $scfg->{server
};
201 my $share = $scfg->{share
};
204 if !cifs_is_mounted
($server, $share, $path, $cache->{mountdata
});
206 return $class->SUPER::status
($storeid, $scfg, $cache);
209 sub activate_storage
{
210 my ($class, $storeid, $scfg, $cache) = @_;
212 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
213 if !$cache->{mountdata
};
215 my $path = $scfg->{path
};
216 my $server = $scfg->{server
};
217 my $share = $scfg->{share
};
219 if (!cifs_is_mounted
($server, $share, $path, $cache->{mountdata
})) {
221 mkpath
$path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir});
223 die "unable to activate storage '$storeid' - " .
224 "directory '$path' does not exist\n" if ! -d
$path;
226 cifs_mount
($server, $share, $path, $storeid, $scfg->{smbversion
},
227 $scfg->{username
}, $scfg->{domain
});
230 $class->SUPER::activate_storage
($storeid, $scfg, $cache);
233 sub deactivate_storage
{
234 my ($class, $storeid, $scfg, $cache) = @_;
236 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
237 if !$cache->{mountdata
};
239 my $path = $scfg->{path
};
240 my $server = $scfg->{server
};
241 my $share = $scfg->{share
};
243 if (cifs_is_mounted
($server, $share, $path, $cache->{mountdata
})) {
244 my $cmd = ['/bin/umount', $path];
245 run_command
($cmd, errmsg
=> 'umount error');
249 sub check_connection
{
250 my ($class, $storeid, $scfg) = @_;
252 my $servicename = '//'.$scfg->{server
}.'/'.$scfg->{share
};
254 my $cmd = ['/usr/bin/smbclient', $servicename, '-d', '0', '-m'];
256 push @$cmd, $scfg->{smbversion
} ?
"smb".int($scfg->{smbversion
}) : 'smb3';
258 if (my $cred_file = get_cred_file
($storeid)) {
259 push @$cmd, '-U', $scfg->{username
}, '-A', $cred_file;
260 push @$cmd, '-W', $scfg->{domain
} if defined($scfg->{domain
});
262 push @$cmd, '-U', 'Guest','-N';
265 push @$cmd, '-c', 'echo 1 0';
269 run_command
($cmd, timeout
=> 2, outfunc
=> sub {$out_str .= shift;},
274 die "$out_str\n" if defined($out_str) &&
275 ($out_str =~ m/NT_STATUS_ACCESS_DENIED/);