]>
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);
20 use PVE
::RPCEnvironment
;
22 use PVE
::QemuServer
::ImportDisk
;
23 use PVE
::QemuServer
::OVF
;
25 use PVE
::API2
::Qemu
::Agent
;
27 use PVE
::JSONSchema
qw(get_standard_option);
32 use base
qw(PVE::CLIHandler);
36 my $status = PVE
::Tools
::upid_read_status
($upid);
37 exit($status eq 'OK' ?
0 : -1);
40 my $nodename = PVE
::INotify
::nodename
();
42 sub setup_environment
{
43 PVE
::RPCEnvironment-
>setup_default_cli_env();
50 while ( ++$c < 10 && !-e
$path ) { sleep(1); }
52 my $s = IO
::Socket
::UNIX-
>new(Peer
=> $path, Timeout
=> 120);
54 die "unable to connect to socket '$path' - $!" if !$s;
56 my $select = new IO
::Select
;
58 $select->add(\
*STDIN
);
61 my $timeout = 60*15; # 15 minutes
64 while ($select->count &&
65 scalar(@handles = $select->can_read ($timeout))) {
66 foreach my $h (@handles) {
68 my $n = $h->sysread($buf, 4096);
78 syswrite(\
*STDOUT
, $buf);
88 sub print_recursive_hash
{
89 my ($prefix, $hash, $key) = @_;
91 if (ref($hash) eq 'HASH') {
93 print "$prefix$key:\n";
95 foreach my $itemkey (keys %$hash) {
96 print_recursive_hash
("\t$prefix", $hash->{$itemkey}, $itemkey);
98 } elsif (ref($hash) eq 'ARRAY') {
100 print "$prefix$key:\n";
102 foreach my $item (@$hash) {
103 print_recursive_hash
("\t$prefix", $item);
105 } elsif (!ref($hash) && defined($hash)) {
107 print "$prefix$key: $hash\n";
109 print "$prefix$hash\n";
114 __PACKAGE__-
>register_method ({
118 description
=> "Show command line which is used to start the VM (debug info).",
120 additionalProperties
=> 0,
122 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
124 description
=> "Puts each option on a new line to enhance human readability",
131 returns
=> { type
=> 'null'},
135 my $storecfg = PVE
::Storage
::config
();
136 my $cmdline = PVE
::QemuServer
::vm_commandline
($storecfg, $param->{vmid
});
138 $cmdline =~ s/ -/ \\\n -/g if $param->{pretty
};
145 __PACKAGE__-
>register_method ({
149 description
=> "Show VM status.",
151 additionalProperties
=> 0,
153 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
155 description
=> "Verbose output format",
161 returns
=> { type
=> 'null'},
166 my $conf = PVE
::QemuConfig-
>load_config ($param->{vmid
});
168 my $vmstatus = PVE
::QemuServer
::vmstatus
($param->{vmid
}, 1);
169 my $stat = $vmstatus->{$param->{vmid
}};
170 if ($param->{verbose
}) {
171 foreach my $k (sort (keys %$stat)) {
172 next if $k eq 'cpu' || $k eq 'relcpu'; # always 0
174 print_recursive_hash
("", $v, $k);
177 my $status = $stat->{qmpstatus
} || 'unknown';
178 print "status: $status\n";
184 __PACKAGE__-
>register_method ({
188 description
=> "Proxy VM VNC traffic to stdin/stdout",
190 additionalProperties
=> 0,
192 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
195 returns
=> { type
=> 'null'},
199 my $vmid = $param->{vmid
};
200 my $vnc_socket = PVE
::QemuServer
::vnc_socket
($vmid);
202 if (my $ticket = $ENV{LC_PVE_TICKET
}) { # NOTE: ssh on debian only pass LC_* variables
203 PVE
::QemuServer
::vm_mon_cmd
($vmid, "change", device
=> 'vnc', target
=> "unix:$vnc_socket,password");
204 PVE
::QemuServer
::vm_mon_cmd
($vmid, "set_password", protocol
=> 'vnc', password
=> $ticket);
205 PVE
::QemuServer
::vm_mon_cmd
($vmid, "expire_password", protocol
=> 'vnc', time => "+30");
207 PVE
::QemuServer
::vm_mon_cmd
($vmid, "change", device
=> 'vnc', target
=> "unix:$vnc_socket,x509,password");
210 run_vnc_proxy
($vnc_socket);
215 __PACKAGE__-
>register_method ({
219 description
=> "Unlock the VM.",
221 additionalProperties
=> 0,
223 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
226 returns
=> { type
=> 'null'},
230 my $vmid = $param->{vmid
};
232 PVE
::QemuConfig-
>lock_config ($vmid, sub {
233 my $conf = PVE
::QemuConfig-
>load_config($vmid);
234 delete $conf->{lock};
235 delete $conf->{pending
}->{lock} if $conf->{pending
}; # just to be sure
236 PVE
::QemuConfig-
>write_config($vmid, $conf);
242 __PACKAGE__-
>register_method ({
246 description
=> "Stop embedded nbd server.",
248 additionalProperties
=> 0,
250 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
253 returns
=> { type
=> 'null'},
257 my $vmid = $param->{vmid
};
259 PVE
::QemuServer
::nbd_stop
($vmid);
264 __PACKAGE__-
>register_method ({
268 description
=> "Used by qmigrate - do not use manually.",
270 additionalProperties
=> 0,
273 returns
=> { type
=> 'null'},
277 if (!PVE
::Cluster
::check_cfs_quorum
(1)) {
282 my $tunnel_write = sub {
289 $tunnel_write->("tunnel online");
290 $tunnel_write->("ver 1");
292 while (my $line = <STDIN
>) {
294 if ($line =~ /^quit$/) {
295 $tunnel_write->("OK");
297 } elsif ($line =~ /^resume (\d+)$/) {
299 if (PVE
::QemuServer
::check_running
($vmid, 1)) {
300 eval { PVE
::QemuServer
::vm_resume
($vmid, 1, 1); };
302 $tunnel_write->("ERR: resume failed - $@");
304 $tunnel_write->("OK");
307 $tunnel_write->("ERR: resume failed - VM $vmid not running");
315 __PACKAGE__-
>register_method ({
319 description
=> "Wait until the VM is stopped.",
321 additionalProperties
=> 0,
323 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
325 description
=> "Timeout in seconds. Default is to wait forever.",
332 returns
=> { type
=> 'null'},
336 my $vmid = $param->{vmid
};
337 my $timeout = $param->{timeout
};
339 my $pid = PVE
::QemuServer
::check_running
($vmid);
342 print "waiting until VM $vmid stopps (PID $pid)\n";
345 while ((!$timeout || ($count < $timeout)) && PVE
::QemuServer
::check_running
($vmid)) {
350 die "wait failed - got timeout\n" if PVE
::QemuServer
::check_running
($vmid);
355 __PACKAGE__-
>register_method ({
359 description
=> "Enter Qemu Monitor interface.",
361 additionalProperties
=> 0,
363 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
366 returns
=> { type
=> 'null'},
370 my $vmid = $param->{vmid
};
372 my $conf = PVE
::QemuConfig-
>load_config ($vmid); # check if VM exists
374 print "Entering Qemu Monitor for VM $vmid - type 'help' for help\n";
376 my $term = new Term
::ReadLine
('qm');
379 while (defined ($input = $term->readline('qm> '))) {
382 next if $input =~ m/^\s*$/;
384 last if $input =~ m/^\s*q(uit)?\s*$/;
387 print PVE
::QemuServer
::vm_human_monitor_command
($vmid, $input);
389 print "ERROR: $@" if $@;
396 __PACKAGE__-
>register_method ({
400 description
=> "Rescan all storages and update disk sizes and unused disk images.",
402 additionalProperties
=> 0,
404 vmid
=> get_standard_option
('pve-vmid', {
406 completion
=> \
&PVE
::QemuServer
::complete_vmid
,
410 returns
=> { type
=> 'null'},
414 PVE
::QemuServer
::rescan
($param->{vmid
});
419 __PACKAGE__-
>register_method ({
420 name
=> 'importdisk',
421 path
=> 'importdisk',
423 description
=> "Import an external disk image as an unused disk in a VM. The
424 image format has to be supported by qemu-img(1).",
426 additionalProperties
=> 0,
428 vmid
=> get_standard_option
('pve-vmid', {completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
430 description
=> 'Path to the disk image to import',
434 storage
=> get_standard_option
('pve-storage-id', {
435 description
=> 'Target storage ID',
436 completion
=> \
&PVE
::QemuServer
::complete_storage
,
441 description
=> 'Target format',
442 enum
=> [ 'raw', 'qcow2', 'vmdk' ],
447 returns
=> { type
=> 'null'},
451 my $vmid = extract_param
($param, 'vmid');
452 my $source = extract_param
($param, 'source');
453 my $storeid = extract_param
($param, 'storage');
454 my $format = extract_param
($param, 'format');
456 my $vm_conf = PVE
::QemuConfig-
>load_config($vmid);
457 PVE
::QemuConfig-
>check_lock($vm_conf);
458 die "$source: non-existent or non-regular file\n" if (! -f
$source);
460 my $storecfg = PVE
::Storage
::config
();
461 PVE
::Storage
::storage_check_enabled
($storecfg, $storeid);
463 my $target_storage_config =
464 PVE
::Storage
::storage_config
($storecfg, $storeid);
465 die "storage $storeid does not support vm images\n"
466 if !$target_storage_config->{content
}->{images
};
468 PVE
::QemuServer
::ImportDisk
::do_import
($source, $vmid, $storeid, { format
=> $format });
473 __PACKAGE__-
>register_method ({
477 description
=> "Open a terminal using a serial device (The VM need to have a serial device configured, for example 'serial0: socket')",
479 additionalProperties
=> 0,
481 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
483 description
=> "Select the serial device. By default we simply use the first suitable device.",
486 enum
=> [qw(serial0 serial1 serial2 serial3)],
489 description
=> "Escape character.",
496 returns
=> { type
=> 'null'},
500 my $vmid = $param->{vmid
};
502 my $escape = $param->{escape
} // '^O';
503 if ($escape =~ /^\^([\x40-\x7a])$/) {
504 $escape = ord($1) & 0x1F;
505 } elsif ($escape =~ /^0x[0-9a-f]+$/i) {
506 $escape = hex($escape);
507 } elsif ($escape =~ /^[0-9]+$/) {
508 $escape = int($escape);
510 die "invalid escape character definition: $escape\n";
514 $escapemsg = sprintf(' (press Ctrl+%c to exit)', $escape+0x40);
515 $escape = sprintf(',escape=0x%x', $escape);
520 my $conf = PVE
::QemuConfig-
>load_config ($vmid); # check if VM exists
522 my $iface = $param->{iface
};
525 die "serial interface '$iface' is not configured\n" if !$conf->{$iface};
526 die "wrong serial type on interface '$iface'\n" if $conf->{$iface} ne 'socket';
528 foreach my $opt (qw(serial0 serial1 serial2 serial3)) {
529 if ($conf->{$opt} && ($conf->{$opt} eq 'socket')) {
534 die "unable to find a serial interface\n" if !$iface;
537 die "VM $vmid not running\n" if !PVE
::QemuServer
::check_running
($vmid);
539 my $socket = "/var/run/qemu-server/${vmid}.$iface";
541 my $cmd = "socat UNIX-CONNECT:$socket STDIO,raw,echo=0$escape";
543 print "starting serial terminal on interface ${iface}${escapemsg}\n";
550 __PACKAGE__-
>register_method ({
553 description
=> "Create a new VM using parameters read from an OVF manifest",
555 additionalProperties
=> 0,
557 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::Cluster
::complete_next_vmid
}),
560 description
=> 'path to the ovf file',
562 storage
=> get_standard_option
('pve-storage-id', {
563 description
=> 'Target storage ID',
564 completion
=> \
&PVE
::QemuServer
::complete_storage
,
569 description
=> 'Target format',
570 enum
=> [ 'raw', 'qcow2', 'vmdk' ],
575 description
=> 'Print a parsed representation of the extracted OVF parameters, but do not create a VM',
580 returns
=> { type
=> 'null' },
584 my $vmid = PVE
::Tools
::extract_param
($param, 'vmid');
585 my $ovf_file = PVE
::Tools
::extract_param
($param, 'manifest');
586 my $storeid = PVE
::Tools
::extract_param
($param, 'storage');
587 my $format = PVE
::Tools
::extract_param
($param, 'format');
588 my $dryrun = PVE
::Tools
::extract_param
($param, 'dryrun');
590 die "$ovf_file: non-existent or non-regular file\n" if (! -f
$ovf_file);
591 my $storecfg = PVE
::Storage
::config
();
592 PVE
::Storage
::storage_check_enabled
($storecfg, $storeid);
594 my $parsed = PVE
::QemuServer
::OVF
::parse_ovf
($ovf_file);
597 print to_json
($parsed, { pretty
=> 1, canonical
=> 1});
601 $param->{name
} = $parsed->{qm
}->{name
} if defined($parsed->{qm
}->{name
});
602 $param->{memory
} = $parsed->{qm
}->{memory
} if defined($parsed->{qm
}->{memory
});
603 $param->{cores
} = $parsed->{qm
}->{cores
} if defined($parsed->{qm
}->{cores
});
607 PVE
::Cluster
::check_vmid_unused
($vmid);
612 # order matters, as do_import() will load_config() internally
613 $conf->{smbios1
} = PVE
::QemuServer
::generate_smbios1_uuid
();
614 PVE
::QemuConfig-
>write_config($vmid, $conf);
616 foreach my $disk (@{ $parsed->{disks
} }) {
617 my ($file, $drive) = ($disk->{backing_file
}, $disk->{disk_address
});
618 PVE
::QemuServer
::ImportDisk
::do_import
($file, $vmid, $storeid,
619 { drive_name
=> $drive, format
=> $format });
622 # reload after disks entries have been created
623 $conf = PVE
::QemuConfig-
>load_config($vmid);
624 PVE
::QemuConfig-
>check_lock($conf);
625 my $firstdisk = PVE
::QemuServer
::resolve_first_disk
($conf);
626 $conf->{bootdisk
} = $firstdisk if $firstdisk;
627 PVE
::QemuConfig-
>write_config($vmid, $conf);
633 eval { PVE
::QemuServer
::vm_destroy
($storecfg, $vmid, $skiplock); };
634 die "import failed - $err";
638 my $wait_for_lock = 1;
639 PVE
::QemuConfig-
>lock_config_full($vmid, $wait_for_lock, $importfn);
646 my $print_agent_result = sub {
649 my $result = $data->{result
};
650 return if !defined($result);
652 my $class = ref($result);
656 return if $result =~ m/^\s*$/;
661 if (($class eq 'HASH') && !scalar(keys %$result)) { # empty hash
665 print to_json
($result, { pretty
=> 1, canonical
=> 1});
671 my $ssh_key_map = ['sshkeys', sub {
672 return URI
::Escape
::uri_escape
(PVE
::Tools
::file_get_contents
($_[0]));
674 my $cipassword_map = ['cipassword', sub {
676 return $value if $value;
678 my $pw = PVE
::PTY
::read_password
('New cloud-init user password: ');
679 my $pw2 = PVE
::PTY
::read_password
('Repeat password: ');
680 die "passwords do not match\n" if $pw ne $pw2;
684 'update_vm' => [$ssh_key_map, $cipassword_map],
685 'create_vm' => [$ssh_key_map, $cipassword_map],
688 return $mapping->{$name};
692 list
=> [ "PVE::API2::Qemu", 'vmlist', [],
693 { node
=> $nodename }, sub {
696 exit 0 if (!scalar(@$vmlist));
698 printf "%10s %-20s %-10s %-10s %12s %-10s\n",
699 qw(VMID NAME STATUS MEM(MB) BOOTDISK
(GB
) PID
);
701 foreach my $rec (sort { $a->{vmid
} <=> $b->{vmid
} } @$vmlist) {
702 printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid
}, $rec->{name
},
703 $rec->{qmpstatus
} || $rec->{status
},
704 ($rec->{maxmem
} || 0)/(1024*1024),
705 ($rec->{maxdisk
} || 0)/(1024*1024*1024),
712 create
=> [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node
=> $nodename }, $upid_exit ],
714 destroy
=> [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { node
=> $nodename }, $upid_exit ],
716 clone
=> [ "PVE::API2::Qemu", 'clone_vm', ['vmid', 'newid'], { node
=> $nodename }, $upid_exit ],
718 migrate
=> [ "PVE::API2::Qemu", 'migrate_vm', ['vmid', 'target'], { node
=> $nodename }, $upid_exit ],
720 set
=> [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node
=> $nodename } ],
722 resize
=> [ "PVE::API2::Qemu", 'resize_vm', ['vmid', 'disk', 'size'], { node
=> $nodename } ],
724 move_disk
=> [ "PVE::API2::Qemu", 'move_vm_disk', ['vmid', 'disk', 'storage'], { node
=> $nodename }, $upid_exit ],
726 unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid'], { node
=> $nodename } ],
728 config
=> [ "PVE::API2::Qemu", 'vm_config', ['vmid'],
729 { node
=> $nodename }, sub {
731 foreach my $k (sort (keys %$config)) {
732 next if $k eq 'digest';
733 my $v = $config->{$k};
734 if ($k eq 'description') {
735 $v = PVE
::Tools
::encode_text
($v);
741 pending
=> [ "PVE::API2::Qemu", 'vm_pending', ['vmid'],
742 { node
=> $nodename }, sub {
744 foreach my $item (sort { $a->{key
} cmp $b->{key
}} @$data) {
745 my $k = $item->{key
};
746 next if $k eq 'digest';
747 my $v = $item->{value
};
748 my $p = $item->{pending
};
749 if ($k eq 'description') {
750 $v = PVE
::Tools
::encode_text
($v) if defined($v);
751 $p = PVE
::Tools
::encode_text
($p) if defined($p);
754 if ($item->{delete}) {
755 print "del $k: $v\n";
756 } elsif (defined($p)) {
757 print "cur $k: $v\n";
758 print "new $k: $p\n";
760 print "cur $k: $v\n";
762 } elsif (defined($p)) {
763 print "new $k: $p\n";
768 showcmd
=> [ __PACKAGE__
, 'showcmd', ['vmid']],
770 status
=> [ __PACKAGE__
, 'status', ['vmid']],
772 snapshot
=> [ "PVE::API2::Qemu", 'snapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
774 delsnapshot
=> [ "PVE::API2::Qemu", 'delsnapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
776 listsnapshot
=> [ "PVE::API2::Qemu", 'snapshot_list', ['vmid'], { node
=> $nodename },
779 foreach my $e (@$res) {
780 my $headline = $e->{description
} || 'no-description';
781 $headline =~ s/\n.*//sg;
782 my $parent = $e->{parent
} // 'no-parent';
783 printf("%-20s %-20s %s\n", $e->{name
}, $parent, $headline);
787 rollback
=> [ "PVE::API2::Qemu", 'rollback', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
789 template
=> [ "PVE::API2::Qemu", 'template', ['vmid'], { node
=> $nodename }],
791 start
=> [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { node
=> $nodename } , $upid_exit ],
793 stop
=> [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { node
=> $nodename }, $upid_exit ],
795 reset => [ "PVE::API2::Qemu", 'vm_reset', ['vmid'], { node
=> $nodename }, $upid_exit ],
797 shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { node
=> $nodename }, $upid_exit ],
799 suspend
=> [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { node
=> $nodename }, $upid_exit ],
801 resume
=> [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { node
=> $nodename }, $upid_exit ],
803 sendkey
=> [ "PVE::API2::Qemu", 'vm_sendkey', ['vmid', 'key'], { node
=> $nodename } ],
805 vncproxy
=> [ __PACKAGE__
, 'vncproxy', ['vmid']],
807 wait => [ __PACKAGE__
, 'wait', ['vmid']],
809 unlock
=> [ __PACKAGE__
, 'unlock', ['vmid']],
811 rescan
=> [ __PACKAGE__
, 'rescan', []],
813 monitor
=> [ __PACKAGE__
, 'monitor', ['vmid']],
815 agent
=> [ "PVE::API2::Qemu::Agent", 'agent', ['vmid', 'command'],
816 { node
=> $nodename }, $print_agent_result ],
818 mtunnel
=> [ __PACKAGE__
, 'mtunnel', []],
820 nbdstop
=> [ __PACKAGE__
, 'nbdstop', ['vmid']],
822 terminal
=> [ __PACKAGE__
, 'terminal', ['vmid']],
824 importdisk
=> [ __PACKAGE__
, 'importdisk', ['vmid', 'source', 'storage']],
826 importovf
=> [ __PACKAGE__
, 'importovf', ['vmid', 'manifest', 'storage']],