]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/NFSPlugin.pm
zfs: list zvol: skip different pools during parsing already
[pve-storage.git] / PVE / Storage / NFSPlugin.pm
CommitLineData
1dc01b9f
DM
1package PVE::Storage::NFSPlugin;
2
3use strict;
4use warnings;
5use IO::File;
da63f588 6use Net::IP;
d9e4e1ce 7use File::Path;
342a5680
FE
8
9use PVE::Network;
be2e0c16 10use PVE::Tools qw(run_command);
bb5520eb 11use PVE::ProcFSTools;
1dc01b9f
DM
12use PVE::Storage::Plugin;
13use PVE::JSONSchema qw(get_standard_option);
14
15use base qw(PVE::Storage::Plugin);
16
17# NFS helper functions
18
1dc01b9f
DM
19sub 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
34sub 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
50sub type {
51 return 'nfs';
52}
53
54sub 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
62sub 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
79sub options {
80 return {
81 path => { fixed => 1 },
b539bb46 82 'content-dirs' => { optional => 1 },
1dc01b9f
DM
83 server => { fixed => 1 },
84 export => { fixed => 1 },
3353698f 85 nodes => { optional => 1 },
1dc01b9f 86 disable => { optional => 1 },
3353698f
FE
87 maxfiles => { optional => 1 },
88 'prune-backups' => { optional => 1 },
8009417d 89 'max-protected-backups' => { optional => 1 },
1dc01b9f
DM
90 options => { optional => 1 },
91 content => { optional => 1 },
92 format => { optional => 1 },
c7616abc 93 mkdir => { optional => 1 },
9edb99a5 94 bwlimit => { optional => 1 },
95ff5dbd 95 preallocation => { optional => 1 },
1dc01b9f
DM
96 };
97}
98
99
100sub 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
110sub status {
111 my ($class, $storeid, $scfg, $cache) = @_;
112
80b64788 113 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
bb5520eb 114 if !$cache->{mountdata};
1dc01b9f
DM
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
125sub activate_storage {
126 my ($class, $storeid, $scfg, $cache) = @_;
127
80b64788 128 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
bb5520eb 129 if !$cache->{mountdata};
1dc01b9f
DM
130
131 my $path = $scfg->{path};
132 my $server = $scfg->{server};
133 my $export = $scfg->{export};
134
74dcca3a
TL
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
c7616abc 137 mkpath $path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir});
1dc01b9f
DM
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
148sub deactivate_storage {
149 my ($class, $storeid, $scfg, $cache) = @_;
150
80b64788 151 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
bb5520eb 152 if !$cache->{mountdata};
1dc01b9f
DM
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
4d284721
AD
164sub check_connection {
165 my ($class, $storeid, $scfg) = @_;
166
167 my $server = $scfg->{server};
89b9ac96
AA
168 my $opts = $scfg->{options};
169
170 my $cmd;
171 if (defined($opts) && $opts =~ /vers=4.*/) {
342a5680
FE
172 my $ip = PVE::JSONSchema::pve_verify_ip($server, 1);
173 if (!defined($ip)) {
174 $ip = PVE::Network::get_ip_from_hostname($server);
175 }
176
177 my $transport = PVE::JSONSchema::pve_verify_ipv4($ip, 1) ? 'tcp' : 'tcp6';
178
89b9ac96
AA
179 # nfsv4 uses a pseudo-filesystem always beginning with /
180 # no exports are listed
342a5680 181 $cmd = ['/usr/sbin/rpcinfo', '-T', $transport, $ip, 'nfs', '4'];
89b9ac96
AA
182 } else {
183 $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
184 }
86a9a680 185
70232472 186 eval { run_command($cmd, timeout => 10, outfunc => sub {}, errfunc => sub {}) };
86a9a680
DM
187 if (my $err = $@) {
188 return 0;
189 }
190
191 return 1;
4d284721 192}
86a9a680 193
f1de8281
FE
194# FIXME remove on the next APIAGE reset.
195# Deprecated, use get_volume_attribute instead.
44fdfb2a
TL
196sub get_volume_notes {
197 my $class = shift;
198 PVE::Storage::DirPlugin::get_volume_notes($class, @_);
199}
f1de8281
FE
200
201# FIXME remove on the next APIAGE reset.
202# Deprecated, use update_volume_attribute instead.
44fdfb2a
TL
203sub update_volume_notes {
204 my $class = shift;
205 PVE::Storage::DirPlugin::update_volume_notes($class, @_);
206}
207
f1de8281
FE
208sub get_volume_attribute {
209 return PVE::Storage::DirPlugin::get_volume_attribute(@_);
210}
211
212sub update_volume_attribute {
213 return PVE::Storage::DirPlugin::update_volume_attribute(@_);
214}
215
1dc01b9f 2161;