mirror of
https://git.proxmox.com/git/qemu-server
synced 2025-04-29 20:18:39 +00:00
replace change_config_nolock with update_config_nolock
We now use cfs_file_write() in order to avoid race conditions between file IO and cfs operations (read after write works now).
This commit is contained in:
parent
5d7a6767be
commit
1858638fe3
@ -58,15 +58,15 @@ my $check_volume_access = sub {
|
|||||||
# Note: $pool is only needed when creating a VM, because pool permissions
|
# Note: $pool is only needed when creating a VM, because pool permissions
|
||||||
# are automatically inherited if VM already exists inside a pool.
|
# are automatically inherited if VM already exists inside a pool.
|
||||||
my $create_disks = sub {
|
my $create_disks = sub {
|
||||||
my ($rpcenv, $authuser, $storecfg, $vmid, $pool, $settings, $default_storage) = @_;
|
my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $settings, $default_storage) = @_;
|
||||||
|
|
||||||
# check permissions first
|
# check permissions first
|
||||||
|
|
||||||
my $alloc = [];
|
my $alloc = [];
|
||||||
foreach_drive($settings, sub {
|
PVE::QemuServer::foreach_drive($settings, sub {
|
||||||
my ($ds, $disk) = @_;
|
my ($ds, $disk) = @_;
|
||||||
|
|
||||||
return if drive_is_cdrom($disk);
|
return if PVE::QemuServer::drive_is_cdrom($disk);
|
||||||
|
|
||||||
my $volid = $disk->{file};
|
my $volid = $disk->{file};
|
||||||
|
|
||||||
@ -112,7 +112,7 @@ my $create_disks = sub {
|
|||||||
foreach my $task (@$alloc) {
|
foreach my $task (@$alloc) {
|
||||||
my ($ds, $disk) = @$task;
|
my ($ds, $disk) = @$task;
|
||||||
delete $disk->{format}; # no longer needed
|
delete $disk->{format}; # no longer needed
|
||||||
$settings->{$ds} = PVE::QemuServer::print_drive($vmid, $disk);
|
$conf->{$ds} = $settings->{$ds} = PVE::QemuServer::print_drive($vmid, $disk);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $vollist;
|
return $vollist;
|
||||||
@ -345,24 +345,26 @@ __PACKAGE__->register_method({
|
|||||||
|
|
||||||
my $vollist = [];
|
my $vollist = [];
|
||||||
|
|
||||||
|
my $conf = $param;
|
||||||
|
|
||||||
eval {
|
eval {
|
||||||
$vollist = &$create_disks($rpcenv, $authuser, $storecfg, $vmid, $pool, $param, $storage);
|
$vollist = &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $param, $storage);
|
||||||
|
|
||||||
# try to be smart about bootdisk
|
# try to be smart about bootdisk
|
||||||
my @disks = PVE::QemuServer::disknames();
|
my @disks = PVE::QemuServer::disknames();
|
||||||
my $firstdisk;
|
my $firstdisk;
|
||||||
foreach my $ds (reverse @disks) {
|
foreach my $ds (reverse @disks) {
|
||||||
next if !$param->{$ds};
|
next if !$conf->{$ds};
|
||||||
my $disk = PVE::QemuServer::parse_drive($ds, $param->{$ds});
|
my $disk = PVE::QemuServer::parse_drive($ds, $conf->{$ds});
|
||||||
next if PVE::QemuServer::drive_is_cdrom($disk);
|
next if PVE::QemuServer::drive_is_cdrom($disk);
|
||||||
$firstdisk = $ds;
|
$firstdisk = $ds;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$param->{bootdisk} && $firstdisk) {
|
if (!$conf->{bootdisk} && $firstdisk) {
|
||||||
$param->{bootdisk} = $firstdisk;
|
$conf->{bootdisk} = $firstdisk;
|
||||||
}
|
}
|
||||||
|
|
||||||
PVE::QemuServer::create_conf_nolock($vmid, $param);
|
PVE::QemuServer::update_conf_nolock($vmid, $conf);
|
||||||
};
|
};
|
||||||
my $err = $@;
|
my $err = $@;
|
||||||
|
|
||||||
@ -546,9 +548,7 @@ __PACKAGE__->register_method({
|
|||||||
}});
|
}});
|
||||||
|
|
||||||
my $delete_drive = sub {
|
my $delete_drive = sub {
|
||||||
my ($conf, $storecfg, $vmid, $drive, $force) = @_;
|
my ($conf, $storecfg, $vmid, $key, $drive, $force) = @_;
|
||||||
|
|
||||||
my $changes = {};
|
|
||||||
|
|
||||||
if (!PVE::QemuServer::drive_is_cdrom($drive)) {
|
if (!PVE::QemuServer::drive_is_cdrom($drive)) {
|
||||||
my $volid = $drive->{file};
|
my $volid = $drive->{file};
|
||||||
@ -561,15 +561,14 @@ my $delete_drive = sub {
|
|||||||
eval { PVE::Storage::vdisk_free($storecfg, $volid); };
|
eval { PVE::Storage::vdisk_free($storecfg, $volid); };
|
||||||
warn $@ if $@;
|
warn $@ if $@;
|
||||||
} else {
|
} else {
|
||||||
PVE::QemuServer::add_unused_volume($conf, $volid, $vmid, $changes);
|
PVE::QemuServer::add_unused_volume($conf, $volid, $vmid);
|
||||||
}
|
}
|
||||||
|
delete $conf->{$key};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $changes;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
my $vm_config_perm_list = [
|
my $vm_config_perm_list = [
|
||||||
@ -703,18 +702,17 @@ __PACKAGE__->register_method({
|
|||||||
|
|
||||||
die "error hot-unplug $opt" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
|
die "error hot-unplug $opt" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
|
||||||
|
|
||||||
my $changes = {};
|
if (PVE::QemuServer::valid_drivename($opt)) {
|
||||||
|
my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
|
||||||
if (my $drive = $drive_hash->{$opt}) { # drives
|
&$delete_drive($conf, $storecfg, $vmid, $opt, $drive, $force);
|
||||||
$changes = &$delete_drive($conf, $storecfg, $vmid, $drive, $force);
|
|
||||||
} elsif ($opt =~ m/^unused/) {
|
} elsif ($opt =~ m/^unused/) {
|
||||||
my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
|
my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
|
||||||
my $volid = $drive->{file};
|
my $volid = $drive->{file};
|
||||||
eval { PVE::Storage::vdisk_free($storecfg, $volid); };
|
eval { PVE::Storage::vdisk_free($storecfg, $volid); };
|
||||||
warn $@ if $@;
|
warn $@ if $@;
|
||||||
|
delete $conf->{$opt};
|
||||||
}
|
}
|
||||||
|
PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
|
||||||
PVE::QemuServer::change_config_nolock($vmid, $changes, { $opt => 1 }, 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach my $opt (keys %$param) { # add/change
|
foreach my $opt (keys %$param) { # add/change
|
||||||
@ -732,8 +730,8 @@ __PACKAGE__->register_method({
|
|||||||
|
|
||||||
die "error hot-unplug $opt" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
|
die "error hot-unplug $opt" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
|
||||||
|
|
||||||
my $changes = &$delete_drive($conf, $storecfg, $vmid, $old_drive, 0);
|
&$delete_drive($conf, $storecfg, $vmid, $opt, $old_drive, 0);
|
||||||
PVE::QemuServer::change_config_nolock($vmid, $changes, { $opt => 1 }, 1);
|
PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
|
||||||
$conf = PVE::QemuServer::load_config($vmid); # update/reload
|
$conf = PVE::QemuServer::load_config($vmid); # update/reload
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -749,13 +747,14 @@ __PACKAGE__->register_method({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PVE::QemuServer::change_config_nolock($vmid, { $opt => $param->{$opt} }, {}, 1);
|
$conf->{$opt} = $param->{$opt};
|
||||||
|
PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
|
||||||
|
|
||||||
} else { #hdd
|
} else { #hdd
|
||||||
|
|
||||||
my $settings = { $opt => $param->{$opt} };
|
my $settings = { $opt => $param->{$opt} };
|
||||||
&$create_disks($rpcenv, $authuser, $storecfg, $vmid, undef, $settings);
|
&$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, undef, $settings);
|
||||||
PVE::QemuServer::change_config_nolock($vmid, { $opt => $settings->{$opt} }, {}, 1);
|
PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
|
||||||
$conf = PVE::QemuServer::load_config($vmid); # update/reload
|
$conf = PVE::QemuServer::load_config($vmid); # update/reload
|
||||||
|
|
||||||
my $newdrive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
|
my $newdrive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
|
||||||
@ -770,15 +769,18 @@ __PACKAGE__->register_method({
|
|||||||
die "error hot-unplug $opt for update" if $conf->{$opt} &&
|
die "error hot-unplug $opt for update" if $conf->{$opt} &&
|
||||||
!PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
|
!PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
|
||||||
|
|
||||||
PVE::QemuServer::change_config_nolock($vmid, { $opt => $param->{$opt} }, {}, 1);
|
$conf->{$opt} = $param->{$opt};
|
||||||
|
PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
|
||||||
$conf = PVE::QemuServer::load_config($vmid); # update/reload
|
$conf = PVE::QemuServer::load_config($vmid); # update/reload
|
||||||
|
|
||||||
my $newnet = PVE::QemuServer::parse_net($conf->{$opt});
|
my $newnet = PVE::QemuServer::parse_net($conf->{$opt});
|
||||||
|
|
||||||
die "error hotplug $opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $newnet);
|
die "error hotplug $opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $newnet);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
PVE::QemuServer::change_config_nolock($vmid, { $opt => $param->{$opt} }, {}, 1);
|
$conf->{$opt} = $param->{$opt};
|
||||||
|
PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -258,7 +258,8 @@ sub phase1 {
|
|||||||
my $conf = $self->{vmconf};
|
my $conf = $self->{vmconf};
|
||||||
|
|
||||||
# set migrate lock in config file
|
# set migrate lock in config file
|
||||||
PVE::QemuServer::change_config_nolock($vmid, { lock => 'migrate' }, {}, 1);
|
$conf->{lock} = 'migrate';
|
||||||
|
PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
|
||||||
|
|
||||||
sync_disks($self, $vmid);
|
sync_disks($self, $vmid);
|
||||||
|
|
||||||
@ -275,8 +276,9 @@ sub phase1_cleanup {
|
|||||||
|
|
||||||
$self->log('info', "aborting phase 1 - cleanup resources");
|
$self->log('info', "aborting phase 1 - cleanup resources");
|
||||||
|
|
||||||
my $unset = { lock => 1 };
|
my $conf = $self->{vmconf};
|
||||||
eval { PVE::QemuServer::change_config_nolock($vmid, {}, $unset, 1) };
|
delete $conf->{lock};
|
||||||
|
eval { PVE::QemuServer::update_config_nolock($vmid, $conf, 1) };
|
||||||
if (my $err = $@) {
|
if (my $err = $@) {
|
||||||
$self->log('err', $err);
|
$self->log('err', $err);
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,9 @@ my $cpuinfo = PVE::ProcFSTools::read_cpuinfo();
|
|||||||
# allowed when such lock is set. But you can ignore this kind of
|
# allowed when such lock is set. But you can ignore this kind of
|
||||||
# lock with the --skiplock flag.
|
# lock with the --skiplock flag.
|
||||||
|
|
||||||
cfs_register_file('/qemu-server/', \&parse_vm_config);
|
cfs_register_file('/qemu-server/',
|
||||||
|
\&parse_vm_config,
|
||||||
|
\&write_vm_config);
|
||||||
|
|
||||||
PVE::JSONSchema::register_standard_option('skiplock', {
|
PVE::JSONSchema::register_standard_option('skiplock', {
|
||||||
description => "Ignore locks - only root is allowed to use this option.",
|
description => "Ignore locks - only root is allowed to use this option.",
|
||||||
@ -1084,7 +1086,7 @@ sub add_random_macs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sub add_unused_volume {
|
sub add_unused_volume {
|
||||||
my ($config, $volid, $changes) = @_;
|
my ($config, $volid) = @_;
|
||||||
|
|
||||||
my $key;
|
my $key;
|
||||||
for (my $ind = $MAX_UNUSED_DISKS - 1; $ind >= 0; $ind--) {
|
for (my $ind = $MAX_UNUSED_DISKS - 1; $ind >= 0; $ind--) {
|
||||||
@ -1098,7 +1100,9 @@ sub add_unused_volume {
|
|||||||
|
|
||||||
die "To many unused volume - please delete them first.\n" if !$key;
|
die "To many unused volume - please delete them first.\n" if !$key;
|
||||||
|
|
||||||
$changes->{$key} = $config->{$key} = $volid;
|
$config->{$key} = $volid;
|
||||||
|
|
||||||
|
return $key;
|
||||||
}
|
}
|
||||||
|
|
||||||
# fixme: remove all thos $noerr parameters?
|
# fixme: remove all thos $noerr parameters?
|
||||||
@ -1483,127 +1487,74 @@ sub parse_vm_config {
|
|||||||
return $res;
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub change_config {
|
sub write_vm_config {
|
||||||
my ($vmid, $settings, $unset, $skiplock) = @_;
|
my ($filename, $conf) = @_;
|
||||||
|
|
||||||
lock_config($vmid, &change_config_nolock, $settings, $unset, $skiplock);
|
if ($conf->{cdrom}) {
|
||||||
}
|
die "option ide2 conflicts with cdrom\n" if $conf->{ide2};
|
||||||
|
$conf->{ide2} = $conf->{cdrom};
|
||||||
sub change_config_nolock {
|
delete $conf->{cdrom};
|
||||||
my ($vmid, $settings, $unset, $skiplock) = @_;
|
}
|
||||||
|
|
||||||
my $res = {};
|
|
||||||
|
|
||||||
$unset->{ide2} = $unset->{cdrom} if $unset->{cdrom};
|
|
||||||
|
|
||||||
check_lock($settings) if !$skiplock;
|
|
||||||
|
|
||||||
# we do not use 'smp' any longer
|
# we do not use 'smp' any longer
|
||||||
if ($settings->{sockets}) {
|
if ($conf->{sockets}) {
|
||||||
$unset->{smp} = 1;
|
delete $conf->{smp};
|
||||||
} elsif ($settings->{smp}) {
|
} elsif ($conf->{smp}) {
|
||||||
$settings->{sockets} = $settings->{smp};
|
$conf->{sockets} = $conf->{smp};
|
||||||
$unset->{smp} = 1;
|
delete $conf->{cores};
|
||||||
|
delete $conf->{smp};
|
||||||
}
|
}
|
||||||
|
|
||||||
my $new_volids = {};
|
my $new_volids = {};
|
||||||
|
foreach my $key (keys %$conf) {
|
||||||
foreach my $key (keys %$settings) {
|
|
||||||
next if $key eq 'digest';
|
next if $key eq 'digest';
|
||||||
my $value = $settings->{$key};
|
my $value = $conf->{$key};
|
||||||
if ($key eq 'description') {
|
if ($key eq 'description') {
|
||||||
$value = PVE::Tools::encode_text($value);
|
$value = PVE::Tools::encode_text($value);
|
||||||
}
|
}
|
||||||
eval { $value = check_type($key, $value); };
|
eval { $value = check_type($key, $value); };
|
||||||
die "unable to parse value of '$key' - $@" if $@;
|
die "unable to parse value of '$key' - $@" if $@;
|
||||||
if ($key eq 'cdrom') {
|
|
||||||
$res->{ide2} = $value;
|
$conf->{$key} = $value;
|
||||||
} else {
|
|
||||||
$res->{$key} = $value;
|
|
||||||
}
|
|
||||||
if (valid_drivename($key)) {
|
if (valid_drivename($key)) {
|
||||||
my $drive = PVE::QemuServer::parse_drive($key, $value);
|
my $drive = PVE::QemuServer::parse_drive($key, $value);
|
||||||
$new_volids->{$drive->{file}} = 1 if $drive && $drive->{file};
|
$new_volids->{$drive->{file}} = 1 if $drive && $drive->{file};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
my $filename = config_file($vmid);
|
|
||||||
my $tmpfn = "$filename.$$.tmp";
|
|
||||||
|
|
||||||
my $fh = new IO::File($filename, "r") ||
|
|
||||||
die "unable to read config for VM $vmid\n";
|
|
||||||
|
|
||||||
my $werror = "unable to write config for VM $vmid\n";
|
|
||||||
|
|
||||||
my $out = new IO::File($tmpfn, "w") || die $werror;
|
|
||||||
|
|
||||||
eval {
|
|
||||||
|
|
||||||
my $done;
|
|
||||||
|
|
||||||
while (my $line = <$fh>) {
|
|
||||||
|
|
||||||
if (($line =~ m/^\#/) || ($line =~ m/^\s*$/)) {
|
|
||||||
die $werror unless print $out $line;
|
|
||||||
next;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($line =~ m/^([a-z][a-z_]*\d*):\s*(.*\S)\s*$/) {
|
|
||||||
my $key = $1;
|
|
||||||
my $value = $2;
|
|
||||||
|
|
||||||
# remove 'unusedX' settings if we re-add a volume
|
# remove 'unusedX' settings if we re-add a volume
|
||||||
next if $key =~ m/^unused/ && $new_volids->{$value};
|
foreach my $key (keys %$conf) {
|
||||||
|
my $value = $conf->{$key};
|
||||||
# convert 'smp' to 'sockets'
|
if ($key =~ m/^unused/ && $new_volids->{$value}) {
|
||||||
$key = 'sockets' if $key eq 'smp';
|
delete $conf->{$key};
|
||||||
|
|
||||||
next if $done->{$key};
|
|
||||||
$done->{$key} = 1;
|
|
||||||
|
|
||||||
if (defined($res->{$key})) {
|
|
||||||
$value = $res->{$key};
|
|
||||||
delete $res->{$key};
|
|
||||||
}
|
|
||||||
if (!defined($unset->{$key})) {
|
|
||||||
die $werror unless print $out "$key: $value\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
next;
|
|
||||||
}
|
|
||||||
|
|
||||||
die "unable to parse config file: $line\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach my $key (keys %$res) {
|
|
||||||
|
|
||||||
if (!defined($unset->{$key})) {
|
|
||||||
die $werror unless print $out "$key: $res->{$key}\n";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
my $err = $@;
|
# gererate RAW data
|
||||||
|
my $raw = '';
|
||||||
$fh->close();
|
foreach my $key (sort keys %$conf) {
|
||||||
|
next if $key eq 'digest';
|
||||||
if ($err) {
|
$raw .= "$key: $conf->{$key}\n";
|
||||||
$out->close();
|
|
||||||
unlink $tmpfn;
|
|
||||||
die $err;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$out->close()) {
|
return $raw;
|
||||||
$err = "close failed - $!\n";
|
}
|
||||||
unlink $tmpfn;
|
|
||||||
die $err;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!rename($tmpfn, $filename)) {
|
sub update_config_nolock {
|
||||||
$err = "rename failed - $!\n";
|
my ($vmid, $conf, $skiplock) = @_;
|
||||||
unlink $tmpfn;
|
|
||||||
die $err;
|
check_lock($conf) if !$skiplock;
|
||||||
}
|
|
||||||
|
my $cfspath = cfs_config_path($vmid);
|
||||||
|
|
||||||
|
PVE::Cluster::cfs_write_file($cfspath, $conf);
|
||||||
|
}
|
||||||
|
|
||||||
|
sub update_config {
|
||||||
|
my ($vmid, $conf, $skiplock) = @_;
|
||||||
|
|
||||||
|
lock_config($vmid, &update_config_nolock, $conf, $skiplock);
|
||||||
}
|
}
|
||||||
|
|
||||||
sub load_defaults {
|
sub load_defaults {
|
||||||
|
4
qm
4
qm
@ -185,7 +185,9 @@ __PACKAGE__->register_method ({
|
|||||||
my $vmid = $param->{vmid};
|
my $vmid = $param->{vmid};
|
||||||
|
|
||||||
PVE::QemuServer::lock_config ($vmid, sub {
|
PVE::QemuServer::lock_config ($vmid, sub {
|
||||||
PVE::QemuServer::change_config_nolock ($vmid, {}, { lock => 1 }, 1);
|
my $conf = PVE::QemuServer::load_config($vmid);
|
||||||
|
delete $conf->{lock};
|
||||||
|
PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
return undef;
|
return undef;
|
||||||
|
Loading…
Reference in New Issue
Block a user