]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/CephFSPlugin.pm
cephfs mount: reload systemd if existing unit gets regenerated
[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
d9ece228 10use PVE::Tools qw(run_command file_set_contents);
e34ce144
AA
11use PVE::ProcFSTools;
12use PVE::Storage::Plugin;
13use PVE::JSONSchema qw(get_standard_option);
4050fcc1 14use PVE::CephConfig;
e34ce144
AA
15
16use base qw(PVE::Storage::Plugin);
17
18sub cephfs_is_mounted {
19 my ($scfg, $storeid, $mountdata) = @_;
20
4050fcc1 21 my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
e34ce144 22 my $configfile = $cmd_option->{ceph_conf};
e34ce144
AA
23
24 my $subdir = $scfg->{subdir} // '/';
25 my $mountpoint = $scfg->{path};
e34ce144
AA
26
27 $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata;
28 return $mountpoint if grep {
29 $_->[2] =~ m#^ceph|fuse\.ceph-fuse# &&
82881c5f 30 $_->[0] =~ m#\Q:$subdir\E$|^ceph-fuse$# &&
e34ce144
AA
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
d9ece228 40
edaaf47a
TL
41# FIXME: duplicate of api/diskmanage one, move to common helper (pve-common's
42# Tools or Systemd ?)
43sub 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
d9ece228
TL
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...
61sub 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]
71Description=${where}
72DefaultDependencies=no
73Requires=system.slice
74Wants=network-online.target
75Before=umount.target remote-fs.target
76After=systemd-journald.socket system.slice network.target -.mount remote-fs-pre.target network-online.target
77Conflicts=umount.target
78
79[Mount]
80Where=${where}
81What=${what}
82Type=${type}
83Options=${opts}
84EOF
85
86 my $unit_fn = systemd_escape($where, 1) . ".mount";
87 my $unit_path = "/run/systemd/system/$unit_fn";
9a80a3ea 88 my $daemon_needs_reload = -e $unit_path;
d9ece228
TL
89
90 file_set_contents($unit_path, $unit);
9a80a3ea
TL
91
92 run_command(['systemctl', 'daemon-reload'], errmsg => "daemon-reload error")
93 if $daemon_needs_reload;
d9ece228
TL
94 run_command(['systemctl', 'start', $unit_fn], errmsg => "mount error");
95
96}
97
e34ce144
AA
98sub cephfs_mount {
99 my ($scfg, $storeid) = @_;
100
e34ce144
AA
101 my $mountpoint = $scfg->{path};
102 my $subdir = $scfg->{subdir} // '/';
103
4050fcc1 104 my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
e34ce144
AA
105 my $configfile = $cmd_option->{ceph_conf};
106 my $secretfile = $cmd_option->{keyring};
4050fcc1 107 my $server = $cmd_option->{mon_host} // PVE::CephConfig::get_monaddr_list($configfile);
25e222ca 108 my $type = 'ceph';
e34ce144 109
25e222ca 110 my @opts = ();
e34ce144 111 if ($scfg->{fuse}) {
25e222ca
TL
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);
5402cea5 116 } else {
25e222ca 117 push @opts, "name=$cmd_option->{userid}";
d9ece228 118 push @opts, "secretfile=$secretfile" if defined($secretfile);
25e222ca 119 push @opts, "conf=$configfile" if defined($configfile);
e34ce144 120 }
25e222ca
TL
121
122 push @opts, $scfg->{options} if $scfg->{options};
123
124 systemd_netmount($mountpoint, $type, "$server:$subdir", join(',', @opts));
e34ce144
AA
125}
126
127# Configuration
128
129sub type {
130 return 'cephfs';
131}
132
133sub plugindata {
134 return {
d1eb35ea 135 content => [ { vztmpl => 1, iso => 1, backup => 1, snippets => 1},
e34ce144
AA
136 { backup => 1 }],
137 };
138}
139
140sub 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
153sub 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 },
d35a0b4b 167 maxfiles => { optional => 1 },
e34ce144
AA
168 };
169}
170
171sub 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
181sub 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
4050fcc1 186 PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid);
e34ce144
AA
187}
188
189sub on_delete_hook {
190 my ($class, $storeid, $scfg) = @_;
191
192 return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph
193
4050fcc1 194 PVE::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid);
e34ce144
AA
195}
196
197sub status {
198 my ($class, $storeid, $scfg, $cache) = @_;
199
5402cea5 200 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
e34ce144
AA
201
202 return undef if !cephfs_is_mounted($scfg, $storeid, $cache->{mountdata});
203
204 return $class->SUPER::status($storeid, $scfg, $cache);
205}
206
207sub activate_storage {
208 my ($class, $storeid, $scfg, $cache) = @_;
209
5402cea5 210 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
e34ce144 211
5402cea5 212 # NOTE: mkpath may hang if storage is mounted but not reachable
e34ce144 213 if (!cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) {
5402cea5 214 my $path = $scfg->{path};
e34ce144
AA
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
227sub deactivate_storage {
228 my ($class, $storeid, $scfg, $cache) = @_;
229
5402cea5 230 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
e34ce144
AA
231
232 my $path = $scfg->{path};
233
234 if (cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) {
5402cea5 235 run_command(['/bin/umount', $path], errmsg => 'umount error');
e34ce144
AA
236 }
237}
238
2391;