6 # Note: disable '+' prefix for Getopt::Long (for resize command)
7 use Getopt
::Long
qw(:config no_getopt_compat);
14 use POSIX
qw(strftime);
18 use PVE
::APIClient
::LWP
;
20 use PVE
::Exception
qw(raise_param_exc);
21 use PVE
::GuestHelpers
;
23 use PVE
::JSONSchema
qw(get_standard_option);
25 use PVE
::RPCEnvironment
;
27 use PVE
::Tools
qw(extract_param);
29 use PVE
::API2
::Qemu
::Agent
;
32 use PVE
::QemuServer
::Drive
;
33 use PVE
::QemuServer
::Helpers
;
34 use PVE
::QemuServer
::Agent
qw(agent_available);
35 use PVE
::QemuServer
::ImportDisk
;
36 use PVE
::QemuServer
::Monitor
qw(mon_cmd);
37 use PVE
::QemuServer
::OVF
;
41 use base
qw(PVE::CLIHandler);
45 my $status = PVE
::Tools
::upid_read_status
($upid);
46 exit(PVE
::Tools
::upid_status_is_error
($status) ?
-1 : 0);
49 my $nodename = PVE
::INotify
::nodename
();
50 my %node = (node
=> $nodename);
52 sub setup_environment
{
53 PVE
::RPCEnvironment-
>setup_default_cli_env();
60 while ( ++$c < 10 && !-e
$path ) { sleep(1); }
62 my $s = IO
::Socket
::UNIX-
>new(Peer
=> $path, Timeout
=> 120);
64 die "unable to connect to socket '$path' - $!" if !$s;
66 my $select = IO
::Select-
>new();
68 $select->add(\
*STDIN
);
71 my $timeout = 60*15; # 15 minutes
74 while ($select->count &&
75 scalar(@handles = $select->can_read ($timeout))) {
76 foreach my $h (@handles) {
78 my $n = $h->sysread($buf, 4096);
88 syswrite(\
*STDOUT
, $buf);
98 sub print_recursive_hash
{
99 my ($prefix, $hash, $key) = @_;
101 if (ref($hash) eq 'HASH') {
103 print "$prefix$key:\n";
105 for my $itemkey (sort keys %$hash) {
106 print_recursive_hash
("\t$prefix", $hash->{$itemkey}, $itemkey);
108 } elsif (ref($hash) eq 'ARRAY') {
110 print "$prefix$key:\n";
112 for my $item (@$hash) {
113 print_recursive_hash
("\t$prefix", $item);
115 } elsif ((!ref($hash) && defined($hash)) || ref($hash) eq 'JSON::PP::Boolean') {
117 print "$prefix$key: $hash\n";
119 print "$prefix$hash\n";
124 __PACKAGE__-
>register_method ({
128 description
=> "Show command line which is used to start the VM (debug info).",
130 additionalProperties
=> 0,
132 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
134 description
=> "Puts each option on a new line to enhance human readability",
139 snapshot
=> get_standard_option
('pve-snapshot-name', {
140 description
=> "Fetch config values from given snapshot.",
143 my ($cmd, $pname, $cur, $args) = @_;
144 PVE
::QemuConfig-
>snapshot_list($args->[0]);
149 returns
=> { type
=> 'null'},
153 my $storecfg = PVE
::Storage
::config
();
154 my $cmdline = PVE
::QemuServer
::vm_commandline
($storecfg, $param->{vmid
}, $param->{snapshot
});
156 $cmdline =~ s/ -/ \\\n -/g if $param->{pretty
};
164 __PACKAGE__-
>register_method({
165 name
=> 'remote_migrate_vm',
166 path
=> 'remote_migrate_vm',
168 description
=> "Migrate virtual machine to a remote cluster. Creates a new migration task. EXPERIMENTAL feature!",
170 check
=> ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
173 additionalProperties
=> 0,
175 node
=> get_standard_option
('pve-node'),
176 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
177 'target-vmid' => get_standard_option
('pve-vmid', { optional
=> 1 }),
178 'target-endpoint' => get_standard_option
('proxmox-remote', {
179 description
=> "Remote target endpoint",
183 description
=> "Use online/live migration if VM is running. Ignored if VM is stopped.",
188 description
=> "Delete the original VM and related data after successful migration. By default the original VM is kept on the source cluster in a stopped state.",
192 'target-storage' => get_standard_option
('pve-targetstorage', {
193 completion
=> \
&PVE
::QemuServer
::complete_migration_storage
,
198 description
=> "Mapping from source to target bridges. Providing only a single bridge ID maps all source bridges to that bridge. Providing the special value '1' will map each source bridge to itself.",
199 format
=> 'bridge-pair-list',
202 description
=> "Override I/O bandwidth limit (in KiB/s).",
206 default => 'migrate limit from datacenter or storage config',
212 description
=> "the task ID.",
217 my $rpcenv = PVE
::RPCEnvironment
::get
();
218 my $authuser = $rpcenv->get_user();
220 my $source_vmid = $param->{vmid
};
221 my $target_endpoint = $param->{'target-endpoint'};
222 my $target_vmid = $param->{'target-vmid'} // $source_vmid;
224 my $remote = PVE
::JSONSchema
::parse_property_string
('proxmox-remote', $target_endpoint);
226 # TODO: move this as helper somewhere appropriate?
229 host
=> $remote->{host
},
230 port
=> $remote->{port
} // 8006,
231 apitoken
=> $remote->{apitoken
},
234 $conn_args->{cached_fingerprints
} = { uc($remote->{fingerprint
}) => 1 }
235 if defined($remote->{fingerprint
});
237 my $api_client = PVE
::APIClient
::LWP-
>new(%$conn_args);
238 my $resources = $api_client->get("/cluster/resources", { type
=> 'vm' });
239 if (grep { defined($_->{vmid
}) && $_->{vmid
} eq $target_vmid } @$resources) {
240 raise_param_exc
({ target_vmid
=> "Guest with ID '$target_vmid' already exists on remote cluster" });
243 my $storages = $api_client->get("/nodes/localhost/storage", { enabled
=> 1 });
245 my $storecfg = PVE
::Storage
::config
();
246 my $target_storage = $param->{'target-storage'};
247 my $storagemap = eval { PVE
::JSONSchema
::parse_idmap
($target_storage, 'pve-storage-id') };
248 raise_param_exc
({ 'target-storage' => "failed to parse storage map: $@" })
251 my $check_remote_storage = sub {
253 my $found = [ grep { $_->{storage
} eq $storage } @$storages ];
254 die "remote: storage '$storage' does not exist (or missing permission)!\n"
259 my $content_types = [ PVE
::Tools
::split_list
($found->{content
}) ];
260 die "remote: storage '$storage' cannot store images\n"
261 if !grep { $_ eq 'images' } @$content_types;
264 foreach my $target_sid (values %{$storagemap->{entries
}}) {
265 $check_remote_storage->($target_sid);
268 $check_remote_storage->($storagemap->{default})
269 if $storagemap->{default};
271 return PVE
::API2
::Qemu-
>remote_migrate_vm($param);
274 __PACKAGE__-
>register_method ({
278 description
=> "Show VM status.",
280 additionalProperties
=> 0,
282 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
284 description
=> "Verbose output format",
290 returns
=> { type
=> 'null'},
295 my $conf = PVE
::QemuConfig-
>load_config ($param->{vmid
});
297 my $vmstatus = PVE
::QemuServer
::vmstatus
($param->{vmid
}, 1);
298 my $stat = $vmstatus->{$param->{vmid
}};
299 if ($param->{verbose
}) {
300 foreach my $k (sort (keys %$stat)) {
301 next if $k eq 'cpu' || $k eq 'relcpu'; # always 0
303 print_recursive_hash
("", $v, $k);
306 my $status = $stat->{qmpstatus
} || 'unknown';
307 print "status: $status\n";
313 __PACKAGE__-
>register_method ({
317 description
=> "Proxy VM VNC traffic to stdin/stdout",
319 additionalProperties
=> 0,
321 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
324 returns
=> { type
=> 'null'},
328 my $vmid = $param->{vmid
};
329 PVE
::QemuConfig
::assert_config_exists_on_node
($vmid);
330 my $vnc_socket = PVE
::QemuServer
::Helpers
::vnc_socket
($vmid);
332 if (my $ticket = $ENV{LC_PVE_TICKET
}) { # NOTE: ssh on debian only pass LC_* variables
333 mon_cmd
($vmid, "set_password", protocol
=> 'vnc', password
=> $ticket);
334 mon_cmd
($vmid, "expire_password", protocol
=> 'vnc', time => "+30");
336 die "LC_PVE_TICKET not set, VNC proxy without password is forbidden\n";
339 run_vnc_proxy
($vnc_socket);
344 __PACKAGE__-
>register_method ({
348 description
=> "Unlock the VM.",
350 additionalProperties
=> 0,
352 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
355 returns
=> { type
=> 'null'},
359 my $vmid = $param->{vmid
};
361 PVE
::QemuConfig-
>lock_config ($vmid, sub {
362 my $conf = PVE
::QemuConfig-
>load_config($vmid);
363 delete $conf->{lock};
364 delete $conf->{pending
}->{lock} if $conf->{pending
}; # just to be sure
365 PVE
::QemuConfig-
>write_config($vmid, $conf);
371 __PACKAGE__-
>register_method ({
375 description
=> "Stop embedded nbd server.",
377 additionalProperties
=> 0,
379 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
382 returns
=> { type
=> 'null'},
386 my $vmid = $param->{vmid
};
388 eval { PVE
::QemuServer
::nbd_stop
($vmid) };
394 __PACKAGE__-
>register_method ({
398 description
=> "Used by qmigrate - do not use manually.",
400 additionalProperties
=> 0,
403 returns
=> { type
=> 'null'},
407 if (!PVE
::Cluster
::check_cfs_quorum
(1)) {
412 my $tunnel_write = sub {
419 $tunnel_write->("tunnel online");
420 $tunnel_write->("ver 1");
422 while (my $line = <STDIN
>) {
424 if ($line =~ /^quit$/) {
425 $tunnel_write->("OK");
427 } elsif ($line =~ /^resume (\d+)$/) {
429 # check_running and vm_resume with nocheck, since local node
430 # might not have processed config move/rename yet
431 if (PVE
::QemuServer
::check_running
($vmid, 1)) {
432 eval { PVE
::QemuServer
::vm_resume
($vmid, 1, 1); };
434 $tunnel_write->("ERR: resume failed - $@");
436 $tunnel_write->("OK");
439 $tunnel_write->("ERR: resume failed - VM $vmid not running");
447 __PACKAGE__-
>register_method ({
451 description
=> "Wait until the VM is stopped.",
453 additionalProperties
=> 0,
455 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
457 description
=> "Timeout in seconds. Default is to wait forever.",
464 returns
=> { type
=> 'null'},
468 my $vmid = $param->{vmid
};
469 my $timeout = $param->{timeout
};
471 my $pid = PVE
::QemuServer
::check_running
($vmid);
474 print "waiting until VM $vmid stopps (PID $pid)\n";
477 while ((!$timeout || ($count < $timeout)) && PVE
::QemuServer
::check_running
($vmid)) {
482 die "wait failed - got timeout\n" if PVE
::QemuServer
::check_running
($vmid);
487 __PACKAGE__-
>register_method ({
491 description
=> "Enter QEMU Monitor interface.",
493 additionalProperties
=> 0,
495 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
498 returns
=> { type
=> 'null'},
502 my $vmid = $param->{vmid
};
504 my $conf = PVE
::QemuConfig-
>load_config ($vmid); # check if VM exists
506 print "Entering QEMU Monitor for VM $vmid - type 'help' for help\n";
508 my $term = Term
::ReadLine-
>new('qm');
510 while (defined(my $input = $term->readline('qm> '))) {
512 next if $input =~ m/^\s*$/;
513 last if $input =~ m/^\s*q(uit)?\s*$/;
515 eval { print PVE
::QemuServer
::Monitor
::hmp_cmd
($vmid, $input) };
516 print "ERROR: $@" if $@;
523 __PACKAGE__-
>register_method ({
527 description
=> "Rescan all storages and update disk sizes and unused disk images.",
529 additionalProperties
=> 0,
531 vmid
=> get_standard_option
('pve-vmid', {
533 completion
=> \
&PVE
::QemuServer
::complete_vmid
,
539 description
=> 'Do not actually write changes out to VM config(s).',
543 returns
=> { type
=> 'null'},
547 my $dryrun = $param->{dryrun
};
549 print "NOTE: running in dry-run mode, won't write changes out!\n" if $dryrun;
551 PVE
::QemuServer
::rescan
($param->{vmid
}, 0, $dryrun);
556 __PACKAGE__-
>register_method ({
557 name
=> 'importdisk',
558 path
=> 'importdisk',
560 description
=> "Import an external disk image as an unused disk in a VM. The
561 image format has to be supported by qemu-img(1).",
563 additionalProperties
=> 0,
565 vmid
=> get_standard_option
('pve-vmid', {completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
567 description
=> 'Path to the disk image to import',
571 storage
=> get_standard_option
('pve-storage-id', {
572 description
=> 'Target storage ID',
573 completion
=> \
&PVE
::QemuServer
::complete_storage
,
578 description
=> 'Target format',
579 enum
=> [ 'raw', 'qcow2', 'vmdk' ],
584 returns
=> { type
=> 'null'},
588 my $vmid = extract_param
($param, 'vmid');
589 my $source = extract_param
($param, 'source');
590 my $storeid = extract_param
($param, 'storage');
591 my $format = extract_param
($param, 'format');
593 my $vm_conf = PVE
::QemuConfig-
>load_config($vmid);
594 PVE
::QemuConfig-
>check_lock($vm_conf);
595 die "$source: non-existent or non-regular file\n" if (! -f
$source);
597 my $storecfg = PVE
::Storage
::config
();
598 PVE
::Storage
::storage_check_enabled
($storecfg, $storeid);
600 my $target_storage_config = PVE
::Storage
::storage_config
($storecfg, $storeid);
601 die "storage $storeid does not support vm images\n"
602 if !$target_storage_config->{content
}->{images
};
604 print "importing disk '$source' to VM $vmid ...\n";
605 my ($drive_id, $volid) = PVE
::QemuServer
::ImportDisk
::do_import
($source, $vmid, $storeid, { format
=> $format });
606 print "Successfully imported disk as '$drive_id:$volid'\n";
611 __PACKAGE__-
>register_method ({
615 description
=> "Open a terminal using a serial device (The VM need to have a serial device configured, for example 'serial0: socket')",
617 additionalProperties
=> 0,
619 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
621 description
=> "Select the serial device. By default we simply use the first suitable device.",
624 enum
=> [qw(serial0 serial1 serial2 serial3)],
627 description
=> "Escape character.",
634 returns
=> { type
=> 'null'},
638 my $vmid = $param->{vmid
};
640 my $escape = $param->{escape
} // '^O';
641 if ($escape =~ /^\^([\x40-\x7a])$/) {
642 $escape = ord($1) & 0x1F;
643 } elsif ($escape =~ /^0x[0-9a-f]+$/i) {
644 $escape = hex($escape);
645 } elsif ($escape =~ /^[0-9]+$/) {
646 $escape = int($escape);
648 die "invalid escape character definition: $escape\n";
652 $escapemsg = sprintf(' (press Ctrl+%c to exit)', $escape+0x40);
653 $escape = sprintf(',escape=0x%x', $escape);
658 my $conf = PVE
::QemuConfig-
>load_config ($vmid); # check if VM exists
660 my $iface = $param->{iface
};
663 die "serial interface '$iface' is not configured\n" if !$conf->{$iface};
664 die "wrong serial type on interface '$iface'\n" if $conf->{$iface} ne 'socket';
666 foreach my $opt (qw(serial0 serial1 serial2 serial3)) {
667 if ($conf->{$opt} && ($conf->{$opt} eq 'socket')) {
672 die "unable to find a serial interface\n" if !$iface;
675 die "VM $vmid not running\n" if !PVE
::QemuServer
::check_running
($vmid);
677 my $socket = "/var/run/qemu-server/${vmid}.$iface";
679 my $cmd = "socat UNIX-CONNECT:$socket STDIO,raw,echo=0$escape";
681 print "starting serial terminal on interface ${iface}${escapemsg}\n";
688 __PACKAGE__-
>register_method ({
691 description
=> "Create a new VM using parameters read from an OVF manifest",
693 additionalProperties
=> 0,
695 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::Cluster
::complete_next_vmid
}),
698 description
=> 'path to the ovf file',
700 storage
=> get_standard_option
('pve-storage-id', {
701 description
=> 'Target storage ID',
702 completion
=> \
&PVE
::QemuServer
::complete_storage
,
707 description
=> 'Target format',
708 enum
=> [ 'raw', 'qcow2', 'vmdk' ],
713 description
=> 'Print a parsed representation of the extracted OVF parameters, but do not create a VM',
718 returns
=> { type
=> 'null' },
722 my $vmid = PVE
::Tools
::extract_param
($param, 'vmid');
723 my $ovf_file = PVE
::Tools
::extract_param
($param, 'manifest');
724 my $storeid = PVE
::Tools
::extract_param
($param, 'storage');
725 my $format = PVE
::Tools
::extract_param
($param, 'format');
726 my $dryrun = PVE
::Tools
::extract_param
($param, 'dryrun');
728 die "$ovf_file: non-existent or non-regular file\n" if (! -f
$ovf_file);
729 my $storecfg = PVE
::Storage
::config
();
730 PVE
::Storage
::storage_check_enabled
($storecfg, $storeid);
732 my $parsed = PVE
::QemuServer
::OVF
::parse_ovf
($ovf_file);
735 print to_json
($parsed, { pretty
=> 1, canonical
=> 1});
739 eval { PVE
::QemuConfig-
>create_and_lock_config($vmid) };
740 die "Reserving empty config for OVF import to VM $vmid failed: $@" if $@;
742 my $conf = PVE
::QemuConfig-
>load_config($vmid);
743 die "Internal error: Expected 'create' lock in config of VM $vmid!"
744 if !PVE
::QemuConfig-
>has_lock($conf, "create");
746 $conf->{name
} = $parsed->{qm
}->{name
} if defined($parsed->{qm
}->{name
});
747 $conf->{memory
} = $parsed->{qm
}->{memory
} if defined($parsed->{qm
}->{memory
});
748 $conf->{cores
} = $parsed->{qm
}->{cores
} if defined($parsed->{qm
}->{cores
});
750 my $imported_disks = [];
752 # order matters, as do_import() will load_config() internally
753 $conf->{vmgenid
} = PVE
::QemuServer
::generate_uuid
();
754 $conf->{smbios1
} = PVE
::QemuServer
::generate_smbios1_uuid
();
755 PVE
::QemuConfig-
>write_config($vmid, $conf);
757 foreach my $disk (@{ $parsed->{disks
} }) {
758 my ($file, $drive) = ($disk->{backing_file
}, $disk->{disk_address
});
759 my ($name, $volid) = PVE
::QemuServer
::ImportDisk
::do_import
($file, $vmid, $storeid, {
760 drive_name
=> $drive,
764 # for cleanup on (later) error
765 push @$imported_disks, $volid;
768 # reload after disks entries have been created
769 $conf = PVE
::QemuConfig-
>load_config($vmid);
770 my $devs = PVE
::QemuServer
::get_default_bootdevices
($conf);
771 $conf->{boot
} = PVE
::QemuServer
::print_bootorder
($devs);
772 PVE
::QemuConfig-
>write_config($vmid, $conf);
777 warn "error during import, cleaning up created resources...\n";
778 for my $volid (@$imported_disks) {
779 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid) };
780 warn "cleanup of $volid failed: $@\n" if $@;
782 eval { PVE
::QemuServer
::destroy_vm
($storecfg, $vmid, $skiplock) };
783 warn "Could not destroy VM $vmid: $@" if "$@";
784 die "import failed - $err";
787 PVE
::QemuConfig-
>remove_lock($vmid, "create");
794 __PACKAGE__-
>register_method({
799 description
=> "Executes the given command via the guest agent",
801 additionalProperties
=> 0,
803 node
=> get_standard_option
('pve-node'),
804 vmid
=> get_standard_option
('pve-vmid', {
805 completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
810 description
=> "If set to off, returns the pid immediately instead of waiting for the commmand to finish or the timeout.",
814 description
=> "The maximum time to wait synchronously for the command to finish. If reached, the pid gets returned. Set to 0 to deactivate",
821 description
=> "When set, read STDIN until EOF and forward to guest agent via 'input-data' (usually treated as STDIN to process launched by guest agent). Allows maximal 1 MiB.",
825 'extra-args' => get_standard_option
('extra-args'),
834 my $vmid = $param->{vmid
};
835 my $sync = $param->{synchronous
} // 1;
836 my $pass_stdin = $param->{'pass-stdin'};
837 if (defined($param->{timeout
}) && !$sync) {
838 raise_param_exc
({ synchronous
=> "needs to be set for 'timeout'"});
841 my $input_data = undef;
844 while (my $line = <STDIN
>) {
845 $input_data .= $line;
846 if (length($input_data) > 1024*1024) {
847 # not sure how QEMU handles large amounts of data being
848 # passed into the QMP socket, so limit to be safe
849 die "'input-data' (STDIN) is limited to 1 MiB, aborting\n";
854 my $args = $param->{'extra-args'};
855 $args = undef if !$args || !@$args;
857 my $res = PVE
::QemuServer
::Agent
::qemu_exec
($vmid, $input_data, $args);
860 my $pid = $res->{pid
};
861 my $timeout = $param->{timeout
} // 30;
862 my $starttime = time();
864 while ($timeout == 0 || (time() - $starttime) < $timeout) {
865 my $out = PVE
::QemuServer
::Agent
::qemu_exec_status
($vmid, $pid);
866 if ($out->{exited
}) {
873 if (!$res->{exited
}) {
874 warn "timeout reached, returning pid\n";
878 return { result
=> $res };
881 __PACKAGE__-
>register_method({
886 description
=> "Cleans up resources like tap devices, vgpus, etc. Called after a vm shuts down, crashes, etc.",
888 additionalProperties
=> 0,
890 node
=> get_standard_option
('pve-node'),
891 vmid
=> get_standard_option
('pve-vmid', {
892 completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
893 'clean-shutdown' => {
895 description
=> "Indicates if qemu shutdown cleanly.",
897 'guest-requested' => {
899 description
=> "Indicates if the shutdown was requested by the guest or via qmp.",
903 returns
=> { type
=> 'null', },
907 my $vmid = $param->{vmid
};
908 my $clean = $param->{'clean-shutdown'};
909 my $guest = $param->{'guest-requested'};
912 # return if we do not have the config anymore
913 return if !-f PVE
::QemuConfig-
>config_file($vmid);
915 my $storecfg = PVE
::Storage
::config
();
916 warn "Starting cleanup for $vmid\n";
918 # mdev cleanup can take a while, so wait up to 60 seconds
919 PVE
::QemuConfig-
>lock_config_full($vmid, 60, sub {
920 my $conf = PVE
::QemuConfig-
>load_config ($vmid);
921 my $pid = PVE
::QemuServer
::check_running
($vmid);
922 die "vm still running\n" if $pid;
925 # we have to cleanup the tap devices after a crash
927 foreach my $opt (keys %$conf) {
928 next if $opt !~ m/^net(\d+)$/;
930 PVE
::Network
::tap_unplug
("tap${vmid}i${interface}");
934 if (!$clean || $guest) {
935 # vm was shutdown from inside the guest or crashed, doing api cleanup
936 PVE
::QemuServer
::vm_stop_cleanup
($storecfg, $vmid, $conf, 0, 0);
938 PVE
::GuestHelpers
::exec_hookscript
($conf, $vmid, 'post-stop');
940 $restart = eval { PVE
::QemuServer
::clear_reboot_request
($vmid) };
944 warn "Finished cleanup for $vmid\n";
947 warn "Restarting VM $vmid\n";
948 PVE
::API2
::Qemu-
>vm_start({
957 my $print_agent_result = sub {
960 my $result = $data->{result
} // $data;
961 return if !defined($result);
963 my $class = ref($result);
967 return if $result =~ m/^\s*$/;
972 if (($class eq 'HASH') && !scalar(keys %$result)) { # empty hash
976 print to_json
($result, { pretty
=> 1, canonical
=> 1, utf8
=> 1});
982 my $ssh_key_map = ['sshkeys', sub {
983 return URI
::Escape
::uri_escape
(PVE
::Tools
::file_get_contents
($_[0]));
985 my $cipassword_map = PVE
::CLIHandler
::get_standard_mapping
('pve-password', { name
=> 'cipassword' });
986 my $password_map = PVE
::CLIHandler
::get_standard_mapping
('pve-password');
988 'update_vm' => [$ssh_key_map, $cipassword_map],
989 'create_vm' => [$ssh_key_map, $cipassword_map],
990 'set-user-password' => [$password_map],
993 return $mapping->{$name};
997 list
=> [ "PVE::API2::Qemu", 'vmlist', [], { %node }, sub {
999 exit 0 if (!scalar(@$vmlist));
1001 printf "%10s %-20s %-10s %-10s %12s %-10s\n",
1002 qw(VMID NAME STATUS MEM(MB) BOOTDISK
(GB
) PID
);
1004 foreach my $rec (sort { $a->{vmid
} <=> $b->{vmid
} } @$vmlist) {
1005 printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid
}, $rec->{name
},
1006 $rec->{qmpstatus
} || $rec->{status
},
1007 ($rec->{maxmem
} || 0)/(1024*1024),
1008 ($rec->{maxdisk
} || 0)/(1024*1024*1024),
1013 create
=> [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { %node }, $upid_exit ],
1014 destroy
=> [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { %node }, $upid_exit ],
1015 clone
=> [ "PVE::API2::Qemu", 'clone_vm', ['vmid', 'newid'], { %node }, $upid_exit ],
1017 migrate
=> [ "PVE::API2::Qemu", 'migrate_vm', ['vmid', 'target'], { %node }, $upid_exit ],
1018 'remote-migrate' => [ __PACKAGE__
, 'remote_migrate_vm', ['vmid', 'target-vmid', 'target-endpoint'], { %node }, $upid_exit ],
1020 set
=> [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { %node } ],
1022 config
=> [ "PVE::API2::Qemu", 'vm_config', ['vmid'], { %node }, sub {
1024 foreach my $k (sort (keys %$config)) {
1025 next if $k eq 'digest';
1026 my $v = $config->{$k};
1027 if ($k eq 'description') {
1028 $v = PVE
::Tools
::encode_text
($v);
1034 pending
=> [ "PVE::API2::Qemu", 'vm_pending', ['vmid'], { %node }, \
&PVE
::GuestHelpers
::format_pending
],
1035 showcmd
=> [ __PACKAGE__
, 'showcmd', ['vmid']],
1037 status
=> [ __PACKAGE__
, 'status', ['vmid']],
1039 # FIXME: for 8.0 move to command group snapshot { create, list, destroy, rollback }
1040 snapshot
=> [ "PVE::API2::Qemu", 'snapshot', ['vmid', 'snapname'], { %node } , $upid_exit ],
1041 delsnapshot
=> [ "PVE::API2::Qemu", 'delsnapshot', ['vmid', 'snapname'], { %node } , $upid_exit ],
1042 listsnapshot
=> [ "PVE::API2::Qemu", 'snapshot_list', ['vmid'], { %node }, \
&PVE
::GuestHelpers
::print_snapshot_tree
],
1043 rollback
=> [ "PVE::API2::Qemu", 'rollback', ['vmid', 'snapname'], { %node } , $upid_exit ],
1045 template
=> [ "PVE::API2::Qemu", 'template', ['vmid'], { %node }],
1047 # FIXME: should be in a power command group?
1048 start
=> [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { %node } , $upid_exit ],
1049 stop
=> [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { %node }, $upid_exit ],
1050 reset => [ "PVE::API2::Qemu", 'vm_reset', ['vmid'], { %node }, $upid_exit ],
1051 shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { %node }, $upid_exit ],
1052 reboot
=> [ "PVE::API2::Qemu", 'vm_reboot', ['vmid'], { %node }, $upid_exit ],
1053 suspend
=> [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { %node }, $upid_exit ],
1054 resume
=> [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { %node }, $upid_exit ],
1056 sendkey
=> [ "PVE::API2::Qemu", 'vm_sendkey', ['vmid', 'key'], { %node } ],
1058 vncproxy
=> [ __PACKAGE__
, 'vncproxy', ['vmid']],
1060 wait => [ __PACKAGE__
, 'wait', ['vmid']],
1062 unlock
=> [ __PACKAGE__
, 'unlock', ['vmid']],
1064 # TODO: evluate dropping below aliases for 8.0, if no usage is left
1065 importdisk
=> { alias
=> 'disk import' },
1066 'move-disk' => { alias
=> 'disk move' },
1067 move_disk
=> { alias
=> 'disk move' },
1068 rescan
=> { alias
=> 'disk rescan' },
1069 resize
=> { alias
=> 'disk resize' },
1070 unlink => { alias
=> 'disk unlink' },
1073 import
=> [ __PACKAGE__
, 'importdisk', ['vmid', 'source', 'storage']],
1074 'move' => [ "PVE::API2::Qemu", 'move_vm_disk', ['vmid', 'disk', 'storage'], { %node }, $upid_exit ],
1075 rescan
=> [ __PACKAGE__
, 'rescan', []],
1076 resize
=> [ "PVE::API2::Qemu", 'resize_vm', ['vmid', 'disk', 'size'], { %node } ],
1077 unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid'], { %node } ],
1080 monitor
=> [ __PACKAGE__
, 'monitor', ['vmid']],
1082 agent
=> { alias
=> 'guest cmd' }, # FIXME: remove with PVE 8.0
1085 cmd
=> [ "PVE::API2::Qemu::Agent", 'agent', ['vmid', 'command'], { %node }, $print_agent_result ],
1086 passwd
=> [ "PVE::API2::Qemu::Agent", 'set-user-password', [ 'vmid', 'username' ], { %node }],
1087 exec => [ __PACKAGE__
, 'exec', [ 'vmid', 'extra-args' ], { %node }, $print_agent_result],
1088 'exec-status' => [ "PVE::API2::Qemu::Agent", 'exec-status', [ 'vmid', 'pid' ], { %node }, $print_agent_result],
1091 mtunnel
=> [ __PACKAGE__
, 'mtunnel', []],
1093 nbdstop
=> [ __PACKAGE__
, 'nbdstop', ['vmid']],
1095 terminal
=> [ __PACKAGE__
, 'terminal', ['vmid']],
1097 importovf
=> [ __PACKAGE__
, 'importovf', ['vmid', 'manifest', 'storage']],
1099 cleanup
=> [ __PACKAGE__
, 'cleanup', ['vmid', 'clean-shutdown', 'guest-requested'], { %node }],
1102 dump => [ "PVE::API2::Qemu", 'cloudinit_generated_config_dump', ['vmid', 'type'], { %node }, sub { print "$_[0]\n"; }],
1103 pending
=> [ "PVE::API2::Qemu", 'cloudinit_pending', ['vmid'], { %node }, \
&PVE
::GuestHelpers
::format_pending
],
1104 update
=> [ "PVE::API2::Qemu", 'cloudinit_update', ['vmid'], { node
=> $nodename }],