]>
git.proxmox.com Git - pve-storage.git/blob - PVE/CephConfig.pm
1 package PVE
::CephConfig
;
6 use PVE
::Tools
qw(run_command);
7 use PVE
::Cluster
qw(cfs_register_file);
9 cfs_register_file
('ceph.conf',
13 sub parse_ceph_config
{
14 my ($filename, $raw) = @_;
17 return $cfg if !defined($raw);
19 my @lines = split /\n/, $raw;
23 foreach my $line (@lines) {
30 $section = $1 if $line =~ m/^\[(\S+)\]$/;
32 warn "no section - skip: $line\n";
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
40 $cfg->{$section}->{$key} = $val;
48 my $parse_ceph_file = sub {
53 return $cfg if ! -f
$filename;
55 my $content = PVE
::Tools
::file_get_contents
($filename);
57 return parse_ceph_config
($filename, $content);
60 sub write_ceph_config
{
61 my ($filename, $cfg) = @_;
65 my $cond_write_sec = sub {
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";
78 &$cond_write_sec('global');
79 &$cond_write_sec('client');
81 &$cond_write_sec('mds');
82 &$cond_write_sec('mon');
83 &$cond_write_sec('osd');
84 &$cond_write_sec('mgr');
86 &$cond_write_sec('mds\..*');
87 &$cond_write_sec('mon\..*');
88 &$cond_write_sec('osd\..*');
89 &$cond_write_sec('mgr\..*');
94 my $ceph_get_key = sub {
95 my ($keyfile, $username) = @_;
97 my $key = $parse_ceph_file->($keyfile);
98 my $secret = $key->{"client.$username"}->{key
};
103 sub get_monaddr_list
{
104 my ($configfile) = shift;
106 if (!defined($configfile)) {
107 warn "No ceph config specified\n";
111 my $config = $parse_ceph_file->($configfile);
113 my @monids = grep { /mon\./ && defined($config->{$_}->{'mon addr'}) } %{$config};
115 return join(',', sort map { $config->{$_}->{'mon addr'} } @monids);
119 my ($list_text, $separator) = @_;
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);
130 my $ceph_check_keyfile = sub {
131 my ($filename, $type) = @_;
133 return if ! -f
$filename;
135 my $content = PVE
::Tools
::file_get_contents
($filename);
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*$/;
145 die "Not a proper $type authentication file: $filename\n" if $@;
150 sub ceph_connect_option
{
151 my ($scfg, $storeid, %options) = @_;
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
});
160 $cmd_option->{ceph_conf
} = $pveceph_config if $pveceph_managed;
162 $ceph_check_keyfile->($keyfile, $scfg->{type
});
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";
168 $cmd_option->{ceph_conf
} = $ceph_storeid_conf;
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
}));
178 foreach my $k (keys %options) {
179 $cmd_option->{$k} = $options{$k};
187 sub ceph_create_keyfile
{
188 my ($type, $storeid) = @_;
190 my $extension = 'keyring';
191 $extension = 'secret' if ($type eq 'cephfs');
193 my $ceph_admin_keyring = '/etc/pve/priv/ceph.client.admin.keyring';
194 my $ceph_storage_keyring = "/etc/pve/priv/ceph/${storeid}.$extension";
196 die "ceph authx keyring file for storage '$storeid' already exists!\n"
197 if -e
$ceph_storage_keyring;
199 if (-e
$ceph_admin_keyring) {
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);
211 unlink $ceph_storage_keyring;
212 die "failed to copy ceph authx $extension for storage '$storeid': $err\n";
215 warn "$ceph_admin_keyring not found, authentication is disabled.\n";
219 sub ceph_remove_keyfile
{
220 my ($type, $storeid) = @_;
222 my $extension = 'keyring';
223 $extension = 'secret' if ($type eq 'cephfs');
224 my $ceph_storage_keyring = "/etc/pve/priv/ceph/${storeid}.$extension";
226 if (-f
$ceph_storage_keyring) {
227 unlink($ceph_storage_keyring) or warn "removing keyring of storage failed: $!\n";