+__PACKAGE__->register_method ({
+ name => 'importovf',
+ path => 'importovf',
+ description => "Create a new VM using parameters read from an OVF manifest",
+ parameters => {
+ additionalProperties => 0,
+ properties => {
+ vmid => get_standard_option('pve-vmid', { completion => \&PVE::Cluster::complete_next_vmid }),
+ manifest => {
+ type => 'string',
+ description => 'path to the ovf file',
+ },
+ storage => get_standard_option('pve-storage-id', {
+ description => 'Target storage ID',
+ completion => \&PVE::QemuServer::complete_storage,
+ optional => 0,
+ }),
+ format => {
+ type => 'string',
+ description => 'Target format',
+ enum => [ 'raw', 'qcow2', 'vmdk' ],
+ optional => 1,
+ },
+ dryrun => {
+ type => 'boolean',
+ description => 'Print a parsed representation of the extracted OVF parameters, but do not create a VM',
+ optional => 1,
+ }
+ },
+ },
+ returns => { type => 'null' },
+ code => sub {
+ my ($param) = @_;
+
+ my $vmid = PVE::Tools::extract_param($param, 'vmid');
+ my $ovf_file = PVE::Tools::extract_param($param, 'manifest');
+ my $storeid = PVE::Tools::extract_param($param, 'storage');
+ my $format = PVE::Tools::extract_param($param, 'format');
+ my $dryrun = PVE::Tools::extract_param($param, 'dryrun');
+
+ die "$ovf_file: non-existent or non-regular file\n" if (! -f $ovf_file);
+ my $storecfg = PVE::Storage::config();
+ PVE::Storage::storage_check_enabled($storecfg, $storeid);
+
+ my $parsed = PVE::QemuServer::OVF::parse_ovf($ovf_file);
+
+ if ($dryrun) {
+ print to_json($parsed, { pretty => 1, canonical => 1});
+ return;
+ }
+
+ $param->{name} = $parsed->{qm}->{name} if defined($parsed->{qm}->{name});
+ $param->{memory} = $parsed->{qm}->{memory} if defined($parsed->{qm}->{memory});
+ $param->{cores} = $parsed->{qm}->{cores} if defined($parsed->{qm}->{cores});
+
+ my $importfn = sub {
+
+ PVE::Cluster::check_vmid_unused($vmid);
+
+ my $conf = $param;
+
+ eval {
+ # order matters, as do_import() will load_config() internally
+ $conf->{vmgenid} = PVE::QemuServer::generate_uuid();
+ $conf->{smbios1} = PVE::QemuServer::generate_smbios1_uuid();
+ PVE::QemuConfig->write_config($vmid, $conf);
+
+ foreach my $disk (@{ $parsed->{disks} }) {
+ my ($file, $drive) = ($disk->{backing_file}, $disk->{disk_address});
+ PVE::QemuServer::ImportDisk::do_import($file, $vmid, $storeid,
+ { drive_name => $drive, format => $format });
+ }
+
+ # reload after disks entries have been created
+ $conf = PVE::QemuConfig->load_config($vmid);
+ PVE::QemuConfig->check_lock($conf);
+ my $firstdisk = PVE::QemuServer::resolve_first_disk($conf);
+ $conf->{bootdisk} = $firstdisk if $firstdisk;
+ PVE::QemuConfig->write_config($vmid, $conf);
+ };
+
+ my $err = $@;
+ if ($err) {
+ my $skiplock = 1;
+ eval { PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock); };
+ die "import failed - $err";
+ }
+ };
+
+ my $wait_for_lock = 1;
+ PVE::QemuConfig->lock_config_full($vmid, $wait_for_lock, $importfn);
+
+ return undef;
+
+ }
+});
+
+__PACKAGE__->register_method({
+ name => 'exec',
+ path => 'exec',
+ method => 'POST',
+ protected => 1,
+ description => "Executes the given command via the guest agent",
+ parameters => {
+ additionalProperties => 0,
+ properties => {
+ node => get_standard_option('pve-node'),
+ vmid => get_standard_option('pve-vmid', {
+ completion => \&PVE::QemuServer::complete_vmid_running }),
+ synchronous => {
+ type => 'boolean',
+ optional => 1,
+ default => 1,
+ description => "If set to off, returns the pid immediately instead of waiting for the commmand to finish or the timeout.",
+ },
+ 'timeout' => {
+ type => 'integer',
+ description => "The maximum time to wait synchronously for the command to finish. If reached, the pid gets returned. Set to 0 to deactivate",
+ minimum => 0,
+ optional => 1,
+ default => 30,
+ },
+ 'extra-args' => get_standard_option('extra-args'),
+ },
+ },
+ returns => {
+ type => 'object',
+ },
+ code => sub {
+ my ($param) = @_;
+
+ my $vmid = $param->{vmid};
+ my $sync = $param->{synchronous} // 1;
+ if (!$param->{'extra-args'} || !@{$param->{'extra-args'}}) {
+ raise_param_exc( { 'extra-args' => "No command given" });
+ }
+ if (defined($param->{timeout}) && !$sync) {
+ raise_param_exc({ synchronous => "needs to be set for 'timeout'"});
+ }
+
+ my $res = PVE::QemuServer::Agent::qemu_exec($vmid, $param->{'extra-args'});
+
+ if ($sync) {
+ my $pid = $res->{pid};
+ my $timeout = $param->{timeout} // 30;
+ my $starttime = time();
+
+ while ($timeout == 0 || (time() - $starttime) < $timeout) {
+ my $out = PVE::QemuServer::Agent::qemu_exec_status($vmid, $pid);
+ if ($out->{exited}) {
+ $res = $out;
+ last;
+ }
+ sleep 1;
+ }
+
+ if (!$res->{exited}) {
+ warn "timeout reached, returning pid\n";
+ }
+ }
+
+ return { result => $res };
+ }});
+
+__PACKAGE__->register_method({
+ name => 'cleanup',
+ path => 'cleanup',
+ method => 'POST',
+ protected => 1,
+ description => "Cleans up resources like tap devices, vgpus, etc. Called after a vm shuts down, crashes, etc.",
+ parameters => {
+ additionalProperties => 0,
+ properties => {
+ node => get_standard_option('pve-node'),
+ vmid => get_standard_option('pve-vmid', {
+ completion => \&PVE::QemuServer::complete_vmid_running }),
+ 'clean-shutdown' => {
+ type => 'boolean',
+ description => "Indicates if qemu shutdown cleanly.",
+ },
+ 'guest-requested' => {
+ type => 'boolean',
+ description => "Indicates if the shutdown was requested by the guest or via qmp.",
+ },
+ },
+ },
+ returns => { type => 'null', },
+ code => sub {
+ my ($param) = @_;
+
+ my $vmid = $param->{vmid};
+ my $clean = $param->{'clean-shutdown'};
+ my $guest = $param->{'guest-requested'};
+
+ # return if we do not have the config anymore
+ return if !-f PVE::QemuConfig->config_file($vmid);
+
+ my $storecfg = PVE::Storage::config();
+ warn "Starting cleanup for $vmid\n";
+
+ PVE::QemuConfig->lock_config($vmid, sub {
+ my $conf = PVE::QemuConfig->load_config ($vmid);
+ my $pid = PVE::QemuServer::check_running ($vmid);
+ die "vm still running\n" if $pid;
+
+ if (!$clean) {
+ # we have to cleanup the tap devices after a crash
+
+ foreach my $opt (keys %$conf) {
+ next if $opt !~ m/^net(\d)+$/;
+ my $interface = $1;
+ PVE::Network::tap_unplug("tap${vmid}i${interface}");
+ }
+ }
+
+ if (!$clean || $guest) {
+ # vm was shutdown from inside the guest or crashed, doing api cleanup
+ PVE::QemuServer::vm_stop_cleanup($storecfg, $vmid, $conf, 0, 0);
+ }
+ });
+
+ warn "Finished cleanup for $vmid\n";
+
+ return undef;
+ }});