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