]>
git.proxmox.com Git - pve-cluster.git/blob - data/PVE/pvecm
8 use Data
::Dumper
; # fixme: remove
15 use base
qw(PVE::CLIHandler);
17 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
19 die "please run as root\n" if $> != 0;
21 my $nodename = PVE
::INotify
::nodename
();
22 # trigger check that we have resolvable name
23 my $local_ip_address = PVE
::Cluster
::remote_node_ip
($nodename);
25 my $basedir = "/etc/pve";
26 my $clusterconf = "$basedir/cluster.conf";
27 my $libdir = "/var/lib/pve-cluster";
28 my $backupdir = "/var/lib/pve-cluster/backup";
29 my $dbfile = "$libdir/config.db";
30 my $authfile = "$libdir/corosync.authkey";
35 print "backup old database\n";
40 my $cmd = "echo '.dump' |";
41 $cmd .= "sqlite3 '$dbfile' |";
42 $cmd .= "gzip - >'${backupdir}/config-${ctime}.sql.gz'";
45 die "can't backup old database: $!\n";
51 foreach my $fn (<$backupdir/config-*.sql
.gz
>) {
52 if ($fn =~ m!/config-(\d+)\.sql.gz$!) {
53 push @bklist, [$fn, $1];
57 @bklist = sort { $b->[1] <=> $a->[1] } @bklist;
59 while (scalar (@bklist) >= $maxfiles) {
61 print "delete old backup '$d->[0]'\n";
66 __PACKAGE__-
>register_method ({
70 description
=> "Generate new cryptographic key for corosync.",
72 additionalProperties
=> 0,
76 description
=> "Output file name"
80 returns
=> { type
=> 'null' },
85 my $filename = $param->{filename
};
88 $> == 0 || die "Error: Authorization key must be generated as root user.\n";
89 my $dirname = dirname
($filename);
90 my $basename = basename
($filename);
92 File
::Path
::make_path
($dirname) if $dirname;
94 my $fh = IO
::File-
>new ("/dev/urandom", 'r') ||
95 die "can't open /dev/urandom - $!\n";
101 while ($bytes < $keysize) {
102 my $rb = sysread($fh, $key, $keysize - $bytes, $bytes);
103 ($rb > 0) || die "Could not read /dev/urandom - $!\n";
109 my $tmpfn = "$filename.tmp.$$";
110 $fh = IO
::File-
>new ($tmpfn, O_CREAT
|O_WRONLY
, 0400);
111 die "can't open temporary file '$tmpfn' - $!\n" if !$fh;
115 (($wb = syswrite($fh, $key)) == $keysize) ||
116 die "writing key failed - short write $wb\n;"
127 if (!rename($tmpfn, $filename)) {
130 die "rename $tmpfn to $filename failed - $err\n";
136 __PACKAGE__-
>register_method ({
140 description
=> "Generate new cluster configuration.",
142 additionalProperties
=> 0,
145 description
=> "The name of the cluster.",
146 type
=> 'string', format
=> 'pve-node',
151 description
=> "Node id for this node.",
157 description
=> "Number of votes for this node",
163 returns
=> { type
=> 'null' },
168 -f
$clusterconf && die "cluster config '$clusterconf' already exists\n";
170 PVE
::Cluster
::setup_ssh_keys
();
172 -f
$authfile || __PACKAGE__-
>keygen({filename
=> $authfile});
174 -f
$authfile || die "no authentication key available\n";
176 my $clustername = $param->{clustername
};
178 $param->{nodeid
} = 1 if !$param->{nodeid
};
180 $param->{votes
} = 1 if !defined($param->{votes
});
183 <?xml version="1.0"?>
184 <cluster name="$clustername" config_version="1">
186 <cman keyfile="$authfile">
190 <clusternode name="${nodename}" votes="$param->{votes}" nodeid="$param->{nodeid}"/>
196 PVE
::Tools
::file_set_contents
($clusterconf, $config);
198 PVE
::Cluster
::ssh_merge_keys
();
200 PVE
::Cluster
::gen_pve_node_files
($nodename, $local_ip_address);
202 PVE
::Cluster
::ssh_merge_known_hosts
($nodename, $local_ip_address, 1);
204 PVE
::Tools
::run_command
('/etc/init.d/pve-cluster restart'); # restart
206 # that cman init script returns strange values - simply ignore for now
207 system('/etc/init.d/cman start');
210 system('/etc/init.d/clvm start');
215 __PACKAGE__-
>register_method ({
219 description
=> "Adds a node to the cluster configuration.",
221 additionalProperties
=> 0,
223 node
=> PVE
::JSONSchema
::get_standard_option
('pve-node'),
226 description
=> "Node id for this node.",
232 description
=> "Number of votes for this node",
238 description
=> "Do not throw error if node already exists.",
243 returns
=> { type
=> 'null' },
248 PVE
::Cluster
::check_cfs_quorum
();
252 my $name = $param->{node
};
254 if (defined(my $res = $lst->{$name})) {
255 $param->{nodeid
} = $res->{nodeid
} if !$param->{nodeid
};
256 $param->{votes
} = $res->{votes
} if !defined($param->{votes
});
258 if ($res->{votes
} == $param->{votes
} &&
259 $res->{nodeid
} == $param->{nodeid
}) {
260 print "node $name already defined\n";
261 if ($param->{force
}) {
267 } elsif (!$param->{nodeid
}) {
272 foreach my $v (values %$lst) {
273 if ($v->{nodeid
} eq $nodeid) {
282 $param->{nodeid
} = $nodeid;
285 $param->{votes
} = 1 if !defined($param->{votes
});
287 PVE
::Cluster
::gen_local_dirs
($name);
289 eval { PVE
::Cluster
::ssh_merge_keys
(); };
292 my $cmd = ['ccs_tool', 'addnode', '-c', $clusterconf];
294 # NOTE: cman does not like votes="0"
295 if ($param->{votes
}) {
296 push @$cmd, '-v', $param->{votes
};
299 push @$cmd, '-n', $param->{nodeid
}, $name;
301 system(@$cmd) == 0 || exit(-1);
307 __PACKAGE__-
>register_method ({
311 description
=> "Removes a node to the cluster configuration.",
313 additionalProperties
=> 0,
315 node
=> PVE
::JSONSchema
::get_standard_option
('pve-node'),
318 returns
=> { type
=> 'null' },
323 PVE
::Cluster
::check_cfs_quorum
();
325 my $cmd = ['ccs_tool', 'delnode', '-c', $clusterconf, $param->{node
}];
329 exit (-1); # should not be reached
332 __PACKAGE__-
>register_method ({
336 description
=> "Adds the current node to an existing cluster.",
338 additionalProperties
=> 0,
342 description
=> "Hostname (or IP) of an existing cluster member."
346 description
=> "Node id for this node.",
352 description
=> "Number of votes for this node",
358 description
=> "Do not throw error if node already exists.",
363 returns
=> { type
=> 'null' },
368 PVE
::Cluster
::setup_ssh_keys
();
370 my $host = $param->{hostname
};
372 if (!$param->{force
}) {
375 die "authentication key already exists\n";
378 if (-f
$clusterconf) {
379 die "cluster config '$clusterconf' already exists\n";
382 my $vmlist = PVE
::Cluster
::get_vmlist
();
383 if ($vmlist && $vmlist->{ids
} && scalar(keys %{$vmlist->{ids
}})) {
384 die "this host already contains virtual machines - please remove the first\n";
387 if (system("cman_tool status >/dev/null 2>&1") == 0) {
388 die "cman is already running\n";
392 # make sure known_hosts is on local filesystem
393 PVE
::Cluster
::ssh_unmerge_known_hosts
();
395 my $cmd = "ssh-copy-id 'root\@$host' >/dev/null 2>&1";
396 system ($cmd) == 0 ||
397 die "unable to copy ssh ID\n";
399 $cmd = ['ssh', $host, '-o', 'BatchMode=yes',
400 'pvecm', 'addnode', $nodename, '--force', 1];
402 push @$cmd, '--nodeid', $param->{nodeid
} if $param->{nodeid
};
404 push @$cmd, '--votes', $param->{votes
} if defined($param->{votes
});
406 if (system (@$cmd) != 0) {
407 my $cmdtxt = join (' ', @$cmd);
408 die "unable to add node: command failed ($cmdtxt)\n";
411 my $tmpdir = "$libdir/.pvecm_add.tmp.$$";
415 print "copy corosync auth key\n";
416 $cmd = ['rsync', '--rsh=ssh -l root -o BatchMode=yes', '-lpgoq',
417 "$host:$authfile $clusterconf", $tmpdir];
419 system(@$cmd) == 0 || die "can't rsync data from host '$host'\n";
421 mkdir "/etc/cluster";
422 my $confbase = basename
($clusterconf);
424 $cmd = "cp '$tmpdir/$confbase' '/etc/cluster/$confbase'";
425 system($cmd) == 0 || die "can't copy cluster configuration\n";
427 my $keybase = basename
($authfile);
428 system ("cp '$tmpdir/$keybase' '$authfile'") == 0 ||
429 die "can't copy '$tmpdir/$keybase' to '$authfile'\n";
431 print "stopping pve-cluster service\n";
433 system("umount $basedir -f >/dev/null 2>&1");
434 system("/etc/init.d/pve-cluster stop") == 0 ||
435 die "can't stop pve-cluster service\n";
441 system("/etc/init.d/pve-cluster start") == 0 ||
442 die "starting pve-cluster failed\n";
444 system("/etc/init.d/cman start");
448 while (!PVE
::Cluster
::check_cfs_quorum
(1)) {
450 print "waiting for quorum...";
456 print "OK\n" if !$printqmsg;
458 system("/etc/init.d/clvm start");
460 print "generating node certificates\n";
461 PVE
::Cluster
::gen_pve_node_files
($nodename, $local_ip_address);
463 print "merge known_hosts file\n";
464 PVE
::Cluster
::ssh_merge_known_hosts
($nodename, $local_ip_address, 1);
466 print "restart services\n";
467 # restart pvedaemon (changed certs)
468 system("/etc/init.d/pvedaemon restart");
469 # restart apache (changed certs)
470 system("/etc/init.d/apache2 restart");
472 print "successfully added node '$nodename' to cluster.\n";
483 __PACKAGE__-
>register_method ({
487 description
=> "Displays the local view of the cluster status.",
489 additionalProperties
=> 0,
492 returns
=> { type
=> 'null' },
497 my $cmd = ['cman_tool', 'status'];
501 exit (-1); # should not be reached
504 __PACKAGE__-
>register_method ({
508 description
=> "Displays the local view of the cluster nodes.",
510 additionalProperties
=> 0,
513 returns
=> { type
=> 'null' },
518 my $cmd = ['cman_tool', 'nodes'];
522 exit (-1); # should not be reached
525 __PACKAGE__-
>register_method ({
529 description
=> "Tells CMAN a new value of expected votes.",
531 additionalProperties
=> 0,
535 description
=> "Expected votes",
540 returns
=> { type
=> 'null' },
545 my $cmd = ['cman_tool', 'expected', '-e', $param->{expected
}];
549 exit (-1); # should not be reached
557 my $cmd = ['ccs_tool', 'lsnode', '-c', $clusterconf];
563 if ($line =~ m/^Nodename\s+Votes\s+Nodeid\s/i) {
568 if ($line =~ m/^(\S+)\s+(\d+)\s+(\d+)\s/) {
576 PVE
::Tools
::run_command
($cmd, outfunc
=> $parser);
580 __PACKAGE__-
>register_method ({
581 name
=> 'updatecerts',
582 path
=> 'updatecerts',
584 description
=> "Update node certificates (and generate all needed files/directories).",
586 additionalProperties
=> 0,
589 description
=> "Force generation of new SSL certifate.",
594 description
=> "Ignore errors (i.e. when cluster has no quorum).",
600 returns
=> { type
=> 'null' },
604 PVE
::Cluster
::gen_pve_vzdump_symlink
();
606 if (!PVE
::Cluster
::check_cfs_quorum
(1)) {
607 return undef if $param->{silent
};
608 die "no quorum - unable to update files\n";
611 PVE
::Cluster
::gen_pve_node_files
($nodename, $local_ip_address, $param->{force
});
612 PVE
::Cluster
::ssh_merge_keys
();
613 PVE
::Cluster
::ssh_merge_known_hosts
($nodename, $local_ip_address);
614 PVE
::Cluster
::gen_pve_vzdump_files
();
621 keygen
=> [ __PACKAGE__
, 'keygen', ['filename']],
622 create
=> [ __PACKAGE__
, 'create', ['clustername']],
623 add
=> [ __PACKAGE__
, 'add', ['hostname']],
624 addnode
=> [ __PACKAGE__
, 'addnode', ['node']],
625 delnode
=> [ __PACKAGE__
, 'delnode', ['node']],
626 status
=> [ __PACKAGE__
, 'status' ],
627 nodes
=> [ __PACKAGE__
, 'nodes' ],
628 expected
=> [ __PACKAGE__
, 'expected', ['expected']],
629 updatecerts
=> [ __PACKAGE__
, 'updatecerts', []],
634 if ($cmd && $cmd ne 'printmanpod' && $cmd ne 'verifyapi') {
635 PVE
::Cluster
::check_cfs_is_mounted
();
636 PVE
::Cluster
::cfs_update
();
639 PVE
::CLIHandler
::handle_cmd
($cmddef, "pvecm", $cmd, \
@ARGV, undef, $0);
647 pvecm - Proxmox VE cluster manager toolkit
655 pvecm is a program to manage the cluster configuration. It can be used
656 to create a new cluster, join nodes to a cluster, leave the cluster,
657 get status information and do various other cluster related tasks.
659 =include pve_copyright