]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/CephFSPlugin.pm
Fix #2705: cephfs: mount fails with bad option
[pve-storage.git] / PVE / Storage / CephFSPlugin.pm
CommitLineData
e34ce144
AA
1package PVE::Storage::CephFSPlugin;
2
3use strict;
4use warnings;
5402cea5 5
e34ce144
AA
6use IO::File;
7use Net::IP;
8use File::Path;
5402cea5 9
b0373adc
TL
10use PVE::CephConfig;
11use PVE::JSONSchema qw(get_standard_option);
e34ce144
AA
12use PVE::ProcFSTools;
13use PVE::Storage::Plugin;
1022a7c4 14use PVE::Systemd;
b0373adc 15use PVE::Tools qw(run_command file_set_contents);
e34ce144
AA
16
17use base qw(PVE::Storage::Plugin);
18
19sub 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
d9ece228
TL
41# FIXME: remove in PVE 7.0 where systemd is recent enough to not have those
42# local-fs/remote-fs dependency cycles generated for _netdev mounts...
43sub systemd_netmount {
44 my ($where, $type, $what, $opts) = @_;
45
46# don't do default deps, systemd v241 generator produces ordering deps on both
47# local-fs(-pre) and remote-fs(-pre) targets if we use the required _netdev
48# option. Over thre corners this gets us an ordering cycle on shutdown, which
49# may make shutdown hang if the random cycle breaking hits the "wrong" unit to
50# delete.
51 my $unit = <<"EOF";
52[Unit]
53Description=${where}
54DefaultDependencies=no
55Requires=system.slice
56Wants=network-online.target
57Before=umount.target remote-fs.target
58After=systemd-journald.socket system.slice network.target -.mount remote-fs-pre.target network-online.target
59Conflicts=umount.target
60
61[Mount]
62Where=${where}
63What=${what}
64Type=${type}
65Options=${opts}
66EOF
67
1022a7c4 68 my $unit_fn = PVE::Systemd::escape_unit($where, 1) . ".mount";
d9ece228 69 my $unit_path = "/run/systemd/system/$unit_fn";
9a80a3ea 70 my $daemon_needs_reload = -e $unit_path;
d9ece228
TL
71
72 file_set_contents($unit_path, $unit);
9a80a3ea
TL
73
74 run_command(['systemctl', 'daemon-reload'], errmsg => "daemon-reload error")
75 if $daemon_needs_reload;
d9ece228
TL
76 run_command(['systemctl', 'start', $unit_fn], errmsg => "mount error");
77
78}
79
e34ce144
AA
80sub cephfs_mount {
81 my ($scfg, $storeid) = @_;
82
e54c3e33 83 my ($subversions) = PVE::CephConfig::ceph_version();
e34ce144
AA
84 my $mountpoint = $scfg->{path};
85 my $subdir = $scfg->{subdir} // '/';
86
4050fcc1 87 my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
e34ce144
AA
88 my $configfile = $cmd_option->{ceph_conf};
89 my $secretfile = $cmd_option->{keyring};
4050fcc1 90 my $server = $cmd_option->{mon_host} // PVE::CephConfig::get_monaddr_list($configfile);
25e222ca 91 my $type = 'ceph';
e34ce144 92
25e222ca 93 my @opts = ();
e34ce144 94 if ($scfg->{fuse}) {
25e222ca
TL
95 $type = 'fuse.ceph';
96 push @opts, "ceph.id=$cmd_option->{userid}";
97 push @opts, "ceph.keyfile=$secretfile" if defined($secretfile);
98 push @opts, "ceph.conf=$configfile" if defined($configfile);
5402cea5 99 } else {
25e222ca 100 push @opts, "name=$cmd_option->{userid}";
d9ece228 101 push @opts, "secretfile=$secretfile" if defined($secretfile);
e54c3e33
AA
102
103 # FIXME: remove subversion check in PVE 7.0, not needed for >= Nautilus
104 # Luminous doesn't know the conf option
105 push @opts, "conf=$configfile" if defined($configfile) && @$subversions[0] > 12;
e34ce144 106 }
25e222ca
TL
107
108 push @opts, $scfg->{options} if $scfg->{options};
109
110 systemd_netmount($mountpoint, $type, "$server:$subdir", join(',', @opts));
e34ce144
AA
111}
112
113# Configuration
114
115sub type {
116 return 'cephfs';
117}
118
119sub plugindata {
120 return {
d1eb35ea 121 content => [ { vztmpl => 1, iso => 1, backup => 1, snippets => 1},
e34ce144
AA
122 { backup => 1 }],
123 };
124}
125
126sub properties {
127 return {
128 fuse => {
129 description => "Mount CephFS through FUSE.",
130 type => 'boolean',
131 },
132 subdir => {
133 description => "Subdir to mount.",
134 type => 'string', format => 'pve-storage-path',
135 },
136 };
137}
138
139sub options {
140 return {
141 path => { fixed => 1 },
142 monhost => { optional => 1},
143 nodes => { optional => 1 },
144 subdir => { optional => 1 },
145 disable => { optional => 1 },
146 options => { optional => 1 },
147 username => { optional => 1 },
148 content => { optional => 1 },
149 format => { optional => 1 },
150 mkdir => { optional => 1 },
151 fuse => { optional => 1 },
152 bwlimit => { optional => 1 },
d35a0b4b 153 maxfiles => { optional => 1 },
e34ce144
AA
154 };
155}
156
157sub check_config {
158 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
159
160 $config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path};
161
162 return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck);
163}
164
165# Storage implementation
166
167sub on_add_hook {
168 my ($class, $storeid, $scfg, %param) = @_;
169
170 return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph
171
4050fcc1 172 PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid);
e34ce144
AA
173}
174
175sub on_delete_hook {
176 my ($class, $storeid, $scfg) = @_;
177
178 return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph
179
4050fcc1 180 PVE::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid);
e34ce144
AA
181}
182
183sub status {
184 my ($class, $storeid, $scfg, $cache) = @_;
185
5402cea5 186 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
e34ce144
AA
187
188 return undef if !cephfs_is_mounted($scfg, $storeid, $cache->{mountdata});
189
190 return $class->SUPER::status($storeid, $scfg, $cache);
191}
192
193sub activate_storage {
194 my ($class, $storeid, $scfg, $cache) = @_;
195
5402cea5 196 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
e34ce144 197
5402cea5 198 # NOTE: mkpath may hang if storage is mounted but not reachable
e34ce144 199 if (!cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) {
5402cea5 200 my $path = $scfg->{path};
e34ce144
AA
201
202 mkpath $path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir});
203
204 die "unable to activate storage '$storeid' - " .
205 "directory '$path' does not exist\n" if ! -d $path;
206
207 cephfs_mount($scfg, $storeid);
208 }
209
210 $class->SUPER::activate_storage($storeid, $scfg, $cache);
211}
212
213sub deactivate_storage {
214 my ($class, $storeid, $scfg, $cache) = @_;
215
5402cea5 216 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
e34ce144
AA
217
218 my $path = $scfg->{path};
219
220 if (cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) {
5402cea5 221 run_command(['/bin/umount', $path], errmsg => 'umount error');
e34ce144
AA
222 }
223}
224
2251;