]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/CephFSPlugin.pm
dcf961c073b6042248ed4d93b756f1ae87007f31
[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);
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 # FIXME: duplicate of api/diskmanage one, move to common helper (pve-common's
41 # Tools or Systemd ?)
42 sub systemd_escape {
43 my ($val, $is_path) = @_;
44
45 # NOTE: this is not complete, but enough for our needs. normally all
46 # characters which are not alpha-numerical, '.' or '_' would need escaping
47 $val =~ s/\-/\\x2d/g;
48
49 if ($is_path) {
50 $val =~ s/^\///g;
51 $val =~ s/\/$//g;
52 }
53 $val =~ s/\//-/g;
54
55 return $val;
56 }
57
58 sub cephfs_mount {
59 my ($scfg, $storeid) = @_;
60
61 my $cmd;
62 my $mountpoint = $scfg->{path};
63 my $subdir = $scfg->{subdir} // '/';
64
65 my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
66 my $configfile = $cmd_option->{ceph_conf};
67 my $secretfile = $cmd_option->{keyring};
68 my $server = $cmd_option->{mon_host} // PVE::CephConfig::get_monaddr_list($configfile);
69
70 # fuse -> client-enforced quotas (kernel doesn't), updates w/ ceph-fuse pkg
71 # kernel -> better performance, less frequent updates
72 if ($scfg->{fuse}) {
73 # FIXME: ceph-fuse client complains about missing ceph.conf or keyring if
74 # not provided on its default locations but still connects. Fix upstream??
75 $cmd = ['/usr/bin/ceph-fuse', '-n', "client.$cmd_option->{userid}", '-m', $server];
76 push @$cmd, '--keyfile', $secretfile if defined($secretfile);
77 push @$cmd, '-r', $subdir if !($subdir =~ m|^/$|);
78 push @$cmd, $mountpoint;
79 push @$cmd, '--conf', $configfile if defined($configfile);
80 } else {
81 my $source = "$server:$subdir";
82 $cmd = ['/bin/mount', '-t', 'ceph', $source, $mountpoint, '-o', "name=$cmd_option->{userid}"];
83 push @$cmd, '-o', "secretfile=$secretfile" if defined($secretfile);
84
85 # tell systemd that we're network dependent, else it umounts us to late
86 # on shutdown, when we couldn't connect to the active MDS and thus
87 # unmount hangs and delays shutdown/reboot (man systemd.mount).
88 push @$cmd, '-o', '_netdev';
89 }
90
91 if ($scfg->{options}) {
92 push @$cmd, '-o', $scfg->{options};
93 }
94
95 run_command($cmd, errmsg => "mount error");
96 }
97
98 # Configuration
99
100 sub type {
101 return 'cephfs';
102 }
103
104 sub plugindata {
105 return {
106 content => [ { vztmpl => 1, iso => 1, backup => 1, snippets => 1},
107 { backup => 1 }],
108 };
109 }
110
111 sub properties {
112 return {
113 fuse => {
114 description => "Mount CephFS through FUSE.",
115 type => 'boolean',
116 },
117 subdir => {
118 description => "Subdir to mount.",
119 type => 'string', format => 'pve-storage-path',
120 },
121 };
122 }
123
124 sub options {
125 return {
126 path => { fixed => 1 },
127 monhost => { optional => 1},
128 nodes => { optional => 1 },
129 subdir => { optional => 1 },
130 disable => { optional => 1 },
131 options => { optional => 1 },
132 username => { optional => 1 },
133 content => { optional => 1 },
134 format => { optional => 1 },
135 mkdir => { optional => 1 },
136 fuse => { optional => 1 },
137 bwlimit => { optional => 1 },
138 maxfiles => { optional => 1 },
139 };
140 }
141
142 sub check_config {
143 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
144
145 $config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path};
146
147 return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck);
148 }
149
150 # Storage implementation
151
152 sub on_add_hook {
153 my ($class, $storeid, $scfg, %param) = @_;
154
155 return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph
156
157 PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid);
158 }
159
160 sub on_delete_hook {
161 my ($class, $storeid, $scfg) = @_;
162
163 return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph
164
165 PVE::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid);
166 }
167
168 sub status {
169 my ($class, $storeid, $scfg, $cache) = @_;
170
171 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
172
173 return undef if !cephfs_is_mounted($scfg, $storeid, $cache->{mountdata});
174
175 return $class->SUPER::status($storeid, $scfg, $cache);
176 }
177
178 sub activate_storage {
179 my ($class, $storeid, $scfg, $cache) = @_;
180
181 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
182
183 # NOTE: mkpath may hang if storage is mounted but not reachable
184 if (!cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) {
185 my $path = $scfg->{path};
186
187 mkpath $path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir});
188
189 die "unable to activate storage '$storeid' - " .
190 "directory '$path' does not exist\n" if ! -d $path;
191
192 cephfs_mount($scfg, $storeid);
193 }
194
195 $class->SUPER::activate_storage($storeid, $scfg, $cache);
196 }
197
198 sub deactivate_storage {
199 my ($class, $storeid, $scfg, $cache) = @_;
200
201 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
202
203 my $path = $scfg->{path};
204
205 if (cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) {
206 run_command(['/bin/umount', $path], errmsg => 'umount error');
207 }
208 }
209
210 1;