[pve-devel] [PATCH v2 guest-common 08/28] Add update_volume_ids

Fabian Grünbichler f.gruenbichler at proxmox.com
Tue Feb 25 12:32:39 CET 2020


On February 24, 2020 1:43 pm, Fabian Ebner wrote:
> This function is intened to be used after doing a migration where some
> of the volume IDs changed.
> 
> Signed-off-by: Fabian Ebner <f.ebner at proxmox.com>
> ---
>  PVE/AbstractConfig.pm | 29 +++++++++++++++++++++++++++++
>  1 file changed, 29 insertions(+)
> 
> diff --git a/PVE/AbstractConfig.pm b/PVE/AbstractConfig.pm
> index 9ce3d12..aa10101 100644
> --- a/PVE/AbstractConfig.pm
> +++ b/PVE/AbstractConfig.pm
> @@ -366,6 +366,35 @@ sub add_unused_volume {
>      return $key;
>  }
>  
> +# $volume_map is a hash of 'old_volid' => 'new_volid' pairs.
> +# This method replaces 'old_volid' by 'new_volid' throughout
> +# the config including snapshots and unused and vmstate volumes
> +sub update_volume_ids {
> +    my ($class, $conf, $volume_map) = @_;
> +
> +    my $opts = {
> +	'include_unused' => 1,
> +	'include_vmstate' => 1,
> +    };
> +
> +    my $do_replace = sub {
> +	my ($key, $volume, $conf) = @_;
> +
> +	my $old_volid = $volume->{$class->volid_key()};

$class->volid_key() can be retrieved once at the beginning or even 
outside of this helper sub, makes the code shorter and easier to parse 
;)

> +	if (my $new_volid = $volume_map->{$old_volid}) {
> +	    $volume->{$class->volid_key()} = $new_volid;
> +	    $conf->{$key} = $class->print_volume($key, $volume);
> +	}
> +    };
> +
> +    $class->snapshot_foreach_volume($conf, $opts, $do_replace, $conf);
> +
> +    foreach my $snap (keys %{$conf->{snapshots}}) {
> +	my $snap_conf = $conf->{snapshots}->{$snap};
> +	$class->snapshot_foreach_volume($snap_conf, $opts, $do_replace, $snap_conf);
> +    }

this recursive iteration could also be defined as option in 
foreach_volume (see my comment on the big Drive.pm patch as well - there 
is a QemuServer sub that iterates over all volids there!)

> +}
> +
>  # Returns whether the template parameter is set in $conf.
>  sub is_template {
>      my ($class, $conf) = @_;
> -- 
> 2.20.1
> 
> 
> _______________________________________________
> pve-devel mailing list
> pve-devel at pve.proxmox.com
> https://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel
> 
> 



More information about the pve-devel mailing list