]> git.proxmox.com Git - pve-container.git/commitdiff
disk formatting and mounting changed
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Thu, 3 Sep 2015 13:45:51 +0000 (15:45 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Sat, 5 Sep 2015 08:30:43 +0000 (10:30 +0200)
-) 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
src/PVE/LXC.pm
src/PVE/LXC/Create.pm

index 13111826aecbff53db15de879ae05c71d322ffec..42febae780108265872d05bee8fa6ee0423cafdf 100644 (file)
@@ -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";
                }
index 7ee1e9d1d3a12cc9bbddefc4cc2bd384da4b2d37..c54b491f3861411080d3f61a4448bab8a3258086 100644 (file)
@@ -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;
 }
 
index d762e69b0169d6f90144b470cea1590d5e148ba3..a7ad52e139bc1a75bbb112a386bd5ea1a38183a5 100644 (file)
@@ -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));