]>
git.proxmox.com Git - pve-cluster.git/blob - data/PVE/Corosync.pm
8 use Net
::IP
qw(ip_is_ipv6);
12 my $basedir = "/etc/pve";
14 my $conf_array_sections = {
19 # a very simply parser ...
21 my ($filename, $raw) = @_;
25 my $digest = Digest
::SHA
::sha1_hex
(defined($raw) ?
$raw : '');
33 my @tokens = split(/\s/, $raw);
35 my $conf = { 'main' => {} };
38 my $section = $conf->{main
};
40 while (defined(my $token = shift @tokens)) {
41 my $nexttok = $tokens[0];
43 if ($nexttok && ($nexttok eq '{')) {
44 shift @tokens; # skip '{'
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;
52 die "section '$token' already exists and not marked as array!\n";
54 push @$stack, $section;
55 $section = $new_section;
60 $section = pop @$stack;
61 die "parse error - uncexpected '}'\n" if !$section;
66 die "missing ':' after key '$key'\n" if ! ($key =~ s/:$//);
68 die "parse error - no value for '$key'\n" if !defined($nexttok);
69 my $value = shift @tokens;
71 $section->{$key} = $value;
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
}} };
79 $conf->{digest
} = $digest;
86 my ($section, $prefix) = @_;
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";
104 die "got undefined value for key '$k'!\n" if !defined($v);
105 $raw .= $prefix . "$k: $v\n";
107 die "unexpected reference in config hash: $k => ". ref($v) ."\n";
115 my ($filename, $conf) = @_;
117 my $c = clone
($conf->{main
}) // die "no main section";
119 # retransform back for easier dumping
120 my $hash_to_array = sub {
122 return [ $hash->@{sort keys %$hash} ];
125 $c->{nodelist
}->{node
} = &$hash_to_array($c->{nodelist
}->{node
});
126 $c->{totem
}->{interface
} = &$hash_to_array($c->{totem
}->{interface
});
128 my $raw = &$dump_section($c, '');
133 # read only - use atomic_write_conf method to write
134 PVE
::Cluster
::cfs_register_file
('corosync.conf', \
&parse_conf
);
136 PVE
::Cluster
::cfs_register_file
('corosync.conf.new', \
&parse_conf
,
139 sub check_conf_exists
{
142 $silent = $silent // 0;
144 my $exists = -f
"$basedir/corosync.conf";
146 warn "Corosync config '$basedir/corosync.conf' does not exist - is this node part of a cluster?\n"
147 if !$silent && !$exists;
152 sub update_nodelist
{
153 my ($conf, $nodelist) = @_;
155 $conf->{main
}->{nodelist
}->{node
} = $nodelist;
157 atomic_write_conf
($conf);
162 return clone
($conf->{main
}->{nodelist
}->{node
});
167 return clone
($conf->{main
}->{totem
});
170 # caller must hold corosync.conf cfs lock if used in read-modify-write cycle
171 sub atomic_write_conf
{
172 my ($conf, $no_increase_version) = @_;
174 if (!$no_increase_version) {
175 die "invalid corosync config: unable to read config version\n"
176 if !defined($conf->{main
}->{totem
}->{config_version
});
177 $conf->{main
}->{totem
}->{config_version
}++;
180 PVE
::Cluster
::cfs_write_file
("corosync.conf.new", $conf);
182 rename("/etc/pve/corosync.conf.new", "/etc/pve/corosync.conf")
183 || die "activating corosync.conf.new failed - $!\n";
186 # for creating a new cluster with the current node
187 # params are those from the API/CLI cluster create call
189 my ($nodename, %param) = @_;
191 my $clustername = $param{clustername
};
192 my $nodeid = $param{nodeid
} || 1;
193 my $votes = $param{votes
} || 1;
195 my $local_ip_address = PVE
::Cluster
::remote_node_ip
($nodename);
196 my $ring0_addr = $param{ring0_addr
} // $local_ip_address;
197 my $bindnet0_addr = $param{bindnet0_addr
} // $ring0_addr;
199 my $use_ipv6 = ip_is_ipv6
($ring0_addr);
200 die "ring 0 addresses must be from same IP family!\n"
201 if $use_ipv6 != ip_is_ipv6
($bindnet0_addr);
205 version
=> 2, # protocol version
207 cluster_name
=> $clustername,
209 ip_version
=> $use_ipv6 ?
'ipv6' : 'ipv4',
212 bindnetaddr
=> $bindnet0_addr,
222 quorum_votes
=> $votes,
223 ring0_addr
=> $ring0_addr,
228 provider
=> 'corosync_votequorum',
236 die "Param bindnet1_addr set but ring1_addr not specified!\n"
237 if (defined($param{bindnet1_addr
}) && !defined($param{ring1_addr
}));
239 my $ring1_addr = $param{ring1_addr
};
240 my $bindnet1_addr = $param{bindnet1_addr
} // $param{ring1_addr
};
242 if ($bindnet1_addr) {
243 die "ring 1 addresses must be from same IP family as ring 0!\n"
244 if $use_ipv6 != ip_is_ipv6
($bindnet1_addr) ||
245 $use_ipv6 != ip_is_ipv6
($ring1_addr);
247 $conf->{totem
}->{interface
}->{1} = {
248 bindnetaddr
=> $bindnet1_addr,
251 $conf->{totem
}->{rrp_mode
} = 'passive';
252 $conf->{nodelist
}->{node
}->{$nodename}->{ring1_addr
} = $ring1_addr;
255 return { main
=> $conf };