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

Timo Grodzinski t.grodzinski at profihost.ag
Mon Feb 15 14:33:48 CET 2016


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 );
+
+    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




More information about the pve-devel mailing list