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