]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/NFSPlugin.pm
drbd: comment that the builtin plugin is depreacated
[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;
be2e0c16 8use PVE::Tools qw(run_command);
bb5520eb 9use PVE::ProcFSTools;
1dc01b9f
DM
10use PVE::Storage::Plugin;
11use PVE::JSONSchema qw(get_standard_option);
12
13use base qw(PVE::Storage::Plugin);
14
15# NFS helper functions
16
1dc01b9f
DM
17sub nfs_is_mounted {
18 my ($server, $export, $mountpoint, $mountdata) = @_;
19
da63f588 20 $server = "[$server]" if Net::IP::ip_is_ipv6($server);
1dc01b9f
DM
21 my $source = "$server:$export";
22
80b64788
WB
23 $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata;
24 return $mountpoint if grep {
1773e785 25 $_->[2] =~ /^nfs/ &&
aed6c85d 26 $_->[0] =~ m|^\Q$source\E/?$| &&
80b64788
WB
27 $_->[1] eq $mountpoint
28 } @$mountdata;
1dc01b9f
DM
29 return undef;
30}
31
32sub nfs_mount {
33 my ($server, $export, $mountpoint, $options) = @_;
34
da63f588 35 $server = "[$server]" if Net::IP::ip_is_ipv6($server);
1dc01b9f
DM
36 my $source = "$server:$export";
37
38 my $cmd = ['/bin/mount', '-t', 'nfs', $source, $mountpoint];
39 if ($options) {
40 push @$cmd, '-o', $options;
41 }
42
43 run_command($cmd, errmsg => "mount error");
44}
45
46# Configuration
47
48sub type {
49 return 'nfs';
50}
51
52sub plugindata {
53 return {
d1eb35ea 54 content => [ { images => 1, rootdir => 1, vztmpl => 1, iso => 1, backup => 1, snippets => 1 },
1dc01b9f
DM
55 { images => 1 }],
56 format => [ { raw => 1, qcow2 => 1, vmdk => 1 } , 'raw' ],
57 };
58}
59
60sub properties {
61 return {
62 export => {
63 description => "NFS export path.",
64 type => 'string', format => 'pve-storage-path',
65 },
66 server => {
67 description => "Server IP or DNS name.",
68 type => 'string', format => 'pve-storage-server',
69 },
70 options => {
71 description => "NFS mount options (see 'man nfs')",
72 type => 'string', format => 'pve-storage-options',
73 },
74 };
75}
76
77sub options {
78 return {
79 path => { fixed => 1 },
80 server => { fixed => 1 },
81 export => { fixed => 1 },
3353698f 82 nodes => { optional => 1 },
1dc01b9f 83 disable => { optional => 1 },
3353698f
FE
84 maxfiles => { optional => 1 },
85 'prune-backups' => { optional => 1 },
1dc01b9f
DM
86 options => { optional => 1 },
87 content => { optional => 1 },
88 format => { optional => 1 },
c7616abc 89 mkdir => { optional => 1 },
9edb99a5 90 bwlimit => { optional => 1 },
1dc01b9f
DM
91 };
92}
93
94
95sub check_config {
96 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
97
98 $config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path};
99
100 return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck);
101}
102
103# Storage implementation
104
105sub status {
106 my ($class, $storeid, $scfg, $cache) = @_;
107
80b64788 108 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
bb5520eb 109 if !$cache->{mountdata};
1dc01b9f
DM
110
111 my $path = $scfg->{path};
112 my $server = $scfg->{server};
113 my $export = $scfg->{export};
114
115 return undef if !nfs_is_mounted($server, $export, $path, $cache->{mountdata});
116
117 return $class->SUPER::status($storeid, $scfg, $cache);
118}
119
120sub activate_storage {
121 my ($class, $storeid, $scfg, $cache) = @_;
122
80b64788 123 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
bb5520eb 124 if !$cache->{mountdata};
1dc01b9f
DM
125
126 my $path = $scfg->{path};
127 my $server = $scfg->{server};
128 my $export = $scfg->{export};
129
74dcca3a
TL
130 if (!nfs_is_mounted($server, $export, $path, $cache->{mountdata})) {
131 # NOTE: only call mkpath when not mounted (avoid hang when NFS server is offline
c7616abc 132 mkpath $path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir});
1dc01b9f
DM
133
134 die "unable to activate storage '$storeid' - " .
135 "directory '$path' does not exist\n" if ! -d $path;
136
137 nfs_mount($server, $export, $path, $scfg->{options});
138 }
139
140 $class->SUPER::activate_storage($storeid, $scfg, $cache);
141}
142
143sub deactivate_storage {
144 my ($class, $storeid, $scfg, $cache) = @_;
145
80b64788 146 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
bb5520eb 147 if !$cache->{mountdata};
1dc01b9f
DM
148
149 my $path = $scfg->{path};
150 my $server = $scfg->{server};
151 my $export = $scfg->{export};
152
153 if (nfs_is_mounted($server, $export, $path, $cache->{mountdata})) {
154 my $cmd = ['/bin/umount', $path];
155 run_command($cmd, errmsg => 'umount error');
156 }
157}
158
4d284721
AD
159sub check_connection {
160 my ($class, $storeid, $scfg) = @_;
161
162 my $server = $scfg->{server};
4d284721 163
f8b0d82f 164 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
86a9a680 165
70232472 166 eval { run_command($cmd, timeout => 10, outfunc => sub {}, errfunc => sub {}) };
86a9a680
DM
167 if (my $err = $@) {
168 return 0;
169 }
170
171 return 1;
4d284721 172}
86a9a680 173
44fdfb2a
TL
174sub get_volume_notes {
175 my $class = shift;
176 PVE::Storage::DirPlugin::get_volume_notes($class, @_);
177}
178sub update_volume_notes {
179 my $class = shift;
180 PVE::Storage::DirPlugin::update_volume_notes($class, @_);
181}
182
1dc01b9f 1831;