[pve-devel] [PATCH v2 pve-container 1/4] disk formatting and mounting changed

Wolfgang Bumiller w.bumiller at proxmox.com
Thu Sep 3 15:45:51 CEST 2015


-) format disks after vdisk_alloc instead of in mount_all
-) make mount_all return the loop device list so it can be
   passed to umount_all optionally
-) umount_all takes an optional loopdevs list to avoid
   listing loopdevs when not necessary
---
 src/PVE/API2/LXC.pm   | 30 ++++++++++++++++++++++++++++++
 src/PVE/LXC.pm        | 15 ++++++---------
 src/PVE/LXC/Create.pm | 11 ++++++++---
 3 files changed, 44 insertions(+), 12 deletions(-)

diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm
index d5acace..f9ef668 100644
--- a/src/PVE/API2/LXC.pm
+++ b/src/PVE/API2/LXC.pm
@@ -53,6 +53,33 @@ my $destroy_disks = sub {
 	warn $@ if $@;
     }
 };
+
+sub mkfs {
+    my ($dev) = @_;
+    my $cmd = ['mkfs.ext4', '-O', 'mmp', $dev];
+    PVE::Tools::run_command($cmd);
+}
+
+sub format_disk {
+    my ($storage_cfg, $volid) = @_;
+
+    if ($volid =~ m@^/dev/.+@) {
+	return mkfs($volid);
+    }
+
+    my ($storage, $volname) = PVE::Storage::parse_volume_id($volid, 1);
+
+    die "cannot format volume $volid with no storage" if !$storage;
+
+    my $path = PVE::Storage::path($storage_cfg, $volid, undef);
+
+    my ($vtype, undef, undef, undef, undef, $isBase, $format) =
+	PVE::Storage::parse_volname($storage_cfg, $volid);
+
+    if ($format eq 'raw' || $format eq 'subvol') {
+	return mkfs($path);
+    }
+}
  
 my $create_disks = sub {
     my ($storecfg, $vmid, $settings, $conf) = @_;
@@ -82,6 +109,7 @@ my $create_disks = sub {
 		    if ($size > 0) {
 			$volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'raw',
 							   undef, $size);
+			format_disk($storecfg, $volid);
 		    } else {
 			$volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'subvol',
 							   undef, 0);
@@ -93,11 +121,13 @@ my $create_disks = sub {
 		} elsif ($scfg->{type} eq 'drbd') {
 
 		    $volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'raw', undef, $size);
+		    format_disk($storecfg, $volid);
 
 		} 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);
+		    format_disk($storecfg, $volid);
 		} else {
 		    die "unable to create containers on storage type '$scfg->{type}'\n";
 		}
diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index e296047..c1f5cc1 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -1914,9 +1914,9 @@ sub detach_loops {
 }
 
 sub umount_all {
-    my ($vmid, $storage_cfg, $conf, $noerr) = @_;
+    my ($vmid, $storage_cfg, $conf, $noerr, $loopdevs) = @_;
 
-    my $loopdevs = loopdevices_list();
+    $loopdevs ||= loopdevices_list();
 
     my $rootdir = "/var/lib/lxc/$vmid/rootfs";
     my $volid_list = get_vm_volumes($conf);
@@ -1949,15 +1949,16 @@ sub umount_all {
 }
 
 sub mount_all {
-    my ($vmid, $storage_cfg, $conf, $format_raw_images) = @_;
+    my ($vmid, $storage_cfg, $conf) = @_;
 
     my $rootdir = "/var/lib/lxc/$vmid/rootfs";
 
     my $volid_list = get_vm_volumes($conf);
     PVE::Storage::activate_volumes($storage_cfg, $volid_list);
 
+    my $loopdevs;
     eval {
-	my $loopdevs = attach_loops($storage_cfg, $volid_list);
+	$loopdevs = attach_loops($storage_cfg, $volid_list);
 
 	foreach_mountpoint($conf, sub {
 	    my ($ms, $mountpoint) = @_;
@@ -1973,11 +1974,6 @@ sub mount_all {
 
 	    die "unable to mount base volume - internal error" if $isBase;
 
-	    if ($format_raw_images && $format eq 'raw') {
-		my $cmd = ['mkfs.ext4', '-O', 'mmp', $image_path];
-		PVE::Tools::run_command($cmd);
-	    }
-
 	    mountpoint_mount($mountpoint, $rootdir, $storage_cfg, $loopdevs);
         });
     };
@@ -1986,6 +1982,7 @@ sub mount_all {
 	umount_all($vmid, $storage_cfg, $conf, 1);
     }
 
+    return ($rootdir, $loopdevs) if wantarray;
     return $rootdir;
 }
 
diff --git a/src/PVE/LXC/Create.pm b/src/PVE/LXC/Create.pm
index d762e69..a7ad52e 100644
--- a/src/PVE/LXC/Create.pm
+++ b/src/PVE/LXC/Create.pm
@@ -205,15 +205,20 @@ sub create_rootfs {
 	PVE::LXC::create_config($vmid, $conf);
     }
 
+    my $loopdevs;
     eval {
-	my $rootdir = PVE::LXC::mount_all($vmid, $storage_cfg, $conf, 1);
+	(my $rootdir, $loopdevs) = PVE::LXC::mount_all($vmid, $storage_cfg, $conf);
         restore_and_configure($vmid, $archive, $rootdir, $conf, $password, $restore);
     };
     if (my $err = $@) {
 	warn $err;
-	PVE::LXC::umount_all($vmid, $storage_cfg, $conf, 1);
+	if ($loopdevs) {
+	    # mount_all unmounts on error so we only need to unmount
+	    # if it actually returns valid $loopdevs
+	    PVE::LXC::umount_all($vmid, $storage_cfg, $conf, 1, $loopdevs);
+	}
     } else {
-	PVE::LXC::umount_all($vmid, $storage_cfg, $conf, 0);
+	PVE::LXC::umount_all($vmid, $storage_cfg, $conf, 0, $loopdevs);
     }
 
     PVE::Storage::deactivate_volumes($storage_cfg, PVE::LXC::get_vm_volumes($conf));
-- 
2.1.4





More information about the pve-devel mailing list