1 package PVE
::CLI
::pvecm
;
11 use Data
::Dumper
; # fixme: remove
18 use base
qw(PVE::CLIHandler);
20 $ENV{HOME
} = '/root'; # for ssh-copy-id
22 my $basedir = "/etc/pve";
23 my $clusterconf = "$basedir/corosync.conf";
24 my $libdir = "/var/lib/pve-cluster";
25 my $backupdir = "/var/lib/pve-cluster/backup";
26 my $dbfile = "$libdir/config.db";
27 my $authfile = "/etc/corosync/authkey";
31 print "backup old database\n";
39 ['gzip', '-', \
">${backupdir}/config-${ctime}.sql.gz"],
42 PVE
::Tools
::run_command
($cmd, 'errmsg' => "cannot backup old database\n");
48 foreach my $fn (<$backupdir/config-*.sql
.gz
>) {
49 if ($fn =~ m!/config-(\d+)\.sql.gz$!) {
50 push @bklist, [$fn, $1];
54 @bklist = sort { $b->[1] <=> $a->[1] } @bklist;
56 while (scalar (@bklist) >= $maxfiles) {
58 print "delete old backup '$d->[0]'\n";
63 __PACKAGE__-
>register_method ({
67 description
=> "Generate new cryptographic key for corosync.",
69 additionalProperties
=> 0,
73 description
=> "Output file name"
77 returns
=> { type
=> 'null' },
82 my $filename = $param->{filename
};
85 $> == 0 || die "Error: Authorization key must be generated as root user.\n";
86 my $dirname = dirname
($filename);
87 my $basename = basename
($filename);
89 die "key file '$filename' already exists\n" if -e
$filename;
91 File
::Path
::make_path
($dirname) if $dirname;
93 my $cmd = ['corosync-keygen', '-l', '-k', $filename];
94 PVE
::Tools
::run_command
($cmd);
99 __PACKAGE__-
>register_method ({
103 description
=> "Generate new cluster configuration.",
105 additionalProperties
=> 0,
108 description
=> "The name of the cluster.",
109 type
=> 'string', format
=> 'pve-node',
114 description
=> "Node id for this node.",
120 description
=> "Number of votes for this node.",
125 type
=> 'string', format
=> 'ip',
126 description
=> "This specifies the network address the corosync ring 0".
127 " executive should bind to and defaults to the local IP address of the node.",
131 type
=> 'string', format
=> 'address',
132 description
=> "Hostname (or IP) of the corosync ring0 address of this node.".
133 " Defaults to the hostname of the node.",
137 type
=> 'string', format
=> 'ip',
138 description
=> "This specifies the network address the corosync ring 1".
139 " executive should bind to and is optional.",
143 type
=> 'string', format
=> 'address',
144 description
=> "Hostname (or IP) of the corosync ring1 address, this".
145 " needs an valid bindnet1_addr.",
150 returns
=> { type
=> 'null' },
155 -f
$clusterconf && die "cluster config '$clusterconf' already exists\n";
157 PVE
::Cluster
::setup_sshd_config
(1);
158 PVE
::Cluster
::setup_rootsshconfig
();
159 PVE
::Cluster
::setup_ssh_keys
();
161 -f
$authfile || __PACKAGE__-
>keygen({filename
=> $authfile});
163 -f
$authfile || die "no authentication key available\n";
165 my $clustername = $param->{clustername
};
167 $param->{nodeid
} = 1 if !$param->{nodeid
};
169 $param->{votes
} = 1 if !defined($param->{votes
});
171 my $nodename = PVE
::INotify
::nodename
();
173 my $local_ip_address = PVE
::Cluster
::remote_node_ip
($nodename);
175 $param->{bindnet0_addr
} = $local_ip_address
176 if !defined($param->{bindnet0_addr
});
178 $param->{ring0_addr
} = $nodename if !defined($param->{ring0_addr
});
180 die "Param bindnet1_addr and ring1_addr are dependend, use both or none!\n"
181 if (defined($param->{bindnet1_addr
}) != defined($param->{ring1_addr
}));
183 my $bind_is_ipv6 = Net
::IP
::ip_is_ipv6
($param->{bindnet0_addr
});
185 # use string as here-doc format distracts more
186 my $interfaces = "interface {\n ringnumber: 0\n" .
187 " bindnetaddr: $param->{bindnet0_addr}\n }";
189 my $ring_addresses = "ring0_addr: $param->{ring0_addr}" ;
191 # allow use of multiple rings (rrp) at cluster creation time
192 if ($param->{bindnet1_addr
}) {
193 die "IPv6 and IPv4 cannot be mixed, use one or the other!\n"
194 if Net
::IP
::ip_is_ipv6
($param->{bindnet1_addr
}) != $bind_is_ipv6;
196 $interfaces .= "\n interface {\n ringnumber: 1\n" .
197 " bindnetaddr: $param->{bindnet1_addr}\n }\n";
199 $interfaces .= "rrp_mode: passive\n"; # only passive is stable and tested
201 $ring_addresses .= "\n ring1_addr: $param->{ring1_addr}";
203 } elsif($param->{rrp_mode
} && $param->{rrp_mode
} ne 'none') {
205 warn "rrp_mode '$param->{rrp_mode}' useless when using only one".
206 " ring, using 'none' instead";
207 # corosync defaults to none if only one interface is configured
208 $param->{rrp_mode
} = undef;
212 # No, corosync cannot deduce this on its own
213 my $ipversion = $bind_is_ipv6 ?
'ipv6' : 'ipv4';
219 cluster_name: $clustername
221 ip_version: $ipversion
229 nodeid: $param->{nodeid}
230 quorum_votes: $param->{votes}
235 provider: corosync_votequorum
244 PVE
::Tools
::file_set_contents
($clusterconf, $config);
246 PVE
::Cluster
::ssh_merge_keys
();
248 PVE
::Cluster
::gen_pve_node_files
($nodename, $local_ip_address);
250 PVE
::Cluster
::ssh_merge_known_hosts
($nodename, $local_ip_address, 1);
252 PVE
::Tools
::run_command
('systemctl restart pve-cluster'); # restart
254 PVE
::Tools
::run_command
('systemctl restart corosync'); # restart
259 __PACKAGE__-
>register_method ({
263 description
=> "Adds a node to the cluster configuration.",
265 additionalProperties
=> 0,
267 node
=> PVE
::JSONSchema
::get_standard_option
('pve-node'),
270 description
=> "Node id for this node.",
276 description
=> "Number of votes for this node",
282 description
=> "Do not throw error if node already exists.",
286 type
=> 'string', format
=> 'address',
287 description
=> "Hostname (or IP) of the corosync ring0 address of this node.".
288 " Defaults to nodes hostname.",
292 type
=> 'string', format
=> 'address',
293 description
=> "Hostname (or IP) of the corosync ring1 address, this".
294 " needs an valid bindnet1_addr.",
299 returns
=> { type
=> 'null' },
304 PVE
::Cluster
::check_cfs_quorum
();
306 my $conf = PVE
::Cluster
::cfs_read_file
("corosync.conf");
308 my $nodelist = PVE
::Cluster
::corosync_nodelist
($conf);
310 my $totem_cfg = PVE
::Cluster
::corosync_totem_config
($conf);
312 my $name = $param->{node
};
314 $param->{ring0_addr
} = $name if !$param->{ring0_addr
};
316 die " ring1_addr needs a configured ring 1 interface!\n"
317 if $param->{ring1_addr
} && !defined($totem_cfg->{interface
}->{1});
319 if (defined(my $res = $nodelist->{$name})) {
320 $param->{nodeid
} = $res->{nodeid
} if !$param->{nodeid
};
321 $param->{votes
} = $res->{quorum_votes
} if !defined($param->{votes
});
323 if ($res->{quorum_votes
} == $param->{votes
} &&
324 $res->{nodeid
} == $param->{nodeid
}) {
325 print "node $name already defined\n";
326 if ($param->{force
}) {
332 die "can't add existing node\n";
334 } elsif (!$param->{nodeid
}) {
339 foreach my $v (values %$nodelist) {
340 if ($v->{nodeid
} eq $nodeid) {
349 $param->{nodeid
} = $nodeid;
352 $param->{votes
} = 1 if !defined($param->{votes
});
354 PVE
::Cluster
::gen_local_dirs
($name);
356 eval { PVE
::Cluster
::ssh_merge_keys
(); };
359 $nodelist->{$name} = {
360 ring0_addr
=> $param->{ring0_addr
},
361 nodeid
=> $param->{nodeid
},
364 $nodelist->{$name}->{ring1_addr
} = $param->{ring1_addr
} if $param->{ring1_addr
};
365 $nodelist->{$name}->{quorum_votes
} = $param->{votes
} if $param->{votes
};
367 PVE
::Cluster
::corosync_update_nodelist
($conf, $nodelist);
373 __PACKAGE__-
>register_method ({
377 description
=> "Removes a node to the cluster configuration.",
379 additionalProperties
=> 0,
383 description
=> "Hostname or IP of the corosync ring0 address of this node.",
387 returns
=> { type
=> 'null' },
392 PVE
::Cluster
::check_cfs_quorum
();
394 my $conf = PVE
::Cluster
::cfs_read_file
("corosync.conf");
396 my $nodelist = PVE
::Cluster
::corosync_nodelist
($conf);
401 foreach my $tmp_node (keys %$nodelist) {
402 my $d = $nodelist->{$tmp_node};
403 my $ring0_addr = $d->{ring0_addr
};
404 my $ring1_addr = $d->{ring1_addr
};
405 if (($tmp_node eq $param->{node
}) ||
406 (defined($ring0_addr) && ($ring0_addr eq $param->{node
})) ||
407 (defined($ring1_addr) && ($ring1_addr eq $param->{node
}))) {
409 $nodeid = $d->{nodeid
};
414 die "Node/IP: $param->{node} is not a known host of the cluster.\n"
417 delete $nodelist->{$node};
419 PVE
::Cluster
::corosync_update_nodelist
($conf, $nodelist);
421 PVE
::Tools
::run_command
(['corosync-cfgtool','-k', $nodeid])
427 __PACKAGE__-
>register_method ({
431 description
=> "Adds the current node to an existing cluster.",
433 additionalProperties
=> 0,
437 description
=> "Hostname (or IP) of an existing cluster member."
441 description
=> "Node id for this node.",
447 description
=> "Number of votes for this node",
453 description
=> "Do not throw error if node already exists.",
457 type
=> 'string', format
=> 'address',
458 description
=> "Hostname (or IP) of the corosync ring0 address of this node.".
459 " Defaults to nodes hostname.",
463 type
=> 'string', format
=> 'address',
464 description
=> "Hostname (or IP) of the corosync ring1 address, this".
465 " needs an valid configured ring 1 interface in the cluster.",
470 returns
=> { type
=> 'null' },
475 my $nodename = PVE
::INotify
::nodename
();
477 PVE
::Cluster
::setup_sshd_config
(1);
478 PVE
::Cluster
::setup_rootsshconfig
();
479 PVE
::Cluster
::setup_ssh_keys
();
481 my $host = $param->{hostname
};
483 if (!$param->{force
}) {
486 die "authentication key already exists\n";
489 if (-f
$clusterconf) {
490 die "cluster config '$clusterconf' already exists\n";
493 my $vmlist = PVE
::Cluster
::get_vmlist
();
494 if ($vmlist && $vmlist->{ids
} && scalar(keys %{$vmlist->{ids
}})) {
495 die "this host already contains virtual machines - please remove them first\n";
498 if (system("corosync-quorumtool -l >/dev/null 2>&1") == 0) {
499 die "corosync is already running\n";
503 # make sure known_hosts is on local filesystem
504 PVE
::Cluster
::ssh_unmerge_known_hosts
();
506 my $cmd = ['ssh-copy-id', '-i', '/root/.ssh/id_rsa', "root\@$host"];
507 PVE
::Tools
::run_command
($cmd, 'outfunc' => sub {}, 'errfunc' => sub {},
508 'errmsg' => "unable to copy ssh ID");
510 $cmd = ['ssh', $host, '-o', 'BatchMode=yes',
511 'pvecm', 'addnode', $nodename, '--force', 1];
513 push @$cmd, '--nodeid', $param->{nodeid
} if $param->{nodeid
};
515 push @$cmd, '--votes', $param->{votes
} if defined($param->{votes
});
517 push @$cmd, '--ring0_addr', $param->{ring0_addr
} if defined($param->{ring0_addr
});
519 push @$cmd, '--ring1_addr', $param->{ring1_addr
} if defined($param->{ring1_addr
});
521 if (system (@$cmd) != 0) {
522 my $cmdtxt = join (' ', @$cmd);
523 die "unable to add node: command failed ($cmdtxt)\n";
526 my $tmpdir = "$libdir/.pvecm_add.tmp.$$";
530 print "copy corosync auth key\n";
531 $cmd = ['rsync', '--rsh=ssh -l root -o BatchMode=yes', '-lpgoq',
532 "[$host]:$authfile $clusterconf", $tmpdir];
534 system(@$cmd) == 0 || die "can't rsync data from host '$host'\n";
536 mkdir "/etc/corosync";
537 my $confbase = basename
($clusterconf);
539 $cmd = "cp '$tmpdir/$confbase' '/etc/corosync/$confbase'";
540 system($cmd) == 0 || die "can't copy cluster configuration\n";
542 my $keybase = basename
($authfile);
543 system ("cp '$tmpdir/$keybase' '$authfile'") == 0 ||
544 die "can't copy '$tmpdir/$keybase' to '$authfile'\n";
546 print "stopping pve-cluster service\n";
548 system("umount $basedir -f >/dev/null 2>&1");
549 system("systemctl stop pve-cluster") == 0 ||
550 die "can't stop pve-cluster service\n";
556 system("systemctl start pve-cluster") == 0 ||
557 die "starting pve-cluster failed\n";
559 system("systemctl start corosync");
563 while (!PVE
::Cluster
::check_cfs_quorum
(1)) {
565 print "waiting for quorum...";
571 print "OK\n" if !$printqmsg;
573 my $local_ip_address = PVE
::Cluster
::remote_node_ip
($nodename);
575 print "generating node certificates\n";
576 PVE
::Cluster
::gen_pve_node_files
($nodename, $local_ip_address);
578 print "merge known_hosts file\n";
579 PVE
::Cluster
::ssh_merge_known_hosts
($nodename, $local_ip_address, 1);
581 print "restart services\n";
582 # restart pvedaemon (changed certs)
583 system("systemctl restart pvedaemon");
584 # restart pveproxy (changed certs)
585 system("systemctl restart pveproxy");
587 print "successfully added node '$nodename' to cluster.\n";
598 __PACKAGE__-
>register_method ({
602 description
=> "Displays the local view of the cluster status.",
604 additionalProperties
=> 0,
607 returns
=> { type
=> 'null' },
612 PVE
::Cluster
::check_corosync_conf_exists
();
614 my $cmd = ['corosync-quorumtool', '-siH'];
618 exit (-1); # should not be reached
621 __PACKAGE__-
>register_method ({
625 description
=> "Displays the local view of the cluster nodes.",
627 additionalProperties
=> 0,
630 returns
=> { type
=> 'null' },
635 PVE
::Cluster
::check_corosync_conf_exists
();
637 my $cmd = ['corosync-quorumtool', '-l'];
641 exit (-1); # should not be reached
644 __PACKAGE__-
>register_method ({
648 description
=> "Tells corosync a new value of expected votes.",
650 additionalProperties
=> 0,
654 description
=> "Expected votes",
659 returns
=> { type
=> 'null' },
664 PVE
::Cluster
::check_corosync_conf_exists
();
666 my $cmd = ['corosync-quorumtool', '-e', $param->{expected
}];
670 exit (-1); # should not be reached
674 __PACKAGE__-
>register_method ({
675 name
=> 'updatecerts',
676 path
=> 'updatecerts',
678 description
=> "Update node certificates (and generate all needed files/directories).",
680 additionalProperties
=> 0,
683 description
=> "Force generation of new SSL certifate.",
688 description
=> "Ignore errors (i.e. when cluster has no quorum).",
694 returns
=> { type
=> 'null' },
698 PVE
::Cluster
::setup_sshd_config
(0);
699 PVE
::Cluster
::setup_rootsshconfig
();
701 PVE
::Cluster
::gen_pve_vzdump_symlink
();
703 if (!PVE
::Cluster
::check_cfs_quorum
(1)) {
704 return undef if $param->{silent
};
705 die "no quorum - unable to update files\n";
708 PVE
::Cluster
::setup_ssh_keys
();
710 my $nodename = PVE
::INotify
::nodename
();
712 my $local_ip_address = PVE
::Cluster
::remote_node_ip
($nodename);
714 PVE
::Cluster
::gen_pve_node_files
($nodename, $local_ip_address, $param->{force
});
715 PVE
::Cluster
::ssh_merge_keys
();
716 PVE
::Cluster
::ssh_merge_known_hosts
($nodename, $local_ip_address);
717 PVE
::Cluster
::gen_pve_vzdump_files
();
722 __PACKAGE__-
>register_method ({
726 description
=> "Used by VM/CT migration - do not use manually.",
728 additionalProperties
=> 0,
730 get_migration_ip
=> {
733 description
=> 'return the migration IP, if configured',
736 migration_network
=> {
739 description
=> 'the migration network used to detect the local migration IP',
744 returns
=> { type
=> 'null'},
748 if (!PVE
::Cluster
::check_cfs_quorum
(1)) {
753 if ($param->{get_migration_ip
}) {
754 my $network = $param->{migration_network
};
755 if (my $ip = PVE
::Cluster
::get_local_migration_ip
($network)) {
760 # do not keep tunnel open when asked for migration ip
764 print "tunnel online\n";
767 while (my $line = <>) {
769 last if $line =~ m/^quit$/;
777 keygen
=> [ __PACKAGE__
, 'keygen', ['filename']],
778 create
=> [ __PACKAGE__
, 'create', ['clustername']],
779 add
=> [ __PACKAGE__
, 'add', ['hostname']],
780 addnode
=> [ __PACKAGE__
, 'addnode', ['node']],
781 delnode
=> [ __PACKAGE__
, 'delnode', ['node']],
782 status
=> [ __PACKAGE__
, 'status' ],
783 nodes
=> [ __PACKAGE__
, 'nodes' ],
784 expected
=> [ __PACKAGE__
, 'expected', ['expected']],
785 updatecerts
=> [ __PACKAGE__
, 'updatecerts', []],
786 mtunnel
=> [ __PACKAGE__
, 'mtunnel', []],