]>
Commit | Line | Data |
---|---|---|
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 | delete $conf->{pending}->{lock} if $conf->{pending}; # just to be sure | |
202 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1); | |
203 | }); | |
204 | ||
205 | return undef; | |
206 | }}); | |
207 | ||
208 | __PACKAGE__->register_method ({ | |
209 | name => 'mtunnel', | |
210 | path => 'mtunnel', | |
211 | method => 'POST', | |
212 | description => "Used by qmigrate - do not use manually.", | |
213 | parameters => { | |
214 | additionalProperties => 0, | |
215 | properties => {}, | |
216 | }, | |
217 | returns => { type => 'null'}, | |
218 | code => sub { | |
219 | my ($param) = @_; | |
220 | ||
221 | if (!PVE::Cluster::check_cfs_quorum(1)) { | |
222 | print "no quorum\n"; | |
223 | return undef; | |
224 | } | |
225 | ||
226 | print "tunnel online\n"; | |
227 | *STDOUT->flush(); | |
228 | ||
229 | while (my $line = <>) { | |
230 | chomp $line; | |
231 | last if $line =~ m/^quit$/; | |
232 | } | |
233 | ||
234 | return undef; | |
235 | }}); | |
236 | ||
237 | __PACKAGE__->register_method ({ | |
238 | name => 'wait', | |
239 | path => 'wait', | |
240 | method => 'GET', | |
241 | description => "Wait until the VM is stopped.", | |
242 | parameters => { | |
243 | additionalProperties => 0, | |
244 | properties => { | |
245 | vmid => get_standard_option('pve-vmid'), | |
246 | timeout => { | |
247 | description => "Timeout in seconds. Default is to wait forever.", | |
248 | type => 'integer', | |
249 | minimum => 1, | |
250 | optional => 1, | |
251 | } | |
252 | }, | |
253 | }, | |
254 | returns => { type => 'null'}, | |
255 | code => sub { | |
256 | my ($param) = @_; | |
257 | ||
258 | my $vmid = $param->{vmid}; | |
259 | my $timeout = $param->{timeout}; | |
260 | ||
261 | my $pid = PVE::QemuServer::check_running ($vmid); | |
262 | return if !$pid; | |
263 | ||
264 | print "waiting until VM $vmid stopps (PID $pid)\n"; | |
265 | ||
266 | my $count = 0; | |
267 | while ((!$timeout || ($count < $timeout)) && PVE::QemuServer::check_running ($vmid)) { | |
268 | $count++; | |
269 | sleep 1; | |
270 | } | |
271 | ||
272 | die "wait failed - got timeout\n" if PVE::QemuServer::check_running ($vmid); | |
273 | ||
274 | return undef; | |
275 | }}); | |
276 | ||
277 | __PACKAGE__->register_method ({ | |
278 | name => 'monitor', | |
279 | path => 'monitor', | |
280 | method => 'POST', | |
281 | description => "Enter Qemu Monitor interface.", | |
282 | parameters => { | |
283 | additionalProperties => 0, | |
284 | properties => { | |
285 | vmid => get_standard_option('pve-vmid'), | |
286 | }, | |
287 | }, | |
288 | returns => { type => 'null'}, | |
289 | code => sub { | |
290 | my ($param) = @_; | |
291 | ||
292 | my $vmid = $param->{vmid}; | |
293 | ||
294 | my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists | |
295 | ||
296 | print "Entering Qemu Monitor for VM $vmid - type 'help' for help\n"; | |
297 | ||
298 | my $term = new Term::ReadLine ('qm'); | |
299 | ||
300 | my $input; | |
301 | while (defined ($input = $term->readline('qm> '))) { | |
302 | chomp $input; | |
303 | ||
304 | next if $input =~ m/^\s*$/; | |
305 | ||
306 | last if $input =~ m/^\s*q(uit)?\s*$/; | |
307 | ||
308 | eval { | |
309 | print PVE::QemuServer::vm_human_monitor_command ($vmid, $input); | |
310 | }; | |
311 | print "ERROR: $@" if $@; | |
312 | } | |
313 | ||
314 | return undef; | |
315 | ||
316 | }}); | |
317 | ||
318 | __PACKAGE__->register_method ({ | |
319 | name => 'rescan', | |
320 | path => 'rescan', | |
321 | method => 'POST', | |
322 | description => "Rescan all storages and update disk sizes and unused disk images.", | |
323 | parameters => { | |
324 | additionalProperties => 0, | |
325 | properties => { | |
326 | vmid => get_standard_option('pve-vmid', {optional => 1}), | |
327 | }, | |
328 | }, | |
329 | returns => { type => 'null'}, | |
330 | code => sub { | |
331 | my ($param) = @_; | |
332 | ||
333 | PVE::QemuServer::rescan($param->{vmid}); | |
334 | ||
335 | return undef; | |
336 | }}); | |
337 | ||
338 | __PACKAGE__->register_method ({ | |
339 | name => 'terminal', | |
340 | path => 'terminal', | |
341 | method => 'POST', | |
342 | description => "Open a terminal using a serial device (The VM need to have a serial device configured, for example 'serial0: socket')", | |
343 | parameters => { | |
344 | additionalProperties => 0, | |
345 | properties => { | |
346 | vmid => get_standard_option('pve-vmid'), | |
347 | iface => { | |
348 | description => "Select the serial device. By default we simply use the first suitable device.", | |
349 | type => 'string', | |
350 | optional => 1, | |
351 | enum => [qw(serial0 serial1 serial2 serial3)], | |
352 | } | |
353 | }, | |
354 | }, | |
355 | returns => { type => 'null'}, | |
356 | code => sub { | |
357 | my ($param) = @_; | |
358 | ||
359 | my $vmid = $param->{vmid}; | |
360 | ||
361 | my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists | |
362 | ||
363 | my $iface = $param->{iface}; | |
364 | ||
365 | if ($iface) { | |
366 | die "serial interface '$iface' is not configured\n" if !$conf->{$iface}; | |
367 | die "wrong serial type on interface '$iface'\n" if $conf->{$iface} ne 'socket'; | |
368 | } else { | |
369 | foreach my $opt (qw(serial0 serial1 serial2 serial3)) { | |
370 | if ($conf->{$opt} && ($conf->{$opt} eq 'socket')) { | |
371 | $iface = $opt; | |
372 | last; | |
373 | } | |
374 | } | |
375 | die "unable to find a serial interface\n" if !$iface; | |
376 | } | |
377 | ||
378 | die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); | |
379 | ||
380 | my $socket = "/var/run/qemu-server/${vmid}.$iface"; | |
381 | ||
382 | my $cmd = "socat UNIX-CONNECT:$socket STDIO,raw,echo=0,escape=0x0f"; | |
383 | ||
384 | print "starting serial terminal on interface $iface (press control-O to exit)\n"; | |
385 | ||
386 | system($cmd); | |
387 | ||
388 | return undef; | |
389 | }}); | |
390 | ||
391 | my $cmddef = { | |
392 | list => [ "PVE::API2::Qemu", 'vmlist', [], | |
393 | { node => $nodename }, sub { | |
394 | my $vmlist = shift; | |
395 | ||
396 | exit 0 if (!scalar(@$vmlist)); | |
397 | ||
398 | printf "%10s %-20s %-10s %-10s %12s %-10s\n", | |
399 | qw(VMID NAME STATUS MEM(MB) BOOTDISK(GB) PID); | |
400 | ||
401 | foreach my $rec (sort { $a->{vmid} <=> $b->{vmid} } @$vmlist) { | |
402 | printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid}, $rec->{name}, | |
403 | $rec->{status}, | |
404 | ($rec->{maxmem} || 0)/(1024*1024), | |
405 | ($rec->{maxdisk} || 0)/(1024*1024*1024), | |
406 | $rec->{pid}||0; | |
407 | } | |
408 | ||
409 | ||
410 | } ], | |
411 | ||
412 | create => [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node => $nodename }, $upid_exit ], | |
413 | ||
414 | destroy => [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { node => $nodename }, $upid_exit ], | |
415 | ||
416 | clone => [ "PVE::API2::Qemu", 'clone_vm', ['vmid', 'newid'], { node => $nodename }, $upid_exit ], | |
417 | ||
418 | migrate => [ "PVE::API2::Qemu", 'migrate_vm', ['vmid', 'target'], { node => $nodename }, $upid_exit ], | |
419 | ||
420 | set => [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node => $nodename } ], | |
421 | ||
422 | resize => [ "PVE::API2::Qemu", 'resize_vm', ['vmid', 'disk', 'size'], { node => $nodename } ], | |
423 | ||
424 | move_disk => [ "PVE::API2::Qemu", 'move_vm_disk', ['vmid', 'disk', 'storage'], { node => $nodename }, $upid_exit ], | |
425 | ||
426 | unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid'], { node => $nodename } ], | |
427 | ||
428 | config => [ "PVE::API2::Qemu", 'vm_config', ['vmid'], | |
429 | { node => $nodename }, sub { | |
430 | my $config = shift; | |
431 | foreach my $k (sort (keys %$config)) { | |
432 | next if $k eq 'digest'; | |
433 | my $v = $config->{$k}; | |
434 | if ($k eq 'description') { | |
435 | $v = PVE::Tools::encode_text($v); | |
436 | } | |
437 | print "$k: $v\n"; | |
438 | } | |
439 | }], | |
440 | ||
441 | pending => [ "PVE::API2::Qemu", 'vm_pending', ['vmid'], | |
442 | { node => $nodename }, sub { | |
443 | my $data = shift; | |
444 | foreach my $item (sort { $a->{key} cmp $b->{key}} @$data) { | |
445 | my $k = $item->{key}; | |
446 | next if $k eq 'digest'; | |
447 | my $v = $item->{value}; | |
448 | my $p = $item->{pending}; | |
449 | if ($k eq 'description') { | |
450 | $v = PVE::Tools::encode_text($v) if defined($v); | |
451 | $p = PVE::Tools::encode_text($p) if defined($p); | |
452 | } | |
453 | if (defined($v)) { | |
454 | if ($item->{delete}) { | |
455 | print "del $k: $v\n"; | |
456 | } elsif (defined($p)) { | |
457 | print "cur $k: $v\n"; | |
458 | print "new $k: $p\n"; | |
459 | } else { | |
460 | print "cur $k: $v\n"; | |
461 | } | |
462 | } elsif (defined($p)) { | |
463 | print "new $k: $p\n"; | |
464 | } | |
465 | } | |
466 | }], | |
467 | ||
468 | showcmd => [ __PACKAGE__, 'showcmd', ['vmid']], | |
469 | ||
470 | status => [ __PACKAGE__, 'status', ['vmid']], | |
471 | ||
472 | snapshot => [ "PVE::API2::Qemu", 'snapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ], | |
473 | ||
474 | delsnapshot => [ "PVE::API2::Qemu", 'delsnapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ], | |
475 | ||
476 | rollback => [ "PVE::API2::Qemu", 'rollback', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ], | |
477 | ||
478 | template => [ "PVE::API2::Qemu", 'template', ['vmid'], { node => $nodename }], | |
479 | ||
480 | start => [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { node => $nodename } , $upid_exit ], | |
481 | ||
482 | stop => [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { node => $nodename }, $upid_exit ], | |
483 | ||
484 | reset => [ "PVE::API2::Qemu", 'vm_reset', ['vmid'], { node => $nodename }, $upid_exit ], | |
485 | ||
486 | shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { node => $nodename }, $upid_exit ], | |
487 | ||
488 | suspend => [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { node => $nodename }, $upid_exit ], | |
489 | ||
490 | resume => [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { node => $nodename }, $upid_exit ], | |
491 | ||
492 | sendkey => [ "PVE::API2::Qemu", 'vm_sendkey', ['vmid', 'key'], { node => $nodename } ], | |
493 | ||
494 | vncproxy => [ __PACKAGE__, 'vncproxy', ['vmid']], | |
495 | ||
496 | wait => [ __PACKAGE__, 'wait', ['vmid']], | |
497 | ||
498 | unlock => [ __PACKAGE__, 'unlock', ['vmid']], | |
499 | ||
500 | rescan => [ __PACKAGE__, 'rescan', []], | |
501 | ||
502 | monitor => [ __PACKAGE__, 'monitor', ['vmid']], | |
503 | ||
504 | mtunnel => [ __PACKAGE__, 'mtunnel', []], | |
505 | ||
506 | terminal => [ __PACKAGE__, 'terminal', ['vmid']], | |
507 | }; | |
508 | ||
509 | my $cmd = shift; | |
510 | ||
511 | # Note: disable '+' prefix for Getopt::Long (for resize command) | |
512 | use Getopt::Long qw(:config no_getopt_compat); | |
513 | ||
514 | PVE::CLIHandler::handle_cmd($cmddef, "qm", $cmd, \@ARGV, undef, $0); | |
515 | ||
516 | exit 0; | |
517 | ||
518 | __END__ | |
519 | ||
520 | =head1 NAME | |
521 | ||
522 | qm - qemu/kvm virtual machine manager | |
523 | ||
524 | =head1 SYNOPSIS | |
525 | ||
526 | =include synopsis | |
527 | ||
528 | =head1 DESCRIPTION | |
529 | ||
530 | qm is a script to manage virtual machines with qemu/kvm. You can | |
531 | create and destroy virtual machines, and control execution | |
532 | (start/stop/suspend/resume). Besides that, you can use qm to set | |
533 | parameters in the associated config file. It is also possible to | |
534 | create and delete virtual disks. | |
535 | ||
536 | =head1 CONFIGURATION | |
537 | ||
538 | All configuration files consists of lines in the form | |
539 | ||
540 | PARAMETER: value | |
541 | ||
542 | See L<vm.conf|vm.conf> for a complete list of options. | |
543 | ||
544 | Configuration files are stored inside the Proxmox configuration file system, and can be access at F</etc/pve/qemu-server/C<VMID>.conf>. | |
545 | ||
546 | The default for option 'keyboard' is read from | |
547 | F</etc/pve/datacenter.conf>. | |
548 | ||
549 | =head1 Locks | |
550 | ||
551 | Online migration and backups (vzdump) set a lock to prevent | |
552 | unintentional action on such VMs. Sometimes you need remove such lock | |
553 | manually (power failure). | |
554 | ||
555 | qm unlock <vmid> | |
556 | ||
557 | =head1 EXAMPLES | |
558 | ||
559 | # create a new VM with 4 GB ide disk | |
560 | qm create 300 -ide0 4 -net0 e1000 -cdrom proxmox-mailgateway_2.1.iso | |
561 | ||
562 | # start the new VM | |
563 | qm start 300 | |
564 | ||
565 | # send shutdown, then wait until VM is stopped | |
566 | qm shutdown 300 && qm wait 300 | |
567 | ||
568 | # same as above, but only wait for 40 seconds | |
569 | qm shutdown 300 && qm wait 300 -timeout 40 | |
570 | ||
571 | ||
572 | =include pve_copyright |