]>
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
;
20 use PVE
::QemuServer
::ImportDisk
;
21 use PVE
::QemuServer
::OVF
;
24 use PVE
::JSONSchema
qw(get_standard_option);
29 use base
qw(PVE::CLIHandler);
33 my $status = PVE
::Tools
::upid_read_status
($upid);
34 exit($status eq 'OK' ?
0 : -1);
37 my $nodename = PVE
::INotify
::nodename
();
39 sub setup_environment
{
40 PVE
::RPCEnvironment-
>setup_default_cli_env();
47 while ( ++$c < 10 && !-e
$path ) { sleep(1); }
49 my $s = IO
::Socket
::UNIX-
>new(Peer
=> $path, Timeout
=> 120);
51 die "unable to connect to socket '$path' - $!" if !$s;
53 my $select = new IO
::Select
;
55 $select->add(\
*STDIN
);
58 my $timeout = 60*15; # 15 minutes
61 while ($select->count &&
62 scalar(@handles = $select->can_read ($timeout))) {
63 foreach my $h (@handles) {
65 my $n = $h->sysread($buf, 4096);
75 syswrite(\
*STDOUT
, $buf);
85 sub print_recursive_hash
{
86 my ($prefix, $hash, $key) = @_;
88 if (ref($hash) eq 'HASH') {
90 print "$prefix$key:\n";
92 foreach my $itemkey (keys %$hash) {
93 print_recursive_hash
("\t$prefix", $hash->{$itemkey}, $itemkey);
95 } elsif (ref($hash) eq 'ARRAY') {
97 print "$prefix$key:\n";
99 foreach my $item (@$hash) {
100 print_recursive_hash
("\t$prefix", $item);
102 } elsif (!ref($hash) && defined($hash)) {
104 print "$prefix$key: $hash\n";
106 print "$prefix$hash\n";
111 __PACKAGE__-
>register_method ({
115 description
=> "Show command line which is used to start the VM (debug info).",
117 additionalProperties
=> 0,
119 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
122 returns
=> { type
=> 'null'},
126 my $storecfg = PVE
::Storage
::config
();
127 print PVE
::QemuServer
::vm_commandline
($storecfg, $param->{vmid
}) . "\n";
132 __PACKAGE__-
>register_method ({
136 description
=> "Show VM status.",
138 additionalProperties
=> 0,
140 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
142 description
=> "Verbose output format",
148 returns
=> { type
=> 'null'},
153 my $conf = PVE
::QemuConfig-
>load_config ($param->{vmid
});
155 my $vmstatus = PVE
::QemuServer
::vmstatus
($param->{vmid
}, 1);
156 my $stat = $vmstatus->{$param->{vmid
}};
157 if ($param->{verbose
}) {
158 foreach my $k (sort (keys %$stat)) {
159 next if $k eq 'cpu' || $k eq 'relcpu'; # always 0
161 print_recursive_hash
("", $v, $k);
164 my $status = $stat->{qmpstatus
} || 'unknown';
165 print "status: $status\n";
171 __PACKAGE__-
>register_method ({
175 description
=> "Proxy VM VNC traffic to stdin/stdout",
177 additionalProperties
=> 0,
179 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
182 returns
=> { type
=> 'null'},
186 my $vmid = $param->{vmid
};
187 my $vnc_socket = PVE
::QemuServer
::vnc_socket
($vmid);
189 if (my $ticket = $ENV{LC_PVE_TICKET
}) { # NOTE: ssh on debian only pass LC_* variables
190 PVE
::QemuServer
::vm_mon_cmd
($vmid, "change", device
=> 'vnc', target
=> "unix:$vnc_socket,password");
191 PVE
::QemuServer
::vm_mon_cmd
($vmid, "set_password", protocol
=> 'vnc', password
=> $ticket);
192 PVE
::QemuServer
::vm_mon_cmd
($vmid, "expire_password", protocol
=> 'vnc', time => "+30");
194 PVE
::QemuServer
::vm_mon_cmd
($vmid, "change", device
=> 'vnc', target
=> "unix:$vnc_socket,x509,password");
197 run_vnc_proxy
($vnc_socket);
202 __PACKAGE__-
>register_method ({
206 description
=> "Unlock the VM.",
208 additionalProperties
=> 0,
210 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
213 returns
=> { type
=> 'null'},
217 my $vmid = $param->{vmid
};
219 PVE
::QemuConfig-
>lock_config ($vmid, sub {
220 my $conf = PVE
::QemuConfig-
>load_config($vmid);
221 delete $conf->{lock};
222 delete $conf->{pending
}->{lock} if $conf->{pending
}; # just to be sure
223 PVE
::QemuConfig-
>write_config($vmid, $conf);
229 __PACKAGE__-
>register_method ({
233 description
=> "Stop embedded nbd server.",
235 additionalProperties
=> 0,
237 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
240 returns
=> { type
=> 'null'},
244 my $vmid = $param->{vmid
};
246 PVE
::QemuServer
::nbd_stop
($vmid);
251 __PACKAGE__-
>register_method ({
255 description
=> "Used by qmigrate - do not use manually.",
257 additionalProperties
=> 0,
260 returns
=> { type
=> 'null'},
264 if (!PVE
::Cluster
::check_cfs_quorum
(1)) {
269 my $tunnel_write = sub {
276 $tunnel_write->("tunnel online");
277 $tunnel_write->("ver 1");
279 while (my $line = <>) {
281 if ($line =~ /^quit$/) {
282 $tunnel_write->("OK");
284 } elsif ($line =~ /^resume (\d+)$/) {
286 if (PVE
::QemuServer
::check_running
($vmid, 1)) {
287 eval { PVE
::QemuServer
::vm_resume
($vmid, 1, 1); };
289 $tunnel_write->("ERR: resume failed - $@");
291 $tunnel_write->("OK");
294 $tunnel_write->("ERR: resume failed - VM $vmid not running");
302 __PACKAGE__-
>register_method ({
306 description
=> "Wait until the VM is stopped.",
308 additionalProperties
=> 0,
310 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
312 description
=> "Timeout in seconds. Default is to wait forever.",
319 returns
=> { type
=> 'null'},
323 my $vmid = $param->{vmid
};
324 my $timeout = $param->{timeout
};
326 my $pid = PVE
::QemuServer
::check_running
($vmid);
329 print "waiting until VM $vmid stopps (PID $pid)\n";
332 while ((!$timeout || ($count < $timeout)) && PVE
::QemuServer
::check_running
($vmid)) {
337 die "wait failed - got timeout\n" if PVE
::QemuServer
::check_running
($vmid);
342 __PACKAGE__-
>register_method ({
346 description
=> "Enter Qemu Monitor interface.",
348 additionalProperties
=> 0,
350 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
353 returns
=> { type
=> 'null'},
357 my $vmid = $param->{vmid
};
359 my $conf = PVE
::QemuConfig-
>load_config ($vmid); # check if VM exists
361 print "Entering Qemu Monitor for VM $vmid - type 'help' for help\n";
363 my $term = new Term
::ReadLine
('qm');
366 while (defined ($input = $term->readline('qm> '))) {
369 next if $input =~ m/^\s*$/;
371 last if $input =~ m/^\s*q(uit)?\s*$/;
374 print PVE
::QemuServer
::vm_human_monitor_command
($vmid, $input);
376 print "ERROR: $@" if $@;
383 __PACKAGE__-
>register_method ({
387 description
=> "Rescan all storages and update disk sizes and unused disk images.",
389 additionalProperties
=> 0,
391 vmid
=> get_standard_option
('pve-vmid', {
393 completion
=> \
&PVE
::QemuServer
::complete_vmid
,
397 returns
=> { type
=> 'null'},
401 PVE
::QemuServer
::rescan
($param->{vmid
});
406 __PACKAGE__-
>register_method ({
407 name
=> 'importdisk',
408 path
=> 'importdisk',
410 description
=> "Import an external disk image as an unused disk in a VM. The
411 image format has to be supported by qemu-img(1).",
413 additionalProperties
=> 0,
415 vmid
=> get_standard_option
('pve-vmid', {completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
417 description
=> 'Path to the disk image to import',
421 storage
=> get_standard_option
('pve-storage-id', {
422 description
=> 'Target storage ID',
423 completion
=> \
&PVE
::QemuServer
::complete_storage
,
428 description
=> 'Target format',
429 enum
=> [ 'raw', 'qcow2', 'vmdk' ],
434 returns
=> { type
=> 'null'},
438 my $vmid = extract_param
($param, 'vmid');
439 my $source = extract_param
($param, 'source');
440 my $storeid = extract_param
($param, 'storage');
441 my $format = extract_param
($param, 'format');
443 my $vm_conf = PVE
::QemuConfig-
>load_config($vmid);
444 PVE
::QemuConfig-
>check_lock($vm_conf);
445 die "$source: non-existent or non-regular file\n" if (! -f
$source);
447 my $storecfg = PVE
::Storage
::config
();
448 PVE
::Storage
::storage_check_enabled
($storecfg, $storeid);
450 my $target_storage_config =
451 PVE
::Storage
::storage_config
($storecfg, $storeid);
452 die "storage $storeid does not support vm images\n"
453 if !$target_storage_config->{content
}->{images
};
455 PVE
::QemuServer
::ImportDisk
::do_import
($source, $vmid, $storeid, { format
=> $format });
460 __PACKAGE__-
>register_method ({
464 description
=> "Open a terminal using a serial device (The VM need to have a serial device configured, for example 'serial0: socket')",
466 additionalProperties
=> 0,
468 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
470 description
=> "Select the serial device. By default we simply use the first suitable device.",
473 enum
=> [qw(serial0 serial1 serial2 serial3)],
477 returns
=> { type
=> 'null'},
481 my $vmid = $param->{vmid
};
483 my $conf = PVE
::QemuConfig-
>load_config ($vmid); # check if VM exists
485 my $iface = $param->{iface
};
488 die "serial interface '$iface' is not configured\n" if !$conf->{$iface};
489 die "wrong serial type on interface '$iface'\n" if $conf->{$iface} ne 'socket';
491 foreach my $opt (qw(serial0 serial1 serial2 serial3)) {
492 if ($conf->{$opt} && ($conf->{$opt} eq 'socket')) {
497 die "unable to find a serial interface\n" if !$iface;
500 die "VM $vmid not running\n" if !PVE
::QemuServer
::check_running
($vmid);
502 my $socket = "/var/run/qemu-server/${vmid}.$iface";
504 my $cmd = "socat UNIX-CONNECT:$socket STDIO,raw,echo=0,escape=0x0f";
506 print "starting serial terminal on interface $iface (press control-O to exit)\n";
513 __PACKAGE__-
>register_method ({
516 description
=> "Create a new VM using parameters read from an OVF manifest",
518 additionalProperties
=> 0,
520 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::Cluster
::complete_next_vmid
}),
523 description
=> 'path to the ovf file',
525 storage
=> get_standard_option
('pve-storage-id', {
526 description
=> 'Target storage ID',
527 completion
=> \
&PVE
::QemuServer
::complete_storage
,
532 description
=> 'Target format',
533 enum
=> [ 'raw', 'qcow2', 'vmdk' ],
538 description
=> 'Print a parsed representation of the extracted OVF parameters, but do not create a VM',
543 returns
=> { type
=> 'null' },
547 my $vmid = PVE
::Tools
::extract_param
($param, 'vmid');
548 my $ovf_file = PVE
::Tools
::extract_param
($param, 'manifest');
549 my $storeid = PVE
::Tools
::extract_param
($param, 'storage');
550 my $format = PVE
::Tools
::extract_param
($param, 'format');
551 my $dryrun = PVE
::Tools
::extract_param
($param, 'dryrun');
553 die "$ovf_file: non-existent or non-regular file\n" if (! -f
$ovf_file);
554 my $storecfg = PVE
::Storage
::config
();
555 PVE
::Storage
::storage_check_enabled
($storecfg, $storeid);
557 my $parsed = PVE
::QemuServer
::OVF
::parse_ovf
($ovf_file);
560 print to_json
($parsed, { pretty
=> 1, canonical
=> 1});
564 $param->{name
} = $parsed->{qm
}->{name
} if defined($parsed->{qm
}->{name
});
565 $param->{memory
} = $parsed->{qm
}->{memory
} if defined($parsed->{qm
}->{memory
});
566 $param->{cores
} = $parsed->{qm
}->{cores
} if defined($parsed->{qm
}->{cores
});
570 PVE
::Cluster
::check_vmid_unused
($vmid);
575 # order matters, as do_import() will load_config() internally
576 $conf->{smbios1
} = PVE
::QemuServer
::generate_smbios1_uuid
();
577 PVE
::QemuConfig-
>write_config($vmid, $conf);
579 foreach my $disk (@{ $parsed->{disks
} }) {
580 my ($file, $drive) = ($disk->{backing_file
}, $disk->{disk_address
});
581 PVE
::QemuServer
::ImportDisk
::do_import
($file, $vmid, $storeid,
582 { drive_name
=> $drive, format
=> $format });
585 # reload after disks entries have been created
586 $conf = PVE
::QemuConfig-
>load_config($vmid);
587 PVE
::QemuConfig-
>check_lock($conf);
588 my $firstdisk = PVE
::QemuServer
::resolve_first_disk
($conf);
589 $conf->{bootdisk
} = $firstdisk if $firstdisk;
590 PVE
::QemuConfig-
>write_config($vmid, $conf);
596 eval { PVE
::QemuServer
::vm_destroy
($storecfg, $vmid, $skiplock); };
597 die "import failed - $err";
601 my $wait_for_lock = 1;
602 PVE
::QemuConfig-
>lock_config_full($vmid, $wait_for_lock, $importfn);
609 my $print_agent_result = sub {
612 my $result = $data->{result
};
613 return if !defined($result);
615 my $class = ref($result);
619 return if $result =~ m/^\s*$/;
624 if (($class eq 'HASH') && !scalar(keys %$result)) { # empty hash
628 print to_json
($result, { pretty
=> 1, canonical
=> 1});
632 list
=> [ "PVE::API2::Qemu", 'vmlist', [],
633 { node
=> $nodename }, sub {
636 exit 0 if (!scalar(@$vmlist));
638 printf "%10s %-20s %-10s %-10s %12s %-10s\n",
639 qw(VMID NAME STATUS MEM(MB) BOOTDISK
(GB
) PID
);
641 foreach my $rec (sort { $a->{vmid
} <=> $b->{vmid
} } @$vmlist) {
642 printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid
}, $rec->{name
},
643 $rec->{qmpstatus
} || $rec->{status
},
644 ($rec->{maxmem
} || 0)/(1024*1024),
645 ($rec->{maxdisk
} || 0)/(1024*1024*1024),
652 create
=> [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node
=> $nodename }, $upid_exit ],
654 destroy
=> [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { node
=> $nodename }, $upid_exit ],
656 clone
=> [ "PVE::API2::Qemu", 'clone_vm', ['vmid', 'newid'], { node
=> $nodename }, $upid_exit ],
658 migrate
=> [ "PVE::API2::Qemu", 'migrate_vm', ['vmid', 'target'], { node
=> $nodename }, $upid_exit ],
660 set
=> [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node
=> $nodename } ],
662 resize
=> [ "PVE::API2::Qemu", 'resize_vm', ['vmid', 'disk', 'size'], { node
=> $nodename } ],
664 move_disk
=> [ "PVE::API2::Qemu", 'move_vm_disk', ['vmid', 'disk', 'storage'], { node
=> $nodename }, $upid_exit ],
666 unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid'], { node
=> $nodename } ],
668 config
=> [ "PVE::API2::Qemu", 'vm_config', ['vmid'],
669 { node
=> $nodename }, sub {
671 foreach my $k (sort (keys %$config)) {
672 next if $k eq 'digest';
673 my $v = $config->{$k};
674 if ($k eq 'description') {
675 $v = PVE
::Tools
::encode_text
($v);
681 pending
=> [ "PVE::API2::Qemu", 'vm_pending', ['vmid'],
682 { node
=> $nodename }, sub {
684 foreach my $item (sort { $a->{key
} cmp $b->{key
}} @$data) {
685 my $k = $item->{key
};
686 next if $k eq 'digest';
687 my $v = $item->{value
};
688 my $p = $item->{pending
};
689 if ($k eq 'description') {
690 $v = PVE
::Tools
::encode_text
($v) if defined($v);
691 $p = PVE
::Tools
::encode_text
($p) if defined($p);
694 if ($item->{delete}) {
695 print "del $k: $v\n";
696 } elsif (defined($p)) {
697 print "cur $k: $v\n";
698 print "new $k: $p\n";
700 print "cur $k: $v\n";
702 } elsif (defined($p)) {
703 print "new $k: $p\n";
708 showcmd
=> [ __PACKAGE__
, 'showcmd', ['vmid']],
710 status
=> [ __PACKAGE__
, 'status', ['vmid']],
712 snapshot
=> [ "PVE::API2::Qemu", 'snapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
714 delsnapshot
=> [ "PVE::API2::Qemu", 'delsnapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
716 listsnapshot
=> [ "PVE::API2::Qemu", 'snapshot_list', ['vmid'], { node
=> $nodename },
719 foreach my $e (@$res) {
720 my $headline = $e->{description
} || 'no-description';
721 $headline =~ s/\n.*//sg;
722 my $parent = $e->{parent
} // 'no-parent';
723 printf("%-20s %-20s %s\n", $e->{name
}, $parent, $headline);
727 rollback
=> [ "PVE::API2::Qemu", 'rollback', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
729 template
=> [ "PVE::API2::Qemu", 'template', ['vmid'], { node
=> $nodename }],
731 start
=> [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { node
=> $nodename } , $upid_exit ],
733 stop
=> [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { node
=> $nodename }, $upid_exit ],
735 reset => [ "PVE::API2::Qemu", 'vm_reset', ['vmid'], { node
=> $nodename }, $upid_exit ],
737 shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { node
=> $nodename }, $upid_exit ],
739 suspend
=> [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { node
=> $nodename }, $upid_exit ],
741 resume
=> [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { node
=> $nodename }, $upid_exit ],
743 sendkey
=> [ "PVE::API2::Qemu", 'vm_sendkey', ['vmid', 'key'], { node
=> $nodename } ],
745 vncproxy
=> [ __PACKAGE__
, 'vncproxy', ['vmid']],
747 wait => [ __PACKAGE__
, 'wait', ['vmid']],
749 unlock
=> [ __PACKAGE__
, 'unlock', ['vmid']],
751 rescan
=> [ __PACKAGE__
, 'rescan', []],
753 monitor
=> [ __PACKAGE__
, 'monitor', ['vmid']],
755 agent
=> [ "PVE::API2::Qemu", 'agent', ['vmid', 'command'],
756 { node
=> $nodename }, $print_agent_result ],
758 mtunnel
=> [ __PACKAGE__
, 'mtunnel', []],
760 nbdstop
=> [ __PACKAGE__
, 'nbdstop', ['vmid']],
762 terminal
=> [ __PACKAGE__
, 'terminal', ['vmid']],
764 importdisk
=> [ __PACKAGE__
, 'importdisk', ['vmid', 'source', 'storage']],
766 importovf
=> [ __PACKAGE__
, 'importovf', ['vmid', 'manifest', 'storage']],