]>
git.proxmox.com Git - pve-cluster.git/blob - data/PVE/CLI/pvecm.pm
1 package PVE
::CLI
::pvecm
;
13 use PVE
::Tools
qw(run_command);
20 use base
qw(PVE::CLIHandler);
22 $ENV{HOME
} = '/root'; # for ssh-copy-id
24 my $basedir = "/etc/pve";
25 my $clusterconf = "$basedir/corosync.conf";
26 my $libdir = "/var/lib/pve-cluster";
27 my $backupdir = "/var/lib/pve-cluster/backup";
28 my $dbfile = "$libdir/config.db";
29 my $authfile = "/etc/corosync/authkey";
33 print "backup old database\n";
41 ['gzip', '-', \
">${backupdir}/config-${ctime}.sql.gz"],
44 run_command
($cmd, 'errmsg' => "cannot backup old database\n");
50 foreach my $fn (<$backupdir/config-*.sql
.gz
>) {
51 if ($fn =~ m!/config-(\d+)\.sql.gz$!) {
52 push @bklist, [$fn, $1];
56 @bklist = sort { $b->[1] <=> $a->[1] } @bklist;
58 while (scalar (@bklist) >= $maxfiles) {
60 print "delete old backup '$d->[0]'\n";
65 __PACKAGE__-
>register_method ({
69 description
=> "Generate new cryptographic key for corosync.",
71 additionalProperties
=> 0,
75 description
=> "Output file name"
79 returns
=> { type
=> 'null' },
84 my $filename = $param->{filename
};
87 $> == 0 || die "Error: Authorization key must be generated as root user.\n";
88 my $dirname = dirname
($filename);
89 my $basename = basename
($filename);
91 die "key file '$filename' already exists\n" if -e
$filename;
93 File
::Path
::make_path
($dirname) if $dirname;
95 run_command
(['corosync-keygen', '-l', '-k', $filename]);
100 __PACKAGE__-
>register_method ({
104 description
=> "Generate new cluster configuration.",
106 additionalProperties
=> 0,
109 description
=> "The name of the cluster.",
110 type
=> 'string', format
=> 'pve-node',
115 description
=> "Node id for this node.",
121 description
=> "Number of votes for this node.",
126 type
=> 'string', format
=> 'ip',
127 description
=> "This specifies the network address the corosync ring 0".
128 " executive should bind to and defaults to the local IP address of the node.",
132 type
=> 'string', format
=> 'address',
133 description
=> "Hostname (or IP) of the corosync ring0 address of this node.".
134 " Defaults to the hostname of the node.",
138 type
=> 'string', format
=> 'ip',
139 description
=> "This specifies the network address the corosync ring 1".
140 " executive should bind to and is optional.",
144 type
=> 'string', format
=> 'address',
145 description
=> "Hostname (or IP) of the corosync ring1 address, this".
146 " needs an valid bindnet1_addr.",
151 returns
=> { type
=> 'null' },
156 -f
$clusterconf && die "cluster config '$clusterconf' already exists\n";
158 PVE
::Cluster
::setup_sshd_config
(1);
159 PVE
::Cluster
::setup_rootsshconfig
();
160 PVE
::Cluster
::setup_ssh_keys
();
162 -f
$authfile || __PACKAGE__-
>keygen({filename
=> $authfile});
164 -f
$authfile || die "no authentication key available\n";
166 my $clustername = $param->{clustername
};
168 $param->{nodeid
} = 1 if !$param->{nodeid
};
170 $param->{votes
} = 1 if !defined($param->{votes
});
172 my $nodename = PVE
::INotify
::nodename
();
174 my $local_ip_address = PVE
::Cluster
::remote_node_ip
($nodename);
176 $param->{bindnet0_addr
} = $local_ip_address
177 if !defined($param->{bindnet0_addr
});
179 $param->{ring0_addr
} = $nodename if !defined($param->{ring0_addr
});
181 die "Param bindnet1_addr and ring1_addr are dependend, use both or none!\n"
182 if (defined($param->{bindnet1_addr
}) != defined($param->{ring1_addr
}));
184 my $bind_is_ipv6 = Net
::IP
::ip_is_ipv6
($param->{bindnet0_addr
});
186 # use string as here-doc format distracts more
187 my $interfaces = "interface {\n ringnumber: 0\n" .
188 " bindnetaddr: $param->{bindnet0_addr}\n }";
190 my $ring_addresses = "ring0_addr: $param->{ring0_addr}" ;
192 # allow use of multiple rings (rrp) at cluster creation time
193 if ($param->{bindnet1_addr
}) {
194 die "IPv6 and IPv4 cannot be mixed, use one or the other!\n"
195 if Net
::IP
::ip_is_ipv6
($param->{bindnet1_addr
}) != $bind_is_ipv6;
197 $interfaces .= "\n interface {\n ringnumber: 1\n" .
198 " bindnetaddr: $param->{bindnet1_addr}\n }\n";
200 $interfaces .= "rrp_mode: passive\n"; # only passive is stable and tested
202 $ring_addresses .= "\n ring1_addr: $param->{ring1_addr}";
204 } elsif($param->{rrp_mode
} && $param->{rrp_mode
} ne 'none') {
206 warn "rrp_mode '$param->{rrp_mode}' useless when using only one".
207 " ring, using 'none' instead";
208 # corosync defaults to none if only one interface is configured
209 $param->{rrp_mode
} = undef;
213 # No, corosync cannot deduce this on its own
214 my $ipversion = $bind_is_ipv6 ?
'ipv6' : 'ipv4';
220 cluster_name: $clustername
222 ip_version: $ipversion
230 nodeid: $param->{nodeid}
231 quorum_votes: $param->{votes}
236 provider: corosync_votequorum
245 PVE
::Tools
::file_set_contents
($clusterconf, $config);
247 PVE
::Cluster
::ssh_merge_keys
();
249 PVE
::Cluster
::gen_pve_node_files
($nodename, $local_ip_address);
251 PVE
::Cluster
::ssh_merge_known_hosts
($nodename, $local_ip_address, 1);
253 run_command
('systemctl restart pve-cluster'); # restart
255 run_command
('systemctl restart corosync'); # restart
260 __PACKAGE__-
>register_method ({
264 description
=> "Adds a node to the cluster configuration.",
266 additionalProperties
=> 0,
268 node
=> PVE
::JSONSchema
::get_standard_option
('pve-node'),
271 description
=> "Node id for this node.",
277 description
=> "Number of votes for this node",
283 description
=> "Do not throw error if node already exists.",
287 type
=> 'string', format
=> 'address',
288 description
=> "Hostname (or IP) of the corosync ring0 address of this node.".
289 " Defaults to nodes hostname.",
293 type
=> 'string', format
=> 'address',
294 description
=> "Hostname (or IP) of the corosync ring1 address, this".
295 " needs an valid bindnet1_addr.",
300 returns
=> { type
=> 'null' },
305 if (!$param->{force
} && (-t STDIN
|| -t STDOUT
)) {
306 die "error: `addnode` should not get called interactively!\nUse ".
307 "`pvecm add <cluster-node>` to add a node to a cluster!\n";
310 PVE
::Cluster
::check_cfs_quorum
();
313 my $conf = PVE
::Cluster
::cfs_read_file
("corosync.conf");
314 my $nodelist = PVE
::Corosync
::nodelist
($conf);
315 my $totem_cfg = PVE
::Corosync
::totem_config
($conf);
317 my $name = $param->{node
};
319 # ensure we do not reuse an address, that can crash the whole cluster!
320 my $check_duplicate_addr = sub {
322 return if !defined($addr);
324 while (my ($k, $v) = each %$nodelist) {
325 next if $k eq $name; # allows re-adding a node if force is set
326 if ($v->{ring0_addr
} eq $addr || ($v->{ring1_addr
} && $v->{ring1_addr
} eq $addr)) {
327 die "corosync: address '$addr' already defined by node '$k'\n";
332 &$check_duplicate_addr($param->{ring0_addr
});
333 &$check_duplicate_addr($param->{ring1_addr
});
335 $param->{ring0_addr
} = $name if !$param->{ring0_addr
};
337 die "corosync: using 'ring1_addr' parameter needs a configured ring 1 interface!\n"
338 if $param->{ring1_addr
} && !defined($totem_cfg->{interface
}->{1});
340 die "corosync: ring 1 interface configured but 'ring1_addr' parameter not defined!\n"
341 if defined($totem_cfg->{interface
}->{1}) && !defined($param->{ring1_addr
});
343 if (defined(my $res = $nodelist->{$name})) {
344 $param->{nodeid
} = $res->{nodeid
} if !$param->{nodeid
};
345 $param->{votes
} = $res->{quorum_votes
} if !defined($param->{votes
});
347 if ($res->{quorum_votes
} == $param->{votes
} &&
348 $res->{nodeid
} == $param->{nodeid
}) {
349 print "node $name already defined\n";
350 if ($param->{force
}) {
356 die "can't add existing node\n";
358 } elsif (!$param->{nodeid
}) {
363 foreach my $v (values %$nodelist) {
364 if ($v->{nodeid
} eq $nodeid) {
373 $param->{nodeid
} = $nodeid;
376 $param->{votes
} = 1 if !defined($param->{votes
});
378 PVE
::Cluster
::gen_local_dirs
($name);
380 eval { PVE
::Cluster
::ssh_merge_keys
(); };
383 $nodelist->{$name} = {
384 ring0_addr
=> $param->{ring0_addr
},
385 nodeid
=> $param->{nodeid
},
388 $nodelist->{$name}->{ring1_addr
} = $param->{ring1_addr
} if $param->{ring1_addr
};
389 $nodelist->{$name}->{quorum_votes
} = $param->{votes
} if $param->{votes
};
391 PVE
::Corosync
::update_nodelist
($conf, $nodelist);
394 PVE
::Cluster
::cfs_lock_file
('corosync.conf', 10, $code);
401 __PACKAGE__-
>register_method ({
405 description
=> "Removes a node to the cluster configuration.",
407 additionalProperties
=> 0,
411 description
=> "Hostname or IP of the corosync ring0 address of this node.",
415 returns
=> { type
=> 'null' },
420 PVE
::Cluster
::check_cfs_quorum
();
423 my $conf = PVE
::Cluster
::cfs_read_file
("corosync.conf");
424 my $nodelist = PVE
::Corosync
::nodelist
($conf);
429 foreach my $tmp_node (keys %$nodelist) {
430 my $d = $nodelist->{$tmp_node};
431 my $ring0_addr = $d->{ring0_addr
};
432 my $ring1_addr = $d->{ring1_addr
};
433 if (($tmp_node eq $param->{node
}) ||
434 (defined($ring0_addr) && ($ring0_addr eq $param->{node
})) ||
435 (defined($ring1_addr) && ($ring1_addr eq $param->{node
}))) {
437 $nodeid = $d->{nodeid
};
442 die "Node/IP: $param->{node} is not a known host of the cluster.\n"
445 my $our_nodename = PVE
::INotify
::nodename
();
446 die "Cannot delete myself from cluster!\n" if $node eq $our_nodename;
448 delete $nodelist->{$node};
450 PVE
::Corosync
::update_nodelist
($conf, $nodelist);
452 run_command
(['corosync-cfgtool','-k', $nodeid]) if defined($nodeid);
455 PVE
::Cluster
::cfs_lock_file
('corosync.conf', 10, $code);
461 __PACKAGE__-
>register_method ({
465 description
=> "Adds the current node to an existing cluster.",
467 additionalProperties
=> 0,
471 description
=> "Hostname (or IP) of an existing cluster member."
475 description
=> "Node id for this node.",
481 description
=> "Number of votes for this node",
487 description
=> "Do not throw error if node already exists.",
491 type
=> 'string', format
=> 'address',
492 description
=> "Hostname (or IP) of the corosync ring0 address of this node.".
493 " Defaults to nodes hostname.",
497 type
=> 'string', format
=> 'address',
498 description
=> "Hostname (or IP) of the corosync ring1 address, this".
499 " needs an valid configured ring 1 interface in the cluster.",
504 returns
=> { type
=> 'null' },
509 my $nodename = PVE
::INotify
::nodename
();
511 PVE
::Cluster
::setup_sshd_config
(1);
512 PVE
::Cluster
::setup_rootsshconfig
();
513 PVE
::Cluster
::setup_ssh_keys
();
515 my $host = $param->{hostname
};
517 my ($errors, $warnings) = ('', '');
520 my ($msg, $suppress) = @_;
523 $warnings .= "* $msg\n";
525 $errors .= "* $msg\n";
529 if (!$param->{force
}) {
532 &$error("authentication key '$authfile' already exists", $param->{force
});
535 if (-f
$clusterconf) {
536 &$error("cluster config '$clusterconf' already exists", $param->{force
});
539 my $vmlist = PVE
::Cluster
::get_vmlist
();
540 if ($vmlist && $vmlist->{ids
} && scalar(keys %{$vmlist->{ids
}})) {
541 &$error("this host already contains virtual guests", $param->{force
});
544 if (system("corosync-quorumtool -l >/dev/null 2>&1") == 0) {
545 &$error("corosync is already running, is this node already in a cluster?!", $param->{force
});
549 # check if corosync ring IPs are configured on the current nodes interfaces
553 if (!PVE
::JSONSchema
::pve_verify_ip
($ip, 1)) {
555 eval { $ip = PVE
::Network
::get_ip_from_hostname
($host); };
557 &$error("cannot use '$host': $@\n") ;
562 my $cidr = (Net
::IP
::ip_is_ipv6
($ip)) ?
"$ip/128" : "$ip/32";
563 my $configured_ips = PVE
::Network
::get_local_ip_from_cidr
($cidr);
565 &$error("cannot use IP '$ip', it must be configured exactly once on local node!\n")
566 if (scalar(@$configured_ips) != 1);
570 &$check_ip($param->{ring0_addr
});
571 &$check_ip($param->{ring1_addr
});
573 warn "warning, ignore the following errors:\n$warnings" if $warnings;
574 die "detected the following error(s):\n$errors" if $errors;
576 # make sure known_hosts is on local filesystem
577 PVE
::Cluster
::ssh_unmerge_known_hosts
();
579 my $cmd = ['ssh-copy-id', '-i', '/root/.ssh/id_rsa', "root\@$host"];
580 run_command
($cmd, 'outfunc' => sub {}, 'errfunc' => sub {},
581 'errmsg' => "unable to copy ssh ID");
583 $cmd = ['ssh', $host, '-o', 'BatchMode=yes',
584 'pvecm', 'addnode', $nodename, '--force', 1];
586 push @$cmd, '--nodeid', $param->{nodeid
} if $param->{nodeid
};
588 push @$cmd, '--votes', $param->{votes
} if defined($param->{votes
});
590 push @$cmd, '--ring0_addr', $param->{ring0_addr
} if defined($param->{ring0_addr
});
592 push @$cmd, '--ring1_addr', $param->{ring1_addr
} if defined($param->{ring1_addr
});
594 if (system (@$cmd) != 0) {
595 my $cmdtxt = join (' ', @$cmd);
596 die "unable to add node: command failed ($cmdtxt)\n";
599 my $tmpdir = "$libdir/.pvecm_add.tmp.$$";
603 print "copy corosync auth key\n";
604 $cmd = ['rsync', '--rsh=ssh -l root -o BatchMode=yes', '-lpgoq',
605 "[$host]:$authfile $clusterconf", $tmpdir];
607 system(@$cmd) == 0 || die "can't rsync data from host '$host'\n";
609 mkdir "/etc/corosync";
610 my $confbase = basename
($clusterconf);
612 $cmd = "cp '$tmpdir/$confbase' '/etc/corosync/$confbase'";
613 system($cmd) == 0 || die "can't copy cluster configuration\n";
615 my $keybase = basename
($authfile);
616 system ("cp '$tmpdir/$keybase' '$authfile'") == 0 ||
617 die "can't copy '$tmpdir/$keybase' to '$authfile'\n";
619 print "stopping pve-cluster service\n";
621 system("umount $basedir -f >/dev/null 2>&1");
622 system("systemctl stop pve-cluster") == 0 ||
623 die "can't stop pve-cluster service\n";
629 system("systemctl start pve-cluster") == 0 ||
630 die "starting pve-cluster failed\n";
632 system("systemctl start corosync");
636 while (!PVE
::Cluster
::check_cfs_quorum
(1)) {
638 print "waiting for quorum...";
644 print "OK\n" if !$printqmsg;
646 my $local_ip_address = PVE
::Cluster
::remote_node_ip
($nodename);
648 print "generating node certificates\n";
649 PVE
::Cluster
::gen_pve_node_files
($nodename, $local_ip_address);
651 print "merge known_hosts file\n";
652 PVE
::Cluster
::ssh_merge_known_hosts
($nodename, $local_ip_address, 1);
654 print "restart services\n";
655 # restart pvedaemon (changed certs)
656 system("systemctl restart pvedaemon");
657 # restart pveproxy (changed certs)
658 system("systemctl restart pveproxy");
660 print "successfully added node '$nodename' to cluster.\n";
671 __PACKAGE__-
>register_method ({
675 description
=> "Displays the local view of the cluster status.",
677 additionalProperties
=> 0,
680 returns
=> { type
=> 'null' },
685 PVE
::Corosync
::check_conf_exists
();
687 my $cmd = ['corosync-quorumtool', '-siH'];
691 exit (-1); # should not be reached
694 __PACKAGE__-
>register_method ({
698 description
=> "Displays the local view of the cluster nodes.",
700 additionalProperties
=> 0,
703 returns
=> { type
=> 'null' },
708 PVE
::Corosync
::check_conf_exists
();
710 my $cmd = ['corosync-quorumtool', '-l'];
714 exit (-1); # should not be reached
717 __PACKAGE__-
>register_method ({
721 description
=> "Tells corosync a new value of expected votes.",
723 additionalProperties
=> 0,
727 description
=> "Expected votes",
732 returns
=> { type
=> 'null' },
737 PVE
::Corosync
::check_conf_exists
();
739 my $cmd = ['corosync-quorumtool', '-e', $param->{expected
}];
743 exit (-1); # should not be reached
747 __PACKAGE__-
>register_method ({
748 name
=> 'updatecerts',
749 path
=> 'updatecerts',
751 description
=> "Update node certificates (and generate all needed files/directories).",
753 additionalProperties
=> 0,
756 description
=> "Force generation of new SSL certifate.",
761 description
=> "Ignore errors (i.e. when cluster has no quorum).",
767 returns
=> { type
=> 'null' },
771 PVE
::Cluster
::setup_sshd_config
(0);
772 PVE
::Cluster
::setup_rootsshconfig
();
774 PVE
::Cluster
::gen_pve_vzdump_symlink
();
776 if (!PVE
::Cluster
::check_cfs_quorum
(1)) {
777 return undef if $param->{silent
};
778 die "no quorum - unable to update files\n";
781 PVE
::Cluster
::setup_ssh_keys
();
783 my $nodename = PVE
::INotify
::nodename
();
785 my $local_ip_address = PVE
::Cluster
::remote_node_ip
($nodename);
787 PVE
::Cluster
::gen_pve_node_files
($nodename, $local_ip_address, $param->{force
});
788 PVE
::Cluster
::ssh_merge_keys
();
789 PVE
::Cluster
::ssh_merge_known_hosts
($nodename, $local_ip_address);
790 PVE
::Cluster
::gen_pve_vzdump_files
();
795 __PACKAGE__-
>register_method ({
799 description
=> "Used by VM/CT migration - do not use manually.",
801 additionalProperties
=> 0,
803 get_migration_ip
=> {
806 description
=> 'return the migration IP, if configured',
809 migration_network
=> {
812 description
=> 'the migration network used to detect the local migration IP',
817 description
=> 'Run a command with a tcp socket as standard input.'
818 .' The IP address and port are printed via this'
819 ." command's stdandard output first, each on a separate line.",
822 'extra-args' => PVE
::JSONSchema
::get_standard_option
('extra-args'),
825 returns
=> { type
=> 'null'},
829 if (!PVE
::Cluster
::check_cfs_quorum
(1)) {
834 my $network = $param->{migration_network
};
835 if ($param->{get_migration_ip
}) {
836 die "cannot use --run-command with --get_migration_ip\n"
837 if $param->{'run-command'};
838 if (my $ip = PVE
::Cluster
::get_local_migration_ip
($network)) {
843 # do not keep tunnel open when asked for migration ip
847 if ($param->{'run-command'}) {
848 my $cmd = $param->{'extra-args'};
849 die "missing command\n"
850 if !$cmd || !scalar(@$cmd);
852 # Get an ip address to listen on, and find a free migration port
854 if (defined($network)) {
855 $ip = PVE
::Cluster
::get_local_migration_ip
($network)
856 or die "failed to get migration IP address to listen on\n";
857 $family = Net
::IP
::ip_is_ipv6
($ip) ? AF_INET6
: AF_INET
;
859 my $nodename = PVE
::INotify
::nodename
();
860 ($ip, $family) = PVE
::Network
::get_ip_from_hostname
($nodename, 0);
862 my $port = PVE
::Tools
::next_migrate_port
($family, $ip);
864 PVE
::Tools
::pipe_socket_to_command
($cmd, $ip, $port);
868 print "tunnel online\n";
871 while (my $line = <>) {
873 last if $line =~ m/^quit$/;
881 keygen
=> [ __PACKAGE__
, 'keygen', ['filename']],
882 create
=> [ __PACKAGE__
, 'create', ['clustername']],
883 add
=> [ __PACKAGE__
, 'add', ['hostname']],
884 addnode
=> [ __PACKAGE__
, 'addnode', ['node']],
885 delnode
=> [ __PACKAGE__
, 'delnode', ['node']],
886 status
=> [ __PACKAGE__
, 'status' ],
887 nodes
=> [ __PACKAGE__
, 'nodes' ],
888 expected
=> [ __PACKAGE__
, 'expected', ['expected']],
889 updatecerts
=> [ __PACKAGE__
, 'updatecerts', []],
890 mtunnel
=> [ __PACKAGE__
, 'mtunnel', ['extra-args']],