]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/CephFSPlugin.pm
fix prune-backups validation (again)
[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
cda32b23 41# FIXME: remove once it's possible to specify _netdev for fuse.ceph mounts
d9ece228
TL
42sub systemd_netmount {
43 my ($where, $type, $what, $opts) = @_;
44
45# don't do default deps, systemd v241 generator produces ordering deps on both
46# local-fs(-pre) and remote-fs(-pre) targets if we use the required _netdev
ffc31266 47# option. Over three corners this gets us an ordering cycle on shutdown, which
d9ece228
TL
48# may make shutdown hang if the random cycle breaking hits the "wrong" unit to
49# delete.
50 my $unit = <<"EOF";
51[Unit]
52Description=${where}
53DefaultDependencies=no
54Requires=system.slice
55Wants=network-online.target
56Before=umount.target remote-fs.target
57After=systemd-journald.socket system.slice network.target -.mount remote-fs-pre.target network-online.target
58Conflicts=umount.target
59
60[Mount]
61Where=${where}
62What=${what}
63Type=${type}
64Options=${opts}
65EOF
66
1022a7c4 67 my $unit_fn = PVE::Systemd::escape_unit($where, 1) . ".mount";
d9ece228 68 my $unit_path = "/run/systemd/system/$unit_fn";
9a80a3ea 69 my $daemon_needs_reload = -e $unit_path;
d9ece228
TL
70
71 file_set_contents($unit_path, $unit);
9a80a3ea
TL
72
73 run_command(['systemctl', 'daemon-reload'], errmsg => "daemon-reload error")
74 if $daemon_needs_reload;
d9ece228
TL
75 run_command(['systemctl', 'start', $unit_fn], errmsg => "mount error");
76
77}
78
e34ce144
AA
79sub cephfs_mount {
80 my ($scfg, $storeid) = @_;
81
e34ce144
AA
82 my $mountpoint = $scfg->{path};
83 my $subdir = $scfg->{subdir} // '/';
84
4050fcc1 85 my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
e34ce144
AA
86 my $configfile = $cmd_option->{ceph_conf};
87 my $secretfile = $cmd_option->{keyring};
4050fcc1 88 my $server = $cmd_option->{mon_host} // PVE::CephConfig::get_monaddr_list($configfile);
25e222ca 89 my $type = 'ceph';
e34ce144 90
25e222ca 91 my @opts = ();
e34ce144 92 if ($scfg->{fuse}) {
25e222ca
TL
93 $type = 'fuse.ceph';
94 push @opts, "ceph.id=$cmd_option->{userid}";
95 push @opts, "ceph.keyfile=$secretfile" if defined($secretfile);
96 push @opts, "ceph.conf=$configfile" if defined($configfile);
5402cea5 97 } else {
25e222ca 98 push @opts, "name=$cmd_option->{userid}";
d9ece228 99 push @opts, "secretfile=$secretfile" if defined($secretfile);
9531988d 100 push @opts, "conf=$configfile" if defined($configfile);
e34ce144 101 }
25e222ca
TL
102
103 push @opts, $scfg->{options} if $scfg->{options};
104
105 systemd_netmount($mountpoint, $type, "$server:$subdir", join(',', @opts));
e34ce144
AA
106}
107
108# Configuration
109
110sub type {
111 return 'cephfs';
112}
113
114sub plugindata {
115 return {
d1eb35ea 116 content => [ { vztmpl => 1, iso => 1, backup => 1, snippets => 1},
e34ce144
AA
117 { backup => 1 }],
118 };
119}
120
121sub properties {
122 return {
123 fuse => {
124 description => "Mount CephFS through FUSE.",
125 type => 'boolean',
126 },
127 subdir => {
128 description => "Subdir to mount.",
129 type => 'string', format => 'pve-storage-path',
130 },
131 };
132}
133
134sub options {
135 return {
136 path => { fixed => 1 },
137 monhost => { optional => 1},
138 nodes => { optional => 1 },
139 subdir => { optional => 1 },
140 disable => { optional => 1 },
141 options => { optional => 1 },
142 username => { optional => 1 },
143 content => { optional => 1 },
144 format => { optional => 1 },
145 mkdir => { optional => 1 },
146 fuse => { optional => 1 },
147 bwlimit => { optional => 1 },
d35a0b4b 148 maxfiles => { optional => 1 },
22b68016 149 keyring => { optional => 1 },
3353698f 150 'prune-backups' => { optional => 1 },
e34ce144
AA
151 };
152}
153
154sub 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
164sub on_add_hook {
165 my ($class, $storeid, $scfg, %param) = @_;
166
22b68016
AL
167 my $secret = $param{keyring} if defined $param{keyring} // undef;
168 PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid, $secret);
e34ce144 169
22b68016
AL
170 return;
171}
172
173sub on_update_hook {
174 my ($class, $storeid, $scfg, %param) = @_;
175
176 if (exists($param{keyring})) {
177 if (defined($param{keyring})) {
178 PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid, $param{keyring});
179 } else {
180 PVE::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid);
181 }
182 }
f3ccd0ef
FE
183
184 return;
e34ce144
AA
185}
186
187sub on_delete_hook {
188 my ($class, $storeid, $scfg) = @_;
4050fcc1 189 PVE::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid);
f3ccd0ef 190 return;
e34ce144
AA
191}
192
193sub status {
194 my ($class, $storeid, $scfg, $cache) = @_;
195
5402cea5 196 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
e34ce144
AA
197
198 return undef if !cephfs_is_mounted($scfg, $storeid, $cache->{mountdata});
199
200 return $class->SUPER::status($storeid, $scfg, $cache);
201}
202
203sub activate_storage {
204 my ($class, $storeid, $scfg, $cache) = @_;
205
5402cea5 206 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
e34ce144 207
5402cea5 208 # NOTE: mkpath may hang if storage is mounted but not reachable
e34ce144 209 if (!cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) {
5402cea5 210 my $path = $scfg->{path};
e34ce144
AA
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
223sub deactivate_storage {
224 my ($class, $storeid, $scfg, $cache) = @_;
225
5402cea5 226 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
e34ce144
AA
227
228 my $path = $scfg->{path};
229
230 if (cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) {
5402cea5 231 run_command(['/bin/umount', $path], errmsg => 'umount error');
e34ce144
AA
232 }
233}
234
2bce96c5
DW
235sub get_volume_notes {
236 my $class = shift;
237 PVE::Storage::DirPlugin::get_volume_notes($class, @_);
238}
239
240sub update_volume_notes {
241 my $class = shift;
242 PVE::Storage::DirPlugin::update_volume_notes($class, @_);
243}
244
e34ce144 2451;