]>
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
();
37 sub setup_environment
{
38 PVE
::RPCEnvironment-
>setup_default_cli_env();
45 while ( ++$c < 10 && !-e
$path ) { sleep(1); }
47 my $s = IO
::Socket
::UNIX-
>new(Peer
=> $path, Timeout
=> 120);
49 die "unable to connect to socket '$path' - $!" if !$s;
51 my $select = new IO
::Select
;
53 $select->add(\
*STDIN
);
56 my $timeout = 60*15; # 15 minutes
59 while ($select->count &&
60 scalar(@handles = $select->can_read ($timeout))) {
61 foreach my $h (@handles) {
63 my $n = $h->sysread($buf, 4096);
73 syswrite(\
*STDOUT
, $buf);
83 sub print_recursive_hash
{
84 my ($prefix, $hash, $key) = @_;
86 if (ref($hash) eq 'HASH') {
88 print "$prefix$key:\n";
90 foreach my $itemkey (keys %$hash) {
91 print_recursive_hash
("\t$prefix", $hash->{$itemkey}, $itemkey);
93 } elsif (ref($hash) eq 'ARRAY') {
95 print "$prefix$key:\n";
97 foreach my $item (@$hash) {
98 print_recursive_hash
("\t$prefix", $item);
100 } elsif (!ref($hash) && defined($hash)) {
102 print "$prefix$key: $hash\n";
104 print "$prefix$hash\n";
109 __PACKAGE__-
>register_method ({
113 description
=> "Show command line which is used to start the VM (debug info).",
115 additionalProperties
=> 0,
117 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
120 returns
=> { type
=> 'null'},
124 my $storecfg = PVE
::Storage
::config
();
125 print PVE
::QemuServer
::vm_commandline
($storecfg, $param->{vmid
}) . "\n";
130 __PACKAGE__-
>register_method ({
134 description
=> "Show VM status.",
136 additionalProperties
=> 0,
138 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
140 description
=> "Verbose output format",
146 returns
=> { type
=> 'null'},
151 my $conf = PVE
::QemuConfig-
>load_config ($param->{vmid
});
153 my $vmstatus = PVE
::QemuServer
::vmstatus
($param->{vmid
}, 1);
154 my $stat = $vmstatus->{$param->{vmid
}};
155 if ($param->{verbose
}) {
156 foreach my $k (sort (keys %$stat)) {
157 next if $k eq 'cpu' || $k eq 'relcpu'; # always 0
159 print_recursive_hash
("", $v, $k);
162 my $status = $stat->{qmpstatus
} || 'unknown';
163 print "status: $status\n";
169 __PACKAGE__-
>register_method ({
173 description
=> "Proxy VM VNC traffic to stdin/stdout",
175 additionalProperties
=> 0,
177 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
180 returns
=> { type
=> 'null'},
184 my $vmid = $param->{vmid
};
185 my $vnc_socket = PVE
::QemuServer
::vnc_socket
($vmid);
187 if (my $ticket = $ENV{LC_PVE_TICKET
}) { # NOTE: ssh on debian only pass LC_* variables
188 PVE
::QemuServer
::vm_mon_cmd
($vmid, "change", device
=> 'vnc', target
=> "unix:$vnc_socket,password");
189 PVE
::QemuServer
::vm_mon_cmd
($vmid, "set_password", protocol
=> 'vnc', password
=> $ticket);
190 PVE
::QemuServer
::vm_mon_cmd
($vmid, "expire_password", protocol
=> 'vnc', time => "+30");
192 PVE
::QemuServer
::vm_mon_cmd
($vmid, "change", device
=> 'vnc', target
=> "unix:$vnc_socket,x509,password");
195 run_vnc_proxy
($vnc_socket);
200 __PACKAGE__-
>register_method ({
204 description
=> "Unlock the VM.",
206 additionalProperties
=> 0,
208 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
211 returns
=> { type
=> 'null'},
215 my $vmid = $param->{vmid
};
217 PVE
::QemuConfig-
>lock_config ($vmid, sub {
218 my $conf = PVE
::QemuConfig-
>load_config($vmid);
219 delete $conf->{lock};
220 delete $conf->{pending
}->{lock} if $conf->{pending
}; # just to be sure
221 PVE
::QemuConfig-
>write_config($vmid, $conf);
227 __PACKAGE__-
>register_method ({
231 description
=> "Stop embedded nbd server.",
233 additionalProperties
=> 0,
235 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
238 returns
=> { type
=> 'null'},
242 my $vmid = $param->{vmid
};
244 PVE
::QemuServer
::nbd_stop
($vmid);
249 __PACKAGE__-
>register_method ({
253 description
=> "Used by qmigrate - do not use manually.",
255 additionalProperties
=> 0,
258 returns
=> { type
=> 'null'},
262 if (!PVE
::Cluster
::check_cfs_quorum
(1)) {
267 print "tunnel online\n";
270 while (my $line = <>) {
272 last if $line =~ m/^quit$/;
278 __PACKAGE__-
>register_method ({
282 description
=> "Wait until the VM is stopped.",
284 additionalProperties
=> 0,
286 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
288 description
=> "Timeout in seconds. Default is to wait forever.",
295 returns
=> { type
=> 'null'},
299 my $vmid = $param->{vmid
};
300 my $timeout = $param->{timeout
};
302 my $pid = PVE
::QemuServer
::check_running
($vmid);
305 print "waiting until VM $vmid stopps (PID $pid)\n";
308 while ((!$timeout || ($count < $timeout)) && PVE
::QemuServer
::check_running
($vmid)) {
313 die "wait failed - got timeout\n" if PVE
::QemuServer
::check_running
($vmid);
318 __PACKAGE__-
>register_method ({
322 description
=> "Enter Qemu Monitor interface.",
324 additionalProperties
=> 0,
326 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
329 returns
=> { type
=> 'null'},
333 my $vmid = $param->{vmid
};
335 my $conf = PVE
::QemuConfig-
>load_config ($vmid); # check if VM exists
337 print "Entering Qemu Monitor for VM $vmid - type 'help' for help\n";
339 my $term = new Term
::ReadLine
('qm');
342 while (defined ($input = $term->readline('qm> '))) {
345 next if $input =~ m/^\s*$/;
347 last if $input =~ m/^\s*q(uit)?\s*$/;
350 print PVE
::QemuServer
::vm_human_monitor_command
($vmid, $input);
352 print "ERROR: $@" if $@;
359 __PACKAGE__-
>register_method ({
363 description
=> "Rescan all storages and update disk sizes and unused disk images.",
365 additionalProperties
=> 0,
367 vmid
=> get_standard_option
('pve-vmid', {
369 completion
=> \
&PVE
::QemuServer
::complete_vmid
,
373 returns
=> { type
=> 'null'},
377 PVE
::QemuServer
::rescan
($param->{vmid
});
382 __PACKAGE__-
>register_method ({
386 description
=> "Open a terminal using a serial device (The VM need to have a serial device configured, for example 'serial0: socket')",
388 additionalProperties
=> 0,
390 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
392 description
=> "Select the serial device. By default we simply use the first suitable device.",
395 enum
=> [qw(serial0 serial1 serial2 serial3)],
399 returns
=> { type
=> 'null'},
403 my $vmid = $param->{vmid
};
405 my $conf = PVE
::QemuConfig-
>load_config ($vmid); # check if VM exists
407 my $iface = $param->{iface
};
410 die "serial interface '$iface' is not configured\n" if !$conf->{$iface};
411 die "wrong serial type on interface '$iface'\n" if $conf->{$iface} ne 'socket';
413 foreach my $opt (qw(serial0 serial1 serial2 serial3)) {
414 if ($conf->{$opt} && ($conf->{$opt} eq 'socket')) {
419 die "unable to find a serial interface\n" if !$iface;
422 die "VM $vmid not running\n" if !PVE
::QemuServer
::check_running
($vmid);
424 my $socket = "/var/run/qemu-server/${vmid}.$iface";
426 my $cmd = "socat UNIX-CONNECT:$socket STDIO,raw,echo=0,escape=0x0f";
428 print "starting serial terminal on interface $iface (press control-O to exit)\n";
436 my $print_agent_result = sub {
439 my $result = $data->{result
};
440 return if !defined($result);
442 my $class = ref($result);
446 return if $result =~ m/^\s*$/;
451 if (($class eq 'HASH') && !scalar(keys %$result)) { # empty hash
455 print to_json
($result, { pretty
=> 1, canonical
=> 1});
459 list
=> [ "PVE::API2::Qemu", 'vmlist', [],
460 { node
=> $nodename }, sub {
463 exit 0 if (!scalar(@$vmlist));
465 printf "%10s %-20s %-10s %-10s %12s %-10s\n",
466 qw(VMID NAME STATUS MEM(MB) BOOTDISK
(GB
) PID
);
468 foreach my $rec (sort { $a->{vmid
} <=> $b->{vmid
} } @$vmlist) {
469 printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid
}, $rec->{name
},
470 $rec->{qmpstatus
} || $rec->{status
},
471 ($rec->{maxmem
} || 0)/(1024*1024),
472 ($rec->{maxdisk
} || 0)/(1024*1024*1024),
479 create
=> [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node
=> $nodename }, $upid_exit ],
481 destroy
=> [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { node
=> $nodename }, $upid_exit ],
483 clone
=> [ "PVE::API2::Qemu", 'clone_vm', ['vmid', 'newid'], { node
=> $nodename }, $upid_exit ],
485 migrate
=> [ "PVE::API2::Qemu", 'migrate_vm', ['vmid', 'target'], { node
=> $nodename }, $upid_exit ],
487 set
=> [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node
=> $nodename } ],
489 resize
=> [ "PVE::API2::Qemu", 'resize_vm', ['vmid', 'disk', 'size'], { node
=> $nodename } ],
491 move_disk
=> [ "PVE::API2::Qemu", 'move_vm_disk', ['vmid', 'disk', 'storage'], { node
=> $nodename }, $upid_exit ],
493 unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid'], { node
=> $nodename } ],
495 config
=> [ "PVE::API2::Qemu", 'vm_config', ['vmid'],
496 { node
=> $nodename }, sub {
498 foreach my $k (sort (keys %$config)) {
499 next if $k eq 'digest';
500 my $v = $config->{$k};
501 if ($k eq 'description') {
502 $v = PVE
::Tools
::encode_text
($v);
508 pending
=> [ "PVE::API2::Qemu", 'vm_pending', ['vmid'],
509 { node
=> $nodename }, sub {
511 foreach my $item (sort { $a->{key
} cmp $b->{key
}} @$data) {
512 my $k = $item->{key
};
513 next if $k eq 'digest';
514 my $v = $item->{value
};
515 my $p = $item->{pending
};
516 if ($k eq 'description') {
517 $v = PVE
::Tools
::encode_text
($v) if defined($v);
518 $p = PVE
::Tools
::encode_text
($p) if defined($p);
521 if ($item->{delete}) {
522 print "del $k: $v\n";
523 } elsif (defined($p)) {
524 print "cur $k: $v\n";
525 print "new $k: $p\n";
527 print "cur $k: $v\n";
529 } elsif (defined($p)) {
530 print "new $k: $p\n";
535 showcmd
=> [ __PACKAGE__
, 'showcmd', ['vmid']],
537 status
=> [ __PACKAGE__
, 'status', ['vmid']],
539 snapshot
=> [ "PVE::API2::Qemu", 'snapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
541 delsnapshot
=> [ "PVE::API2::Qemu", 'delsnapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
543 listsnapshot
=> [ "PVE::API2::Qemu", 'snapshot_list', ['vmid'], { node
=> $nodename },
546 foreach my $e (@$res) {
547 my $headline = $e->{description
} || 'no-description';
548 $headline =~ s/\n.*//sg;
549 my $parent = $e->{parent
} // 'no-parent';
550 printf("%-20s %-20s %s\n", $e->{name
}, $parent, $headline);
554 rollback
=> [ "PVE::API2::Qemu", 'rollback', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
556 template
=> [ "PVE::API2::Qemu", 'template', ['vmid'], { node
=> $nodename }],
558 start
=> [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { node
=> $nodename } , $upid_exit ],
560 stop
=> [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { node
=> $nodename }, $upid_exit ],
562 reset => [ "PVE::API2::Qemu", 'vm_reset', ['vmid'], { node
=> $nodename }, $upid_exit ],
564 shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { node
=> $nodename }, $upid_exit ],
566 suspend
=> [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { node
=> $nodename }, $upid_exit ],
568 resume
=> [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { node
=> $nodename }, $upid_exit ],
570 sendkey
=> [ "PVE::API2::Qemu", 'vm_sendkey', ['vmid', 'key'], { node
=> $nodename } ],
572 vncproxy
=> [ __PACKAGE__
, 'vncproxy', ['vmid']],
574 wait => [ __PACKAGE__
, 'wait', ['vmid']],
576 unlock
=> [ __PACKAGE__
, 'unlock', ['vmid']],
578 rescan
=> [ __PACKAGE__
, 'rescan', []],
580 monitor
=> [ __PACKAGE__
, 'monitor', ['vmid']],
582 agent
=> [ "PVE::API2::Qemu", 'agent', ['vmid', 'command'],
583 { node
=> $nodename }, $print_agent_result ],
585 mtunnel
=> [ __PACKAGE__
, 'mtunnel', []],
587 nbdstop
=> [ __PACKAGE__
, 'nbdstop', ['vmid']],
589 terminal
=> [ __PACKAGE__
, 'terminal', ['vmid']],