]>
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
;
22 use PVE
::JSONSchema
qw(get_standard_option);
27 use base
qw(PVE::CLIHandler);
31 my $status = PVE
::Tools
::upid_read_status
($upid);
32 exit($status eq 'OK' ?
0 : -1);
35 my $nodename = PVE
::INotify
::nodename
();
41 while ( ++$c < 10 && !-e
$path ) { sleep(1); }
43 my $s = IO
::Socket
::UNIX-
>new(Peer
=> $path, Timeout
=> 120);
45 die "unable to connect to socket '$path' - $!" if !$s;
47 my $select = new IO
::Select
;
49 $select->add(\
*STDIN
);
52 my $timeout = 60*15; # 15 minutes
55 while ($select->count &&
56 scalar(@handles = $select->can_read ($timeout))) {
57 foreach my $h (@handles) {
59 my $n = $h->sysread($buf, 4096);
69 syswrite(\
*STDOUT
, $buf);
79 sub print_recursive_hash
{
80 my ($prefix, $hash, $key) = @_;
82 if (ref($hash) eq 'HASH') {
84 print "$prefix$key:\n";
86 foreach my $itemkey (keys %$hash) {
87 print_recursive_hash
("\t$prefix", $hash->{$itemkey}, $itemkey);
89 } elsif (ref($hash) eq 'ARRAY') {
91 print "$prefix$key:\n";
93 foreach my $item (@$hash) {
94 print_recursive_hash
("\t$prefix", $item);
96 } elsif (!ref($hash) && defined($hash)) {
98 print "$prefix$key: $hash\n";
100 print "$prefix$hash\n";
105 __PACKAGE__-
>register_method ({
109 description
=> "Show command line which is used to start the VM (debug info).",
111 additionalProperties
=> 0,
113 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
116 returns
=> { type
=> 'null'},
120 my $storecfg = PVE
::Storage
::config
();
121 print PVE
::QemuServer
::vm_commandline
($storecfg, $param->{vmid
}) . "\n";
126 __PACKAGE__-
>register_method ({
130 description
=> "Show VM status.",
132 additionalProperties
=> 0,
134 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
136 description
=> "Verbose output format",
142 returns
=> { type
=> 'null'},
147 my $conf = PVE
::QemuConfig-
>load_config ($param->{vmid
});
149 my $vmstatus = PVE
::QemuServer
::vmstatus
($param->{vmid
}, 1);
150 my $stat = $vmstatus->{$param->{vmid
}};
151 if ($param->{verbose
}) {
152 foreach my $k (sort (keys %$stat)) {
153 next if $k eq 'cpu' || $k eq 'relcpu'; # always 0
155 print_recursive_hash
("", $v, $k);
158 my $status = $stat->{qmpstatus
} || 'unknown';
159 print "status: $status\n";
165 __PACKAGE__-
>register_method ({
169 description
=> "Proxy VM VNC traffic to stdin/stdout",
171 additionalProperties
=> 0,
173 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
176 returns
=> { type
=> 'null'},
180 my $vmid = $param->{vmid
};
181 my $vnc_socket = PVE
::QemuServer
::vnc_socket
($vmid);
183 if (my $ticket = $ENV{LC_PVE_TICKET
}) { # NOTE: ssh on debian only pass LC_* variables
184 PVE
::QemuServer
::vm_mon_cmd
($vmid, "change", device
=> 'vnc', target
=> "unix:$vnc_socket,password");
185 PVE
::QemuServer
::vm_mon_cmd
($vmid, "set_password", protocol
=> 'vnc', password
=> $ticket);
186 PVE
::QemuServer
::vm_mon_cmd
($vmid, "expire_password", protocol
=> 'vnc', time => "+30");
188 PVE
::QemuServer
::vm_mon_cmd
($vmid, "change", device
=> 'vnc', target
=> "unix:$vnc_socket,x509,password");
191 run_vnc_proxy
($vnc_socket);
196 __PACKAGE__-
>register_method ({
200 description
=> "Unlock the VM.",
202 additionalProperties
=> 0,
204 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
207 returns
=> { type
=> 'null'},
211 my $vmid = $param->{vmid
};
213 PVE
::QemuConfig-
>lock_config ($vmid, sub {
214 my $conf = PVE
::QemuConfig-
>load_config($vmid);
215 delete $conf->{lock};
216 delete $conf->{pending
}->{lock} if $conf->{pending
}; # just to be sure
217 PVE
::QemuConfig-
>write_config($vmid, $conf);
223 __PACKAGE__-
>register_method ({
227 description
=> "Stop embedded nbd server.",
229 additionalProperties
=> 0,
231 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
234 returns
=> { type
=> 'null'},
238 my $vmid = $param->{vmid
};
240 PVE
::QemuServer
::nbd_stop
($vmid);
245 __PACKAGE__-
>register_method ({
249 description
=> "Used by qmigrate - do not use manually.",
251 additionalProperties
=> 0,
254 returns
=> { type
=> 'null'},
258 if (!PVE
::Cluster
::check_cfs_quorum
(1)) {
263 print "tunnel online\n";
266 while (my $line = <>) {
268 last if $line =~ m/^quit$/;
274 __PACKAGE__-
>register_method ({
278 description
=> "Wait until the VM is stopped.",
280 additionalProperties
=> 0,
282 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
284 description
=> "Timeout in seconds. Default is to wait forever.",
291 returns
=> { type
=> 'null'},
295 my $vmid = $param->{vmid
};
296 my $timeout = $param->{timeout
};
298 my $pid = PVE
::QemuServer
::check_running
($vmid);
301 print "waiting until VM $vmid stopps (PID $pid)\n";
304 while ((!$timeout || ($count < $timeout)) && PVE
::QemuServer
::check_running
($vmid)) {
309 die "wait failed - got timeout\n" if PVE
::QemuServer
::check_running
($vmid);
314 __PACKAGE__-
>register_method ({
318 description
=> "Enter Qemu Monitor interface.",
320 additionalProperties
=> 0,
322 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
325 returns
=> { type
=> 'null'},
329 my $vmid = $param->{vmid
};
331 my $conf = PVE
::QemuConfig-
>load_config ($vmid); # check if VM exists
333 print "Entering Qemu Monitor for VM $vmid - type 'help' for help\n";
335 my $term = new Term
::ReadLine
('qm');
338 while (defined ($input = $term->readline('qm> '))) {
341 next if $input =~ m/^\s*$/;
343 last if $input =~ m/^\s*q(uit)?\s*$/;
346 print PVE
::QemuServer
::vm_human_monitor_command
($vmid, $input);
348 print "ERROR: $@" if $@;
355 __PACKAGE__-
>register_method ({
359 description
=> "Rescan all storages and update disk sizes and unused disk images.",
361 additionalProperties
=> 0,
363 vmid
=> get_standard_option
('pve-vmid', {
365 completion
=> \
&PVE
::QemuServer
::complete_vmid
,
369 returns
=> { type
=> 'null'},
373 PVE
::QemuServer
::rescan
($param->{vmid
});
378 __PACKAGE__-
>register_method ({
382 description
=> "Open a terminal using a serial device (The VM need to have a serial device configured, for example 'serial0: socket')",
384 additionalProperties
=> 0,
386 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
388 description
=> "Select the serial device. By default we simply use the first suitable device.",
391 enum
=> [qw(serial0 serial1 serial2 serial3)],
395 returns
=> { type
=> 'null'},
399 my $vmid = $param->{vmid
};
401 my $conf = PVE
::QemuConfig-
>load_config ($vmid); # check if VM exists
403 my $iface = $param->{iface
};
406 die "serial interface '$iface' is not configured\n" if !$conf->{$iface};
407 die "wrong serial type on interface '$iface'\n" if $conf->{$iface} ne 'socket';
409 foreach my $opt (qw(serial0 serial1 serial2 serial3)) {
410 if ($conf->{$opt} && ($conf->{$opt} eq 'socket')) {
415 die "unable to find a serial interface\n" if !$iface;
418 die "VM $vmid not running\n" if !PVE
::QemuServer
::check_running
($vmid);
420 my $socket = "/var/run/qemu-server/${vmid}.$iface";
422 my $cmd = "socat UNIX-CONNECT:$socket STDIO,raw,echo=0,escape=0x0f";
424 print "starting serial terminal on interface $iface (press control-O to exit)\n";
432 my $print_agent_result = sub {
435 my $result = $data->{result
};
436 return if !defined($result);
438 my $class = ref($result);
442 return if $result =~ m/^\s*$/;
447 if (($class eq 'HASH') && !scalar(keys %$result)) { # empty hash
451 print to_json
($result, { pretty
=> 1, canonical
=> 1});
455 list
=> [ "PVE::API2::Qemu", 'vmlist', [],
456 { node
=> $nodename }, sub {
459 exit 0 if (!scalar(@$vmlist));
461 printf "%10s %-20s %-10s %-10s %12s %-10s\n",
462 qw(VMID NAME STATUS MEM(MB) BOOTDISK
(GB
) PID
);
464 foreach my $rec (sort { $a->{vmid
} <=> $b->{vmid
} } @$vmlist) {
465 printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid
}, $rec->{name
},
466 $rec->{qmpstatus
} || $rec->{status
},
467 ($rec->{maxmem
} || 0)/(1024*1024),
468 ($rec->{maxdisk
} || 0)/(1024*1024*1024),
475 create
=> [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node
=> $nodename }, $upid_exit ],
477 destroy
=> [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { node
=> $nodename }, $upid_exit ],
479 clone
=> [ "PVE::API2::Qemu", 'clone_vm', ['vmid', 'newid'], { node
=> $nodename }, $upid_exit ],
481 migrate
=> [ "PVE::API2::Qemu", 'migrate_vm', ['vmid', 'target'], { node
=> $nodename }, $upid_exit ],
483 set
=> [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node
=> $nodename } ],
485 resize
=> [ "PVE::API2::Qemu", 'resize_vm', ['vmid', 'disk', 'size'], { node
=> $nodename } ],
487 move_disk
=> [ "PVE::API2::Qemu", 'move_vm_disk', ['vmid', 'disk', 'storage'], { node
=> $nodename }, $upid_exit ],
489 unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid'], { node
=> $nodename } ],
491 config
=> [ "PVE::API2::Qemu", 'vm_config', ['vmid'],
492 { node
=> $nodename }, sub {
494 foreach my $k (sort (keys %$config)) {
495 next if $k eq 'digest';
496 my $v = $config->{$k};
497 if ($k eq 'description') {
498 $v = PVE
::Tools
::encode_text
($v);
504 pending
=> [ "PVE::API2::Qemu", 'vm_pending', ['vmid'],
505 { node
=> $nodename }, sub {
507 foreach my $item (sort { $a->{key
} cmp $b->{key
}} @$data) {
508 my $k = $item->{key
};
509 next if $k eq 'digest';
510 my $v = $item->{value
};
511 my $p = $item->{pending
};
512 if ($k eq 'description') {
513 $v = PVE
::Tools
::encode_text
($v) if defined($v);
514 $p = PVE
::Tools
::encode_text
($p) if defined($p);
517 if ($item->{delete}) {
518 print "del $k: $v\n";
519 } elsif (defined($p)) {
520 print "cur $k: $v\n";
521 print "new $k: $p\n";
523 print "cur $k: $v\n";
525 } elsif (defined($p)) {
526 print "new $k: $p\n";
531 showcmd
=> [ __PACKAGE__
, 'showcmd', ['vmid']],
533 status
=> [ __PACKAGE__
, 'status', ['vmid']],
535 snapshot
=> [ "PVE::API2::Qemu", 'snapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
537 delsnapshot
=> [ "PVE::API2::Qemu", 'delsnapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
539 listsnapshot
=> [ "PVE::API2::Qemu", 'snapshot_list', ['vmid'], { node
=> $nodename },
542 foreach my $e (@$res) {
543 my $headline = $e->{description
} || 'no-description';
544 $headline =~ s/\n.*//sg;
545 my $parent = $e->{parent
} // 'no-parent';
546 printf("%-20s %-20s %s\n", $e->{name
}, $parent, $headline);
550 rollback
=> [ "PVE::API2::Qemu", 'rollback', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
552 template
=> [ "PVE::API2::Qemu", 'template', ['vmid'], { node
=> $nodename }],
554 start
=> [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { node
=> $nodename } , $upid_exit ],
556 stop
=> [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { node
=> $nodename }, $upid_exit ],
558 reset => [ "PVE::API2::Qemu", 'vm_reset', ['vmid'], { node
=> $nodename }, $upid_exit ],
560 shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { node
=> $nodename }, $upid_exit ],
562 suspend
=> [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { node
=> $nodename }, $upid_exit ],
564 resume
=> [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { node
=> $nodename }, $upid_exit ],
566 sendkey
=> [ "PVE::API2::Qemu", 'vm_sendkey', ['vmid', 'key'], { node
=> $nodename } ],
568 vncproxy
=> [ __PACKAGE__
, 'vncproxy', ['vmid']],
570 wait => [ __PACKAGE__
, 'wait', ['vmid']],
572 unlock
=> [ __PACKAGE__
, 'unlock', ['vmid']],
574 rescan
=> [ __PACKAGE__
, 'rescan', []],
576 monitor
=> [ __PACKAGE__
, 'monitor', ['vmid']],
578 agent
=> [ "PVE::API2::Qemu", 'agent', ['vmid', 'command'],
579 { node
=> $nodename }, $print_agent_result ],
581 mtunnel
=> [ __PACKAGE__
, 'mtunnel', []],
583 nbdstop
=> [ __PACKAGE__
, 'nbdstop', ['vmid']],
585 terminal
=> [ __PACKAGE__
, 'terminal', ['vmid']],