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