]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/CephFSPlugin.pm
cephfs: revert safe-guard check for Luminous
[pve-storage.git] / PVE / Storage / CephFSPlugin.pm
1 package PVE::Storage::CephFSPlugin;
2
3 use strict;
4 use warnings;
5
6 use IO::File;
7 use Net::IP;
8 use File::Path;
9
10 use PVE::CephConfig;
11 use PVE::JSONSchema qw(get_standard_option);
12 use PVE::ProcFSTools;
13 use PVE::Storage::Plugin;
14 use PVE::Systemd;
15 use PVE::Tools qw(run_command file_set_contents);
16
17 use base qw(PVE::Storage::Plugin);
18
19 sub cephfs_is_mounted {
20 my ($scfg, $storeid, $mountdata) = @_;
21
22 my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
23 my $configfile = $cmd_option->{ceph_conf};
24
25 my $subdir = $scfg->{subdir} // '/';
26 my $mountpoint = $scfg->{path};
27
28 $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata;
29 return $mountpoint if grep {
30 $_->[2] =~ m#^ceph|fuse\.ceph-fuse# &&
31 $_->[0] =~ m#\Q:$subdir\E$|^ceph-fuse$# &&
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
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...
43 sub 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]
53 Description=${where}
54 DefaultDependencies=no
55 Requires=system.slice
56 Wants=network-online.target
57 Before=umount.target remote-fs.target
58 After=systemd-journald.socket system.slice network.target -.mount remote-fs-pre.target network-online.target
59 Conflicts=umount.target
60
61 [Mount]
62 Where=${where}
63 What=${what}
64 Type=${type}
65 Options=${opts}
66 EOF
67
68 my $unit_fn = PVE::Systemd::escape_unit($where, 1) . ".mount";
69 my $unit_path = "/run/systemd/system/$unit_fn";
70 my $daemon_needs_reload = -e $unit_path;
71
72 file_set_contents($unit_path, $unit);
73
74 run_command(['systemctl', 'daemon-reload'], errmsg => "daemon-reload error")
75 if $daemon_needs_reload;
76 run_command(['systemctl', 'start', $unit_fn], errmsg => "mount error");
77
78 }
79
80 sub cephfs_mount {
81 my ($scfg, $storeid) = @_;
82
83 my $mountpoint = $scfg->{path};
84 my $subdir = $scfg->{subdir} // '/';
85
86 my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
87 my $configfile = $cmd_option->{ceph_conf};
88 my $secretfile = $cmd_option->{keyring};
89 my $server = $cmd_option->{mon_host} // PVE::CephConfig::get_monaddr_list($configfile);
90 my $type = 'ceph';
91
92 my @opts = ();
93 if ($scfg->{fuse}) {
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);
98 } else {
99 push @opts, "name=$cmd_option->{userid}";
100 push @opts, "secretfile=$secretfile" if defined($secretfile);
101 push @opts, "conf=$configfile" if defined($configfile);
102 }
103
104 push @opts, $scfg->{options} if $scfg->{options};
105
106 systemd_netmount($mountpoint, $type, "$server:$subdir", join(',', @opts));
107 }
108
109 # Configuration
110
111 sub type {
112 return 'cephfs';
113 }
114
115 sub plugindata {
116 return {
117 content => [ { vztmpl => 1, iso => 1, backup => 1, snippets => 1},
118 { backup => 1 }],
119 };
120 }
121
122 sub properties {
123 return {
124 fuse => {
125 description => "Mount CephFS through FUSE.",
126 type => 'boolean',
127 },
128 subdir => {
129 description => "Subdir to mount.",
130 type => 'string', format => 'pve-storage-path',
131 },
132 };
133 }
134
135 sub options {
136 return {
137 path => { fixed => 1 },
138 monhost => { optional => 1},
139 nodes => { optional => 1 },
140 subdir => { optional => 1 },
141 disable => { optional => 1 },
142 options => { optional => 1 },
143 username => { optional => 1 },
144 content => { optional => 1 },
145 format => { optional => 1 },
146 mkdir => { optional => 1 },
147 fuse => { optional => 1 },
148 bwlimit => { optional => 1 },
149 maxfiles => { optional => 1 },
150 'prune-backups' => { optional => 1 },
151 };
152 }
153
154 sub check_config {
155 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
156
157 $config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path};
158
159 return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck);
160 }
161
162 # Storage implementation
163
164 sub on_add_hook {
165 my ($class, $storeid, $scfg, %param) = @_;
166
167 return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph
168
169 PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid);
170
171 return;
172 }
173
174 sub on_delete_hook {
175 my ($class, $storeid, $scfg) = @_;
176
177 return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph
178
179 PVE::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid);
180
181 return;
182 }
183
184 sub status {
185 my ($class, $storeid, $scfg, $cache) = @_;
186
187 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
188
189 return undef if !cephfs_is_mounted($scfg, $storeid, $cache->{mountdata});
190
191 return $class->SUPER::status($storeid, $scfg, $cache);
192 }
193
194 sub activate_storage {
195 my ($class, $storeid, $scfg, $cache) = @_;
196
197 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
198
199 # NOTE: mkpath may hang if storage is mounted but not reachable
200 if (!cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) {
201 my $path = $scfg->{path};
202
203 mkpath $path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir});
204
205 die "unable to activate storage '$storeid' - " .
206 "directory '$path' does not exist\n" if ! -d $path;
207
208 cephfs_mount($scfg, $storeid);
209 }
210
211 $class->SUPER::activate_storage($storeid, $scfg, $cache);
212 }
213
214 sub deactivate_storage {
215 my ($class, $storeid, $scfg, $cache) = @_;
216
217 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
218
219 my $path = $scfg->{path};
220
221 if (cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) {
222 run_command(['/bin/umount', $path], errmsg => 'umount error');
223 }
224 }
225
226 sub get_volume_notes {
227 my $class = shift;
228 PVE::Storage::DirPlugin::get_volume_notes($class, @_);
229 }
230
231 sub update_volume_notes {
232 my $class = shift;
233 PVE::Storage::DirPlugin::update_volume_notes($class, @_);
234 }
235
236 1;