]>
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);
30 use base
qw(PVE::CLIHandler);
34 my $status = PVE
::Tools
::upid_read_status
($upid);
35 exit($status eq 'OK' ?
0 : -1);
38 my $nodename = PVE
::INotify
::nodename
();
40 sub setup_environment
{
41 PVE
::RPCEnvironment-
>setup_default_cli_env();
48 while ( ++$c < 10 && !-e
$path ) { sleep(1); }
50 my $s = IO
::Socket
::UNIX-
>new(Peer
=> $path, Timeout
=> 120);
52 die "unable to connect to socket '$path' - $!" if !$s;
54 my $select = new IO
::Select
;
56 $select->add(\
*STDIN
);
59 my $timeout = 60*15; # 15 minutes
62 while ($select->count &&
63 scalar(@handles = $select->can_read ($timeout))) {
64 foreach my $h (@handles) {
66 my $n = $h->sysread($buf, 4096);
76 syswrite(\
*STDOUT
, $buf);
86 sub print_recursive_hash
{
87 my ($prefix, $hash, $key) = @_;
89 if (ref($hash) eq 'HASH') {
91 print "$prefix$key:\n";
93 foreach my $itemkey (keys %$hash) {
94 print_recursive_hash
("\t$prefix", $hash->{$itemkey}, $itemkey);
96 } elsif (ref($hash) eq 'ARRAY') {
98 print "$prefix$key:\n";
100 foreach my $item (@$hash) {
101 print_recursive_hash
("\t$prefix", $item);
103 } elsif (!ref($hash) && defined($hash)) {
105 print "$prefix$key: $hash\n";
107 print "$prefix$hash\n";
112 __PACKAGE__-
>register_method ({
116 description
=> "Show command line which is used to start the VM (debug info).",
118 additionalProperties
=> 0,
120 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
123 returns
=> { type
=> 'null'},
127 my $storecfg = PVE
::Storage
::config
();
128 print PVE
::QemuServer
::vm_commandline
($storecfg, $param->{vmid
}) . "\n";
133 __PACKAGE__-
>register_method ({
137 description
=> "Show VM status.",
139 additionalProperties
=> 0,
141 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
143 description
=> "Verbose output format",
149 returns
=> { type
=> 'null'},
154 my $conf = PVE
::QemuConfig-
>load_config ($param->{vmid
});
156 my $vmstatus = PVE
::QemuServer
::vmstatus
($param->{vmid
}, 1);
157 my $stat = $vmstatus->{$param->{vmid
}};
158 if ($param->{verbose
}) {
159 foreach my $k (sort (keys %$stat)) {
160 next if $k eq 'cpu' || $k eq 'relcpu'; # always 0
162 print_recursive_hash
("", $v, $k);
165 my $status = $stat->{qmpstatus
} || 'unknown';
166 print "status: $status\n";
172 __PACKAGE__-
>register_method ({
176 description
=> "Proxy VM VNC traffic to stdin/stdout",
178 additionalProperties
=> 0,
180 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
183 returns
=> { type
=> 'null'},
187 my $vmid = $param->{vmid
};
188 my $vnc_socket = PVE
::QemuServer
::vnc_socket
($vmid);
190 if (my $ticket = $ENV{LC_PVE_TICKET
}) { # NOTE: ssh on debian only pass LC_* variables
191 PVE
::QemuServer
::vm_mon_cmd
($vmid, "change", device
=> 'vnc', target
=> "unix:$vnc_socket,password");
192 PVE
::QemuServer
::vm_mon_cmd
($vmid, "set_password", protocol
=> 'vnc', password
=> $ticket);
193 PVE
::QemuServer
::vm_mon_cmd
($vmid, "expire_password", protocol
=> 'vnc', time => "+30");
195 PVE
::QemuServer
::vm_mon_cmd
($vmid, "change", device
=> 'vnc', target
=> "unix:$vnc_socket,x509,password");
198 run_vnc_proxy
($vnc_socket);
203 __PACKAGE__-
>register_method ({
207 description
=> "Unlock the VM.",
209 additionalProperties
=> 0,
211 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
214 returns
=> { type
=> 'null'},
218 my $vmid = $param->{vmid
};
220 PVE
::QemuConfig-
>lock_config ($vmid, sub {
221 my $conf = PVE
::QemuConfig-
>load_config($vmid);
222 delete $conf->{lock};
223 delete $conf->{pending
}->{lock} if $conf->{pending
}; # just to be sure
224 PVE
::QemuConfig-
>write_config($vmid, $conf);
230 __PACKAGE__-
>register_method ({
234 description
=> "Stop embedded nbd server.",
236 additionalProperties
=> 0,
238 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
241 returns
=> { type
=> 'null'},
245 my $vmid = $param->{vmid
};
247 PVE
::QemuServer
::nbd_stop
($vmid);
252 __PACKAGE__-
>register_method ({
256 description
=> "Used by qmigrate - do not use manually.",
258 additionalProperties
=> 0,
261 returns
=> { type
=> 'null'},
265 if (!PVE
::Cluster
::check_cfs_quorum
(1)) {
270 my $tunnel_write = sub {
277 $tunnel_write->("tunnel online");
278 $tunnel_write->("ver 1");
280 while (my $line = <>) {
282 if ($line =~ /^quit$/) {
283 $tunnel_write->("OK");
285 } elsif ($line =~ /^resume (\d+)$/) {
287 if (PVE
::QemuServer
::check_running
($vmid, 1)) {
288 eval { PVE
::QemuServer
::vm_resume
($vmid, 1, 1); };
290 $tunnel_write->("ERR: resume failed - $@");
292 $tunnel_write->("OK");
295 $tunnel_write->("ERR: resume failed - VM $vmid not running");
303 __PACKAGE__-
>register_method ({
307 description
=> "Wait until the VM is stopped.",
309 additionalProperties
=> 0,
311 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
313 description
=> "Timeout in seconds. Default is to wait forever.",
320 returns
=> { type
=> 'null'},
324 my $vmid = $param->{vmid
};
325 my $timeout = $param->{timeout
};
327 my $pid = PVE
::QemuServer
::check_running
($vmid);
330 print "waiting until VM $vmid stopps (PID $pid)\n";
333 while ((!$timeout || ($count < $timeout)) && PVE
::QemuServer
::check_running
($vmid)) {
338 die "wait failed - got timeout\n" if PVE
::QemuServer
::check_running
($vmid);
343 __PACKAGE__-
>register_method ({
347 description
=> "Enter Qemu Monitor interface.",
349 additionalProperties
=> 0,
351 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
354 returns
=> { type
=> 'null'},
358 my $vmid = $param->{vmid
};
360 my $conf = PVE
::QemuConfig-
>load_config ($vmid); # check if VM exists
362 print "Entering Qemu Monitor for VM $vmid - type 'help' for help\n";
364 my $term = new Term
::ReadLine
('qm');
367 while (defined ($input = $term->readline('qm> '))) {
370 next if $input =~ m/^\s*$/;
372 last if $input =~ m/^\s*q(uit)?\s*$/;
375 print PVE
::QemuServer
::vm_human_monitor_command
($vmid, $input);
377 print "ERROR: $@" if $@;
384 __PACKAGE__-
>register_method ({
388 description
=> "Rescan all storages and update disk sizes and unused disk images.",
390 additionalProperties
=> 0,
392 vmid
=> get_standard_option
('pve-vmid', {
394 completion
=> \
&PVE
::QemuServer
::complete_vmid
,
398 returns
=> { type
=> 'null'},
402 PVE
::QemuServer
::rescan
($param->{vmid
});
407 __PACKAGE__-
>register_method ({
408 name
=> 'importdisk',
409 path
=> 'importdisk',
411 description
=> "Import an external disk image as an unused disk in a VM. The
412 image format has to be supported by qemu-img(1).",
414 additionalProperties
=> 0,
416 vmid
=> get_standard_option
('pve-vmid', {completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
418 description
=> 'Path to the disk image to import',
422 storage
=> get_standard_option
('pve-storage-id', {
423 description
=> 'Target storage ID',
424 completion
=> \
&PVE
::QemuServer
::complete_storage
,
429 description
=> 'Target format',
430 enum
=> [ 'raw', 'qcow2', 'vmdk' ],
435 returns
=> { type
=> 'null'},
439 my $vmid = extract_param
($param, 'vmid');
440 my $source = extract_param
($param, 'source');
441 my $storeid = extract_param
($param, 'storage');
442 my $format = extract_param
($param, 'format');
444 my $vm_conf = PVE
::QemuConfig-
>load_config($vmid);
445 PVE
::QemuConfig-
>check_lock($vm_conf);
446 die "$source: non-existent or non-regular file\n" if (! -f
$source);
448 my $storecfg = PVE
::Storage
::config
();
449 PVE
::Storage
::storage_check_enabled
($storecfg, $storeid);
451 my $target_storage_config =
452 PVE
::Storage
::storage_config
($storecfg, $storeid);
453 die "storage $storeid does not support vm images\n"
454 if !$target_storage_config->{content
}->{images
};
456 PVE
::QemuServer
::ImportDisk
::do_import
($source, $vmid, $storeid, { format
=> $format });
461 __PACKAGE__-
>register_method ({
465 description
=> "Open a terminal using a serial device (The VM need to have a serial device configured, for example 'serial0: socket')",
467 additionalProperties
=> 0,
469 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
471 description
=> "Select the serial device. By default we simply use the first suitable device.",
474 enum
=> [qw(serial0 serial1 serial2 serial3)],
478 returns
=> { type
=> 'null'},
482 my $vmid = $param->{vmid
};
484 my $conf = PVE
::QemuConfig-
>load_config ($vmid); # check if VM exists
486 my $iface = $param->{iface
};
489 die "serial interface '$iface' is not configured\n" if !$conf->{$iface};
490 die "wrong serial type on interface '$iface'\n" if $conf->{$iface} ne 'socket';
492 foreach my $opt (qw(serial0 serial1 serial2 serial3)) {
493 if ($conf->{$opt} && ($conf->{$opt} eq 'socket')) {
498 die "unable to find a serial interface\n" if !$iface;
501 die "VM $vmid not running\n" if !PVE
::QemuServer
::check_running
($vmid);
503 my $socket = "/var/run/qemu-server/${vmid}.$iface";
505 my $cmd = "socat UNIX-CONNECT:$socket STDIO,raw,echo=0,escape=0x0f";
507 print "starting serial terminal on interface $iface (press control-O to exit)\n";
514 __PACKAGE__-
>register_method ({
517 description
=> "Create a new VM using parameters read from an OVF manifest",
519 additionalProperties
=> 0,
521 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::Cluster
::complete_next_vmid
}),
524 description
=> 'path to the ovf file',
526 storage
=> get_standard_option
('pve-storage-id', {
527 description
=> 'Target storage ID',
528 completion
=> \
&PVE
::QemuServer
::complete_storage
,
533 description
=> 'Target format',
534 enum
=> [ 'raw', 'qcow2', 'vmdk' ],
539 description
=> 'Print a parsed representation of the extracted OVF parameters, but do not create a VM',
544 returns
=> { type
=> 'null' },
548 my $vmid = PVE
::Tools
::extract_param
($param, 'vmid');
549 my $ovf_file = PVE
::Tools
::extract_param
($param, 'manifest');
550 my $storeid = PVE
::Tools
::extract_param
($param, 'storage');
551 my $format = PVE
::Tools
::extract_param
($param, 'format');
552 my $dryrun = PVE
::Tools
::extract_param
($param, 'dryrun');
554 die "$ovf_file: non-existent or non-regular file\n" if (! -f
$ovf_file);
555 my $storecfg = PVE
::Storage
::config
();
556 PVE
::Storage
::storage_check_enabled
($storecfg, $storeid);
558 my $parsed = PVE
::QemuServer
::OVF
::parse_ovf
($ovf_file);
561 print Dumper
($parsed);
565 $param->{name
} = $parsed->{qm
}->{name
} if defined($parsed->{qm
}->{name
});
566 $param->{memory
} = $parsed->{qm
}->{memory
} if defined($parsed->{qm
}->{memory
});
567 $param->{cores
} = $parsed->{qm
}->{cores
} if defined($parsed->{qm
}->{cores
});
571 PVE
::Cluster
::check_vmid_unused
($vmid);
576 # order matters, as do_import() will load_config() internally
577 $conf->{smbios1
} = PVE
::QemuServer
::generate_smbios1_uuid
();
578 PVE
::QemuConfig-
>write_config($vmid, $conf);
580 foreach my $disk (@{ $parsed->{disks
} }) {
581 my ($file, $drive) = ($disk->{backing_file
}, $disk->{disk_address
});
582 PVE
::QemuServer
::ImportDisk
::do_import
($file, $vmid, $storeid,
583 { drive_name
=> $drive, format
=> $format });
586 # reload after disks entries have been created
587 $conf = PVE
::QemuConfig-
>load_config($vmid);
588 PVE
::QemuConfig-
>check_lock($conf);
589 my $firstdisk = PVE
::QemuServer
::resolve_first_disk
($conf);
590 $conf->{bootdisk
} = $firstdisk if $firstdisk;
591 PVE
::QemuConfig-
>write_config($vmid, $conf);
597 eval { PVE
::QemuServer
::vm_destroy
($storecfg, $vmid, $skiplock); };
598 die "import failed - $err";
602 my $wait_for_lock = 1;
603 return PVE
::QemuConfig-
>lock_config_full($vmid, $wait_for_lock, $importfn);
608 my $print_agent_result = sub {
611 my $result = $data->{result
};
612 return if !defined($result);
614 my $class = ref($result);
618 return if $result =~ m/^\s*$/;
623 if (($class eq 'HASH') && !scalar(keys %$result)) { # empty hash
627 print to_json
($result, { pretty
=> 1, canonical
=> 1});
631 list
=> [ "PVE::API2::Qemu", 'vmlist', [],
632 { node
=> $nodename }, sub {
635 exit 0 if (!scalar(@$vmlist));
637 printf "%10s %-20s %-10s %-10s %12s %-10s\n",
638 qw(VMID NAME STATUS MEM(MB) BOOTDISK
(GB
) PID
);
640 foreach my $rec (sort { $a->{vmid
} <=> $b->{vmid
} } @$vmlist) {
641 printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid
}, $rec->{name
},
642 $rec->{qmpstatus
} || $rec->{status
},
643 ($rec->{maxmem
} || 0)/(1024*1024),
644 ($rec->{maxdisk
} || 0)/(1024*1024*1024),
651 create
=> [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node
=> $nodename }, $upid_exit ],
653 destroy
=> [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { node
=> $nodename }, $upid_exit ],
655 clone
=> [ "PVE::API2::Qemu", 'clone_vm', ['vmid', 'newid'], { node
=> $nodename }, $upid_exit ],
657 migrate
=> [ "PVE::API2::Qemu", 'migrate_vm', ['vmid', 'target'], { node
=> $nodename }, $upid_exit ],
659 set
=> [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node
=> $nodename } ],
661 resize
=> [ "PVE::API2::Qemu", 'resize_vm', ['vmid', 'disk', 'size'], { node
=> $nodename } ],
663 move_disk
=> [ "PVE::API2::Qemu", 'move_vm_disk', ['vmid', 'disk', 'storage'], { node
=> $nodename }, $upid_exit ],
665 unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid'], { node
=> $nodename } ],
667 config
=> [ "PVE::API2::Qemu", 'vm_config', ['vmid'],
668 { node
=> $nodename }, sub {
670 foreach my $k (sort (keys %$config)) {
671 next if $k eq 'digest';
672 my $v = $config->{$k};
673 if ($k eq 'description') {
674 $v = PVE
::Tools
::encode_text
($v);
680 pending
=> [ "PVE::API2::Qemu", 'vm_pending', ['vmid'],
681 { node
=> $nodename }, sub {
683 foreach my $item (sort { $a->{key
} cmp $b->{key
}} @$data) {
684 my $k = $item->{key
};
685 next if $k eq 'digest';
686 my $v = $item->{value
};
687 my $p = $item->{pending
};
688 if ($k eq 'description') {
689 $v = PVE
::Tools
::encode_text
($v) if defined($v);
690 $p = PVE
::Tools
::encode_text
($p) if defined($p);
693 if ($item->{delete}) {
694 print "del $k: $v\n";
695 } elsif (defined($p)) {
696 print "cur $k: $v\n";
697 print "new $k: $p\n";
699 print "cur $k: $v\n";
701 } elsif (defined($p)) {
702 print "new $k: $p\n";
707 showcmd
=> [ __PACKAGE__
, 'showcmd', ['vmid']],
709 status
=> [ __PACKAGE__
, 'status', ['vmid']],
711 snapshot
=> [ "PVE::API2::Qemu", 'snapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
713 delsnapshot
=> [ "PVE::API2::Qemu", 'delsnapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
715 listsnapshot
=> [ "PVE::API2::Qemu", 'snapshot_list', ['vmid'], { node
=> $nodename },
718 foreach my $e (@$res) {
719 my $headline = $e->{description
} || 'no-description';
720 $headline =~ s/\n.*//sg;
721 my $parent = $e->{parent
} // 'no-parent';
722 printf("%-20s %-20s %s\n", $e->{name
}, $parent, $headline);
726 rollback
=> [ "PVE::API2::Qemu", 'rollback', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
728 template
=> [ "PVE::API2::Qemu", 'template', ['vmid'], { node
=> $nodename }],
730 start
=> [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { node
=> $nodename } , $upid_exit ],
732 stop
=> [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { node
=> $nodename }, $upid_exit ],
734 reset => [ "PVE::API2::Qemu", 'vm_reset', ['vmid'], { node
=> $nodename }, $upid_exit ],
736 shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { node
=> $nodename }, $upid_exit ],
738 suspend
=> [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { node
=> $nodename }, $upid_exit ],
740 resume
=> [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { node
=> $nodename }, $upid_exit ],
742 sendkey
=> [ "PVE::API2::Qemu", 'vm_sendkey', ['vmid', 'key'], { node
=> $nodename } ],
744 vncproxy
=> [ __PACKAGE__
, 'vncproxy', ['vmid']],
746 wait => [ __PACKAGE__
, 'wait', ['vmid']],
748 unlock
=> [ __PACKAGE__
, 'unlock', ['vmid']],
750 rescan
=> [ __PACKAGE__
, 'rescan', []],
752 monitor
=> [ __PACKAGE__
, 'monitor', ['vmid']],
754 agent
=> [ "PVE::API2::Qemu", 'agent', ['vmid', 'command'],
755 { node
=> $nodename }, $print_agent_result ],
757 mtunnel
=> [ __PACKAGE__
, 'mtunnel', []],
759 nbdstop
=> [ __PACKAGE__
, 'nbdstop', ['vmid']],
761 terminal
=> [ __PACKAGE__
, 'terminal', ['vmid']],
763 importdisk
=> [ __PACKAGE__
, 'importdisk', ['vmid', 'source', 'storage']],
765 importovf
=> [ __PACKAGE__
, 'importovf', ['vmid', 'manifest', 'storage']],