[pve-devel] [PATCH pve-container 5/9] add function copy disk

Wolfgang Link w.link at proxmox.com
Wed Nov 18 09:30:02 CET 2015


this is a hady function to copy mountpoints and will be used be move_moutpoint or clone_ct
---
 src/PVE/LXC.pm | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 101 insertions(+), 1 deletion(-)

diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index d8bb81f..172d8ec 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -2080,7 +2080,7 @@ sub umount_all {
 	my $mount_path = "$rootdir/$mount";
 	$mount_path =~ s!/+!/!g;
 
-	$umount_mp($mount_path, $noerr);
+	umount_mp($mount_path, $noerr);
 
     });
 }
@@ -2156,6 +2156,105 @@ sub rsync_disk {
     print "sync finished ($delay seconds)\n";
 }
 
+sub copy_lxc_disk {
+    my ($src_volid, $dst_volid, $storecfg);
+
+    my (undef, undef, $src_vmid, undef, undef, undef, $src_format) =
+	PVE::Storage::parse_volname($storecfg, $src_volid);
+
+    my (undef, undef, $dst_vmid, undef, undef, undef, $dst_format) =
+	PVE::Storage::parse_volname($storecfg, $dst_volid);
+
+    my $running = check_running($src_vmid);
+
+    my $snapable = volume_has_feature($storecfg, 'snapshot', $src_volid, undef, 1);
+
+    die "Can't copy Disk: $src_volid to $dst_volid, if it is running" if
+	($running && $src_vmid == $dst_vmid && $snapable);
+
+    my $src_mountpoint;
+    my $dst_mountpoint;
+
+    my $src_loopdev;
+    my $dst_loopdev;
+    my $mountpoint = "/tmp/CT";
+    my $src_dev = undef;
+    my $dst_dev = undef;
+    my $snap = "clone";
+
+    my $cleanup = sub {
+
+	umount_raw_file($src_mountpoint, $src_dev ,1) if $src_dev;
+	umount_raw_file($dst_mountpoint, $dst_dev ,1) if $dst_dev;
+
+	eval { File::Path::remove_tree($src_mountpoint) if $src_format eq "raw"; };
+	warn $@ if $@;
+
+	eval { File::Path::remove_tree($dst_mountpoint) if $dst_format eq "raw"; };
+	warn $@ if $@;
+
+	if ($running) {
+	    eval {
+		PVE::Storage::volume_snapshot_delete($storecfg, $src_volid, $snap);
+	    };
+	    warn $@ if $@;
+	}
+    };
+
+    eval {
+
+	if ($running) {
+
+	    PVE::Storage::volume_snapshot($storecfg, $src_volid, $snap);
+
+	    $src_mountpoint = $mountpoint."SRC${src_vmid}";
+	    File::Path::make_path($src_mountpoint);
+	    $src_dev = mount_raw_file($storecfg, $src_mountpoint, $snap, $src_volid);
+
+
+	} else {
+
+	    if ($src_format eq "raw") {
+		$src_mountpoint = $mountpoint."SRC${src_vmid}";
+		File::Path::make_path($src_mountpoint);
+		$src_dev = mount_raw_file($storecfg, $src_mountpoint, undef, $src_volid);
+
+	    } else {
+
+		$src_mountpoint = PVE::Storage::path($storecfg, $src_volid);
+	    }
+	}
+
+	if ($dst_format eq "raw") {
+
+
+	    $dst_mountpoint = $mountpoint."DST${dst_vmid}";
+	    File::Path::make_path($dst_mountpoint);
+	    $dst_dev = mount_raw_file($storecfg, $dst_mountpoint, undef, $dst_volid);
+
+	} elsif ($dst_format eq "subvol") {
+
+	    $dst_mountpoint = PVE::Storage::path($storecfg, $dst_volid);
+
+	} else {
+
+	    die "Can't copy VM: $src_vmid from $src_volid to $dst_volid\n";
+
+	}
+
+    };
+
+    my $err = @$;
+    if ($err) {
+	&$cleanup();
+	die "can't copy $src_volid to $dst_volid: $err\n";
+    }
+
+    rsync_disk($src_mountpoint, $dst_mountpoint);
+
+    &$cleanup();
+}
+
 # use $rootdir = undef to just return the corresponding mount path
 sub mountpoint_mount {
     my ($mountpoint, $rootdir, $storage_cfg, $snapname) = @_;
@@ -2279,6 +2378,7 @@ sub mount_raw_file {
 	    umount_raw_file($mountpoint, $dev);
 	    die "$err\n";
 	}
+	return $dev;
 
     } elsif ($format eq 'raw' && !$snapname) {
 
-- 
2.1.4





More information about the pve-devel mailing list