[pve-devel] [PATCH 11/11] add qemu_img_convert

Alexandre Derumier aderumier at odiso.com
Tue Dec 18 10:15:00 CET 2012


also work with snapshot as source for qcow2,rbd,sheepdog.

Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 PVE/QemuServer.pm |   64 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 3a17e1b..3ffd45a 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -4459,4 +4459,68 @@ sub is_template {
     return undef;
 }
 
+sub qemu_img_convert {
+    my ($src_volid, $dst_volid, $snapname) = @_;
+
+    my $storecfg = PVE::Storage::config();
+    my ($src_storeid, $src_volname) = PVE::Storage::parse_volume_id($src_volid, 1);
+    my ($dst_storeid, $dst_volname) = PVE::Storage::parse_volume_id($dst_volid, 1);
+
+    if ($src_storeid && $dst_storeid) {
+        my $src_scfg = PVE::Storage::storage_config($storecfg, $src_storeid);
+        my $dst_scfg = PVE::Storage::storage_config($storecfg, $dst_storeid);
+
+        my $src_format = qemu_img_format($src_scfg, $src_volname);
+        my $dst_format = qemu_img_format($dst_scfg, $dst_volname);
+
+        my $src_path = PVE::Storage::path($storecfg, $src_volid);
+        my $dst_path = PVE::Storage::path($storecfg, $dst_volid);
+
+        my $cmd = [];
+        push @$cmd, '/usr/bin/qemu-img', 'convert', '-p', '-C';
+
+	if($snapname && $src_format eq "qcow2"){
+	    push @$cmd, '-s', $snapname if $snapname;
+	}elsif ($snapname && $src_format eq "rbd"){
+	    $dst_format .= '@'.$snapname;
+        }elsif ($snapname && $src_format eq "sheepdog"){
+	    $dst_format .= ':'.$snapname;
+	}elsif ($snapname){
+            die "copy from snapshot is not implemented for format $src_format";
+	}
+
+        push @$cmd, '-f', $src_format, '-O', $dst_format, $src_path, $dst_path;
+
+        #fixme, display progress percent while cmd is running 
+        my $parser = sub {
+            my $line = shift;
+            print $line."\n";
+        };
+
+        eval  { run_command($cmd, timeout => undef, errfunc => sub {}, outfunc => $parser); };
+        my $err = $@;
+        die "copy failed: $err" if $err;
+
+    }
+
+}
+
+sub qemu_img_format {
+    my ($scfg, $volname) = @_;
+
+    if ($scfg->{path} && $volname =~ m/\.(raw|qcow2|qed|vmdk)$/){
+        return $1;
+    }
+    elsif ($scfg->{type} eq 'nexenta' || $scfg->{type} eq 'iscsidirect'){
+        return "iscsi";
+    }
+    elsif ($scfg->{type} eq 'lvm' || $scfg->{type} eq 'iscsi'){
+        return "host_device";
+    }
+    #sheepdog,rbd,or other qemu block driver
+    else{
+        return $scfg->{type};
+    }
+}
+
 1;
-- 
1.7.10.4




More information about the pve-devel mailing list