]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/NFSPlugin.pm
cephfs: fix variable declared in conditional statement
[pve-storage.git] / PVE / Storage / NFSPlugin.pm
1 package PVE::Storage::NFSPlugin;
2
3 use strict;
4 use warnings;
5 use IO::File;
6 use Net::IP;
7 use File::Path;
8
9 use PVE::Network;
10 use PVE::Tools qw(run_command);
11 use PVE::ProcFSTools;
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
19 sub nfs_is_mounted {
20 my ($server, $export, $mountpoint, $mountdata) = @_;
21
22 $server = "[$server]" if Net::IP::ip_is_ipv6($server);
23 my $source = "$server:$export";
24
25 $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata;
26 return $mountpoint if grep {
27 $_->[2] =~ /^nfs/ &&
28 $_->[0] =~ m|^\Q$source\E/?$| &&
29 $_->[1] eq $mountpoint
30 } @$mountdata;
31 return undef;
32 }
33
34 sub nfs_mount {
35 my ($server, $export, $mountpoint, $options) = @_;
36
37 $server = "[$server]" if Net::IP::ip_is_ipv6($server);
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 {
56 content => [ { images => 1, rootdir => 1, vztmpl => 1, iso => 1, backup => 1, snippets => 1 },
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 'content-dirs' => { optional => 1 },
83 server => { fixed => 1 },
84 export => { fixed => 1 },
85 nodes => { optional => 1 },
86 disable => { optional => 1 },
87 maxfiles => { optional => 1 },
88 'prune-backups' => { optional => 1 },
89 'max-protected-backups' => { optional => 1 },
90 options => { optional => 1 },
91 content => { optional => 1 },
92 format => { optional => 1 },
93 mkdir => { optional => 1 },
94 bwlimit => { optional => 1 },
95 preallocation => { optional => 1 },
96 };
97 }
98
99
100 sub check_config {
101 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
102
103 $config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path};
104
105 return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck);
106 }
107
108 # Storage implementation
109
110 sub status {
111 my ($class, $storeid, $scfg, $cache) = @_;
112
113 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
114 if !$cache->{mountdata};
115
116 my $path = $scfg->{path};
117 my $server = $scfg->{server};
118 my $export = $scfg->{export};
119
120 return undef if !nfs_is_mounted($server, $export, $path, $cache->{mountdata});
121
122 return $class->SUPER::status($storeid, $scfg, $cache);
123 }
124
125 sub activate_storage {
126 my ($class, $storeid, $scfg, $cache) = @_;
127
128 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
129 if !$cache->{mountdata};
130
131 my $path = $scfg->{path};
132 my $server = $scfg->{server};
133 my $export = $scfg->{export};
134
135 if (!nfs_is_mounted($server, $export, $path, $cache->{mountdata})) {
136 # NOTE: only call mkpath when not mounted (avoid hang when NFS server is offline
137 mkpath $path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir});
138
139 die "unable to activate storage '$storeid' - " .
140 "directory '$path' does not exist\n" if ! -d $path;
141
142 nfs_mount($server, $export, $path, $scfg->{options});
143 }
144
145 $class->SUPER::activate_storage($storeid, $scfg, $cache);
146 }
147
148 sub deactivate_storage {
149 my ($class, $storeid, $scfg, $cache) = @_;
150
151 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
152 if !$cache->{mountdata};
153
154 my $path = $scfg->{path};
155 my $server = $scfg->{server};
156 my $export = $scfg->{export};
157
158 if (nfs_is_mounted($server, $export, $path, $cache->{mountdata})) {
159 my $cmd = ['/bin/umount', $path];
160 run_command($cmd, errmsg => 'umount error');
161 }
162 }
163
164 sub check_connection {
165 my ($class, $storeid, $scfg) = @_;
166
167 my $server = $scfg->{server};
168 my $opts = $scfg->{options};
169
170 my $cmd;
171
172 my $is_v4 = defined($opts) && $opts =~ /vers=4.*/;
173 if ($is_v4) {
174 my $ip = PVE::JSONSchema::pve_verify_ip($server, 1);
175 if (!defined($ip)) {
176 $ip = PVE::Network::get_ip_from_hostname($server);
177 }
178
179 my $transport = PVE::JSONSchema::pve_verify_ipv4($ip, 1) ? 'tcp' : 'tcp6';
180
181 # nfsv4 uses a pseudo-filesystem always beginning with /
182 # no exports are listed
183 $cmd = ['/usr/sbin/rpcinfo', '-T', $transport, $ip, 'nfs', '4'];
184 } else {
185 $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
186 }
187
188 eval { run_command($cmd, timeout => 10, outfunc => sub {}, errfunc => sub {}) };
189 if (my $err = $@) {
190 if ($is_v4) {
191 my $port = 2049;
192 $port = $1 if defined($opts) && $opts =~ /port=(\d+)/;
193
194 # rpcinfo is expected to work when the port is 0 (see 'man 5 nfs') and tcp_ping()
195 # defaults to port 7 when passing in 0.
196 return 0 if $port == 0;
197
198 return PVE::Network::tcp_ping($server, $port, 2);
199 }
200 return 0;
201 }
202
203 return 1;
204 }
205
206 # FIXME remove on the next APIAGE reset.
207 # Deprecated, use get_volume_attribute instead.
208 sub get_volume_notes {
209 my $class = shift;
210 PVE::Storage::DirPlugin::get_volume_notes($class, @_);
211 }
212
213 # FIXME remove on the next APIAGE reset.
214 # Deprecated, use update_volume_attribute instead.
215 sub update_volume_notes {
216 my $class = shift;
217 PVE::Storage::DirPlugin::update_volume_notes($class, @_);
218 }
219
220 sub get_volume_attribute {
221 return PVE::Storage::DirPlugin::get_volume_attribute(@_);
222 }
223
224 sub update_volume_attribute {
225 return PVE::Storage::DirPlugin::update_volume_attribute(@_);
226 }
227
228 1;