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
;
20 use PVE
::QemuServer
::ImportDisk
;
23 use PVE
::JSONSchema
qw(get_standard_option);
28 use base
qw(PVE::CLIHandler);
32 my $status = PVE
::Tools
::upid_read_status
($upid);
33 exit($status eq 'OK' ?
0 : -1);
36 my $nodename = PVE
::INotify
::nodename
();
38 sub setup_environment
{
39 PVE
::RPCEnvironment-
>setup_default_cli_env();
46 while ( ++$c < 10 && !-e
$path ) { sleep(1); }
48 my $s = IO
::Socket
::UNIX-
>new(Peer
=> $path, Timeout
=> 120);
50 die "unable to connect to socket '$path' - $!" if !$s;
52 my $select = new IO
::Select
;
54 $select->add(\
*STDIN
);
57 my $timeout = 60*15; # 15 minutes
60 while ($select->count &&
61 scalar(@handles = $select->can_read ($timeout))) {
62 foreach my $h (@handles) {
64 my $n = $h->sysread($buf, 4096);
74 syswrite(\
*STDOUT
, $buf);
84 sub print_recursive_hash
{
85 my ($prefix, $hash, $key) = @_;
87 if (ref($hash) eq 'HASH') {
89 print "$prefix$key:\n";
91 foreach my $itemkey (keys %$hash) {
92 print_recursive_hash
("\t$prefix", $hash->{$itemkey}, $itemkey);
94 } elsif (ref($hash) eq 'ARRAY') {
96 print "$prefix$key:\n";
98 foreach my $item (@$hash) {
99 print_recursive_hash
("\t$prefix", $item);
101 } elsif (!ref($hash) && defined($hash)) {
103 print "$prefix$key: $hash\n";
105 print "$prefix$hash\n";
110 __PACKAGE__-
>register_method ({
114 description
=> "Show command line which is used to start the VM (debug info).",
116 additionalProperties
=> 0,
118 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
121 returns
=> { type
=> 'null'},
125 my $storecfg = PVE
::Storage
::config
();
126 print PVE
::QemuServer
::vm_commandline
($storecfg, $param->{vmid
}) . "\n";
131 __PACKAGE__-
>register_method ({
135 description
=> "Show VM status.",
137 additionalProperties
=> 0,
139 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
141 description
=> "Verbose output format",
147 returns
=> { type
=> 'null'},
152 my $conf = PVE
::QemuConfig-
>load_config ($param->{vmid
});
154 my $vmstatus = PVE
::QemuServer
::vmstatus
($param->{vmid
}, 1);
155 my $stat = $vmstatus->{$param->{vmid
}};
156 if ($param->{verbose
}) {
157 foreach my $k (sort (keys %$stat)) {
158 next if $k eq 'cpu' || $k eq 'relcpu'; # always 0
160 print_recursive_hash
("", $v, $k);
163 my $status = $stat->{qmpstatus
} || 'unknown';
164 print "status: $status\n";
170 __PACKAGE__-
>register_method ({
174 description
=> "Proxy VM VNC traffic to stdin/stdout",
176 additionalProperties
=> 0,
178 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
181 returns
=> { type
=> 'null'},
185 my $vmid = $param->{vmid
};
186 my $vnc_socket = PVE
::QemuServer
::vnc_socket
($vmid);
188 if (my $ticket = $ENV{LC_PVE_TICKET
}) { # NOTE: ssh on debian only pass LC_* variables
189 PVE
::QemuServer
::vm_mon_cmd
($vmid, "change", device
=> 'vnc', target
=> "unix:$vnc_socket,password");
190 PVE
::QemuServer
::vm_mon_cmd
($vmid, "set_password", protocol
=> 'vnc', password
=> $ticket);
191 PVE
::QemuServer
::vm_mon_cmd
($vmid, "expire_password", protocol
=> 'vnc', time => "+30");
193 PVE
::QemuServer
::vm_mon_cmd
($vmid, "change", device
=> 'vnc', target
=> "unix:$vnc_socket,x509,password");
196 run_vnc_proxy
($vnc_socket);
201 __PACKAGE__-
>register_method ({
205 description
=> "Unlock the VM.",
207 additionalProperties
=> 0,
209 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
212 returns
=> { type
=> 'null'},
216 my $vmid = $param->{vmid
};
218 PVE
::QemuConfig-
>lock_config ($vmid, sub {
219 my $conf = PVE
::QemuConfig-
>load_config($vmid);
220 delete $conf->{lock};
221 delete $conf->{pending
}->{lock} if $conf->{pending
}; # just to be sure
222 PVE
::QemuConfig-
>write_config($vmid, $conf);
228 __PACKAGE__-
>register_method ({
232 description
=> "Stop embedded nbd server.",
234 additionalProperties
=> 0,
236 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
239 returns
=> { type
=> 'null'},
243 my $vmid = $param->{vmid
};
245 PVE
::QemuServer
::nbd_stop
($vmid);
250 __PACKAGE__-
>register_method ({
254 description
=> "Used by qmigrate - do not use manually.",
256 additionalProperties
=> 0,
259 returns
=> { type
=> 'null'},
263 if (!PVE
::Cluster
::check_cfs_quorum
(1)) {
268 my $tunnel_write = sub {
275 $tunnel_write->("tunnel online");
276 $tunnel_write->("ver 1");
278 while (my $line = <>) {
280 if ($line =~ /^quit$/) {
281 $tunnel_write->("OK");
283 } elsif ($line =~ /^resume (\d+)$/) {
285 if (PVE
::QemuServer
::check_running
($vmid, 1)) {
286 eval { PVE
::QemuServer
::vm_resume
($vmid, 1, 1); };
288 $tunnel_write->("ERR: resume failed - $@");
290 $tunnel_write->("OK");
293 $tunnel_write->("ERR: resume failed - VM $vmid not running");
301 __PACKAGE__-
>register_method ({
305 description
=> "Wait until the VM is stopped.",
307 additionalProperties
=> 0,
309 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
311 description
=> "Timeout in seconds. Default is to wait forever.",
318 returns
=> { type
=> 'null'},
322 my $vmid = $param->{vmid
};
323 my $timeout = $param->{timeout
};
325 my $pid = PVE
::QemuServer
::check_running
($vmid);
328 print "waiting until VM $vmid stopps (PID $pid)\n";
331 while ((!$timeout || ($count < $timeout)) && PVE
::QemuServer
::check_running
($vmid)) {
336 die "wait failed - got timeout\n" if PVE
::QemuServer
::check_running
($vmid);
341 __PACKAGE__-
>register_method ({
345 description
=> "Enter Qemu Monitor interface.",
347 additionalProperties
=> 0,
349 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
352 returns
=> { type
=> 'null'},
356 my $vmid = $param->{vmid
};
358 my $conf = PVE
::QemuConfig-
>load_config ($vmid); # check if VM exists
360 print "Entering Qemu Monitor for VM $vmid - type 'help' for help\n";
362 my $term = new Term
::ReadLine
('qm');
365 while (defined ($input = $term->readline('qm> '))) {
368 next if $input =~ m/^\s*$/;
370 last if $input =~ m/^\s*q(uit)?\s*$/;
373 print PVE
::QemuServer
::vm_human_monitor_command
($vmid, $input);
375 print "ERROR: $@" if $@;
382 __PACKAGE__-
>register_method ({
386 description
=> "Rescan all storages and update disk sizes and unused disk images.",
388 additionalProperties
=> 0,
390 vmid
=> get_standard_option
('pve-vmid', {
392 completion
=> \
&PVE
::QemuServer
::complete_vmid
,
396 returns
=> { type
=> 'null'},
400 PVE
::QemuServer
::rescan
($param->{vmid
});
405 __PACKAGE__-
>register_method ({
406 name
=> 'importdisk',
407 path
=> 'importdisk',
409 description
=> "Import an external disk image as an unused disk in a VM. The
410 image format has to be supported by qemu-img(1).",
412 additionalProperties
=> 0,
414 vmid
=> get_standard_option
('pve-vmid', {completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
416 description
=> 'Path to the disk image to import',
420 storage
=> get_standard_option
('pve-storage-id', {
421 description
=> 'Target storage ID',
422 completion
=> \
&PVE
::QemuServer
::complete_storage
,
427 description
=> 'Target format',
428 enum
=> [ 'raw', 'qcow2', 'vmdk' ],
433 returns
=> { type
=> 'null'},
437 my $vmid = extract_param
($param, 'vmid');
438 my $source = extract_param
($param, 'source');
439 my $storeid = extract_param
($param, 'storage');
440 my $format = extract_param
($param, 'format');
442 my $vm_conf = PVE
::QemuConfig-
>load_config($vmid);
443 PVE
::QemuConfig-
>check_lock($vm_conf);
444 die "$source: non-existent or non-regular file\n" if (! -f
$source);
446 my $storecfg = PVE
::Storage
::config
();
447 PVE
::Storage
::storage_check_enabled
($storecfg, $storeid);
449 my $target_storage_config =
450 PVE
::Storage
::storage_config
($storecfg, $storeid);
451 die "storage $storeid does not support vm images\n"
452 if !$target_storage_config->{content
}->{images
};
454 PVE
::QemuServer
::ImportDisk
::do_import
($source, $vmid, $storeid, { format
=> $format });
459 __PACKAGE__-
>register_method ({
463 description
=> "Open a terminal using a serial device (The VM need to have a serial device configured, for example 'serial0: socket')",
465 additionalProperties
=> 0,
467 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
469 description
=> "Select the serial device. By default we simply use the first suitable device.",
472 enum
=> [qw(serial0 serial1 serial2 serial3)],
476 returns
=> { type
=> 'null'},
480 my $vmid = $param->{vmid
};
482 my $conf = PVE
::QemuConfig-
>load_config ($vmid); # check if VM exists
484 my $iface = $param->{iface
};
487 die "serial interface '$iface' is not configured\n" if !$conf->{$iface};
488 die "wrong serial type on interface '$iface'\n" if $conf->{$iface} ne 'socket';
490 foreach my $opt (qw(serial0 serial1 serial2 serial3)) {
491 if ($conf->{$opt} && ($conf->{$opt} eq 'socket')) {
496 die "unable to find a serial interface\n" if !$iface;
499 die "VM $vmid not running\n" if !PVE
::QemuServer
::check_running
($vmid);
501 my $socket = "/var/run/qemu-server/${vmid}.$iface";
503 my $cmd = "socat UNIX-CONNECT:$socket STDIO,raw,echo=0,escape=0x0f";
505 print "starting serial terminal on interface $iface (press control-O to exit)\n";
513 my $print_agent_result = sub {
516 my $result = $data->{result
};
517 return if !defined($result);
519 my $class = ref($result);
523 return if $result =~ m/^\s*$/;
528 if (($class eq 'HASH') && !scalar(keys %$result)) { # empty hash
532 print to_json
($result, { pretty
=> 1, canonical
=> 1});
536 list
=> [ "PVE::API2::Qemu", 'vmlist', [],
537 { node
=> $nodename }, sub {
540 exit 0 if (!scalar(@$vmlist));
542 printf "%10s %-20s %-10s %-10s %12s %-10s\n",
543 qw(VMID NAME STATUS MEM(MB) BOOTDISK
(GB
) PID
);
545 foreach my $rec (sort { $a->{vmid
} <=> $b->{vmid
} } @$vmlist) {
546 printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid
}, $rec->{name
},
547 $rec->{qmpstatus
} || $rec->{status
},
548 ($rec->{maxmem
} || 0)/(1024*1024),
549 ($rec->{maxdisk
} || 0)/(1024*1024*1024),
556 create
=> [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node
=> $nodename }, $upid_exit ],
558 destroy
=> [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { node
=> $nodename }, $upid_exit ],
560 clone
=> [ "PVE::API2::Qemu", 'clone_vm', ['vmid', 'newid'], { node
=> $nodename }, $upid_exit ],
562 migrate
=> [ "PVE::API2::Qemu", 'migrate_vm', ['vmid', 'target'], { node
=> $nodename }, $upid_exit ],
564 set
=> [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node
=> $nodename } ],
566 resize
=> [ "PVE::API2::Qemu", 'resize_vm', ['vmid', 'disk', 'size'], { node
=> $nodename } ],
568 move_disk
=> [ "PVE::API2::Qemu", 'move_vm_disk', ['vmid', 'disk', 'storage'], { node
=> $nodename }, $upid_exit ],
570 unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid'], { node
=> $nodename } ],
572 config
=> [ "PVE::API2::Qemu", 'vm_config', ['vmid'],
573 { node
=> $nodename }, sub {
575 foreach my $k (sort (keys %$config)) {
576 next if $k eq 'digest';
577 my $v = $config->{$k};
578 if ($k eq 'description') {
579 $v = PVE
::Tools
::encode_text
($v);
585 pending
=> [ "PVE::API2::Qemu", 'vm_pending', ['vmid'],
586 { node
=> $nodename }, sub {
588 foreach my $item (sort { $a->{key
} cmp $b->{key
}} @$data) {
589 my $k = $item->{key
};
590 next if $k eq 'digest';
591 my $v = $item->{value
};
592 my $p = $item->{pending
};
593 if ($k eq 'description') {
594 $v = PVE
::Tools
::encode_text
($v) if defined($v);
595 $p = PVE
::Tools
::encode_text
($p) if defined($p);
598 if ($item->{delete}) {
599 print "del $k: $v\n";
600 } elsif (defined($p)) {
601 print "cur $k: $v\n";
602 print "new $k: $p\n";
604 print "cur $k: $v\n";
606 } elsif (defined($p)) {
607 print "new $k: $p\n";
612 showcmd
=> [ __PACKAGE__
, 'showcmd', ['vmid']],
614 status
=> [ __PACKAGE__
, 'status', ['vmid']],
616 snapshot
=> [ "PVE::API2::Qemu", 'snapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
618 delsnapshot
=> [ "PVE::API2::Qemu", 'delsnapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
620 listsnapshot
=> [ "PVE::API2::Qemu", 'snapshot_list', ['vmid'], { node
=> $nodename },
623 foreach my $e (@$res) {
624 my $headline = $e->{description
} || 'no-description';
625 $headline =~ s/\n.*//sg;
626 my $parent = $e->{parent
} // 'no-parent';
627 printf("%-20s %-20s %s\n", $e->{name
}, $parent, $headline);
631 rollback
=> [ "PVE::API2::Qemu", 'rollback', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
633 template
=> [ "PVE::API2::Qemu", 'template', ['vmid'], { node
=> $nodename }],
635 start
=> [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { node
=> $nodename } , $upid_exit ],
637 stop
=> [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { node
=> $nodename }, $upid_exit ],
639 reset => [ "PVE::API2::Qemu", 'vm_reset', ['vmid'], { node
=> $nodename }, $upid_exit ],
641 shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { node
=> $nodename }, $upid_exit ],
643 suspend
=> [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { node
=> $nodename }, $upid_exit ],
645 resume
=> [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { node
=> $nodename }, $upid_exit ],
647 sendkey
=> [ "PVE::API2::Qemu", 'vm_sendkey', ['vmid', 'key'], { node
=> $nodename } ],
649 vncproxy
=> [ __PACKAGE__
, 'vncproxy', ['vmid']],
651 wait => [ __PACKAGE__
, 'wait', ['vmid']],
653 unlock
=> [ __PACKAGE__
, 'unlock', ['vmid']],
655 rescan
=> [ __PACKAGE__
, 'rescan', []],
657 monitor
=> [ __PACKAGE__
, 'monitor', ['vmid']],
659 agent
=> [ "PVE::API2::Qemu", 'agent', ['vmid', 'command'],
660 { node
=> $nodename }, $print_agent_result ],
662 mtunnel
=> [ __PACKAGE__
, 'mtunnel', []],
664 nbdstop
=> [ __PACKAGE__
, 'nbdstop', ['vmid']],
666 terminal
=> [ __PACKAGE__
, 'terminal', ['vmid']],
668 importdisk
=> [ __PACKAGE__
, 'importdisk', ['vmid', 'source', 'storage']],