[pve-devel] [PATCH v2 qemu-server 06/18] refactor pending changes related code

Thomas Lamprecht t.lamprecht at proxmox.com
Wed Oct 2 11:57:07 CEST 2019


subject:

> use new config helpers from guest-common for pending changes

from a quick glance: OK besides that

On 9/30/19 2:44 PM, Oguz Bektas wrote:
> most of the pending changes related code has been moved into
> AbstractConfig, so we have to call them as class methods from QemuConfig instead.
> 
> Signed-off-by: Oguz Bektas <o.bektas at proxmox.com>
> ---
>  PVE/API2/Qemu.pm  | 14 ++++-----
>  PVE/QemuServer.pm | 79 +++++------------------------------------------
>  2 files changed, 15 insertions(+), 78 deletions(-)
> 
> diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
> index aa1cd16..9dc07a6 100644
> --- a/PVE/API2/Qemu.pm
> +++ b/PVE/API2/Qemu.pm
> @@ -919,7 +919,7 @@ __PACKAGE__->register_method({
>  
>  	my $conf = PVE::QemuConfig->load_config($param->{vmid});
>  
> -	my $pending_delete_hash = PVE::QemuServer::split_flagged_list($conf->{pending}->{delete});
> +	my $pending_delete_hash = PVE::QemuConfig->parse_pending_delete($conf->{pending}->{delete});
>  
>  	my $res = [];
>  
> @@ -1170,7 +1170,7 @@ my $update_vm_api  = sub {
>  		    $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
>  		    PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $val))
>  			if $is_pending_val;
> -		    PVE::QemuServer::vmconfig_delete_pending_option($conf, $opt, $force);
> +		    PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force);
>  		    PVE::QemuConfig->write_config($vmid, $conf);
>  		} elsif ($opt =~ m/^serial\d+$/) {
>  		    if ($val eq 'socket') {
> @@ -1178,7 +1178,7 @@ my $update_vm_api  = sub {
>  		    } elsif ($authuser ne 'root at pam') {
>  			die "only root can delete '$opt' config for real devices\n";
>  		    }
> -		    PVE::QemuServer::vmconfig_delete_pending_option($conf, $opt, $force);
> +		    PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force);
>  		    PVE::QemuConfig->write_config($vmid, $conf);
>  		} elsif ($opt =~ m/^usb\d+$/) {
>  		    if ($val =~ m/spice/) {
> @@ -1186,10 +1186,10 @@ my $update_vm_api  = sub {
>  		    } elsif ($authuser ne 'root at pam') {
>  			die "only root can delete '$opt' config for real devices\n";
>  		    }
> -		    PVE::QemuServer::vmconfig_delete_pending_option($conf, $opt, $force);
> +		    PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force);
>  		    PVE::QemuConfig->write_config($vmid, $conf);
>  		} else {
> -		    PVE::QemuServer::vmconfig_delete_pending_option($conf, $opt, $force);
> +		    PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force);
>  		    PVE::QemuConfig->write_config($vmid, $conf);
>  		}
>  	    }
> @@ -1230,13 +1230,13 @@ my $update_vm_api  = sub {
>  		} else {
>  		    $conf->{pending}->{$opt} = $param->{$opt};
>  		}
> -		PVE::QemuServer::vmconfig_undelete_pending_option($conf, $opt);
> +		PVE::QemuConfig->remove_from_pending_delete($conf, $opt);
>  		PVE::QemuConfig->write_config($vmid, $conf);
>  	    }
>  
>  	    # remove pending changes when nothing changed
>  	    $conf = PVE::QemuConfig->load_config($vmid); # update/reload
> -	    my $changes = PVE::QemuServer::vmconfig_cleanup_pending($conf);
> +	    my $changes = PVE::QemuConfig->cleanup_pending($conf);
>  	    PVE::QemuConfig->write_config($vmid, $conf) if $changes;
>  
>  	    return if !scalar(keys %{$conf->{pending}});
> diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
> index 70ed910..6c499c9 100644
> --- a/PVE/QemuServer.pm
> +++ b/PVE/QemuServer.pm
> @@ -2345,40 +2345,6 @@ sub vm_is_volid_owner {
>      return undef;
>  }
>  
> -sub split_flagged_list {
> -    my $text = shift || '';
> -    $text =~ s/[,;]/ /g;
> -    $text =~ s/^\s+//;
> -    return { map { /^(!?)(.*)$/ && ($2, $1) } ($text =~ /\S+/g) };
> -}
> -
> -sub join_flagged_list {
> -    my ($how, $lst) = @_;
> -    join $how, map { $lst->{$_} . $_ } keys %$lst;
> -}
> -
> -sub vmconfig_delete_pending_option {
> -    my ($conf, $key, $force) = @_;
> -
> -    delete $conf->{pending}->{$key};
> -    my $pending_delete_hash = split_flagged_list($conf->{pending}->{delete});
> -    $pending_delete_hash->{$key} = $force ? '!' : '';
> -    $conf->{pending}->{delete} = join_flagged_list(',', $pending_delete_hash);
> -}
> -
> -sub vmconfig_undelete_pending_option {
> -    my ($conf, $key) = @_;
> -
> -    my $pending_delete_hash = split_flagged_list($conf->{pending}->{delete});
> -    delete $pending_delete_hash->{$key};
> -
> -    if (%$pending_delete_hash) {
> -	$conf->{pending}->{delete} = join_flagged_list(',', $pending_delete_hash);
> -    } else {
> -	delete $conf->{pending}->{delete};
> -    }
> -}
> -
>  sub vmconfig_register_unused_drive {
>      my ($storecfg, $vmid, $conf, $drive) = @_;
>  
> @@ -2393,37 +2359,6 @@ sub vmconfig_register_unused_drive {
>      }
>  }
>  
> -sub vmconfig_cleanup_pending {
> -    my ($conf) = @_;
> -
> -    # remove pending changes when nothing changed
> -    my $changes;
> -    foreach my $opt (keys %{$conf->{pending}}) {
> -	if (defined($conf->{$opt}) && ($conf->{pending}->{$opt} eq  $conf->{$opt})) {
> -	    $changes = 1;
> -	    delete $conf->{pending}->{$opt};
> -	}
> -    }
> -
> -    my $current_delete_hash = split_flagged_list($conf->{pending}->{delete});
> -    my $pending_delete_hash = {};
> -    while (my ($opt, $force) = each %$current_delete_hash) {
> -	if (defined($conf->{$opt})) {
> -	    $pending_delete_hash->{$opt} = $force;
> -	} else {
> -	    $changes = 1;
> -	}
> -    }
> -
> -    if (%$pending_delete_hash) {
> -	$conf->{pending}->{delete} = join_flagged_list(',', $pending_delete_hash);
> -    } else {
> -	delete $conf->{pending}->{delete};
> -    }
> -
> -    return $changes;
> -}
> -
>  # smbios: [manufacturer=str][,product=str][,version=str][,serial=str][,uuid=uuid][,sku=str][,family=str][,base64=bool]
>  my $smbios1_fmt = {
>      uuid => {
> @@ -4919,7 +4854,7 @@ sub vmconfig_hotplug_pending {
>  
>      my $hotplug_features = parse_hotplug_features(defined($conf->{hotplug}) ? $conf->{hotplug} : '1');
>  
> -    my $pending_delete_hash = split_flagged_list($conf->{pending}->{delete});
> +    my $pending_delete_hash = PVE::QemuConfig->parse_pending_delete($conf->{pending}->{delete});
>      while (my ($opt, $force) = each %$pending_delete_hash) {
>  	next if $selection && !$selection->{$opt};
>  	eval {
> @@ -4975,7 +4910,7 @@ sub vmconfig_hotplug_pending {
>  	} else {
>  	    # save new config if hotplug was successful
>  	    delete $conf->{$opt};
> -	    vmconfig_undelete_pending_option($conf, $opt);
> +	    PVE::QemuConfig->remove_from_pending_delete($conf, $opt);
>  	    PVE::QemuConfig->write_config($vmid, $conf);
>  	    $conf = PVE::QemuConfig->load_config($vmid); # update/reload
>  	}
> @@ -5110,25 +5045,27 @@ sub vmconfig_delete_or_detach_drive {
>      }
>  }
>  
> +
> +
>  sub vmconfig_apply_pending {
>      my ($vmid, $conf, $storecfg) = @_;
>  
>      # cold plug
>  
> -    my $pending_delete_hash = split_flagged_list($conf->{pending}->{delete});
> +    my $pending_delete_hash = PVE::QemuConfig->parse_pending_delete($conf->{pending}->{delete});
>      while (my ($opt, $force) = each %$pending_delete_hash) {
>  	die "internal error" if $opt =~ m/^unused/;
>  	$conf = PVE::QemuConfig->load_config($vmid); # update/reload
>  	if (!defined($conf->{$opt})) {
> -	    vmconfig_undelete_pending_option($conf, $opt);
> +	    PVE::QemuConfig->remove_from_pending_delete($conf, $opt);
>  	    PVE::QemuConfig->write_config($vmid, $conf);
>  	} elsif (is_valid_drivename($opt)) {
>  	    vmconfig_delete_or_detach_drive($vmid, $storecfg, $conf, $opt, $force);
> -	    vmconfig_undelete_pending_option($conf, $opt);
> +	    PVE::QemuConfig->remove_from_pending_delete($conf, $opt);
>  	    delete $conf->{$opt};
>  	    PVE::QemuConfig->write_config($vmid, $conf);
>  	} else {
> -	    vmconfig_undelete_pending_option($conf, $opt);
> +	    PVE::QemuConfig->remove_from_pending_delete($conf, $opt);
>  	    delete $conf->{$opt};
>  	    PVE::QemuConfig->write_config($vmid, $conf);
>  	}
> 





More information about the pve-devel mailing list