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