]>
git.proxmox.com Git - qemu-server.git/blob - PVE/CLI/qm.pm
6 # Note: disable '+' prefix for Getopt::Long (for resize command)
7 use Getopt
::Long
qw(:config no_getopt_compat);
14 use PVE
::Tools
qw(extract_param);
18 use PVE
::RPCEnvironment
;
21 use PVE
::JSONSchema
qw(get_standard_option);
26 use base
qw(PVE::CLIHandler);
30 my $status = PVE
::Tools
::upid_read_status
($upid);
31 exit($status eq 'OK' ?
0 : -1);
34 my $nodename = PVE
::INotify
::nodename
();
40 while ( ++$c < 10 && !-e
$path ) { sleep(1); }
42 my $s = IO
::Socket
::UNIX-
>new(Peer
=> $path, Timeout
=> 120);
44 die "unable to connect to socket '$path' - $!" if !$s;
46 my $select = new IO
::Select
;
48 $select->add(\
*STDIN
);
51 my $timeout = 60*15; # 15 minutes
54 while ($select->count &&
55 scalar(@handles = $select->can_read ($timeout))) {
56 foreach my $h (@handles) {
58 my $n = $h->sysread($buf, 4096);
68 syswrite(\
*STDOUT
, $buf);
78 sub print_recursive_hash
{
79 my ($prefix, $hash, $key) = @_;
81 if (ref($hash) eq 'HASH') {
83 print "$prefix$key:\n";
85 foreach my $itemkey (keys %$hash) {
86 print_recursive_hash
("\t$prefix", $hash->{$itemkey}, $itemkey);
88 } elsif (ref($hash) eq 'ARRAY') {
90 print "$prefix$key:\n";
92 foreach my $item (@$hash) {
93 print_recursive_hash
("\t$prefix", $item);
95 } elsif (!ref($hash) && defined($hash)) {
97 print "$prefix$key: $hash\n";
99 print "$prefix$hash\n";
104 __PACKAGE__-
>register_method ({
108 description
=> "Show command line which is used to start the VM (debug info).",
110 additionalProperties
=> 0,
112 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
115 returns
=> { type
=> 'null'},
119 my $storecfg = PVE
::Storage
::config
();
120 print PVE
::QemuServer
::vm_commandline
($storecfg, $param->{vmid
}) . "\n";
125 __PACKAGE__-
>register_method ({
129 description
=> "Show VM status.",
131 additionalProperties
=> 0,
133 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
135 description
=> "Verbose output format",
141 returns
=> { type
=> 'null'},
146 my $conf = PVE
::QemuConfig-
>load_config ($param->{vmid
});
148 my $vmstatus = PVE
::QemuServer
::vmstatus
($param->{vmid
}, 1);
149 my $stat = $vmstatus->{$param->{vmid
}};
150 if ($param->{verbose
}) {
151 foreach my $k (sort (keys %$stat)) {
152 next if $k eq 'cpu' || $k eq 'relcpu'; # always 0
154 print_recursive_hash
("", $v, $k);
157 my $status = $stat->{qmpstatus
} || 'unknown';
158 print "status: $status\n";
164 __PACKAGE__-
>register_method ({
168 description
=> "Proxy VM VNC traffic to stdin/stdout",
170 additionalProperties
=> 0,
172 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
175 returns
=> { type
=> 'null'},
179 my $vmid = $param->{vmid
};
180 my $vnc_socket = PVE
::QemuServer
::vnc_socket
($vmid);
182 if (my $ticket = $ENV{LC_PVE_TICKET
}) { # NOTE: ssh on debian only pass LC_* variables
183 PVE
::QemuServer
::vm_mon_cmd
($vmid, "change", device
=> 'vnc', target
=> "unix:$vnc_socket,password");
184 PVE
::QemuServer
::vm_mon_cmd
($vmid, "set_password", protocol
=> 'vnc', password
=> $ticket);
185 PVE
::QemuServer
::vm_mon_cmd
($vmid, "expire_password", protocol
=> 'vnc', time => "+30");
187 PVE
::QemuServer
::vm_mon_cmd
($vmid, "change", device
=> 'vnc', target
=> "unix:$vnc_socket,x509,password");
190 run_vnc_proxy
($vnc_socket);
195 __PACKAGE__-
>register_method ({
199 description
=> "Unlock the VM.",
201 additionalProperties
=> 0,
203 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
206 returns
=> { type
=> 'null'},
210 my $vmid = $param->{vmid
};
212 PVE
::QemuConfig-
>lock_config ($vmid, sub {
213 my $conf = PVE
::QemuConfig-
>load_config($vmid);
214 delete $conf->{lock};
215 delete $conf->{pending
}->{lock} if $conf->{pending
}; # just to be sure
216 PVE
::QemuConfig-
>write_config($vmid, $conf);
222 __PACKAGE__-
>register_method ({
226 description
=> "Used by qmigrate - do not use manually.",
228 additionalProperties
=> 0,
231 returns
=> { type
=> 'null'},
235 if (!PVE
::Cluster
::check_cfs_quorum
(1)) {
240 print "tunnel online\n";
243 while (my $line = <>) {
245 last if $line =~ m/^quit$/;
251 __PACKAGE__-
>register_method ({
255 description
=> "Wait until the VM is stopped.",
257 additionalProperties
=> 0,
259 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
261 description
=> "Timeout in seconds. Default is to wait forever.",
268 returns
=> { type
=> 'null'},
272 my $vmid = $param->{vmid
};
273 my $timeout = $param->{timeout
};
275 my $pid = PVE
::QemuServer
::check_running
($vmid);
278 print "waiting until VM $vmid stopps (PID $pid)\n";
281 while ((!$timeout || ($count < $timeout)) && PVE
::QemuServer
::check_running
($vmid)) {
286 die "wait failed - got timeout\n" if PVE
::QemuServer
::check_running
($vmid);
291 __PACKAGE__-
>register_method ({
295 description
=> "Enter Qemu Monitor interface.",
297 additionalProperties
=> 0,
299 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
302 returns
=> { type
=> 'null'},
306 my $vmid = $param->{vmid
};
308 my $conf = PVE
::QemuConfig-
>load_config ($vmid); # check if VM exists
310 print "Entering Qemu Monitor for VM $vmid - type 'help' for help\n";
312 my $term = new Term
::ReadLine
('qm');
315 while (defined ($input = $term->readline('qm> '))) {
318 next if $input =~ m/^\s*$/;
320 last if $input =~ m/^\s*q(uit)?\s*$/;
323 print PVE
::QemuServer
::vm_human_monitor_command
($vmid, $input);
325 print "ERROR: $@" if $@;
332 __PACKAGE__-
>register_method ({
336 description
=> "Rescan all storages and update disk sizes and unused disk images.",
338 additionalProperties
=> 0,
340 vmid
=> get_standard_option
('pve-vmid', {
342 completion
=> \
&PVE
::QemuServer
::complete_vmid
,
346 returns
=> { type
=> 'null'},
350 PVE
::QemuServer
::rescan
($param->{vmid
});
355 __PACKAGE__-
>register_method ({
359 description
=> "Open a terminal using a serial device (The VM need to have a serial device configured, for example 'serial0: socket')",
361 additionalProperties
=> 0,
363 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
365 description
=> "Select the serial device. By default we simply use the first suitable device.",
368 enum
=> [qw(serial0 serial1 serial2 serial3)],
372 returns
=> { type
=> 'null'},
376 my $vmid = $param->{vmid
};
378 my $conf = PVE
::QemuConfig-
>load_config ($vmid); # check if VM exists
380 my $iface = $param->{iface
};
383 die "serial interface '$iface' is not configured\n" if !$conf->{$iface};
384 die "wrong serial type on interface '$iface'\n" if $conf->{$iface} ne 'socket';
386 foreach my $opt (qw(serial0 serial1 serial2 serial3)) {
387 if ($conf->{$opt} && ($conf->{$opt} eq 'socket')) {
392 die "unable to find a serial interface\n" if !$iface;
395 die "VM $vmid not running\n" if !PVE
::QemuServer
::check_running
($vmid);
397 my $socket = "/var/run/qemu-server/${vmid}.$iface";
399 my $cmd = "socat UNIX-CONNECT:$socket STDIO,raw,echo=0,escape=0x0f";
401 print "starting serial terminal on interface $iface (press control-O to exit)\n";
409 list
=> [ "PVE::API2::Qemu", 'vmlist', [],
410 { node
=> $nodename }, sub {
413 exit 0 if (!scalar(@$vmlist));
415 printf "%10s %-20s %-10s %-10s %12s %-10s\n",
416 qw(VMID NAME STATUS MEM(MB) BOOTDISK
(GB
) PID
);
418 foreach my $rec (sort { $a->{vmid
} <=> $b->{vmid
} } @$vmlist) {
419 printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid
}, $rec->{name
},
420 $rec->{qmpstatus
} || $rec->{status
},
421 ($rec->{maxmem
} || 0)/(1024*1024),
422 ($rec->{maxdisk
} || 0)/(1024*1024*1024),
429 create
=> [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node
=> $nodename }, $upid_exit ],
431 destroy
=> [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { node
=> $nodename }, $upid_exit ],
433 clone
=> [ "PVE::API2::Qemu", 'clone_vm', ['vmid', 'newid'], { node
=> $nodename }, $upid_exit ],
435 migrate
=> [ "PVE::API2::Qemu", 'migrate_vm', ['vmid', 'target'], { node
=> $nodename }, $upid_exit ],
437 set
=> [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node
=> $nodename } ],
439 resize
=> [ "PVE::API2::Qemu", 'resize_vm', ['vmid', 'disk', 'size'], { node
=> $nodename } ],
441 move_disk
=> [ "PVE::API2::Qemu", 'move_vm_disk', ['vmid', 'disk', 'storage'], { node
=> $nodename }, $upid_exit ],
443 unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid'], { node
=> $nodename } ],
445 config
=> [ "PVE::API2::Qemu", 'vm_config', ['vmid'],
446 { node
=> $nodename }, sub {
448 foreach my $k (sort (keys %$config)) {
449 next if $k eq 'digest';
450 my $v = $config->{$k};
451 if ($k eq 'description') {
452 $v = PVE
::Tools
::encode_text
($v);
458 pending
=> [ "PVE::API2::Qemu", 'vm_pending', ['vmid'],
459 { node
=> $nodename }, sub {
461 foreach my $item (sort { $a->{key
} cmp $b->{key
}} @$data) {
462 my $k = $item->{key
};
463 next if $k eq 'digest';
464 my $v = $item->{value
};
465 my $p = $item->{pending
};
466 if ($k eq 'description') {
467 $v = PVE
::Tools
::encode_text
($v) if defined($v);
468 $p = PVE
::Tools
::encode_text
($p) if defined($p);
471 if ($item->{delete}) {
472 print "del $k: $v\n";
473 } elsif (defined($p)) {
474 print "cur $k: $v\n";
475 print "new $k: $p\n";
477 print "cur $k: $v\n";
479 } elsif (defined($p)) {
480 print "new $k: $p\n";
485 showcmd
=> [ __PACKAGE__
, 'showcmd', ['vmid']],
487 status
=> [ __PACKAGE__
, 'status', ['vmid']],
489 snapshot
=> [ "PVE::API2::Qemu", 'snapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
491 delsnapshot
=> [ "PVE::API2::Qemu", 'delsnapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
493 listsnapshot
=> [ "PVE::API2::Qemu", 'snapshot_list', ['vmid'], { node
=> $nodename },
496 foreach my $e (@$res) {
497 my $headline = $e->{description
} || 'no-description';
498 $headline =~ s/\n.*//sg;
499 my $parent = $e->{parent
} // 'no-parent';
500 printf("%-20s %-20s %s\n", $e->{name
}, $parent, $headline);
504 rollback
=> [ "PVE::API2::Qemu", 'rollback', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
506 template
=> [ "PVE::API2::Qemu", 'template', ['vmid'], { node
=> $nodename }],
508 start
=> [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { node
=> $nodename } , $upid_exit ],
510 stop
=> [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { node
=> $nodename }, $upid_exit ],
512 reset => [ "PVE::API2::Qemu", 'vm_reset', ['vmid'], { node
=> $nodename }, $upid_exit ],
514 shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { node
=> $nodename }, $upid_exit ],
516 suspend
=> [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { node
=> $nodename }, $upid_exit ],
518 resume
=> [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { node
=> $nodename }, $upid_exit ],
520 sendkey
=> [ "PVE::API2::Qemu", 'vm_sendkey', ['vmid', 'key'], { node
=> $nodename } ],
522 vncproxy
=> [ __PACKAGE__
, 'vncproxy', ['vmid']],
524 wait => [ __PACKAGE__
, 'wait', ['vmid']],
526 unlock
=> [ __PACKAGE__
, 'unlock', ['vmid']],
528 rescan
=> [ __PACKAGE__
, 'rescan', []],
530 monitor
=> [ __PACKAGE__
, 'monitor', ['vmid']],
532 agent
=> [ "PVE::API2::Qemu", 'agent', ['vmid'], { node
=> $nodename }],
534 mtunnel
=> [ __PACKAGE__
, 'mtunnel', []],
536 terminal
=> [ __PACKAGE__
, 'terminal', ['vmid']],