]>
Commit | Line | Data |
---|---|---|
1dc01b9f DM |
1 | package PVE::Storage::NFSPlugin; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use IO::File; | |
da63f588 | 6 | use Net::IP; |
d9e4e1ce | 7 | use File::Path; |
342a5680 FE |
8 | |
9 | use PVE::Network; | |
be2e0c16 | 10 | use PVE::Tools qw(run_command); |
bb5520eb | 11 | use PVE::ProcFSTools; |
1dc01b9f DM |
12 | use PVE::Storage::Plugin; |
13 | use PVE::JSONSchema qw(get_standard_option); | |
14 | ||
15 | use base qw(PVE::Storage::Plugin); | |
16 | ||
17 | # NFS helper functions | |
18 | ||
1dc01b9f DM |
19 | sub nfs_is_mounted { |
20 | my ($server, $export, $mountpoint, $mountdata) = @_; | |
21 | ||
da63f588 | 22 | $server = "[$server]" if Net::IP::ip_is_ipv6($server); |
1dc01b9f DM |
23 | my $source = "$server:$export"; |
24 | ||
80b64788 WB |
25 | $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata; |
26 | return $mountpoint if grep { | |
1773e785 | 27 | $_->[2] =~ /^nfs/ && |
aed6c85d | 28 | $_->[0] =~ m|^\Q$source\E/?$| && |
80b64788 WB |
29 | $_->[1] eq $mountpoint |
30 | } @$mountdata; | |
1dc01b9f DM |
31 | return undef; |
32 | } | |
33 | ||
34 | sub nfs_mount { | |
35 | my ($server, $export, $mountpoint, $options) = @_; | |
36 | ||
da63f588 | 37 | $server = "[$server]" if Net::IP::ip_is_ipv6($server); |
1dc01b9f DM |
38 | my $source = "$server:$export"; |
39 | ||
40 | my $cmd = ['/bin/mount', '-t', 'nfs', $source, $mountpoint]; | |
41 | if ($options) { | |
42 | push @$cmd, '-o', $options; | |
43 | } | |
44 | ||
45 | run_command($cmd, errmsg => "mount error"); | |
46 | } | |
47 | ||
48 | # Configuration | |
49 | ||
50 | sub type { | |
51 | return 'nfs'; | |
52 | } | |
53 | ||
54 | sub plugindata { | |
55 | return { | |
d1eb35ea | 56 | content => [ { images => 1, rootdir => 1, vztmpl => 1, iso => 1, backup => 1, snippets => 1 }, |
1dc01b9f DM |
57 | { images => 1 }], |
58 | format => [ { raw => 1, qcow2 => 1, vmdk => 1 } , 'raw' ], | |
59 | }; | |
60 | } | |
61 | ||
62 | sub properties { | |
63 | return { | |
64 | export => { | |
65 | description => "NFS export path.", | |
66 | type => 'string', format => 'pve-storage-path', | |
67 | }, | |
68 | server => { | |
69 | description => "Server IP or DNS name.", | |
70 | type => 'string', format => 'pve-storage-server', | |
71 | }, | |
72 | options => { | |
73 | description => "NFS mount options (see 'man nfs')", | |
74 | type => 'string', format => 'pve-storage-options', | |
75 | }, | |
76 | }; | |
77 | } | |
78 | ||
79 | sub options { | |
80 | return { | |
81 | path => { fixed => 1 }, | |
82 | server => { fixed => 1 }, | |
83 | export => { fixed => 1 }, | |
3353698f | 84 | nodes => { optional => 1 }, |
1dc01b9f | 85 | disable => { optional => 1 }, |
3353698f FE |
86 | maxfiles => { optional => 1 }, |
87 | 'prune-backups' => { optional => 1 }, | |
8009417d | 88 | 'max-protected-backups' => { optional => 1 }, |
1dc01b9f DM |
89 | options => { optional => 1 }, |
90 | content => { optional => 1 }, | |
91 | format => { optional => 1 }, | |
c7616abc | 92 | mkdir => { optional => 1 }, |
9edb99a5 | 93 | bwlimit => { optional => 1 }, |
95ff5dbd | 94 | preallocation => { optional => 1 }, |
1dc01b9f DM |
95 | }; |
96 | } | |
97 | ||
98 | ||
99 | sub check_config { | |
100 | my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_; | |
101 | ||
102 | $config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path}; | |
103 | ||
104 | return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck); | |
105 | } | |
106 | ||
107 | # Storage implementation | |
108 | ||
109 | sub status { | |
110 | my ($class, $storeid, $scfg, $cache) = @_; | |
111 | ||
80b64788 | 112 | $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts() |
bb5520eb | 113 | if !$cache->{mountdata}; |
1dc01b9f DM |
114 | |
115 | my $path = $scfg->{path}; | |
116 | my $server = $scfg->{server}; | |
117 | my $export = $scfg->{export}; | |
118 | ||
119 | return undef if !nfs_is_mounted($server, $export, $path, $cache->{mountdata}); | |
120 | ||
121 | return $class->SUPER::status($storeid, $scfg, $cache); | |
122 | } | |
123 | ||
124 | sub activate_storage { | |
125 | my ($class, $storeid, $scfg, $cache) = @_; | |
126 | ||
80b64788 | 127 | $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts() |
bb5520eb | 128 | if !$cache->{mountdata}; |
1dc01b9f DM |
129 | |
130 | my $path = $scfg->{path}; | |
131 | my $server = $scfg->{server}; | |
132 | my $export = $scfg->{export}; | |
133 | ||
74dcca3a TL |
134 | if (!nfs_is_mounted($server, $export, $path, $cache->{mountdata})) { |
135 | # NOTE: only call mkpath when not mounted (avoid hang when NFS server is offline | |
c7616abc | 136 | mkpath $path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir}); |
1dc01b9f DM |
137 | |
138 | die "unable to activate storage '$storeid' - " . | |
139 | "directory '$path' does not exist\n" if ! -d $path; | |
140 | ||
141 | nfs_mount($server, $export, $path, $scfg->{options}); | |
142 | } | |
143 | ||
144 | $class->SUPER::activate_storage($storeid, $scfg, $cache); | |
145 | } | |
146 | ||
147 | sub deactivate_storage { | |
148 | my ($class, $storeid, $scfg, $cache) = @_; | |
149 | ||
80b64788 | 150 | $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts() |
bb5520eb | 151 | if !$cache->{mountdata}; |
1dc01b9f DM |
152 | |
153 | my $path = $scfg->{path}; | |
154 | my $server = $scfg->{server}; | |
155 | my $export = $scfg->{export}; | |
156 | ||
157 | if (nfs_is_mounted($server, $export, $path, $cache->{mountdata})) { | |
158 | my $cmd = ['/bin/umount', $path]; | |
159 | run_command($cmd, errmsg => 'umount error'); | |
160 | } | |
161 | } | |
162 | ||
4d284721 AD |
163 | sub check_connection { |
164 | my ($class, $storeid, $scfg) = @_; | |
165 | ||
166 | my $server = $scfg->{server}; | |
89b9ac96 AA |
167 | my $opts = $scfg->{options}; |
168 | ||
169 | my $cmd; | |
170 | if (defined($opts) && $opts =~ /vers=4.*/) { | |
342a5680 FE |
171 | my $ip = PVE::JSONSchema::pve_verify_ip($server, 1); |
172 | if (!defined($ip)) { | |
173 | $ip = PVE::Network::get_ip_from_hostname($server); | |
174 | } | |
175 | ||
176 | my $transport = PVE::JSONSchema::pve_verify_ipv4($ip, 1) ? 'tcp' : 'tcp6'; | |
177 | ||
89b9ac96 AA |
178 | # nfsv4 uses a pseudo-filesystem always beginning with / |
179 | # no exports are listed | |
342a5680 | 180 | $cmd = ['/usr/sbin/rpcinfo', '-T', $transport, $ip, 'nfs', '4']; |
89b9ac96 AA |
181 | } else { |
182 | $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server]; | |
183 | } | |
86a9a680 | 184 | |
70232472 | 185 | eval { run_command($cmd, timeout => 10, outfunc => sub {}, errfunc => sub {}) }; |
86a9a680 DM |
186 | if (my $err = $@) { |
187 | return 0; | |
188 | } | |
189 | ||
190 | return 1; | |
4d284721 | 191 | } |
86a9a680 | 192 | |
f1de8281 FE |
193 | # FIXME remove on the next APIAGE reset. |
194 | # Deprecated, use get_volume_attribute instead. | |
44fdfb2a TL |
195 | sub get_volume_notes { |
196 | my $class = shift; | |
197 | PVE::Storage::DirPlugin::get_volume_notes($class, @_); | |
198 | } | |
f1de8281 FE |
199 | |
200 | # FIXME remove on the next APIAGE reset. | |
201 | # Deprecated, use update_volume_attribute instead. | |
44fdfb2a TL |
202 | sub update_volume_notes { |
203 | my $class = shift; | |
204 | PVE::Storage::DirPlugin::update_volume_notes($class, @_); | |
205 | } | |
206 | ||
f1de8281 FE |
207 | sub get_volume_attribute { |
208 | return PVE::Storage::DirPlugin::get_volume_attribute(@_); | |
209 | } | |
210 | ||
211 | sub update_volume_attribute { | |
212 | return PVE::Storage::DirPlugin::update_volume_attribute(@_); | |
213 | } | |
214 | ||
1dc01b9f | 215 | 1; |