]>
git.proxmox.com Git - pve-cluster.git/blob - data/PVE/CLI/pvecm.pm
1 package PVE
::CLI
::pvecm
;
9 use PVE
::Tools
qw(run_command);
14 use PVE
::API2
::ClusterConfig
;
17 use base
qw(PVE::CLIHandler);
19 $ENV{HOME
} = '/root'; # for ssh-copy-id
21 my $basedir = "/etc/pve";
22 my $clusterconf = "$basedir/corosync.conf";
23 my $libdir = "/var/lib/pve-cluster";
24 my $backupdir = "/var/lib/pve-cluster/backup";
25 my $dbfile = "$libdir/config.db";
26 my $authfile = "/etc/corosync/authkey";
30 print "backup old database\n";
38 ['gzip', '-', \
">${backupdir}/config-${ctime}.sql.gz"],
41 run_command
($cmd, 'errmsg' => "cannot backup old database\n");
47 foreach my $fn (<$backupdir/config-*.sql
.gz
>) {
48 if ($fn =~ m!/config-(\d+)\.sql.gz$!) {
49 push @bklist, [$fn, $1];
53 @bklist = sort { $b->[1] <=> $a->[1] } @bklist;
55 while (scalar (@bklist) >= $maxfiles) {
57 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);
88 die "key file '$filename' already exists\n" if -e
$filename;
90 File
::Path
::make_path
($dirname) if $dirname;
92 run_command
(['corosync-keygen', '-l', '-k', $filename]);
97 __PACKAGE__-
>register_method ({
101 description
=> "Generate new cluster configuration.",
103 additionalProperties
=> 0,
106 description
=> "The name of the cluster.",
107 type
=> 'string', format
=> 'pve-node',
112 description
=> "Node id for this node.",
118 description
=> "Number of votes for this node.",
123 type
=> 'string', format
=> 'ip',
124 description
=> "This specifies the network address the corosync ring 0".
125 " executive should bind to and defaults to the local IP address of the node.",
129 type
=> 'string', format
=> 'address',
130 description
=> "Hostname (or IP) of the corosync ring0 address of this node.".
131 " Defaults to the hostname of the node.",
135 type
=> 'string', format
=> 'ip',
136 description
=> "This specifies the network address the corosync ring 1".
137 " executive should bind to and is optional.",
141 type
=> 'string', format
=> 'address',
142 description
=> "Hostname (or IP) of the corosync ring1 address, this".
143 " needs an valid bindnet1_addr.",
148 returns
=> { type
=> 'null' },
153 -f
$clusterconf && die "cluster config '$clusterconf' already exists\n";
155 PVE
::Cluster
::setup_sshd_config
(1);
156 PVE
::Cluster
::setup_rootsshconfig
();
157 PVE
::Cluster
::setup_ssh_keys
();
159 -f
$authfile || __PACKAGE__-
>keygen({filename
=> $authfile});
161 -f
$authfile || die "no authentication key available\n";
163 my $clustername = $param->{clustername
};
165 $param->{nodeid
} = 1 if !$param->{nodeid
};
167 $param->{votes
} = 1 if !defined($param->{votes
});
169 my $nodename = PVE
::INotify
::nodename
();
171 my $local_ip_address = PVE
::Cluster
::remote_node_ip
($nodename);
173 $param->{bindnet0_addr
} = $local_ip_address
174 if !defined($param->{bindnet0_addr
});
176 $param->{ring0_addr
} = $nodename if !defined($param->{ring0_addr
});
178 die "Param bindnet1_addr and ring1_addr are dependend, use both or none!\n"
179 if (defined($param->{bindnet1_addr
}) != defined($param->{ring1_addr
}));
181 my $bind_is_ipv6 = Net
::IP
::ip_is_ipv6
($param->{bindnet0_addr
});
183 # use string as here-doc format distracts more
184 my $interfaces = "interface {\n ringnumber: 0\n" .
185 " bindnetaddr: $param->{bindnet0_addr}\n }";
187 my $ring_addresses = "ring0_addr: $param->{ring0_addr}" ;
189 # allow use of multiple rings (rrp) at cluster creation time
190 if ($param->{bindnet1_addr
}) {
191 die "IPv6 and IPv4 cannot be mixed, use one or the other!\n"
192 if Net
::IP
::ip_is_ipv6
($param->{bindnet1_addr
}) != $bind_is_ipv6;
194 $interfaces .= "\n interface {\n ringnumber: 1\n" .
195 " bindnetaddr: $param->{bindnet1_addr}\n }\n";
197 $interfaces .= "rrp_mode: passive\n"; # only passive is stable and tested
199 $ring_addresses .= "\n ring1_addr: $param->{ring1_addr}";
202 # No, corosync cannot deduce this on its own
203 my $ipversion = $bind_is_ipv6 ?
'ipv6' : 'ipv4';
209 cluster_name: $clustername
211 ip_version: $ipversion
219 nodeid: $param->{nodeid}
220 quorum_votes: $param->{votes}
225 provider: corosync_votequorum
234 PVE
::Tools
::file_set_contents
($clusterconf, $config);
236 PVE
::Cluster
::ssh_merge_keys
();
238 PVE
::Cluster
::gen_pve_node_files
($nodename, $local_ip_address);
240 PVE
::Cluster
::ssh_merge_known_hosts
($nodename, $local_ip_address, 1);
242 run_command
('systemctl restart pve-cluster'); # restart
244 run_command
('systemctl restart corosync'); # restart
249 __PACKAGE__-
>register_method ({
253 description
=> "Adds the current node to an existing cluster.",
255 additionalProperties
=> 0,
259 description
=> "Hostname (or IP) of an existing cluster member."
263 description
=> "Node id for this node.",
269 description
=> "Number of votes for this node",
275 description
=> "Do not throw error if node already exists.",
279 type
=> 'string', format
=> 'address',
280 description
=> "Hostname (or IP) of the corosync ring0 address of this node.".
281 " Defaults to nodes hostname.",
285 type
=> 'string', format
=> 'address',
286 description
=> "Hostname (or IP) of the corosync ring1 address, this".
287 " needs an valid configured ring 1 interface in the cluster.",
292 returns
=> { type
=> 'null' },
297 my $nodename = PVE
::INotify
::nodename
();
299 PVE
::Cluster
::setup_sshd_config
();
300 PVE
::Cluster
::setup_rootsshconfig
();
301 PVE
::Cluster
::setup_ssh_keys
();
303 PVE
::Cluster
::assert_joinable
($param->{ring0_addr
}, $param->{ring1_addr
}, $param->{force
});
305 my $host = $param->{hostname
};
307 # make sure known_hosts is on local filesystem
308 PVE
::Cluster
::ssh_unmerge_known_hosts
();
310 my $cmd = ['ssh-copy-id', '-i', '/root/.ssh/id_rsa', "root\@$host"];
311 run_command
($cmd, 'outfunc' => sub {}, 'errfunc' => sub {},
312 'errmsg' => "unable to copy ssh ID");
314 $cmd = ['ssh', $host, '-o', 'BatchMode=yes',
315 'pvecm', 'addnode', $nodename, '--force', 1];
317 push @$cmd, '--nodeid', $param->{nodeid
} if $param->{nodeid
};
318 push @$cmd, '--votes', $param->{votes
} if defined($param->{votes
});
319 push @$cmd, '--ring0_addr', $param->{ring0_addr
} if defined($param->{ring0_addr
});
320 push @$cmd, '--ring1_addr', $param->{ring1_addr
} if defined($param->{ring1_addr
});
322 if (system (@$cmd) != 0) {
323 my $cmdtxt = join (' ', @$cmd);
324 die "unable to add node: command failed ($cmdtxt)\n";
327 my $tmpdir = "$libdir/.pvecm_add.tmp.$$";
331 print "copy corosync auth key\n";
332 $cmd = ['rsync', '--rsh=ssh -l root -o BatchMode=yes', '-lpgoq',
333 "[$host]:$authfile $clusterconf", $tmpdir];
335 system(@$cmd) == 0 || die "can't rsync data from host '$host'\n";
337 mkdir "/etc/corosync";
338 my $confbase = basename
($clusterconf);
340 $cmd = "cp '$tmpdir/$confbase' '/etc/corosync/$confbase'";
341 system($cmd) == 0 || die "can't copy cluster configuration\n";
343 my $keybase = basename
($authfile);
344 system ("cp '$tmpdir/$keybase' '$authfile'") == 0 ||
345 die "can't copy '$tmpdir/$keybase' to '$authfile'\n";
347 print "stopping pve-cluster service\n";
349 system("umount $basedir -f >/dev/null 2>&1");
350 system("systemctl stop pve-cluster") == 0 ||
351 die "can't stop pve-cluster service\n";
357 system("systemctl start pve-cluster") == 0 ||
358 die "starting pve-cluster failed\n";
360 system("systemctl start corosync");
364 while (!PVE
::Cluster
::check_cfs_quorum
(1)) {
366 print "waiting for quorum...";
372 print "OK\n" if !$printqmsg;
374 my $local_ip_address = PVE
::Cluster
::remote_node_ip
($nodename);
376 print "generating node certificates\n";
377 PVE
::Cluster
::gen_pve_node_files
($nodename, $local_ip_address);
379 print "merge known_hosts file\n";
380 PVE
::Cluster
::ssh_merge_known_hosts
($nodename, $local_ip_address, 1);
382 print "restart services\n";
383 # restart pvedaemon (changed certs)
384 system("systemctl restart pvedaemon");
385 # restart pveproxy (changed certs)
386 system("systemctl restart pveproxy");
388 print "successfully added node '$nodename' to cluster.\n";
399 __PACKAGE__-
>register_method ({
403 description
=> "Displays the local view of the cluster status.",
405 additionalProperties
=> 0,
408 returns
=> { type
=> 'null' },
413 PVE
::Corosync
::check_conf_exists
();
415 my $cmd = ['corosync-quorumtool', '-siH'];
419 exit (-1); # should not be reached
422 __PACKAGE__-
>register_method ({
426 description
=> "Displays the local view of the cluster nodes.",
428 additionalProperties
=> 0,
431 returns
=> { type
=> 'null' },
436 PVE
::Corosync
::check_conf_exists
();
438 my $cmd = ['corosync-quorumtool', '-l'];
442 exit (-1); # should not be reached
445 __PACKAGE__-
>register_method ({
449 description
=> "Tells corosync a new value of expected votes.",
451 additionalProperties
=> 0,
455 description
=> "Expected votes",
460 returns
=> { type
=> 'null' },
465 PVE
::Corosync
::check_conf_exists
();
467 my $cmd = ['corosync-quorumtool', '-e', $param->{expected
}];
471 exit (-1); # should not be reached
475 __PACKAGE__-
>register_method ({
476 name
=> 'updatecerts',
477 path
=> 'updatecerts',
479 description
=> "Update node certificates (and generate all needed files/directories).",
481 additionalProperties
=> 0,
484 description
=> "Force generation of new SSL certifate.",
489 description
=> "Ignore errors (i.e. when cluster has no quorum).",
495 returns
=> { type
=> 'null' },
499 PVE
::Cluster
::setup_rootsshconfig
();
501 PVE
::Cluster
::gen_pve_vzdump_symlink
();
503 if (!PVE
::Cluster
::check_cfs_quorum
(1)) {
504 return undef if $param->{silent
};
505 die "no quorum - unable to update files\n";
508 PVE
::Cluster
::setup_ssh_keys
();
510 my $nodename = PVE
::INotify
::nodename
();
512 my $local_ip_address = PVE
::Cluster
::remote_node_ip
($nodename);
514 PVE
::Cluster
::gen_pve_node_files
($nodename, $local_ip_address, $param->{force
});
515 PVE
::Cluster
::ssh_merge_keys
();
516 PVE
::Cluster
::ssh_merge_known_hosts
($nodename, $local_ip_address);
517 PVE
::Cluster
::gen_pve_vzdump_files
();
522 __PACKAGE__-
>register_method ({
526 description
=> "Used by VM/CT migration - do not use manually.",
528 additionalProperties
=> 0,
530 get_migration_ip
=> {
533 description
=> 'return the migration IP, if configured',
536 migration_network
=> {
539 description
=> 'the migration network used to detect the local migration IP',
544 description
=> 'Run a command with a tcp socket as standard input.'
545 .' The IP address and port are printed via this'
546 ." command's stdandard output first, each on a separate line.",
549 'extra-args' => PVE
::JSONSchema
::get_standard_option
('extra-args'),
552 returns
=> { type
=> 'null'},
556 if (!PVE
::Cluster
::check_cfs_quorum
(1)) {
561 my $network = $param->{migration_network
};
562 if ($param->{get_migration_ip
}) {
563 die "cannot use --run-command with --get_migration_ip\n"
564 if $param->{'run-command'};
565 if (my $ip = PVE
::Cluster
::get_local_migration_ip
($network)) {
570 # do not keep tunnel open when asked for migration ip
574 if ($param->{'run-command'}) {
575 my $cmd = $param->{'extra-args'};
576 die "missing command\n"
577 if !$cmd || !scalar(@$cmd);
579 # Get an ip address to listen on, and find a free migration port
581 if (defined($network)) {
582 $ip = PVE
::Cluster
::get_local_migration_ip
($network)
583 or die "failed to get migration IP address to listen on\n";
584 $family = PVE
::Tools
::get_host_address_family
($ip);
586 my $nodename = PVE
::INotify
::nodename
();
587 ($ip, $family) = PVE
::Network
::get_ip_from_hostname
($nodename, 0);
589 my $port = PVE
::Tools
::next_migrate_port
($family, $ip);
591 PVE
::Tools
::pipe_socket_to_command
($cmd, $ip, $port);
595 print "tunnel online\n";
598 while (my $line = <STDIN
>) {
600 last if $line =~ m/^quit$/;
608 keygen
=> [ __PACKAGE__
, 'keygen', ['filename']],
609 create
=> [ __PACKAGE__
, 'create', ['clustername']],
610 add
=> [ __PACKAGE__
, 'add', ['hostname']],
611 addnode
=> [ 'PVE::API2::ClusterConfig', 'addnode', ['node']],
612 delnode
=> [ 'PVE::API2::ClusterConfig', 'delnode', ['node']],
613 status
=> [ __PACKAGE__
, 'status' ],
614 nodes
=> [ __PACKAGE__
, 'nodes' ],
615 expected
=> [ __PACKAGE__
, 'expected', ['expected']],
616 updatecerts
=> [ __PACKAGE__
, 'updatecerts', []],
617 mtunnel
=> [ __PACKAGE__
, 'mtunnel', ['extra-args']],