]>
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);
15 use PVE
::Tools
qw(extract_param);
19 use PVE
::RPCEnvironment
;
21 use PVE
::QemuServer
::ImportDisk
;
22 use PVE
::QemuServer
::OVF
;
24 use PVE
::API2
::Qemu
::Agent
;
26 use PVE
::JSONSchema
qw(get_standard_option);
31 use base
qw(PVE::CLIHandler);
35 my $status = PVE
::Tools
::upid_read_status
($upid);
36 exit($status eq 'OK' ?
0 : -1);
39 my $nodename = PVE
::INotify
::nodename
();
41 sub setup_environment
{
42 PVE
::RPCEnvironment-
>setup_default_cli_env();
49 while ( ++$c < 10 && !-e
$path ) { sleep(1); }
51 my $s = IO
::Socket
::UNIX-
>new(Peer
=> $path, Timeout
=> 120);
53 die "unable to connect to socket '$path' - $!" if !$s;
55 my $select = new IO
::Select
;
57 $select->add(\
*STDIN
);
60 my $timeout = 60*15; # 15 minutes
63 while ($select->count &&
64 scalar(@handles = $select->can_read ($timeout))) {
65 foreach my $h (@handles) {
67 my $n = $h->sysread($buf, 4096);
77 syswrite(\
*STDOUT
, $buf);
87 sub print_recursive_hash
{
88 my ($prefix, $hash, $key) = @_;
90 if (ref($hash) eq 'HASH') {
92 print "$prefix$key:\n";
94 foreach my $itemkey (keys %$hash) {
95 print_recursive_hash
("\t$prefix", $hash->{$itemkey}, $itemkey);
97 } elsif (ref($hash) eq 'ARRAY') {
99 print "$prefix$key:\n";
101 foreach my $item (@$hash) {
102 print_recursive_hash
("\t$prefix", $item);
104 } elsif (!ref($hash) && defined($hash)) {
106 print "$prefix$key: $hash\n";
108 print "$prefix$hash\n";
113 __PACKAGE__-
>register_method ({
117 description
=> "Show command line which is used to start the VM (debug info).",
119 additionalProperties
=> 0,
121 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
123 description
=> "Puts each option on a new line to enhance human readability",
130 returns
=> { type
=> 'null'},
134 my $storecfg = PVE
::Storage
::config
();
135 my $cmdline = PVE
::QemuServer
::vm_commandline
($storecfg, $param->{vmid
});
137 $cmdline =~ s/ -/ \\\n -/g if $param->{pretty
};
144 __PACKAGE__-
>register_method ({
148 description
=> "Show VM status.",
150 additionalProperties
=> 0,
152 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
154 description
=> "Verbose output format",
160 returns
=> { type
=> 'null'},
165 my $conf = PVE
::QemuConfig-
>load_config ($param->{vmid
});
167 my $vmstatus = PVE
::QemuServer
::vmstatus
($param->{vmid
}, 1);
168 my $stat = $vmstatus->{$param->{vmid
}};
169 if ($param->{verbose
}) {
170 foreach my $k (sort (keys %$stat)) {
171 next if $k eq 'cpu' || $k eq 'relcpu'; # always 0
173 print_recursive_hash
("", $v, $k);
176 my $status = $stat->{qmpstatus
} || 'unknown';
177 print "status: $status\n";
183 __PACKAGE__-
>register_method ({
187 description
=> "Proxy VM VNC traffic to stdin/stdout",
189 additionalProperties
=> 0,
191 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
194 returns
=> { type
=> 'null'},
198 my $vmid = $param->{vmid
};
199 my $vnc_socket = PVE
::QemuServer
::vnc_socket
($vmid);
201 if (my $ticket = $ENV{LC_PVE_TICKET
}) { # NOTE: ssh on debian only pass LC_* variables
202 PVE
::QemuServer
::vm_mon_cmd
($vmid, "change", device
=> 'vnc', target
=> "unix:$vnc_socket,password");
203 PVE
::QemuServer
::vm_mon_cmd
($vmid, "set_password", protocol
=> 'vnc', password
=> $ticket);
204 PVE
::QemuServer
::vm_mon_cmd
($vmid, "expire_password", protocol
=> 'vnc', time => "+30");
206 PVE
::QemuServer
::vm_mon_cmd
($vmid, "change", device
=> 'vnc', target
=> "unix:$vnc_socket,x509,password");
209 run_vnc_proxy
($vnc_socket);
214 __PACKAGE__-
>register_method ({
218 description
=> "Unlock the VM.",
220 additionalProperties
=> 0,
222 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
225 returns
=> { type
=> 'null'},
229 my $vmid = $param->{vmid
};
231 PVE
::QemuConfig-
>lock_config ($vmid, sub {
232 my $conf = PVE
::QemuConfig-
>load_config($vmid);
233 delete $conf->{lock};
234 delete $conf->{pending
}->{lock} if $conf->{pending
}; # just to be sure
235 PVE
::QemuConfig-
>write_config($vmid, $conf);
241 __PACKAGE__-
>register_method ({
245 description
=> "Stop embedded nbd server.",
247 additionalProperties
=> 0,
249 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
252 returns
=> { type
=> 'null'},
256 my $vmid = $param->{vmid
};
258 PVE
::QemuServer
::nbd_stop
($vmid);
263 __PACKAGE__-
>register_method ({
267 description
=> "Used by qmigrate - do not use manually.",
269 additionalProperties
=> 0,
272 returns
=> { type
=> 'null'},
276 if (!PVE
::Cluster
::check_cfs_quorum
(1)) {
281 my $tunnel_write = sub {
288 $tunnel_write->("tunnel online");
289 $tunnel_write->("ver 1");
291 while (my $line = <STDIN
>) {
293 if ($line =~ /^quit$/) {
294 $tunnel_write->("OK");
296 } elsif ($line =~ /^resume (\d+)$/) {
298 if (PVE
::QemuServer
::check_running
($vmid, 1)) {
299 eval { PVE
::QemuServer
::vm_resume
($vmid, 1, 1); };
301 $tunnel_write->("ERR: resume failed - $@");
303 $tunnel_write->("OK");
306 $tunnel_write->("ERR: resume failed - VM $vmid not running");
314 __PACKAGE__-
>register_method ({
318 description
=> "Wait until the VM is stopped.",
320 additionalProperties
=> 0,
322 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
324 description
=> "Timeout in seconds. Default is to wait forever.",
331 returns
=> { type
=> 'null'},
335 my $vmid = $param->{vmid
};
336 my $timeout = $param->{timeout
};
338 my $pid = PVE
::QemuServer
::check_running
($vmid);
341 print "waiting until VM $vmid stopps (PID $pid)\n";
344 while ((!$timeout || ($count < $timeout)) && PVE
::QemuServer
::check_running
($vmid)) {
349 die "wait failed - got timeout\n" if PVE
::QemuServer
::check_running
($vmid);
354 __PACKAGE__-
>register_method ({
358 description
=> "Enter Qemu Monitor interface.",
360 additionalProperties
=> 0,
362 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
365 returns
=> { type
=> 'null'},
369 my $vmid = $param->{vmid
};
371 my $conf = PVE
::QemuConfig-
>load_config ($vmid); # check if VM exists
373 print "Entering Qemu Monitor for VM $vmid - type 'help' for help\n";
375 my $term = new Term
::ReadLine
('qm');
378 while (defined ($input = $term->readline('qm> '))) {
381 next if $input =~ m/^\s*$/;
383 last if $input =~ m/^\s*q(uit)?\s*$/;
386 print PVE
::QemuServer
::vm_human_monitor_command
($vmid, $input);
388 print "ERROR: $@" if $@;
395 __PACKAGE__-
>register_method ({
399 description
=> "Rescan all storages and update disk sizes and unused disk images.",
401 additionalProperties
=> 0,
403 vmid
=> get_standard_option
('pve-vmid', {
405 completion
=> \
&PVE
::QemuServer
::complete_vmid
,
409 returns
=> { type
=> 'null'},
413 PVE
::QemuServer
::rescan
($param->{vmid
});
418 __PACKAGE__-
>register_method ({
419 name
=> 'importdisk',
420 path
=> 'importdisk',
422 description
=> "Import an external disk image as an unused disk in a VM. The
423 image format has to be supported by qemu-img(1).",
425 additionalProperties
=> 0,
427 vmid
=> get_standard_option
('pve-vmid', {completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
429 description
=> 'Path to the disk image to import',
433 storage
=> get_standard_option
('pve-storage-id', {
434 description
=> 'Target storage ID',
435 completion
=> \
&PVE
::QemuServer
::complete_storage
,
440 description
=> 'Target format',
441 enum
=> [ 'raw', 'qcow2', 'vmdk' ],
446 returns
=> { type
=> 'null'},
450 my $vmid = extract_param
($param, 'vmid');
451 my $source = extract_param
($param, 'source');
452 my $storeid = extract_param
($param, 'storage');
453 my $format = extract_param
($param, 'format');
455 my $vm_conf = PVE
::QemuConfig-
>load_config($vmid);
456 PVE
::QemuConfig-
>check_lock($vm_conf);
457 die "$source: non-existent or non-regular file\n" if (! -f
$source);
459 my $storecfg = PVE
::Storage
::config
();
460 PVE
::Storage
::storage_check_enabled
($storecfg, $storeid);
462 my $target_storage_config =
463 PVE
::Storage
::storage_config
($storecfg, $storeid);
464 die "storage $storeid does not support vm images\n"
465 if !$target_storage_config->{content
}->{images
};
467 PVE
::QemuServer
::ImportDisk
::do_import
($source, $vmid, $storeid, { format
=> $format });
472 __PACKAGE__-
>register_method ({
476 description
=> "Open a terminal using a serial device (The VM need to have a serial device configured, for example 'serial0: socket')",
478 additionalProperties
=> 0,
480 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
482 description
=> "Select the serial device. By default we simply use the first suitable device.",
485 enum
=> [qw(serial0 serial1 serial2 serial3)],
488 description
=> "Escape character.",
495 returns
=> { type
=> 'null'},
499 my $vmid = $param->{vmid
};
501 my $escape = $param->{escape
} // '^O';
502 if ($escape =~ /^\^([\x40-\x7a])$/) {
503 $escape = ord($1) & 0x1F;
504 } elsif ($escape =~ /^0x[0-9a-f]+$/i) {
505 $escape = hex($escape);
506 } elsif ($escape =~ /^[0-9]+$/) {
507 $escape = int($escape);
509 die "invalid escape character definition: $escape\n";
513 $escapemsg = sprintf(' (press Ctrl+%c to exit)', $escape+0x40);
514 $escape = sprintf(',escape=0x%x', $escape);
519 my $conf = PVE
::QemuConfig-
>load_config ($vmid); # check if VM exists
521 my $iface = $param->{iface
};
524 die "serial interface '$iface' is not configured\n" if !$conf->{$iface};
525 die "wrong serial type on interface '$iface'\n" if $conf->{$iface} ne 'socket';
527 foreach my $opt (qw(serial0 serial1 serial2 serial3)) {
528 if ($conf->{$opt} && ($conf->{$opt} eq 'socket')) {
533 die "unable to find a serial interface\n" if !$iface;
536 die "VM $vmid not running\n" if !PVE
::QemuServer
::check_running
($vmid);
538 my $socket = "/var/run/qemu-server/${vmid}.$iface";
540 my $cmd = "socat UNIX-CONNECT:$socket STDIO,raw,echo=0$escape";
542 print "starting serial terminal on interface ${iface}${escapemsg}\n";
549 __PACKAGE__-
>register_method ({
552 description
=> "Create a new VM using parameters read from an OVF manifest",
554 additionalProperties
=> 0,
556 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::Cluster
::complete_next_vmid
}),
559 description
=> 'path to the ovf file',
561 storage
=> get_standard_option
('pve-storage-id', {
562 description
=> 'Target storage ID',
563 completion
=> \
&PVE
::QemuServer
::complete_storage
,
568 description
=> 'Target format',
569 enum
=> [ 'raw', 'qcow2', 'vmdk' ],
574 description
=> 'Print a parsed representation of the extracted OVF parameters, but do not create a VM',
579 returns
=> { type
=> 'null' },
583 my $vmid = PVE
::Tools
::extract_param
($param, 'vmid');
584 my $ovf_file = PVE
::Tools
::extract_param
($param, 'manifest');
585 my $storeid = PVE
::Tools
::extract_param
($param, 'storage');
586 my $format = PVE
::Tools
::extract_param
($param, 'format');
587 my $dryrun = PVE
::Tools
::extract_param
($param, 'dryrun');
589 die "$ovf_file: non-existent or non-regular file\n" if (! -f
$ovf_file);
590 my $storecfg = PVE
::Storage
::config
();
591 PVE
::Storage
::storage_check_enabled
($storecfg, $storeid);
593 my $parsed = PVE
::QemuServer
::OVF
::parse_ovf
($ovf_file);
596 print to_json
($parsed, { pretty
=> 1, canonical
=> 1});
600 $param->{name
} = $parsed->{qm
}->{name
} if defined($parsed->{qm
}->{name
});
601 $param->{memory
} = $parsed->{qm
}->{memory
} if defined($parsed->{qm
}->{memory
});
602 $param->{cores
} = $parsed->{qm
}->{cores
} if defined($parsed->{qm
}->{cores
});
606 PVE
::Cluster
::check_vmid_unused
($vmid);
611 # order matters, as do_import() will load_config() internally
612 $conf->{smbios1
} = PVE
::QemuServer
::generate_smbios1_uuid
();
613 PVE
::QemuConfig-
>write_config($vmid, $conf);
615 foreach my $disk (@{ $parsed->{disks
} }) {
616 my ($file, $drive) = ($disk->{backing_file
}, $disk->{disk_address
});
617 PVE
::QemuServer
::ImportDisk
::do_import
($file, $vmid, $storeid,
618 { drive_name
=> $drive, format
=> $format });
621 # reload after disks entries have been created
622 $conf = PVE
::QemuConfig-
>load_config($vmid);
623 PVE
::QemuConfig-
>check_lock($conf);
624 my $firstdisk = PVE
::QemuServer
::resolve_first_disk
($conf);
625 $conf->{bootdisk
} = $firstdisk if $firstdisk;
626 PVE
::QemuConfig-
>write_config($vmid, $conf);
632 eval { PVE
::QemuServer
::vm_destroy
($storecfg, $vmid, $skiplock); };
633 die "import failed - $err";
637 my $wait_for_lock = 1;
638 PVE
::QemuConfig-
>lock_config_full($vmid, $wait_for_lock, $importfn);
645 my $print_agent_result = sub {
648 my $result = $data->{result
};
649 return if !defined($result);
651 my $class = ref($result);
655 return if $result =~ m/^\s*$/;
660 if (($class eq 'HASH') && !scalar(keys %$result)) { # empty hash
664 print to_json
($result, { pretty
=> 1, canonical
=> 1});
670 my $ssh_key_map = ['sshkeys', sub {
671 return URI
::Escape
::uri_escape
(PVE
::Tools
::file_get_contents
($_[0]));
673 my $cipassword_map = PVE
::CLIHandler
::get_standard_mapping
('pve-password', { name
=> 'cipassword' });
675 'update_vm' => [$ssh_key_map, $cipassword_map],
676 'create_vm' => [$ssh_key_map, $cipassword_map],
679 return $mapping->{$name};
683 list
=> [ "PVE::API2::Qemu", 'vmlist', [],
684 { node
=> $nodename }, sub {
687 exit 0 if (!scalar(@$vmlist));
689 printf "%10s %-20s %-10s %-10s %12s %-10s\n",
690 qw(VMID NAME STATUS MEM(MB) BOOTDISK
(GB
) PID
);
692 foreach my $rec (sort { $a->{vmid
} <=> $b->{vmid
} } @$vmlist) {
693 printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid
}, $rec->{name
},
694 $rec->{qmpstatus
} || $rec->{status
},
695 ($rec->{maxmem
} || 0)/(1024*1024),
696 ($rec->{maxdisk
} || 0)/(1024*1024*1024),
703 create
=> [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node
=> $nodename }, $upid_exit ],
705 destroy
=> [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { node
=> $nodename }, $upid_exit ],
707 clone
=> [ "PVE::API2::Qemu", 'clone_vm', ['vmid', 'newid'], { node
=> $nodename }, $upid_exit ],
709 migrate
=> [ "PVE::API2::Qemu", 'migrate_vm', ['vmid', 'target'], { node
=> $nodename }, $upid_exit ],
711 set
=> [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node
=> $nodename } ],
713 resize
=> [ "PVE::API2::Qemu", 'resize_vm', ['vmid', 'disk', 'size'], { node
=> $nodename } ],
715 move_disk
=> [ "PVE::API2::Qemu", 'move_vm_disk', ['vmid', 'disk', 'storage'], { node
=> $nodename }, $upid_exit ],
717 unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid'], { node
=> $nodename } ],
719 config
=> [ "PVE::API2::Qemu", 'vm_config', ['vmid'],
720 { node
=> $nodename }, sub {
722 foreach my $k (sort (keys %$config)) {
723 next if $k eq 'digest';
724 my $v = $config->{$k};
725 if ($k eq 'description') {
726 $v = PVE
::Tools
::encode_text
($v);
732 pending
=> [ "PVE::API2::Qemu", 'vm_pending', ['vmid'],
733 { node
=> $nodename }, sub {
735 foreach my $item (sort { $a->{key
} cmp $b->{key
}} @$data) {
736 my $k = $item->{key
};
737 next if $k eq 'digest';
738 my $v = $item->{value
};
739 my $p = $item->{pending
};
740 if ($k eq 'description') {
741 $v = PVE
::Tools
::encode_text
($v) if defined($v);
742 $p = PVE
::Tools
::encode_text
($p) if defined($p);
745 if ($item->{delete}) {
746 print "del $k: $v\n";
747 } elsif (defined($p)) {
748 print "cur $k: $v\n";
749 print "new $k: $p\n";
751 print "cur $k: $v\n";
753 } elsif (defined($p)) {
754 print "new $k: $p\n";
759 showcmd
=> [ __PACKAGE__
, 'showcmd', ['vmid']],
761 status
=> [ __PACKAGE__
, 'status', ['vmid']],
763 snapshot
=> [ "PVE::API2::Qemu", 'snapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
765 delsnapshot
=> [ "PVE::API2::Qemu", 'delsnapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
767 listsnapshot
=> [ "PVE::API2::Qemu", 'snapshot_list', ['vmid'], { node
=> $nodename },
770 foreach my $e (@$res) {
771 my $headline = $e->{description
} || 'no-description';
772 $headline =~ s/\n.*//sg;
773 my $parent = $e->{parent
} // 'no-parent';
774 printf("%-20s %-20s %s\n", $e->{name
}, $parent, $headline);
778 rollback
=> [ "PVE::API2::Qemu", 'rollback', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
780 template
=> [ "PVE::API2::Qemu", 'template', ['vmid'], { node
=> $nodename }],
782 start
=> [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { node
=> $nodename } , $upid_exit ],
784 stop
=> [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { node
=> $nodename }, $upid_exit ],
786 reset => [ "PVE::API2::Qemu", 'vm_reset', ['vmid'], { node
=> $nodename }, $upid_exit ],
788 shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { node
=> $nodename }, $upid_exit ],
790 suspend
=> [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { node
=> $nodename }, $upid_exit ],
792 resume
=> [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { node
=> $nodename }, $upid_exit ],
794 sendkey
=> [ "PVE::API2::Qemu", 'vm_sendkey', ['vmid', 'key'], { node
=> $nodename } ],
796 vncproxy
=> [ __PACKAGE__
, 'vncproxy', ['vmid']],
798 wait => [ __PACKAGE__
, 'wait', ['vmid']],
800 unlock
=> [ __PACKAGE__
, 'unlock', ['vmid']],
802 rescan
=> [ __PACKAGE__
, 'rescan', []],
804 monitor
=> [ __PACKAGE__
, 'monitor', ['vmid']],
806 agent
=> [ "PVE::API2::Qemu::Agent", 'agent', ['vmid', 'command'],
807 { node
=> $nodename }, $print_agent_result ],
809 mtunnel
=> [ __PACKAGE__
, 'mtunnel', []],
811 nbdstop
=> [ __PACKAGE__
, 'nbdstop', ['vmid']],
813 terminal
=> [ __PACKAGE__
, 'terminal', ['vmid']],
815 importdisk
=> [ __PACKAGE__
, 'importdisk', ['vmid', 'source', 'storage']],
817 importovf
=> [ __PACKAGE__
, 'importovf', ['vmid', 'manifest', 'storage']],