Import OVF: Lock config with "lock" property

Previously a VMID conflict was possible when creating a VM on another node
between locking the config with lock_config_full and writing to it for the
first time with write_config.

Using create_and_lock_config eliminates this possibility. This means that now
the "lock" property is set in the config instead of using flock only.

$param was empty when it was assigned the three values "name", "memory" and
"cores" before being assigned to $conf later on. Assigning those values
directly to $conf avoids confusion about what the two variables contain.

Signed-off-by: Dominic Jäger <d.jaeger@proxmox.com>
This commit is contained in:
Dominic Jäger 2019-10-28 12:47:34 +01:00 committed by Thomas Lamprecht
parent 7f384190de
commit 439390e868

View File

@ -621,15 +621,16 @@ __PACKAGE__->register_method ({
return; return;
} }
$param->{name} = $parsed->{qm}->{name} if defined($parsed->{qm}->{name}); eval { PVE::QemuConfig->create_and_lock_config($vmid, 0) };
$param->{memory} = $parsed->{qm}->{memory} if defined($parsed->{qm}->{memory}); die "Reserving empty config for OVF import failed: $@" if $@;
$param->{cores} = $parsed->{qm}->{cores} if defined($parsed->{qm}->{cores});
my $importfn = sub { my $conf = PVE::QemuConfig->load_config($vmid);
die "Internal error: Expected 'create' lock in config of VM $vmid!"
if !PVE::QemuConfig->has_lock($conf, "create");
PVE::Cluster::check_vmid_unused($vmid); $conf->{name} = $parsed->{qm}->{name} if defined($parsed->{qm}->{name});
$conf->{memory} = $parsed->{qm}->{memory} if defined($parsed->{qm}->{memory});
my $conf = $param; $conf->{cores} = $parsed->{qm}->{cores} if defined($parsed->{qm}->{cores});
eval { eval {
# order matters, as do_import() will load_config() internally # order matters, as do_import() will load_config() internally
@ -640,7 +641,7 @@ __PACKAGE__->register_method ({
foreach my $disk (@{ $parsed->{disks} }) { foreach my $disk (@{ $parsed->{disks} }) {
my ($file, $drive) = ($disk->{backing_file}, $disk->{disk_address}); my ($file, $drive) = ($disk->{backing_file}, $disk->{disk_address});
PVE::QemuServer::ImportDisk::do_import($file, $vmid, $storeid, PVE::QemuServer::ImportDisk::do_import($file, $vmid, $storeid,
0, { drive_name => $drive, format => $format }); 1, { drive_name => $drive, format => $format });
} }
# reload after disks entries have been created # reload after disks entries have been created
@ -658,10 +659,7 @@ __PACKAGE__->register_method ({
warn "Could not destroy VM $vmid: $@" if "$@"; warn "Could not destroy VM $vmid: $@" if "$@";
die "import failed - $err"; die "import failed - $err";
} }
}; PVE::QemuConfig->remove_lock ($vmid, "create");
my $wait_for_lock = 1;
PVE::QemuConfig->lock_config_full($vmid, $wait_for_lock, $importfn);
return undef; return undef;