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

Wolfgang Bumiller w.bumiller at proxmox.com
Fri Dec 2 15:44:16 CET 2016


---
 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 810fae5..c61da3e 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -1336,6 +1336,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);
+}
+
 sub create_disks {
     my ($storecfg, $vmid, $settings, $conf) = @_;
 
@@ -1358,37 +1411,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.1.4





More information about the pve-devel mailing list