]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/NFSPlugin.pm
bump version to 8.2.1
[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 server => { fixed => 1 },
83 export => { fixed => 1 },
84 nodes => { optional => 1 },
85 disable => { optional => 1 },
86 maxfiles => { optional => 1 },
87 'prune-backups' => { optional => 1 },
88 options => { optional => 1 },
89 content => { optional => 1 },
90 format => { optional => 1 },
91 mkdir => { optional => 1 },
92 bwlimit => { optional => 1 },
93 preallocation => { optional => 1 },
94 };
95 }
96
97
98 sub check_config {
99 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
100
101 $config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path};
102
103 return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck);
104 }
105
106 # Storage implementation
107
108 sub status {
109 my ($class, $storeid, $scfg, $cache) = @_;
110
111 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
112 if !$cache->{mountdata};
113
114 my $path = $scfg->{path};
115 my $server = $scfg->{server};
116 my $export = $scfg->{export};
117
118 return undef if !nfs_is_mounted($server, $export, $path, $cache->{mountdata});
119
120 return $class->SUPER::status($storeid, $scfg, $cache);
121 }
122
123 sub activate_storage {
124 my ($class, $storeid, $scfg, $cache) = @_;
125
126 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
127 if !$cache->{mountdata};
128
129 my $path = $scfg->{path};
130 my $server = $scfg->{server};
131 my $export = $scfg->{export};
132
133 if (!nfs_is_mounted($server, $export, $path, $cache->{mountdata})) {
134 # NOTE: only call mkpath when not mounted (avoid hang when NFS server is offline
135 mkpath $path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir});
136
137 die "unable to activate storage '$storeid' - " .
138 "directory '$path' does not exist\n" if ! -d $path;
139
140 nfs_mount($server, $export, $path, $scfg->{options});
141 }
142
143 $class->SUPER::activate_storage($storeid, $scfg, $cache);
144 }
145
146 sub deactivate_storage {
147 my ($class, $storeid, $scfg, $cache) = @_;
148
149 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
150 if !$cache->{mountdata};
151
152 my $path = $scfg->{path};
153 my $server = $scfg->{server};
154 my $export = $scfg->{export};
155
156 if (nfs_is_mounted($server, $export, $path, $cache->{mountdata})) {
157 my $cmd = ['/bin/umount', $path];
158 run_command($cmd, errmsg => 'umount error');
159 }
160 }
161
162 sub check_connection {
163 my ($class, $storeid, $scfg) = @_;
164
165 my $server = $scfg->{server};
166 my $opts = $scfg->{options};
167
168 my $cmd;
169 if (defined($opts) && $opts =~ /vers=4.*/) {
170 my $ip = PVE::JSONSchema::pve_verify_ip($server, 1);
171 if (!defined($ip)) {
172 $ip = PVE::Network::get_ip_from_hostname($server);
173 }
174
175 my $transport = PVE::JSONSchema::pve_verify_ipv4($ip, 1) ? 'tcp' : 'tcp6';
176
177 # nfsv4 uses a pseudo-filesystem always beginning with /
178 # no exports are listed
179 $cmd = ['/usr/sbin/rpcinfo', '-T', $transport, $ip, 'nfs', '4'];
180 } else {
181 $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
182 }
183
184 eval { run_command($cmd, timeout => 10, outfunc => sub {}, errfunc => sub {}) };
185 if (my $err = $@) {
186 return 0;
187 }
188
189 return 1;
190 }
191
192 sub get_volume_notes {
193 my $class = shift;
194 PVE::Storage::DirPlugin::get_volume_notes($class, @_);
195 }
196 sub update_volume_notes {
197 my $class = shift;
198 PVE::Storage::DirPlugin::update_volume_notes($class, @_);
199 }
200
201 1;