]>
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
34 my $cred_file = '/etc/pve/priv/'.$storeid.'.cred';
43 my ($server, $share, $mountpoint, $storeid, $smbver, $user, $domain) = @_;
45 $server = "[$server]" if Net
::IP
::ip_is_ipv6
($server);
46 my $source = "//${server}/$share";
48 my $cmd = ['/bin/mount', '-t', 'cifs', $source, $mountpoint, '-o', 'soft', '-o'];
50 if (my $cred_file = get_cred_file
($storeid)) {
51 push @$cmd, "username=$user", '-o', "credentials=$cred_file";
52 push @$cmd, '-o', "domain=$domain" if defined($domain);
54 push @$cmd, 'guest,username=guest';
57 push @$cmd, '-o', defined($smbver) ?
"vers=$smbver" : "vers=3.0";
59 run_command
($cmd, errmsg
=> "mount error");
70 content
=> [ { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1,
71 backup
=> 1}, { images
=> 1 }],
72 format
=> [ { raw
=> 1, qcow2
=> 1, vmdk
=> 1 } , 'raw' ],
79 description
=> "CIFS share.",
83 description
=> "Password for CIFS share.",
88 description
=> "CIFS domain.",
103 path
=> { fixed
=> 1 },
104 server
=> { fixed
=> 1 },
105 share
=> { fixed
=> 1 },
106 nodes
=> { optional
=> 1 },
107 disable
=> { optional
=> 1 },
108 maxfiles
=> { optional
=> 1 },
109 content
=> { optional
=> 1 },
110 format
=> { optional
=> 1 },
111 username
=> { optional
=> 1 },
112 password
=> { optional
=> 1},
113 domain
=> { optional
=> 1},
114 smbversion
=> { optional
=> 1},
120 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
122 $config->{path
} = "/mnt/pve/$sectionId" if $create && !$config->{path
};
124 return $class->SUPER::check_config
($sectionId, $config, $create, $skipSchemaCheck);
127 # Storage implementation
130 my ($class, $storeid, $scfg, $cache) = @_;
132 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
133 if !$cache->{mountdata
};
135 my $path = $scfg->{path
};
136 my $server = $scfg->{server
};
137 my $share = $scfg->{share
};
140 if !cifs_is_mounted
($server, $share, $path, $cache->{mountdata
});
142 return $class->SUPER::status
($storeid, $scfg, $cache);
145 sub activate_storage
{
146 my ($class, $storeid, $scfg, $cache) = @_;
148 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
149 if !$cache->{mountdata
};
151 my $path = $scfg->{path
};
152 my $server = $scfg->{server
};
153 my $share = $scfg->{share
};
155 if (!cifs_is_mounted
($server, $share, $path, $cache->{mountdata
})) {
159 die "unable to activate storage '$storeid' - " .
160 "directory '$path' does not exist\n" if ! -d
$path;
162 cifs_mount
($server, $share, $path, $storeid, $scfg->{smbversion
},
163 $scfg->{username
}, $scfg->{domain
});
166 $class->SUPER::activate_storage
($storeid, $scfg, $cache);
169 sub deactivate_storage
{
170 my ($class, $storeid, $scfg, $cache) = @_;
172 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
173 if !$cache->{mountdata
};
175 my $path = $scfg->{path
};
176 my $server = $scfg->{server
};
177 my $share = $scfg->{share
};
179 if (cifs_is_mounted
($server, $share, $path, $cache->{mountdata
})) {
180 my $cmd = ['/bin/umount', $path];
181 run_command
($cmd, errmsg
=> 'umount error');
185 sub check_connection
{
186 my ($class, $storeid, $scfg) = @_;
188 my $server = $scfg->{server
};
190 my $cmd = ['/usr/bin/smbclient', '-L', $server, '-d', '0', '-m'];
192 push @$cmd, $scfg->{smbversion
} ?
"smb".int($scfg->{smbversion
}) : 'smb3';
194 if (my $cred_file = get_cred_file
($storeid)) {
195 push @$cmd, '-U', $scfg->{username
}, '-A', $cred_file;
196 push @$cmd, '-W', $scfg->{domain
} if defined($scfg->{domain
});
198 push @$cmd, '-U', 'Guest','-N';
203 run_command
($cmd, timeout
=> 2, outfunc
=> sub {$out_str .= shift;},
208 die "$out_str\n" if ($out_str =~ m/NT_STATUS_ACCESS_DENIED/);