]>
git.proxmox.com Git - pve-cluster.git/blob - data/PVE/pvecm
78e8e7a898e9f4da932d857177bb9c1726b457c1
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 die "cluster not ready - no quorum?\n"
324 if !PVE
::Cluster
::check_cfs_quorum
();
326 my $cmd = ['ccs_tool', 'delnode', '-c', $clusterconf, $param->{node
}];
330 exit (-1); # should not be reached
333 __PACKAGE__-
>register_method ({
337 description
=> "Adds the current node to an existing cluster.",
339 additionalProperties
=> 0,
343 description
=> "Hostname (or IP) of an existing cluster member."
347 description
=> "Node id for this node.",
353 description
=> "Number of votes for this node",
359 description
=> "Do not throw error if node already exists.",
364 returns
=> { type
=> 'null' },
369 PVE
::Cluster
::setup_ssh_keys
();
371 my $host = $param->{hostname
};
373 if (!$param->{force
}) {
376 die "authentication key already exists\n";
379 if (-f
$clusterconf) {
380 die "cluster config '$clusterconf' already exists\n";
383 my $vmlist = PVE
::Cluster
::get_vmlist
();
384 if ($vmlist && $vmlist->{ids
} && scalar(keys %{$vmlist->{ids
}})) {
385 die "this host already contains virtual machines - please remove the first\n";
388 if (system("cman_tool status >/dev/null 2>&1") == 0) {
389 die "cman is already running\n";
393 # make sure known_hosts is on local filesystem
394 PVE
::Cluster
::ssh_unmerge_known_hosts
();
396 my $cmd = "ssh-copy-id 'root\@$host' >/dev/null 2>&1";
397 system ($cmd) == 0 ||
398 die "unable to copy ssh ID\n";
400 $cmd = ['ssh', $host, '-o', 'BatchMode=yes',
401 'pvecm', 'addnode', $nodename, '--force', 1];
403 push @$cmd, '-n', $param->{nodeid
} if $param->{nodeid
};
405 push @$cmd, '-v', $param->{votes
} if defined($param->{votes
});
407 if (system (@$cmd) != 0) {
408 my $cmdtxt = join (' ', @$cmd);
409 die "unable to add node: command failed ($cmdtxt)\n";
412 my $tmpdir = "$libdir/.pvecm_add.tmp.$$";
416 print "copy corosync auth key\n";
417 $cmd = ['rsync', '--rsh=ssh -l root -o BatchMode=yes', '-lpgoq',
418 "$host:$authfile $clusterconf", $tmpdir];
420 system(@$cmd) == 0 || die "can't rsync data from host '$host'\n";
422 mkdir "/etc/cluster";
423 my $confbase = basename
($clusterconf);
425 $cmd = "cp '$tmpdir/$confbase' '/etc/cluster/$confbase'";
426 system($cmd) == 0 || die "can't copy cluster configuration\n";
428 my $keybase = basename
($authfile);
429 system ("cp '$tmpdir/$keybase' '$authfile'") == 0 ||
430 die "can't copy '$tmpdir/$keybase' to '$authfile'\n";
432 print "stopping pve-cluster service\n";
434 system("umount $basedir -f >/dev/null 2>&1");
435 system("/etc/init.d/pve-cluster stop") == 0 ||
436 die "can't stop pve-cluster service\n";
442 system("/etc/init.d/pve-cluster start") == 0 ||
443 die "starting pve-cluster failed\n";
445 system("/etc/init.d/cman start");
449 while (!PVE
::Cluster
::check_cfs_quorum
()) {
451 print "waiting for quorum...";
457 print "OK\n" if !$printqmsg;
459 system("/etc/init.d/clvm start");
461 print "generating node certificates\n";
462 PVE
::Cluster
::gen_pve_node_files
($nodename, $local_ip_address);
464 print "merge known_hosts file\n";
465 PVE
::Cluster
::ssh_merge_known_hosts
($nodename, $local_ip_address, 1);
467 print "restart services\n";
468 # restart apache (changed certs)
469 system("/etc/init.d/apache2 restart");
471 print "successfully added node '$nodename' to cluster.\n";
482 __PACKAGE__-
>register_method ({
486 description
=> "Displays the local view of the cluster status.",
488 additionalProperties
=> 0,
491 returns
=> { type
=> 'null' },
496 my $cmd = ['cman_tool', 'status'];
500 exit (-1); # should not be reached
503 __PACKAGE__-
>register_method ({
507 description
=> "Displays the local view of the cluster nodes.",
509 additionalProperties
=> 0,
512 returns
=> { type
=> 'null' },
517 my $cmd = ['cman_tool', 'nodes'];
521 exit (-1); # should not be reached
524 __PACKAGE__-
>register_method ({
528 description
=> "Tells CMAN a new value of expected votes.",
530 additionalProperties
=> 0,
534 description
=> "Expected votes",
539 returns
=> { type
=> 'null' },
544 my $cmd = ['cman_tool', 'expected', '-e', $param->{expected
}];
548 exit (-1); # should not be reached
556 my $cmd = ['ccs_tool', 'lsnode', '-c', $clusterconf];
562 if ($line =~ m/^Nodename\s+Votes\s+Nodeid\s/i) {
567 if ($line =~ m/^(\S+)\s+(\d+)\s+(\d+)\s/) {
575 PVE
::Tools
::run_command
($cmd, outfunc
=> $parser);
579 __PACKAGE__-
>register_method ({
580 name
=> 'updatecerts',
581 path
=> 'updatecerts',
583 description
=> "Update node certificates (and generate all needed files/directories).",
585 additionalProperties
=> 0,
588 description
=> "Force generation of new SSL certifate.",
593 description
=> "Ignore errors (i.e. when cluster has no quorum).",
599 returns
=> { type
=> 'null' },
603 PVE
::Cluster
::gen_pve_vzdump_symlink
();
605 if (!PVE
::Cluster
::check_cfs_quorum
(1)) {
606 return undef if $param->{silent
};
607 die "no quorum - unable to update files\n";
610 PVE
::Cluster
::gen_pve_node_files
($nodename, $local_ip_address, $param->{force
});
611 PVE
::Cluster
::ssh_merge_keys
();
612 PVE
::Cluster
::ssh_merge_known_hosts
($nodename, $local_ip_address);
613 PVE
::Cluster
::gen_pve_vzdump_files
();
620 keygen
=> [ __PACKAGE__
, 'keygen', ['filename']],
621 create
=> [ __PACKAGE__
, 'create', ['clustername']],
622 add
=> [ __PACKAGE__
, 'add', ['hostname']],
623 addnode
=> [ __PACKAGE__
, 'addnode', ['node']],
624 delnode
=> [ __PACKAGE__
, 'delnode', ['node']],
625 status
=> [ __PACKAGE__
, 'status' ],
626 nodes
=> [ __PACKAGE__
, 'nodes' ],
627 expected
=> [ __PACKAGE__
, 'expected', ['expected']],
628 updatecerts
=> [ __PACKAGE__
, 'updatecerts', []],
633 if ($cmd && $cmd ne 'printmanpod' && $cmd ne 'verifyapi') {
634 PVE
::Cluster
::check_cfs_is_mounted
();
635 PVE
::Cluster
::cfs_update
();
638 PVE
::CLIHandler
::handle_cmd
($cmddef, "pvecm", $cmd, \
@ARGV, undef, $0);
646 pvecm - Proxmox VE cluster manager toolkit
654 pvecm is a program to manage the cluster configuration. It can be used
655 to create a new cluster, join nodes to a cluster, leave the cluster,
656 get status information and do various other cluster related tasks.
658 =include pve_copyright