]> git.proxmox.com Git - pve-storage.git/blob - PVE/CephConfig.pm
CephConfig: do not always interpret '; ' as a comment
[pve-storage.git] / PVE / CephConfig.pm
1 package PVE::CephConfig;
2
3 use strict;
4 use warnings;
5 use Net::IP;
6 use PVE::Tools qw(run_command);
7 use PVE::Cluster qw(cfs_register_file);
8
9 cfs_register_file('ceph.conf',
10 \&parse_ceph_config,
11 \&write_ceph_config);
12
13 sub parse_ceph_config {
14 my ($filename, $raw) = @_;
15
16 my $cfg = {};
17 return $cfg if !defined($raw);
18
19 my @lines = split /\n/, $raw;
20
21 my $section;
22
23 foreach my $line (@lines) {
24 $line =~ s/#.*$//;
25 $line =~ s/^\s+//;
26 $line =~ s/^;.*$//;
27 $line =~ s/\s+$//;
28 next if !$line;
29
30 $section = $1 if $line =~ m/^\[(\S+)\]$/;
31 if (!$section) {
32 warn "no section - skip: $line\n";
33 next;
34 }
35
36 if ($line =~ m/^(.*?\S)\s*=\s*(\S.*)$/) {
37 my ($key, $val) = ($1, $2);
38 # ceph treats ' ', '_' and '-' in keys the same, so lets do too
39 $key =~ s/[-\ ]/_/g;
40 $cfg->{$section}->{$key} = $val;
41 }
42
43 }
44
45 return $cfg;
46 }
47
48 my $parse_ceph_file = sub {
49 my ($filename) = @_;
50
51 my $cfg = {};
52
53 return $cfg if ! -f $filename;
54
55 my $content = PVE::Tools::file_get_contents($filename);
56
57 return parse_ceph_config($filename, $content);
58 };
59
60 sub write_ceph_config {
61 my ($filename, $cfg) = @_;
62
63 my $out = '';
64
65 my $cond_write_sec = sub {
66 my $re = shift;
67
68 foreach my $section (keys %$cfg) {
69 next if $section !~ m/^$re$/;
70 $out .= "[$section]\n";
71 foreach my $key (sort keys %{$cfg->{$section}}) {
72 $out .= "\t $key = $cfg->{$section}->{$key}\n";
73 }
74 $out .= "\n";
75 }
76 };
77
78 &$cond_write_sec('global');
79 &$cond_write_sec('client');
80
81 &$cond_write_sec('mds');
82 &$cond_write_sec('mon');
83 &$cond_write_sec('osd');
84 &$cond_write_sec('mgr');
85
86 &$cond_write_sec('mds\..*');
87 &$cond_write_sec('mon\..*');
88 &$cond_write_sec('osd\..*');
89 &$cond_write_sec('mgr\..*');
90
91 return $out;
92 }
93
94 my $ceph_get_key = sub {
95 my ($keyfile, $username) = @_;
96
97 my $key = $parse_ceph_file->($keyfile);
98 my $secret = $key->{"client.$username"}->{key};
99
100 return $secret;
101 };
102
103 sub get_monaddr_list {
104 my ($configfile) = shift;
105
106 if (!defined($configfile)) {
107 warn "No ceph config specified\n";
108 return;
109 }
110
111 my $config = $parse_ceph_file->($configfile);
112
113 my @monids = grep { /mon\./ && defined($config->{$_}->{'mon addr'}) } %{$config};
114
115 return join(',', sort map { $config->{$_}->{'mon addr'} } @monids);
116 };
117
118 sub hostlist {
119 my ($list_text, $separator) = @_;
120
121 my @monhostlist = PVE::Tools::split_list($list_text);
122 return join($separator, map {
123 my ($host, $port) = PVE::Tools::parse_host_and_port($_);
124 $port = defined($port) ? ":$port" : '';
125 $host = "[$host]" if Net::IP::ip_is_ipv6($host);
126 "${host}${port}"
127 } @monhostlist);
128 }
129
130 my $ceph_check_keyfile = sub {
131 my ($filename, $type) = @_;
132
133 return if ! -f $filename;
134
135 my $content = PVE::Tools::file_get_contents($filename);
136 eval {
137 die if !$content;
138
139 if ($type eq 'rbd') {
140 die if $content !~ /\s*\[\S+\]\s*key\s*=\s*\S+==\s*$/m;
141 } elsif ($type eq 'cephfs') {
142 die if $content !~ /\S+==\s*$/;
143 }
144 };
145 die "Not a proper $type authentication file: $filename\n" if $@;
146
147 return undef;
148 };
149
150 sub ceph_connect_option {
151 my ($scfg, $storeid, %options) = @_;
152
153 my $cmd_option = {};
154 my $ceph_storeid_conf = "/etc/pve/priv/ceph/${storeid}.conf";
155 my $pveceph_config = '/etc/pve/ceph.conf';
156 my $keyfile = "/etc/pve/priv/ceph/${storeid}.keyring";
157 $keyfile = "/etc/pve/priv/ceph/${storeid}.secret" if ($scfg->{type} eq 'cephfs');
158 my $pveceph_managed = !defined($scfg->{monhost});
159
160 $cmd_option->{ceph_conf} = $pveceph_config if $pveceph_managed;
161
162 $ceph_check_keyfile->($keyfile, $scfg->{type});
163
164 if (-e $ceph_storeid_conf) {
165 if ($pveceph_managed) {
166 warn "ignoring custom ceph config for storage '$storeid', 'monhost' is not set (assuming pveceph managed cluster)!\n";
167 } else {
168 $cmd_option->{ceph_conf} = $ceph_storeid_conf;
169 }
170 }
171
172 $cmd_option->{keyring} = $keyfile if (-e $keyfile);
173 $cmd_option->{auth_supported} = (defined $cmd_option->{keyring}) ? 'cephx' : 'none';
174 $cmd_option->{userid} = $scfg->{username} ? $scfg->{username} : 'admin';
175 $cmd_option->{mon_host} = hostlist($scfg->{monhost}, ',') if (defined($scfg->{monhost}));
176
177 if (%options) {
178 foreach my $k (keys %options) {
179 $cmd_option->{$k} = $options{$k};
180 }
181 }
182
183 return $cmd_option;
184
185 }
186
187 sub ceph_create_keyfile {
188 my ($type, $storeid) = @_;
189
190 my $extension = 'keyring';
191 $extension = 'secret' if ($type eq 'cephfs');
192
193 my $ceph_admin_keyring = '/etc/pve/priv/ceph.client.admin.keyring';
194 my $ceph_storage_keyring = "/etc/pve/priv/ceph/${storeid}.$extension";
195
196 die "ceph authx keyring file for storage '$storeid' already exists!\n"
197 if -e $ceph_storage_keyring;
198
199 if (-e $ceph_admin_keyring) {
200 eval {
201 if ($type eq 'rbd') {
202 mkdir '/etc/pve/priv/ceph';
203 PVE::Tools::file_copy($ceph_admin_keyring, $ceph_storage_keyring);
204 } elsif ($type eq 'cephfs') {
205 my $secret = $ceph_get_key->($ceph_admin_keyring, 'admin');
206 mkdir '/etc/pve/priv/ceph';
207 PVE::Tools::file_set_contents($ceph_storage_keyring, $secret, 0400);
208 }
209 };
210 if (my $err = $@) {
211 unlink $ceph_storage_keyring;
212 die "failed to copy ceph authx $extension for storage '$storeid': $err\n";
213 }
214 } else {
215 warn "$ceph_admin_keyring not found, authentication is disabled.\n";
216 }
217 }
218
219 sub ceph_remove_keyfile {
220 my ($type, $storeid) = @_;
221
222 my $extension = 'keyring';
223 $extension = 'secret' if ($type eq 'cephfs');
224 my $ceph_storage_keyring = "/etc/pve/priv/ceph/${storeid}.$extension";
225
226 if (-f $ceph_storage_keyring) {
227 unlink($ceph_storage_keyring) or warn "removing keyring of storage failed: $!\n";
228 }
229 }
230
231 1;