]>
Commit | Line | Data |
---|---|---|
e34ce144 AA |
1 | package PVE::Storage::CephFSPlugin; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5402cea5 | 5 | |
e34ce144 AA |
6 | use IO::File; |
7 | use Net::IP; | |
8 | use File::Path; | |
5402cea5 | 9 | |
b0373adc TL |
10 | use PVE::CephConfig; |
11 | use PVE::JSONSchema qw(get_standard_option); | |
e34ce144 AA |
12 | use PVE::ProcFSTools; |
13 | use PVE::Storage::Plugin; | |
1022a7c4 | 14 | use PVE::Systemd; |
b0373adc | 15 | use PVE::Tools qw(run_command file_set_contents); |
e34ce144 AA |
16 | |
17 | use base qw(PVE::Storage::Plugin); | |
18 | ||
19 | sub cephfs_is_mounted { | |
20 | my ($scfg, $storeid, $mountdata) = @_; | |
21 | ||
4050fcc1 | 22 | my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid); |
e34ce144 | 23 | my $configfile = $cmd_option->{ceph_conf}; |
e34ce144 AA |
24 | |
25 | my $subdir = $scfg->{subdir} // '/'; | |
26 | my $mountpoint = $scfg->{path}; | |
e34ce144 AA |
27 | |
28 | $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata; | |
29 | return $mountpoint if grep { | |
30 | $_->[2] =~ m#^ceph|fuse\.ceph-fuse# && | |
82881c5f | 31 | $_->[0] =~ m#\Q:$subdir\E$|^ceph-fuse$# && |
e34ce144 AA |
32 | $_->[1] eq $mountpoint |
33 | } @$mountdata; | |
34 | ||
35 | warn "A filesystem is already mounted on $mountpoint\n" | |
36 | if grep { $_->[1] eq $mountpoint } @$mountdata; | |
37 | ||
38 | return undef; | |
39 | } | |
40 | ||
cda32b23 | 41 | # FIXME: remove once it's possible to specify _netdev for fuse.ceph mounts |
d9ece228 TL |
42 | sub systemd_netmount { |
43 | my ($where, $type, $what, $opts) = @_; | |
44 | ||
45 | # don't do default deps, systemd v241 generator produces ordering deps on both | |
46 | # local-fs(-pre) and remote-fs(-pre) targets if we use the required _netdev | |
ffc31266 | 47 | # option. Over three corners this gets us an ordering cycle on shutdown, which |
d9ece228 TL |
48 | # may make shutdown hang if the random cycle breaking hits the "wrong" unit to |
49 | # delete. | |
50 | my $unit = <<"EOF"; | |
51 | [Unit] | |
52 | Description=${where} | |
53 | DefaultDependencies=no | |
54 | Requires=system.slice | |
55 | Wants=network-online.target | |
56 | Before=umount.target remote-fs.target | |
57 | After=systemd-journald.socket system.slice network.target -.mount remote-fs-pre.target network-online.target | |
58 | Conflicts=umount.target | |
59 | ||
60 | [Mount] | |
61 | Where=${where} | |
62 | What=${what} | |
63 | Type=${type} | |
64 | Options=${opts} | |
65 | EOF | |
66 | ||
1022a7c4 | 67 | my $unit_fn = PVE::Systemd::escape_unit($where, 1) . ".mount"; |
d9ece228 | 68 | my $unit_path = "/run/systemd/system/$unit_fn"; |
9a80a3ea | 69 | my $daemon_needs_reload = -e $unit_path; |
d9ece228 TL |
70 | |
71 | file_set_contents($unit_path, $unit); | |
9a80a3ea TL |
72 | |
73 | run_command(['systemctl', 'daemon-reload'], errmsg => "daemon-reload error") | |
74 | if $daemon_needs_reload; | |
d9ece228 TL |
75 | run_command(['systemctl', 'start', $unit_fn], errmsg => "mount error"); |
76 | ||
77 | } | |
78 | ||
e34ce144 AA |
79 | sub cephfs_mount { |
80 | my ($scfg, $storeid) = @_; | |
81 | ||
e34ce144 AA |
82 | my $mountpoint = $scfg->{path}; |
83 | my $subdir = $scfg->{subdir} // '/'; | |
84 | ||
4050fcc1 | 85 | my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid); |
e34ce144 AA |
86 | my $configfile = $cmd_option->{ceph_conf}; |
87 | my $secretfile = $cmd_option->{keyring}; | |
4050fcc1 | 88 | my $server = $cmd_option->{mon_host} // PVE::CephConfig::get_monaddr_list($configfile); |
25e222ca | 89 | my $type = 'ceph'; |
bc7fecb0 | 90 | my $fs_name = $scfg->{'fs-name'}; |
e34ce144 | 91 | |
25e222ca | 92 | my @opts = (); |
e34ce144 | 93 | if ($scfg->{fuse}) { |
25e222ca TL |
94 | $type = 'fuse.ceph'; |
95 | push @opts, "ceph.id=$cmd_option->{userid}"; | |
96 | push @opts, "ceph.keyfile=$secretfile" if defined($secretfile); | |
97 | push @opts, "ceph.conf=$configfile" if defined($configfile); | |
bc7fecb0 | 98 | push @opts, "ceph.client_fs=$fs_name" if defined($fs_name); |
5402cea5 | 99 | } else { |
25e222ca | 100 | push @opts, "name=$cmd_option->{userid}"; |
d9ece228 | 101 | push @opts, "secretfile=$secretfile" if defined($secretfile); |
9531988d | 102 | push @opts, "conf=$configfile" if defined($configfile); |
bc7fecb0 | 103 | push @opts, "fs=$fs_name" if defined($fs_name); |
e34ce144 | 104 | } |
25e222ca TL |
105 | |
106 | push @opts, $scfg->{options} if $scfg->{options}; | |
107 | ||
108 | systemd_netmount($mountpoint, $type, "$server:$subdir", join(',', @opts)); | |
e34ce144 AA |
109 | } |
110 | ||
111 | # Configuration | |
112 | ||
113 | sub type { | |
114 | return 'cephfs'; | |
115 | } | |
116 | ||
117 | sub plugindata { | |
118 | return { | |
d1eb35ea | 119 | content => [ { vztmpl => 1, iso => 1, backup => 1, snippets => 1}, |
e34ce144 AA |
120 | { backup => 1 }], |
121 | }; | |
122 | } | |
123 | ||
124 | sub properties { | |
125 | return { | |
126 | fuse => { | |
127 | description => "Mount CephFS through FUSE.", | |
128 | type => 'boolean', | |
129 | }, | |
130 | subdir => { | |
131 | description => "Subdir to mount.", | |
132 | type => 'string', format => 'pve-storage-path', | |
133 | }, | |
bc7fecb0 DC |
134 | 'fs-name' => { |
135 | description => "The Ceph filesystem name.", | |
136 | type => 'string', format => 'pve-configid', | |
137 | }, | |
e34ce144 AA |
138 | }; |
139 | } | |
140 | ||
141 | sub options { | |
142 | return { | |
143 | path => { fixed => 1 }, | |
144 | monhost => { optional => 1}, | |
145 | nodes => { optional => 1 }, | |
146 | subdir => { optional => 1 }, | |
147 | disable => { optional => 1 }, | |
148 | options => { optional => 1 }, | |
149 | username => { optional => 1 }, | |
150 | content => { optional => 1 }, | |
151 | format => { optional => 1 }, | |
152 | mkdir => { optional => 1 }, | |
153 | fuse => { optional => 1 }, | |
154 | bwlimit => { optional => 1 }, | |
d35a0b4b | 155 | maxfiles => { optional => 1 }, |
22b68016 | 156 | keyring => { optional => 1 }, |
3353698f | 157 | 'prune-backups' => { optional => 1 }, |
8009417d | 158 | 'max-protected-backups' => { optional => 1 }, |
bc7fecb0 | 159 | 'fs-name' => { optional => 1 }, |
e34ce144 AA |
160 | }; |
161 | } | |
162 | ||
163 | sub check_config { | |
164 | my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_; | |
165 | ||
166 | $config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path}; | |
167 | ||
168 | return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck); | |
169 | } | |
170 | ||
171 | # Storage implementation | |
172 | ||
173 | sub on_add_hook { | |
174 | my ($class, $storeid, $scfg, %param) = @_; | |
175 | ||
22b68016 AL |
176 | my $secret = $param{keyring} if defined $param{keyring} // undef; |
177 | PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid, $secret); | |
e34ce144 | 178 | |
22b68016 AL |
179 | return; |
180 | } | |
181 | ||
182 | sub on_update_hook { | |
183 | my ($class, $storeid, $scfg, %param) = @_; | |
184 | ||
185 | if (exists($param{keyring})) { | |
186 | if (defined($param{keyring})) { | |
187 | PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid, $param{keyring}); | |
188 | } else { | |
189 | PVE::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid); | |
190 | } | |
191 | } | |
f3ccd0ef FE |
192 | |
193 | return; | |
e34ce144 AA |
194 | } |
195 | ||
196 | sub on_delete_hook { | |
197 | my ($class, $storeid, $scfg) = @_; | |
4050fcc1 | 198 | PVE::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid); |
f3ccd0ef | 199 | return; |
e34ce144 AA |
200 | } |
201 | ||
202 | sub status { | |
203 | my ($class, $storeid, $scfg, $cache) = @_; | |
204 | ||
5402cea5 | 205 | $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts(); |
e34ce144 AA |
206 | |
207 | return undef if !cephfs_is_mounted($scfg, $storeid, $cache->{mountdata}); | |
208 | ||
209 | return $class->SUPER::status($storeid, $scfg, $cache); | |
210 | } | |
211 | ||
212 | sub activate_storage { | |
213 | my ($class, $storeid, $scfg, $cache) = @_; | |
214 | ||
5402cea5 | 215 | $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts(); |
e34ce144 | 216 | |
5402cea5 | 217 | # NOTE: mkpath may hang if storage is mounted but not reachable |
e34ce144 | 218 | if (!cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) { |
5402cea5 | 219 | my $path = $scfg->{path}; |
e34ce144 AA |
220 | |
221 | mkpath $path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir}); | |
222 | ||
223 | die "unable to activate storage '$storeid' - " . | |
224 | "directory '$path' does not exist\n" if ! -d $path; | |
225 | ||
226 | cephfs_mount($scfg, $storeid); | |
227 | } | |
228 | ||
229 | $class->SUPER::activate_storage($storeid, $scfg, $cache); | |
230 | } | |
231 | ||
232 | sub deactivate_storage { | |
233 | my ($class, $storeid, $scfg, $cache) = @_; | |
234 | ||
5402cea5 | 235 | $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts(); |
e34ce144 AA |
236 | |
237 | my $path = $scfg->{path}; | |
238 | ||
239 | if (cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) { | |
5402cea5 | 240 | run_command(['/bin/umount', $path], errmsg => 'umount error'); |
e34ce144 AA |
241 | } |
242 | } | |
243 | ||
f1de8281 FE |
244 | # FIXME remove on the next APIAGE reset. |
245 | # Deprecated, use get_volume_attribute instead. | |
2bce96c5 DW |
246 | sub get_volume_notes { |
247 | my $class = shift; | |
248 | PVE::Storage::DirPlugin::get_volume_notes($class, @_); | |
249 | } | |
250 | ||
f1de8281 FE |
251 | # FIXME remove on the next APIAGE reset. |
252 | # Deprecated, use update_volume_attribute instead. | |
2bce96c5 DW |
253 | sub update_volume_notes { |
254 | my $class = shift; | |
255 | PVE::Storage::DirPlugin::update_volume_notes($class, @_); | |
256 | } | |
257 | ||
f1de8281 FE |
258 | sub get_volume_attribute { |
259 | return PVE::Storage::DirPlugin::get_volume_attribute(@_); | |
260 | } | |
261 | ||
262 | sub update_volume_attribute { | |
263 | return PVE::Storage::DirPlugin::update_volume_attribute(@_); | |
264 | } | |
265 | ||
e34ce144 | 266 | 1; |