and move code from prestart hook inside it

Signed-off-by: Alexandre Derumier <aderum...@odiso.com>
---
 src/PVE/LXC.pm            | 30 ++++++++++++++++++++++++++++++
 src/lxc-pve-prestart-hook | 19 +++++++------------
 2 files changed, 37 insertions(+), 12 deletions(-)

diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index aaeee7f..73c9f7c 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -1876,4 +1876,34 @@ sub volid_path {
        return $path;
 
 }
+
+sub attach_loops {
+    my ($storage_cfg, $vollist) = @_;
+
+    my $loopdevs = {};
+
+    foreach my $volid (@$vollist) {
+
+       my ($storage, $volname) = PVE::Storage::parse_volume_id($volid);
+       my $scfg = PVE::Storage::storage_config($storage_cfg, $storage);
+
+       my ($vtype, undef, undef, undef, undef, $isBase, $format) =
+           PVE::Storage::parse_volname($storage_cfg, $volid);
+
+       if($format ne 'subvol' && ($scfg->{type} eq 'dir' || $scfg->{type} eq 
'nfs')) {
+           my $path = PVE::Storage::path($storage_cfg, $volid);
+           my $loopdev;
+
+           my $parser = sub {
+               my $line = shift;
+               $loopdev = $line if $line =~m|^/dev/loop\d+$|;
+               $loopdevs->{$loopdev} = $path;
+           };
+
+           PVE::Tools::run_command(['losetup', '--find', '--show', $path], 
outfunc => $parser);
+       }
+    }
+    return $loopdevs;
+}
+
 1;
diff --git a/src/lxc-pve-prestart-hook b/src/lxc-pve-prestart-hook
index 2f3088b..8ff4672 100755
--- a/src/lxc-pve-prestart-hook
+++ b/src/lxc-pve-prestart-hook
@@ -78,25 +78,20 @@ __PACKAGE__->register_method ({
 
        my $storage_cfg = PVE::Storage::config();
 
+       my $vollist = [];
+       my $loopdevlist = [];
+
        PVE::LXC::foreach_mountpoint($conf, sub {
            my ($ms, $mountpoint) = @_;
 
            my $volid = $mountpoint->{volume};
            return if !$volid || $volid =~ m|^/dev/.+|;
-
-           PVE::Storage::activate_volumes($storage_cfg, [$volid]);
-
-           my ($storage, $volname) = PVE::Storage::parse_volume_id($volid);
-           my $scfg = PVE::Storage::storage_config($storage_cfg, $storage);
-           my ($vtype, undef, undef, undef, undef, $isBase, $format) =
-               PVE::Storage::parse_volname($storage_cfg, $volid);
-
-           if($ms ne 'rootfs' && $format ne 'subvol' && ($scfg->{type} eq 
'dir' || $scfg->{type} eq 'nfs')) {
-               my $path = PVE::Storage::path($storage_cfg, $volid);
-               PVE::Tools::run_command(['losetup', '--find', '--show', $path]);
-           }
+           push @$vollist, $volid;
+           push @$loopdevlist, $volid if $ms ne 'rootfs';
        });
 
+       PVE::Storage::activate_volumes($storage_cfg, $vollist);
+       PVE::LXC::attach_loops($storage_cfg, $loopdevlist);
        return undef;
     }});
 
-- 
2.1.4

_______________________________________________
pve-devel mailing list
pve-devel@pve.proxmox.com
http://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

Reply via email to