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