]>
Commit | Line | Data |
---|---|---|
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 | use PVE::Tools qw(run_command); | |
9 | use PVE::ProcFSTools; | |
10 | use PVE::Storage::Plugin; | |
11 | use PVE::JSONSchema qw(get_standard_option); | |
12 | ||
13 | use base qw(PVE::Storage::Plugin); | |
14 | ||
15 | # NFS helper functions | |
16 | ||
17 | sub nfs_is_mounted { | |
18 | my ($server, $export, $mountpoint, $mountdata) = @_; | |
19 | ||
20 | $server = "[$server]" if Net::IP::ip_is_ipv6($server); | |
21 | my $source = "$server:$export"; | |
22 | ||
23 | $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata; | |
24 | return $mountpoint if grep { | |
25 | $_->[2] =~ /^nfs/ && | |
26 | $_->[0] =~ m|^\Q$source\E/?$| && | |
27 | $_->[1] eq $mountpoint | |
28 | } @$mountdata; | |
29 | return undef; | |
30 | } | |
31 | ||
32 | sub nfs_mount { | |
33 | my ($server, $export, $mountpoint, $options) = @_; | |
34 | ||
35 | $server = "[$server]" if Net::IP::ip_is_ipv6($server); | |
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 | ||
48 | sub type { | |
49 | return 'nfs'; | |
50 | } | |
51 | ||
52 | sub plugindata { | |
53 | return { | |
54 | content => [ { images => 1, rootdir => 1, vztmpl => 1, iso => 1, backup => 1}, | |
55 | { images => 1 }], | |
56 | format => [ { raw => 1, qcow2 => 1, vmdk => 1 } , 'raw' ], | |
57 | }; | |
58 | } | |
59 | ||
60 | sub 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 | ||
77 | sub options { | |
78 | return { | |
79 | path => { fixed => 1 }, | |
80 | server => { fixed => 1 }, | |
81 | export => { fixed => 1 }, | |
82 | nodes => { optional => 1 }, | |
83 | disable => { optional => 1 }, | |
84 | maxfiles => { optional => 1 }, | |
85 | options => { optional => 1 }, | |
86 | content => { optional => 1 }, | |
87 | format => { optional => 1 }, | |
88 | mkdir => { optional => 1 }, | |
89 | bwlimit => { optional => 1 }, | |
90 | }; | |
91 | } | |
92 | ||
93 | ||
94 | sub check_config { | |
95 | my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_; | |
96 | ||
97 | $config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path}; | |
98 | ||
99 | return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck); | |
100 | } | |
101 | ||
102 | # Storage implementation | |
103 | ||
104 | sub status { | |
105 | my ($class, $storeid, $scfg, $cache) = @_; | |
106 | ||
107 | $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts() | |
108 | if !$cache->{mountdata}; | |
109 | ||
110 | my $path = $scfg->{path}; | |
111 | my $server = $scfg->{server}; | |
112 | my $export = $scfg->{export}; | |
113 | ||
114 | return undef if !nfs_is_mounted($server, $export, $path, $cache->{mountdata}); | |
115 | ||
116 | return $class->SUPER::status($storeid, $scfg, $cache); | |
117 | } | |
118 | ||
119 | sub activate_storage { | |
120 | my ($class, $storeid, $scfg, $cache) = @_; | |
121 | ||
122 | $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts() | |
123 | if !$cache->{mountdata}; | |
124 | ||
125 | my $path = $scfg->{path}; | |
126 | my $server = $scfg->{server}; | |
127 | my $export = $scfg->{export}; | |
128 | ||
129 | if (!nfs_is_mounted($server, $export, $path, $cache->{mountdata})) { | |
130 | ||
131 | # NOTE: only call mkpath when not mounted (avoid hang | |
132 | # when NFS server is offline | |
133 | ||
134 | mkpath $path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir}); | |
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 | ||
145 | sub deactivate_storage { | |
146 | my ($class, $storeid, $scfg, $cache) = @_; | |
147 | ||
148 | $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts() | |
149 | if !$cache->{mountdata}; | |
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 | ||
161 | sub check_connection { | |
162 | my ($class, $storeid, $scfg) = @_; | |
163 | ||
164 | my $server = $scfg->{server}; | |
165 | ||
166 | my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server]; | |
167 | ||
168 | eval { | |
169 | run_command($cmd, timeout => 2, outfunc => sub {}, errfunc => sub {}); | |
170 | }; | |
171 | if (my $err = $@) { | |
172 | return 0; | |
173 | } | |
174 | ||
175 | return 1; | |
176 | } | |
177 | ||
178 | 1; |