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