]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/CephFSPlugin.pm
cephfs mount: reload systemd if existing unit gets regenerated
[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 my $daemon_needs_reload = -e $unit_path;
89
90 file_set_contents($unit_path, $unit);
91
92 run_command(['systemctl', 'daemon-reload'], errmsg => "daemon-reload error")
93 if $daemon_needs_reload;
94 run_command(['systemctl', 'start', $unit_fn], errmsg => "mount error");
95
96 }
97
98 sub cephfs_mount {
99 my ($scfg, $storeid) = @_;
100
101 my $mountpoint = $scfg->{path};
102 my $subdir = $scfg->{subdir} // '/';
103
104 my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
105 my $configfile = $cmd_option->{ceph_conf};
106 my $secretfile = $cmd_option->{keyring};
107 my $server = $cmd_option->{mon_host} // PVE::CephConfig::get_monaddr_list($configfile);
108 my $type = 'ceph';
109
110 my @opts = ();
111 if ($scfg->{fuse}) {
112 $type = 'fuse.ceph';
113 push @opts, "ceph.id=$cmd_option->{userid}";
114 push @opts, "ceph.keyfile=$secretfile" if defined($secretfile);
115 push @opts, "ceph.conf=$configfile" if defined($configfile);
116 } else {
117 push @opts, "name=$cmd_option->{userid}";
118 push @opts, "secretfile=$secretfile" if defined($secretfile);
119 push @opts, "conf=$configfile" if defined($configfile);
120 }
121
122 push @opts, $scfg->{options} if $scfg->{options};
123
124 systemd_netmount($mountpoint, $type, "$server:$subdir", join(',', @opts));
125 }
126
127 # Configuration
128
129 sub type {
130 return 'cephfs';
131 }
132
133 sub plugindata {
134 return {
135 content => [ { vztmpl => 1, iso => 1, backup => 1, snippets => 1},
136 { backup => 1 }],
137 };
138 }
139
140 sub properties {
141 return {
142 fuse => {
143 description => "Mount CephFS through FUSE.",
144 type => 'boolean',
145 },
146 subdir => {
147 description => "Subdir to mount.",
148 type => 'string', format => 'pve-storage-path',
149 },
150 };
151 }
152
153 sub options {
154 return {
155 path => { fixed => 1 },
156 monhost => { optional => 1},
157 nodes => { optional => 1 },
158 subdir => { optional => 1 },
159 disable => { optional => 1 },
160 options => { optional => 1 },
161 username => { optional => 1 },
162 content => { optional => 1 },
163 format => { optional => 1 },
164 mkdir => { optional => 1 },
165 fuse => { optional => 1 },
166 bwlimit => { optional => 1 },
167 maxfiles => { optional => 1 },
168 };
169 }
170
171 sub check_config {
172 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
173
174 $config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path};
175
176 return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck);
177 }
178
179 # Storage implementation
180
181 sub on_add_hook {
182 my ($class, $storeid, $scfg, %param) = @_;
183
184 return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph
185
186 PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid);
187 }
188
189 sub on_delete_hook {
190 my ($class, $storeid, $scfg) = @_;
191
192 return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph
193
194 PVE::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid);
195 }
196
197 sub status {
198 my ($class, $storeid, $scfg, $cache) = @_;
199
200 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
201
202 return undef if !cephfs_is_mounted($scfg, $storeid, $cache->{mountdata});
203
204 return $class->SUPER::status($storeid, $scfg, $cache);
205 }
206
207 sub activate_storage {
208 my ($class, $storeid, $scfg, $cache) = @_;
209
210 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
211
212 # NOTE: mkpath may hang if storage is mounted but not reachable
213 if (!cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) {
214 my $path = $scfg->{path};
215
216 mkpath $path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir});
217
218 die "unable to activate storage '$storeid' - " .
219 "directory '$path' does not exist\n" if ! -d $path;
220
221 cephfs_mount($scfg, $storeid);
222 }
223
224 $class->SUPER::activate_storage($storeid, $scfg, $cache);
225 }
226
227 sub deactivate_storage {
228 my ($class, $storeid, $scfg, $cache) = @_;
229
230 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
231
232 my $path = $scfg->{path};
233
234 if (cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) {
235 run_command(['/bin/umount', $path], errmsg => 'umount error');
236 }
237 }
238
239 1;