]>
Commit | Line | Data |
---|---|---|
e34ce144 AA |
1 | package PVE::Storage::CephFSPlugin; |
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 | use PVE::Storage::CephTools; | |
13 | ||
14 | use base qw(PVE::Storage::Plugin); | |
15 | ||
16 | sub cephfs_is_mounted { | |
17 | my ($scfg, $storeid, $mountdata) = @_; | |
18 | ||
19 | my $cmd_option = PVE::Storage::CephTools::ceph_connect_option($scfg, $storeid); | |
20 | my $configfile = $cmd_option->{ceph_conf}; | |
21 | my $server = $cmd_option->{mon_host} // PVE::Storage::CephTools::get_monaddr_list($configfile); | |
22 | ||
23 | my $subdir = $scfg->{subdir} // '/'; | |
24 | my $mountpoint = $scfg->{path}; | |
25 | my $source = "$server:$subdir"; | |
26 | ||
27 | $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata; | |
28 | return $mountpoint if grep { | |
29 | $_->[2] =~ m#^ceph|fuse\.ceph-fuse# && | |
30 | $_->[0] =~ m#^\Q$source\E|ceph-fuse$# && | |
31 | $_->[1] eq $mountpoint | |
32 | } @$mountdata; | |
33 | ||
34 | warn "A filesystem is already mounted on $mountpoint\n" | |
35 | if grep { $_->[1] eq $mountpoint } @$mountdata; | |
36 | ||
37 | return undef; | |
38 | } | |
39 | ||
40 | sub cephfs_mount { | |
41 | my ($scfg, $storeid) = @_; | |
42 | ||
43 | my $cmd; | |
44 | my $mountpoint = $scfg->{path}; | |
45 | my $subdir = $scfg->{subdir} // '/'; | |
46 | ||
47 | my $cmd_option = PVE::Storage::CephTools::ceph_connect_option($scfg, $storeid); | |
48 | my $configfile = $cmd_option->{ceph_conf}; | |
49 | my $secretfile = $cmd_option->{keyring}; | |
50 | my $server = $cmd_option->{mon_host} // PVE::Storage::CephTools::get_monaddr_list($configfile); | |
51 | ||
52 | # fuse -> client-enforced quotas (kernel doesn't), updates w/ ceph-fuse pkg | |
53 | # kernel -> better performance, less frequent updates | |
54 | if ($scfg->{fuse}) { | |
55 | # FIXME: ceph-fuse client complains about missing ceph.conf or keyring if | |
56 | # not provided on its default locations but still connects. Fix upstream?? | |
57 | $cmd = ['/usr/bin/ceph-fuse', '-n', "client.$cmd_option->{userid}", '-m', $server]; | |
58 | push @$cmd, '--keyfile', $secretfile if defined($secretfile); | |
59 | push @$cmd, '-r', $subdir if !($subdir =~ m|^/$|); | |
60 | push @$cmd, $mountpoint; | |
61 | push @$cmd, '--conf', $configfile if defined($configfile); | |
62 | }else { | |
63 | my $source = "$server:$subdir"; | |
64 | $cmd = ['/bin/mount', '-t', 'ceph', $source, $mountpoint, '-o', "name=$cmd_option->{userid}"]; | |
65 | push @$cmd, '-o', "secretfile=$secretfile" if defined($secretfile); | |
66 | } | |
67 | ||
68 | if ($scfg->{options}) { | |
69 | push @$cmd, '-o', $scfg->{options}; | |
70 | } | |
71 | ||
72 | run_command($cmd, errmsg => "mount error"); | |
73 | } | |
74 | ||
75 | # Configuration | |
76 | ||
77 | sub type { | |
78 | return 'cephfs'; | |
79 | } | |
80 | ||
81 | sub plugindata { | |
82 | return { | |
83 | content => [ { vztmpl => 1, iso => 1, backup => 1}, | |
84 | { backup => 1 }], | |
85 | }; | |
86 | } | |
87 | ||
88 | sub properties { | |
89 | return { | |
90 | fuse => { | |
91 | description => "Mount CephFS through FUSE.", | |
92 | type => 'boolean', | |
93 | }, | |
94 | subdir => { | |
95 | description => "Subdir to mount.", | |
96 | type => 'string', format => 'pve-storage-path', | |
97 | }, | |
98 | }; | |
99 | } | |
100 | ||
101 | sub options { | |
102 | return { | |
103 | path => { fixed => 1 }, | |
104 | monhost => { optional => 1}, | |
105 | nodes => { optional => 1 }, | |
106 | subdir => { optional => 1 }, | |
107 | disable => { optional => 1 }, | |
108 | options => { optional => 1 }, | |
109 | username => { optional => 1 }, | |
110 | content => { optional => 1 }, | |
111 | format => { optional => 1 }, | |
112 | mkdir => { optional => 1 }, | |
113 | fuse => { optional => 1 }, | |
114 | bwlimit => { optional => 1 }, | |
115 | }; | |
116 | } | |
117 | ||
118 | sub check_config { | |
119 | my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_; | |
120 | ||
121 | $config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path}; | |
122 | ||
123 | return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck); | |
124 | } | |
125 | ||
126 | # Storage implementation | |
127 | ||
128 | sub on_add_hook { | |
129 | my ($class, $storeid, $scfg, %param) = @_; | |
130 | ||
131 | return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph | |
132 | ||
133 | PVE::Storage::CephTools::ceph_create_keyfile($scfg->{type}, $storeid); | |
134 | } | |
135 | ||
136 | sub on_delete_hook { | |
137 | my ($class, $storeid, $scfg) = @_; | |
138 | ||
139 | return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph | |
140 | ||
141 | PVE::Storage::CephTools::ceph_remove_keyfile($scfg->{type}, $storeid); | |
142 | ||
143 | } | |
144 | ||
145 | sub status { | |
146 | my ($class, $storeid, $scfg, $cache) = @_; | |
147 | ||
148 | $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts() | |
149 | if !$cache->{mountdata}; | |
150 | ||
151 | return undef if !cephfs_is_mounted($scfg, $storeid, $cache->{mountdata}); | |
152 | ||
153 | return $class->SUPER::status($storeid, $scfg, $cache); | |
154 | } | |
155 | ||
156 | sub activate_storage { | |
157 | my ($class, $storeid, $scfg, $cache) = @_; | |
158 | ||
159 | $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts() | |
160 | if !$cache->{mountdata}; | |
161 | ||
162 | my $path = $scfg->{path}; | |
163 | ||
164 | if (!cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) { | |
165 | ||
166 | # NOTE: only call mkpath when not mounted (avoid hang | |
167 | # when cephfs is offline | |
168 | ||
169 | mkpath $path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir}); | |
170 | ||
171 | die "unable to activate storage '$storeid' - " . | |
172 | "directory '$path' does not exist\n" if ! -d $path; | |
173 | ||
174 | cephfs_mount($scfg, $storeid); | |
175 | } | |
176 | ||
177 | $class->SUPER::activate_storage($storeid, $scfg, $cache); | |
178 | } | |
179 | ||
180 | sub deactivate_storage { | |
181 | my ($class, $storeid, $scfg, $cache) = @_; | |
182 | ||
183 | $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts() | |
184 | if !$cache->{mountdata}; | |
185 | ||
186 | my $path = $scfg->{path}; | |
187 | ||
188 | if (cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) { | |
189 | my $cmd = ['/bin/umount', $path]; | |
190 | run_command($cmd, errmsg => 'umount error'); | |
191 | } | |
192 | } | |
193 | ||
194 | 1; |