[pve-devel] [PATCH REBASED v4 container 1/5] factor out disk allocation+formatting for reuse

Wolfgang Bumiller w.bumiller at proxmox.com
Tue Oct 17 12:58:53 CEST 2017


---
 src/PVE/LXC.pm | 87 +++++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 56 insertions(+), 31 deletions(-)

diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index ebe369d..12957d4 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -1329,6 +1329,59 @@ sub destroy_disks {
     }
 }
 
+sub alloc_disk {
+    my ($storecfg, $vmid, $storage, $size_kb, $rootuid, $rootgid) = @_;
+
+    my $needs_chown = 0;
+    my $volid;
+
+    my $scfg = PVE::Storage::storage_config($storecfg, $storage);
+    # fixme: use better naming ct-$vmid-disk-X.raw?
+
+    eval {
+	my $do_format = 0;
+	if ($scfg->{type} eq 'dir' || $scfg->{type} eq 'nfs') {
+	    if ($size_kb > 0) {
+		$volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'raw',
+						   undef, $size_kb);
+		$do_format = 1;
+	    } else {
+		$volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'subvol',
+						   undef, 0);
+		$needs_chown = 1;
+	    }
+	} elsif ($scfg->{type} eq 'zfspool') {
+
+	    $volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'subvol',
+					       undef, $size_kb);
+	    $needs_chown = 1;
+	} elsif ($scfg->{type} eq 'drbd' || $scfg->{type} eq 'lvm' || $scfg->{type} eq 'lvmthin') {
+
+	    $volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'raw', undef, $size_kb);
+	    $do_format = 1;
+
+	} elsif ($scfg->{type} eq 'rbd') {
+
+	    die "krbd option must be enabled on storage type '$scfg->{type}'\n" if !$scfg->{krbd};
+	    $volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'raw', undef, $size_kb);
+	    $do_format = 1;
+	} else {
+	    die "unable to create containers on storage type '$scfg->{type}'\n";
+	}
+	format_disk($storecfg, $volid, $rootuid, $rootgid) if $do_format;
+    };
+    if (my $err = $@) {
+	# in case formatting got interrupted:
+	if (defined($volid)) {
+	    eval { PVE::Storage::vdisk_free($storecfg, $volid); };
+	    warn $@ if $@;
+	}
+	die $err;
+    }
+
+    return ($volid, $needs_chown);
+}
+
 our $NEW_DISK_RE = qr/^([^:\s]+):(\d+(\.\d+)?)$/;
 sub create_disks {
     my ($storecfg, $vmid, $settings, $conf) = @_;
@@ -1352,37 +1405,9 @@ sub create_disks {
 
 		my $size_kb = int(${size_gb}*1024) * 1024;
 
-		my $scfg = PVE::Storage::storage_config($storecfg, $storage);
-		# fixme: use better naming ct-$vmid-disk-X.raw?
-
-		if ($scfg->{type} eq 'dir' || $scfg->{type} eq 'nfs') {
-		    if ($size_kb > 0) {
-			$volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'raw',
-							   undef, $size_kb);
-			format_disk($storecfg, $volid, $rootuid, $rootgid);
-		    } else {
-			$volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'subvol',
-							   undef, 0);
-			push @$chown_vollist, $volid;
-		    }
-		} elsif ($scfg->{type} eq 'zfspool') {
-
-		    $volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'subvol',
-					               undef, $size_kb);
-		    push @$chown_vollist, $volid;
-		} elsif ($scfg->{type} eq 'drbd' || $scfg->{type} eq 'lvm' || $scfg->{type} eq 'lvmthin') {
-
-		    $volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'raw', undef, $size_kb);
-		    format_disk($storecfg, $volid, $rootuid, $rootgid);
-
-		} elsif ($scfg->{type} eq 'rbd') {
-
-		    die "krbd option must be enabled on storage type '$scfg->{type}'\n" if !$scfg->{krbd};
-		    $volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'raw', undef, $size_kb);
-		    format_disk($storecfg, $volid, $rootuid, $rootgid);
-		} else {
-		    die "unable to create containers on storage type '$scfg->{type}'\n";
-		}
+		my $needs_chown = 0;
+		($volid, $needs_chown) = alloc_disk($storecfg, $vmid, $storage, $size_kb, $rootuid, $rootgid);
+		push @$chown_vollist, $volid if $needs_chown;
 		push @$vollist, $volid;
 		$mountpoint->{volume} = $volid;
 		$mountpoint->{size} = $size_kb * 1024;
-- 
2.11.0





More information about the pve-devel mailing list