]> git.proxmox.com Git - pve-cluster.git/blob - data/PVE/Corosync.pm
560a49813614bfb9d0f06eadd266ceb5c80687af
[pve-cluster.git] / data / PVE / Corosync.pm
1 package PVE::Corosync;
2
3 use strict;
4 use warnings;
5
6 use Digest::SHA;
7 use Clone 'clone';
8 use Net::IP qw(ip_is_ipv6);
9
10 use PVE::Cluster;
11
12 my $basedir = "/etc/pve";
13
14 my $conf_array_sections = {
15 node => 1,
16 interface => 1,
17 };
18
19 # a very simply parser ...
20 sub parse_conf {
21 my ($filename, $raw) = @_;
22
23 return {} if !$raw;
24
25 my $digest = Digest::SHA::sha1_hex(defined($raw) ? $raw : '');
26
27 $raw =~ s/#.*$//mg;
28 $raw =~ s/\r?\n/ /g;
29 $raw =~ s/\s+/ /g;
30 $raw =~ s/^\s+//;
31 $raw =~ s/\s*$//;
32
33 my @tokens = split(/\s/, $raw);
34
35 my $conf = { 'main' => {} };
36
37 my $stack = [];
38 my $section = $conf->{main};
39
40 while (defined(my $token = shift @tokens)) {
41 my $nexttok = $tokens[0];
42
43 if ($nexttok && ($nexttok eq '{')) {
44 shift @tokens; # skip '{'
45 my $new_section = {};
46 if ($conf_array_sections->{$token}) {
47 $section->{$token} = [] if !defined($section->{$token});
48 push @{$section->{$token}}, $new_section;
49 } elsif (!defined($section->{$token})) {
50 $section->{$token} = $new_section;
51 } else {
52 die "section '$token' already exists and not marked as array!\n";
53 }
54 push @$stack, $section;
55 $section = $new_section;
56 next;
57 }
58
59 if ($token eq '}') {
60 $section = pop @$stack;
61 die "parse error - uncexpected '}'\n" if !$section;
62 next;
63 }
64
65 my $key = $token;
66 die "missing ':' after key '$key'\n" if ! ($key =~ s/:$//);
67
68 die "parse error - no value for '$key'\n" if !defined($nexttok);
69 my $value = shift @tokens;
70
71 $section->{$key} = $value;
72 }
73
74 # make working with the config way easier
75 my ($totem, $nodelist) = $conf->{main}->@{"totem", "nodelist"};
76 $nodelist->{node} = { map { $_->{name} // $_->{ring0_addr} => $_ } @{$nodelist->{node}} };
77 $totem->{interface} = { map { $_->{ringnumber} => $_ } @{$totem->{interface}} };
78
79 $conf->{digest} = $digest;
80
81 return $conf;
82 }
83
84 my $dump_section;
85 $dump_section = sub {
86 my ($section, $prefix) = @_;
87
88 my $raw = '';
89
90 foreach my $k (sort keys %$section) {
91 my $v = $section->{$k};
92 if (ref($v) eq 'HASH') {
93 $raw .= $prefix . "$k {\n";
94 $raw .= &$dump_section($v, "$prefix ");
95 $raw .= $prefix . "}\n";
96 $raw .= "\n" if !$prefix; # add extra newline at 1st level only
97 } elsif (ref($v) eq 'ARRAY') {
98 foreach my $child (@$v) {
99 $raw .= $prefix . "$k {\n";
100 $raw .= &$dump_section($child, "$prefix ");
101 $raw .= $prefix . "}\n";
102 }
103 } elsif (!ref($v)) {
104 $raw .= $prefix . "$k: $v\n";
105 } else {
106 die "unexpected reference in config hash: $k => ". ref($v) ."\n";
107 }
108 }
109
110 return $raw;
111 };
112
113 sub write_conf {
114 my ($filename, $conf) = @_;
115
116 my $c = clone($conf->{main}) // die "no main section";
117
118 # retransform back for easier dumping
119 my $hash_to_array = sub {
120 my ($hash) = @_;
121 return [ $hash->@{sort keys %$hash} ];
122 };
123
124 $c->{nodelist}->{node} = &$hash_to_array($c->{nodelist}->{node});
125 $c->{totem}->{interface} = &$hash_to_array($c->{totem}->{interface});
126
127 my $raw = &$dump_section($c, '');
128
129 return $raw;
130 }
131
132 # read only - use atomic_write_conf method to write
133 PVE::Cluster::cfs_register_file('corosync.conf', \&parse_conf);
134 # this is read/write
135 PVE::Cluster::cfs_register_file('corosync.conf.new', \&parse_conf,
136 \&write_conf);
137
138 sub check_conf_exists {
139 my ($silent) = @_;
140
141 $silent = $silent // 0;
142
143 my $exists = -f "$basedir/corosync.conf";
144
145 warn "Corosync config '$basedir/corosync.conf' does not exist - is this node part of a cluster?\n"
146 if !$silent && !$exists;
147
148 return $exists;
149 }
150
151 sub update_nodelist {
152 my ($conf, $nodelist) = @_;
153
154 $conf->{main}->{nodelist}->{node} = $nodelist;
155
156 atomic_write_conf($conf);
157 }
158
159 sub nodelist {
160 my ($conf) = @_;
161 return clone($conf->{main}->{nodelist}->{node});
162 }
163
164 sub totem_config {
165 my ($conf) = @_;
166 return clone($conf->{main}->{totem});
167 }
168
169 # caller must hold corosync.conf cfs lock if used in read-modify-write cycle
170 sub atomic_write_conf {
171 my ($conf, $no_increase_version) = @_;
172
173 if (!$no_increase_version) {
174 die "invalid corosync config: unable to read config version\n"
175 if !defined($conf->{main}->{totem}->{config_version});
176 $conf->{main}->{totem}->{config_version}++;
177 }
178
179 PVE::Cluster::cfs_write_file("corosync.conf.new", $conf);
180
181 rename("/etc/pve/corosync.conf.new", "/etc/pve/corosync.conf")
182 || die "activating corosync.conf.new failed - $!\n";
183 }
184
185 # for creating a new cluster with the current node
186 # params are those from the API/CLI cluster create call
187 sub create_conf {
188 my ($nodename, %param) = @_;
189
190 my $clustername = $param{clustername};
191 my $nodeid = $param{nodeid} || 1;
192 my $votes = $param{votes} || 1;
193
194 my $local_ip_address = PVE::Cluster::remote_node_ip($nodename);
195 my $ring0_addr = $param{ring0_addr} // $local_ip_address;
196 my $bindnet0_addr = $param{bindnet0_addr} // $ring0_addr;
197
198 my $use_ipv6 = ip_is_ipv6($ring0_addr);
199 die "ring 0 addresses must be from same IP family!\n"
200 if $use_ipv6 != ip_is_ipv6($bindnet0_addr);
201
202 my $conf = {
203 totem => {
204 version => 2, # protocol version
205 secauth => 'on',
206 cluster_name => $clustername,
207 config_version => 0,
208 ip_version => $use_ipv6 ? 'ipv6' : 'ipv4',
209 interface => {
210 0 => {
211 bindnetaddr => $bindnet0_addr,
212 ringnumber => 0,
213 },
214 },
215 },
216 nodelist => {
217 node => {
218 $nodename => {
219 name => $nodename,
220 nodeid => $nodeid,
221 quorum_votes => $votes,
222 ring0_addr => $ring0_addr,
223 },
224 },
225 },
226 quorum => {
227 provider => 'corosync_votequorum',
228 },
229 logging => {
230 to_syslog => 'yes',
231 debug => 'off',
232 },
233 };
234
235 die "Param bindnet1_addr set but ring1_addr not specified!\n"
236 if (defined($param{bindnet1_addr}) && !defined($param{ring1_addr}));
237
238 my $ring1_addr = $param{ring1_addr};
239 my $bindnet1_addr = $param{bindnet1_addr} // $param{ring1_addr};
240
241 if ($bindnet1_addr) {
242 die "ring 1 addresses must be from same IP family as ring 0!\n"
243 if $use_ipv6 != ip_is_ipv6($bindnet1_addr) ||
244 $use_ipv6 != ip_is_ipv6($ring1_addr);
245
246 $conf->{totem}->{interface}->{1} = {
247 bindnetaddr => $bindnet1_addr,
248 ringnumber => 1,
249 };
250 $conf->{totem}->{rrp_mode} = 'passive';
251 $conf->{nodelist}->{node}->{$nodename}->{ring1_addr} = $ring1_addr;
252 }
253
254 return { main => $conf };
255 }
256
257 1;