]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/CephFSPlugin.pm
api: fix get content call response type for RBD/ZFS/iSCSI volumes
[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 once it's possible to specify _netdev for fuse.ceph mounts
42 sub 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
47 # option. Over three corners this gets us an ordering cycle on shutdown, which
48 # may make shutdown hang if the random cycle breaking hits the "wrong" unit to
49 # delete.
50 my $unit = <<"EOF";
51 [Unit]
52 Description=${where}
53 DefaultDependencies=no
54 Requires=system.slice
55 Wants=network-online.target
56 Before=umount.target remote-fs.target
57 After=systemd-journald.socket system.slice network.target -.mount remote-fs-pre.target network-online.target
58 Conflicts=umount.target
59
60 [Mount]
61 Where=${where}
62 What=${what}
63 Type=${type}
64 Options=${opts}
65 EOF
66
67 my $unit_fn = PVE::Systemd::escape_unit($where, 1) . ".mount";
68 my $unit_path = "/run/systemd/system/$unit_fn";
69 my $daemon_needs_reload = -e $unit_path;
70
71 file_set_contents($unit_path, $unit);
72
73 run_command(['systemctl', 'daemon-reload'], errmsg => "daemon-reload error")
74 if $daemon_needs_reload;
75 run_command(['systemctl', 'start', $unit_fn], errmsg => "mount error");
76
77 }
78
79 sub cephfs_mount {
80 my ($scfg, $storeid) = @_;
81
82 my $mountpoint = $scfg->{path};
83 my $subdir = $scfg->{subdir} // '/';
84
85 my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
86 my $configfile = $cmd_option->{ceph_conf};
87 my $secretfile = $cmd_option->{keyring};
88 my $server = $cmd_option->{mon_host} // PVE::CephConfig::get_monaddr_list($configfile);
89 my $type = 'ceph';
90 my $fs_name = $scfg->{'fs-name'};
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 push @opts, "ceph.client_fs=$fs_name" if defined($fs_name);
99 } else {
100 push @opts, "name=$cmd_option->{userid}";
101 push @opts, "secretfile=$secretfile" if defined($secretfile);
102 push @opts, "conf=$configfile" if defined($configfile);
103 push @opts, "fs=$fs_name" if defined($fs_name);
104 }
105
106 push @opts, $scfg->{options} if $scfg->{options};
107
108 systemd_netmount($mountpoint, $type, "$server:$subdir", join(',', @opts));
109 }
110
111 # Configuration
112
113 sub type {
114 return 'cephfs';
115 }
116
117 sub plugindata {
118 return {
119 content => [ { vztmpl => 1, iso => 1, backup => 1, snippets => 1},
120 { backup => 1 }],
121 };
122 }
123
124 sub properties {
125 return {
126 fuse => {
127 description => "Mount CephFS through FUSE.",
128 type => 'boolean',
129 },
130 'fs-name' => {
131 description => "The Ceph filesystem name.",
132 type => 'string', format => 'pve-configid',
133 },
134 };
135 }
136
137 sub options {
138 return {
139 path => { fixed => 1 },
140 monhost => { optional => 1},
141 nodes => { optional => 1 },
142 subdir => { optional => 1 },
143 disable => { optional => 1 },
144 options => { optional => 1 },
145 username => { optional => 1 },
146 content => { optional => 1 },
147 format => { optional => 1 },
148 mkdir => { optional => 1 },
149 fuse => { optional => 1 },
150 bwlimit => { optional => 1 },
151 maxfiles => { optional => 1 },
152 keyring => { optional => 1 },
153 'prune-backups' => { optional => 1 },
154 'max-protected-backups' => { optional => 1 },
155 'fs-name' => { optional => 1 },
156 };
157 }
158
159 sub check_config {
160 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
161
162 $config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path};
163
164 return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck);
165 }
166
167 # Storage implementation
168
169 sub on_add_hook {
170 my ($class, $storeid, $scfg, %param) = @_;
171
172 my $secret = $param{keyring} if defined $param{keyring} // undef;
173 PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid, $secret);
174
175 return;
176 }
177
178 sub on_update_hook {
179 my ($class, $storeid, $scfg, %param) = @_;
180
181 if (exists($param{keyring})) {
182 if (defined($param{keyring})) {
183 PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid, $param{keyring});
184 } else {
185 PVE::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid);
186 }
187 }
188
189 return;
190 }
191
192 sub on_delete_hook {
193 my ($class, $storeid, $scfg) = @_;
194 PVE::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid);
195 return;
196 }
197
198 sub status {
199 my ($class, $storeid, $scfg, $cache) = @_;
200
201 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
202
203 return undef if !cephfs_is_mounted($scfg, $storeid, $cache->{mountdata});
204
205 return $class->SUPER::status($storeid, $scfg, $cache);
206 }
207
208 sub activate_storage {
209 my ($class, $storeid, $scfg, $cache) = @_;
210
211 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
212
213 # NOTE: mkpath may hang if storage is mounted but not reachable
214 if (!cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) {
215 my $path = $scfg->{path};
216
217 mkpath $path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir});
218
219 die "unable to activate storage '$storeid' - " .
220 "directory '$path' does not exist\n" if ! -d $path;
221
222 cephfs_mount($scfg, $storeid);
223 }
224
225 $class->SUPER::activate_storage($storeid, $scfg, $cache);
226 }
227
228 sub deactivate_storage {
229 my ($class, $storeid, $scfg, $cache) = @_;
230
231 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
232
233 my $path = $scfg->{path};
234
235 if (cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) {
236 run_command(['/bin/umount', $path], errmsg => 'umount error');
237 }
238 }
239
240 # FIXME remove on the next APIAGE reset.
241 # Deprecated, use get_volume_attribute instead.
242 sub get_volume_notes {
243 my $class = shift;
244 PVE::Storage::DirPlugin::get_volume_notes($class, @_);
245 }
246
247 # FIXME remove on the next APIAGE reset.
248 # Deprecated, use update_volume_attribute instead.
249 sub update_volume_notes {
250 my $class = shift;
251 PVE::Storage::DirPlugin::update_volume_notes($class, @_);
252 }
253
254 sub get_volume_attribute {
255 return PVE::Storage::DirPlugin::get_volume_attribute(@_);
256 }
257
258 sub update_volume_attribute {
259 return PVE::Storage::DirPlugin::update_volume_attribute(@_);
260 }
261
262 1;