[pve-devel] [PATCH pve-container 2/2] Start a worker in lxc resize.

Wolfgang Link w.link at proxmox.com
Wed Oct 28 09:40:41 CET 2015


It is necessary because if we resize a disk it can take longer. so to prevent long waiting time fork a worker process.
---
 src/PVE/API2/LXC.pm | 72 ++++++++++++++++++++++++++++-------------------------
 1 file changed, 38 insertions(+), 34 deletions(-)

diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm
index 972f628..ffa3f4c 100644
--- a/src/PVE/API2/LXC.pm
+++ b/src/PVE/API2/LXC.pm
@@ -941,7 +941,10 @@ __PACKAGE__->register_method({
 		}
 	    }),
     },
-    returns => { type => 'null'},
+    returns => {
+	type => 'string',
+	description => "the task ID.",
+    },
     code => sub {
 	my ($param) = @_;
 
@@ -1015,42 +1018,43 @@ __PACKAGE__->register_method({
 	    return if $size == $newsize;
 
 	    PVE::Cluster::log_msg('info', $authuser, "update CT $vmid: resize --disk $disk --size $sizestr");
-
-	    # FIXME: volume_resize doesn't do anything if $running=1?
-	    PVE::Storage::volume_resize($storage_cfg, $volid, $newsize, 0);
-
-	    $mp->{size} = $newsize;
-	    $conf->{$disk} = PVE::LXC::print_ct_mountpoint($mp, $disk eq 'rootfs');
-
-	    PVE::LXC::write_config($vmid, $conf);
-
-	    if ($format eq 'raw') {
-		my $path = PVE::Storage::path($storage_cfg, $volid, undef);
-		if ($running) {
-		    $path = &$query_loopdev($path);
-		    die "internal error: CT running but mountpoint not attached to a loop device"
-			if !$path; # fixme: zvols and other storages?
-		    PVE::Tools::run_command(['losetup', '--set-capacity', $path]);
-
-		    # In order for resize2fs to know that we need online-resizing a mountpoint needs
-		    # to be visible to it in its namespace.
-		    # To not interfere with the rest of the system we unshare the current mount namespace,
-		    # mount over /tmp and then run resize2fs.
-
-		    # interestingly we don't need to e2fsck on mounted systems...
-		    my $quoted = PVE::Tools::shellquote($path);
-		    my $cmd = "mount --make-rprivate / && mount $quoted /tmp && resize2fs $quoted";
-		    PVE::Tools::run_command(['unshare', '-m', '--', 'sh', '-c', $cmd]);
-		} else {
-		    PVE::Tools::run_command(['e2fsck', '-f', '-y', $path]);
-		    PVE::Tools::run_command(['resize2fs', $path]);
+	    my $realcmd = sub {
+		# FIXME: volume_resize doesn't do anything if $running=1?
+		PVE::Storage::volume_resize($storage_cfg, $volid, $newsize, 0);
+
+		$mp->{size} = $newsize;
+		$conf->{$disk} = PVE::LXC::print_ct_mountpoint($mp, $disk eq 'rootfs');
+
+		PVE::LXC::write_config($vmid, $conf);
+
+		if ($format eq 'raw') {
+		    my $path = PVE::Storage::path($storage_cfg, $volid, undef);
+		    if ($running) {
+			$path = &$query_loopdev($path);
+			die "internal error: CT running but mountpoint not attached to a loop device"
+			    if !$path; # fixme: zvols and other storages?
+			PVE::Tools::run_command(['losetup', '--set-capacity', $path]);
+
+			# In order for resize2fs to know that we need online-resizing a mountpoint needs
+			# to be visible to it in its namespace.
+			# To not interfere with the rest of the system we unshare the current mount namespace,
+			# mount over /tmp and then run resize2fs.
+
+			# interestingly we don't need to e2fsck on mounted systems...
+			my $quoted = PVE::Tools::shellquote($path);
+			my $cmd = "mount --make-rprivate / && mount $quoted /tmp && resize2fs $quoted";
+			PVE::Tools::run_command(['unshare', '-m', '--', 'sh', '-c', $cmd]);
+		    } else {
+			PVE::Tools::run_command(['e2fsck', '-f', '-y', $path]);
+			PVE::Tools::run_command(['resize2fs', $path]);
+		    }
 		}
-	    }
-	};
+	    };
 
-	PVE::LXC::lock_container($vmid, undef, $code);
+	    return $rpcenv->fork_worker('resize', $vmid, $authuser, $realcmd);
+	};
 
-	return undef;
+	return PVE::LXC::lock_container($vmid, undef, $code);;
     }});
 
 1;
-- 
2.1.4





More information about the pve-devel mailing list