[pve-devel] [PATCH qemu-server 3/4] add function importing disk image to existing vm

Wolfgang Bumiller w.bumiller at proxmox.com
Wed Feb 17 15:25:40 CET 2016


Forgot something in my previous answer (see inline):

On Mon, Feb 15, 2016 at 02:33:48PM +0100, Timo Grodzinski wrote:
> Signed-off-by: Timo Grodzinski <t.grodzinski at profihost.ag>
> ---
>  PVE/QemuServer.pm | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 60 insertions(+), 1 deletion(-)
> 
> diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
> index 6fafa59..6c777d5 100644
> --- a/PVE/QemuServer.pm
> +++ b/PVE/QemuServer.pm
> @@ -6368,7 +6368,7 @@ sub is_template {
>  }
>  
>  sub qemu_img_convert {
> -    my ($src_volid, $dst_volid, $size, $snapname, $dont_create_target) = @_;
> +    my ($src_volid, $dst_volid, $size, $snapname, %params) = @_;
>  
>      my $storecfg = PVE::Storage::config();
>      my ($src_storeid, $src_volname) = PVE::Storage::parse_volume_id($src_volid, 1);
> @@ -6547,6 +6547,65 @@ sub clone_disk {
>      return $disk;
>  }
>  
> +sub import_disk {
> +    my ( $storecfg, $vmid, $img_volid, $drive ) = @_;
> +
> +    print "\nimport disk $img_volid\n";
> +
> +    my $old_volid = $drive->{file};
> +
> +    my $size_existing = $drive->{size};
> +    my $size_new = get_image_size( $storecfg, $img_volid );
> +
> +    die "cannot determine of existing drive\n"                    if !$size_existing;
> +    die "cannot determine virtual image size of uploaded image\n" if !$size_new;
> +
> +    print "\n";
> +    print "size of uploaded image: $size_new bytes\n";
> +    print "size of drive: $size_existing bytes\n";
> +
> +    die "virtual image size ($size_new bytes) is bigger than existing drive ($size_existing bytes)\n"
> +      if $size_new > $size_existing;
> +
> +    my ( $size ) = PVE::Storage::volume_size_info( $storecfg, $img_volid, 3 );
> +
> +    PVE::Storage::activate_volumes( $storecfg, [$old_volid] );
> +
> +    my ( $dst_storeid, $dst_volname ) = PVE::Storage::parse_volume_id( $old_volid );
> +    my $dst_scfg = PVE::Storage::storage_config( $storecfg, $dst_storeid );
> +    my $dst_format = qemu_img_format( $dst_scfg, $dst_volname );
> +
> +    print "\nremove existing disk '$old_volid'...\n";
> +    PVE::Storage::vdisk_free( $storecfg, $old_volid );

While it might fail when there isn't much space left I'd suggest
allocating and converting first, and deleting the old volume last.
Otherwise if anything past this point fails both the old and new disks
are gone and you might not have permission to allocate a "new" one.
Since this function is applied to a VM (has a $vmid parameter) it would
probably even make sense to update the config before doing vdisk_free()
to make sure you're in a functioning state before deleting the old
data.)

With this in mind the API call should then also check for the
Datastore.AllocateSpace permission.

> +    print "\n";
> +    print "create new disk on storage '$dst_storeid'...\n";
> +    my $new_volid = PVE::Storage::vdisk_alloc( $storecfg, $dst_storeid, $vmid, $dst_format, undef, ( $size / 1024 ) );
> +    print "new disk '$new_volid' created\n";
> +
> +    print "\nconverting image...";
> +    eval {
> +        qemu_img_convert(
> +            $img_volid, $old_volid, $size, undef,
> +            dont_create_target => 1,
> +            auto_src_format    => 1,
> +            verbose            => 1
> +        );
> +    };
> +    die "converting image failed: '$@'\n" if $@;
> +
> +    ( $size ) = PVE::Storage::volume_size_info( $storecfg, $old_volid, 3 );
> +
> +    if ( $size_new != $size_existing ) {
> +        print "\nstarting resize of image...\n";
> +        PVE::Storage::volume_resize( $storecfg, $old_volid, $size_existing, check_running( $vmid ) );
> +    }
> +
> +    print "\nimport of disk $img_volid complete\n";
> +
> +    return $drive;
> +}
> +
>  sub get_image_size {
>      my ( $storecfg, $volid ) = @_;
>  
> -- 
> 2.1.4
> 
> _______________________________________________
> pve-devel mailing list
> pve-devel at pve.proxmox.com
> http://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel
> 




More information about the pve-devel mailing list