[pve-devel] [PATCH 2/4] add copy_disks sub

Alexandre Derumier aderumier at odiso.com
Fri Oct 26 14:32:02 CEST 2012


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

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index 6ff3b79..f1e8a16 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -58,6 +58,68 @@ my $check_storage_access = sub {
     });
 };
 
+my $copy_disks = sub {
+    my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $settings) = @_;
+
+    my $vollist = [];
+
+    my $res = {};
+    PVE::QemuServer::foreach_drive($conf, sub {
+	my ($ds, $disk) = @_;
+	my $volid = $disk->{file};
+
+	next if (!$volid || $volid eq 'none' || $volid eq 'cdrom');
+
+	my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
+	die "no storage ID specified (and no default storage)\n" if !$storeid;
+
+	my $fmt = undef;
+	if ($volname =~ m/\.(raw|qcow2|qed|vmdk)$/){
+            $fmt = $1;
+	}
+	if($settings->{$ds} && $settings->{$ds} =~ m/^(\S+):(raw|qcow2|qed|vmdk)?$/){
+	    ($storeid, $fmt) = ($1, $2);
+	}
+
+	PVE::Storage::activate_volumes($storecfg, [ $volid ]);
+
+	my ($size) = PVE::Storage::volume_size_info($storecfg, $volid, 1);
+
+	#alloc image if needed (lvm,iscsi devices)
+	my $dstvolid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid,
+                                                  $fmt, undef, ($size/1024), 1);
+	push @$vollist, $dstvolid;
+
+	#copy from source
+	PVE::QemuServer::qemu_img_convert($volid, $dstvolid);
+	#verify size
+	my ($dstsize) = PVE::Storage::volume_size_info($storecfg, $dstvolid, 1);
+
+	$disk->{file} = $dstvolid;
+	$disk->{size} = $dstsize;
+	delete $disk->{format}; # no longer needed
+	$res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk);
+    });
+
+    # free allocated images on error
+    if (my $err = $@) {
+	syslog('err', "VM $vmid creating disks failed");
+	foreach my $volid (@$vollist) {
+	    eval { PVE::Storage::vdisk_free($storecfg, $volid); };
+	    warn $@ if $@;
+	}
+	die $err;
+    }
+
+    # modify vm config if everything went well
+    foreach my $ds (keys %$res) {
+	$conf->{$ds} = $res->{$ds};
+    }
+
+    return $vollist;
+
+};
+
 # Note: $pool is only needed when creating a VM, because pool permissions
 # are automatically inherited if VM already exists inside a pool.
 my $create_disks = sub {
-- 
1.7.10.4




More information about the pve-devel mailing list