]> git.proxmox.com Git - qemu-server.git/blob - qm
try to detect errors before starting the background task
[qemu-server.git] / qm
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Getopt::Long;
5 use Fcntl ':flock';
6 use File::Path;
7 use IO::Socket::UNIX;
8 use IO::Select;
9
10 use PVE::Tools qw(extract_param);
11 use PVE::Cluster;
12 use PVE::SafeSyslog;
13 use PVE::INotify;
14 use PVE::RPCEnvironment;
15 use PVE::QemuServer;
16 use PVE::API2::Qemu;
17 use PVE::JSONSchema qw(get_standard_option);
18 use Term::ReadLine;
19
20 use PVE::CLIHandler;
21
22 use base qw(PVE::CLIHandler);
23
24 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
25
26 initlog('qm');
27
28 die "please run as root\n" if $> != 0;
29
30 PVE::INotify::inotify_init();
31
32 my $rpcenv = PVE::RPCEnvironment->init('cli');
33
34 $rpcenv->init_request();
35 $rpcenv->set_language($ENV{LANG});
36 $rpcenv->set_user('root@pam');
37
38 my $nodename = PVE::INotify::nodename();
39
40 sub run_vnc_proxy {
41 my ($vmid) = @_;
42
43 my $path = PVE::QemuServer::vnc_socket($vmid);
44
45 my $s = IO::Socket::UNIX->new(Peer => $path, Timeout => 120);
46
47 die "unable to connect to socket '$path' - $!" if !$s;
48
49 my $select = new IO::Select;
50
51 $select->add(\*STDIN);
52 $select->add($s);
53
54 my $timeout = 60*15; # 15 minutes
55
56 my @handles;
57 while ($select->count &&
58 scalar(@handles = $select->can_read ($timeout))) {
59 foreach my $h (@handles) {
60 my $buf;
61 my $n = $h->sysread($buf, 4096);
62
63 if ($h == \*STDIN) {
64 if ($n) {
65 syswrite($s, $buf);
66 } else {
67 exit(0);
68 }
69 } elsif ($h == $s) {
70 if ($n) {
71 syswrite(\*STDOUT, $buf);
72 } else {
73 exit(0);
74 }
75 }
76 }
77 }
78 exit(0);
79 }
80
81 __PACKAGE__->register_method ({
82 name => 'showcmd',
83 path => 'showcmd',
84 method => 'GET',
85 description => "Show command line which is used to start the VM (debug info).",
86 parameters => {
87 additionalProperties => 0,
88 properties => {
89 vmid => get_standard_option('pve-vmid'),
90 },
91 },
92 returns => { type => 'null'},
93 code => sub {
94 my ($param) = @_;
95
96 my $storecfg = PVE::Storage::config();
97 print PVE::QemuServer::vm_commandline($storecfg, $param->{vmid}) . "\n";
98
99 return undef;
100 }});
101
102 __PACKAGE__->register_method ({
103 name => 'status',
104 path => 'status',
105 method => 'GET',
106 description => "Show VM status.",
107 parameters => {
108 additionalProperties => 0,
109 properties => {
110 vmid => get_standard_option('pve-vmid'),
111 verbose => {
112 description => "Verbose output format",
113 type => 'boolean',
114 optional => 1,
115 }
116 },
117 },
118 returns => { type => 'null'},
119 code => sub {
120 my ($param) = @_;
121
122 # test if VM exists
123 my $conf = PVE::QemuServer::load_config ($param->{vmid});
124
125 my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid});
126 my $stat = $vmstatus->{$param->{vmid}};
127 if ($param->{verbose}) {
128 foreach my $k (sort (keys %$stat)) {
129 next if $k eq 'cpu' || $k eq 'relcpu'; # always 0
130 my $v = $stat->{$k};
131 next if !defined($v);
132 print "$k: $v\n";
133 }
134 } else {
135 my $status = $stat->{status} || 'unknown';
136 print "status: $status\n";
137 }
138
139 return undef;
140 }});
141
142 __PACKAGE__->register_method ({
143 name => 'vncproxy',
144 path => 'vncproxy',
145 method => 'PUT',
146 description => "Proxy VM VNC traffic to stdin/stdout",
147 parameters => {
148 additionalProperties => 0,
149 properties => {
150 vmid => get_standard_option('pve-vmid'),
151 },
152 },
153 returns => { type => 'null'},
154 code => sub {
155 my ($param) = @_;
156
157 my $vmid = $param->{vmid};
158
159 run_vnc_proxy ($vmid);
160
161 return undef;
162 }});
163
164 __PACKAGE__->register_method ({
165 name => 'unlock',
166 path => 'unlock',
167 method => 'PUT',
168 description => "Unlock the VM.",
169 parameters => {
170 additionalProperties => 0,
171 properties => {
172 vmid => get_standard_option('pve-vmid'),
173 },
174 },
175 returns => { type => 'null'},
176 code => sub {
177 my ($param) = @_;
178
179 my $vmid = $param->{vmid};
180
181 PVE::QemuServer::lock_config ($vmid, sub {
182 PVE::QemuServer::change_config_nolock ($vmid, {}, { lock => 1 }, 1);
183 });
184
185 return undef;
186 }});
187
188 __PACKAGE__->register_method ({
189 name => 'mtunnel',
190 path => 'mtunnel',
191 method => 'POST',
192 description => "Used by qmigrate - do not use manually.",
193 parameters => {
194 additionalProperties => 0,
195 properties => {},
196 },
197 returns => { type => 'null'},
198 code => sub {
199 my ($param) = @_;
200
201 if (!PVE::Cluster::check_cfs_quorum(1)) {
202 print "no quorum\n";
203 return undef;
204 }
205
206 print "tunnel online\n";
207 *STDOUT->flush();
208
209 while (my $line = <>) {
210 chomp $line;
211 last if $line =~ m/^quit$/;
212 }
213
214 return undef;
215 }});
216
217 __PACKAGE__->register_method ({
218 name => 'startall',
219 path => 'startall',
220 method => 'POST',
221 description => "Start all virtual machines (when onboot=1).",
222 parameters => {
223 additionalProperties => 0,
224 properties => {},
225 },
226 returns => { type => 'null'},
227 code => sub {
228 my ($param) = @_;
229
230 my $vzlist = PVE::QemuServer::vzlist();
231 my $storecfg = PVE::Storage::config();
232
233 PVE::Cluster::check_cfs_quorum();
234
235 my $count = 0;
236 foreach my $vmid (keys %$vzlist) {
237 next if $vzlist->{$vmid}->{pid}; # already running
238
239 sleep(2) if $count != 0; # reduce load
240 $count++;
241
242 PVE::Cluster::check_cfs_quorum(); # abort when we loose quorum
243
244 eval {
245 my $conf = PVE::QemuServer::load_config($vmid);
246 if ($conf->{onboot}) {
247 print STDERR "Starting Qemu VM $vmid\n";
248 PVE::QemuServer::vm_start($storecfg, $vmid);
249 }
250 };
251 print STDERR $@ if $@;
252 }
253
254 return undef;
255 }});
256
257 __PACKAGE__->register_method ({
258 name => 'stopall',
259 path => 'stopall',
260 method => 'POST',
261 description => "Stop all virtual machines.",
262 parameters => {
263 additionalProperties => 0,
264 properties => {
265 timeout => {
266 description => "Timeout in seconds. Default is to wait 3 minutes.",
267 type => 'integer',
268 minimum => 1,
269 optional => 1,
270 }
271 },
272 },
273 returns => { type => 'null'},
274 code => sub {
275 my ($param) = @_;
276
277 my $timeout = $param->{timeout};
278
279 my $storecfg = PVE::Storage::config();
280
281 PVE::QemuServer::vm_stopall($storecfg, $timeout);
282
283 return undef;
284 }});
285
286 __PACKAGE__->register_method ({
287 name => 'wait',
288 path => 'wait',
289 method => 'GET',
290 description => "Wait until the VM is stopped.",
291 parameters => {
292 additionalProperties => 0,
293 properties => {
294 vmid => get_standard_option('pve-vmid'),
295 timeout => {
296 description => "Timeout in seconds. Default is to wait forever.",
297 type => 'integer',
298 minimum => 1,
299 optional => 1,
300 }
301 },
302 },
303 returns => { type => 'null'},
304 code => sub {
305 my ($param) = @_;
306
307 my $vmid = $param->{vmid};
308 my $timeout = $param->{timeout};
309
310 my $pid = PVE::QemuServer::check_running ($vmid);
311 return if !$pid;
312
313 print "waiting until VM $vmid stopps (PID $pid)\n";
314
315 my $count = 0;
316 while ((!$timeout || ($count < $timeout)) && PVE::QemuServer::check_running ($vmid)) {
317 $count++;
318 sleep 1;
319 }
320
321 die "wait failed - got timeout\n" if PVE::QemuServer::check_running ($vmid);
322
323 return undef;
324 }});
325
326 __PACKAGE__->register_method ({
327 name => 'monitor',
328 path => 'monitor',
329 method => 'POST',
330 description => "Enter Qemu Monitor interface.",
331 parameters => {
332 additionalProperties => 0,
333 properties => {
334 vmid => get_standard_option('pve-vmid'),
335 },
336 },
337 returns => { type => 'null'},
338 code => sub {
339 my ($param) = @_;
340
341 my $vmid = $param->{vmid};
342
343 my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists
344
345 print "Entering Qemu Monitor for VM $vmid - type 'help' for help\n";
346
347 my $term = new Term::ReadLine ('qm');
348
349 my $input;
350 while (defined ($input = $term->readline('qm> '))) {
351 chomp $input;
352
353 next if $input =~ m/^\s*$/;
354
355 last if $input =~ m/^\s*q(uit)?\s*$/;
356
357 eval {
358 print PVE::QemuServer::vm_monitor_command ($vmid, $input);
359 };
360 print "ERROR: $@" if $@;
361 }
362
363 return undef;
364
365 }});
366
367 my $cmddef = {
368 list => [ "PVE::API2::Qemu", 'vmlist', [],
369 { node => $nodename }, sub {
370 my $vmlist = shift;
371
372 exit 0 if (!scalar(@$vmlist));
373
374 printf "%10s %-20s %-10s %-10s %12s %-10s\n",
375 qw(VMID NAME STATUS MEM(MB) BOOTDISK(GB) PID);
376
377 foreach my $rec (sort { $a->{vmid} <=> $b->{vmid} } @$vmlist) {
378 printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid}, $rec->{name},
379 $rec->{status},
380 ($rec->{maxmem} || 0)/(1024*1024),
381 ($rec->{maxdisk} || 0)/(1024*1024*1024),
382 $rec->{pid}||0;
383 }
384
385
386 } ],
387
388 create => [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node => $nodename } ],
389
390 destroy => [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { node => $nodename } ],
391
392 migrate => [ "PVE::API2::Qemu", 'migrate_vm', ['target', 'vmid'], { node => $nodename },
393 sub {
394 my $upid = shift;
395 my $status = PVE::Tools::upid_read_status($upid);
396 exit($status eq 'OK' ? 0 : -1);
397 }],
398
399 set => [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node => $nodename } ],
400
401 unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid', 'idlist'], { node => $nodename } ],
402
403 config => [ "PVE::API2::Qemu", 'vm_config', ['vmid'],
404 { node => $nodename }, sub {
405 my $config = shift;
406 foreach my $k (sort (keys %$config)) {
407 next if $k eq 'digest';
408 my $v = $config->{$k};
409 if ($k eq 'description') {
410 $v = PVE::Tools::encode_text($v);
411 }
412 print "$k: $v\n";
413 }
414 }],
415
416 showcmd => [ __PACKAGE__, 'showcmd', ['vmid']],
417
418 status => [ __PACKAGE__, 'status', ['vmid']],
419
420 start => [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { node => $nodename } ],
421
422 stop => [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { node => $nodename } ],
423
424 reset => [ "PVE::API2::Qemu", 'vm_reset', ['vmid'], { node => $nodename } ],
425
426 shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { node => $nodename } ],
427
428 suspend => [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { node => $nodename } ],
429
430 resume => [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { node => $nodename } ],
431
432 sendkey => [ "PVE::API2::Qemu", 'vm_sendkey', ['vmid', 'key'], { node => $nodename } ],
433
434 vncproxy => [ __PACKAGE__, 'vncproxy', ['vmid']],
435
436 wait => [ __PACKAGE__, 'wait', ['vmid']],
437
438 unlock => [ __PACKAGE__, 'unlock', ['vmid']],
439
440 monitor => [ __PACKAGE__, 'monitor', ['vmid']],
441
442 startall => [ __PACKAGE__, 'startall', []],
443
444 stopall => [ __PACKAGE__, 'stopall', []],
445
446 mtunnel => [ __PACKAGE__, 'mtunnel', []],
447 };
448
449 my $cmd = shift;
450
451 PVE::CLIHandler::handle_cmd($cmddef, "qm", $cmd, \@ARGV, undef, $0);
452
453 exit 0;
454
455 __END__
456
457 =head1 NAME
458
459 qm - qemu/kvm virtual machine manager
460
461 =head1 SYNOPSIS
462
463 =include synopsis
464
465 =head1 DESCRIPTION
466
467 qm is a script to manage virtual machines with qemu/kvm. You can
468 create and destroy virtual machines, and control execution
469 (start/stop/suspend/resume). Besides that, you can use qm to set
470 parameters in the associated config file. It is also possible to
471 create and delete virtual disks.
472
473 =head1 CONFIGURATION
474
475 All configuration files consists of lines in the form
476
477 PARAMETER: value
478
479 See L<vm.conf|vm.conf> for a complete list of options.
480
481 Configuration files are stored inside the Proxmox configuration file system, and can be access at F</etc/pve/qemu-server/C<VMID>.conf>.
482
483 The default for option 'keyboard' is read from
484 F</etc/pve/datacenter.conf>.
485
486 =head1 Locks
487
488 Online migration and backups (vzdump) set a lock to prevent
489 unintentional action on such VMs. Sometimes you need remove such lock
490 manually (power failure).
491
492 qm unlock <vmid>
493
494 =head1 EXAMPLES
495
496 # create a new VM with 4 GB ide disk
497 qm create 300 -ide0 4 -net0 e1000 -cdrom proxmox-mailgateway_2.1.iso
498
499 # start the new VM
500 qm start 300
501
502 # send shutdown, then wait until VM is stopped
503 qm shutdown 300 && qm wait 300
504
505 # same as above, but only wait for 40 seconds
506 qm shutdown 300 && qm wait 300 -timeout 40
507
508
509 =include pve_copyright