[pve-devel] [PATCH container v3] Create: fix architecture detection in restore_archive

Thomas Lamprecht t.lamprecht at proxmox.com
Wed Dec 21 09:14:20 CET 2016


Something like the following method:

---
sub forked_call {
     my ($self, $sub, $noerr) = @_;

     die "internal error" if !$self->{plugin};

     # avoid recursion:
     return $sub->() if $self->{in_chroot};

     my $rootdir = $self->{rootdir};
     if (!-d "$rootdir/dev" && !mkdir("$rootdir/dev")) {
         die "failed to create temporary /dev directory: $!\n";
     }

     my $child = fork();
     die "fork failed: $!\n" if !defined($child);

     if (!$child) {
         # avoid recursive forks
         $self->{in_chroot} = 1;
         $self->{plugin}->{in_chroot} = 1;
         my $ec;
         eval {
             chroot($rootdir) or die "failed to change root to: 
$rootdir: $!\n";
             chdir('/') or die "failed to change to root directory\n";
             $ec = $sub->();
         };
         if (my $err = $@) {
             warn $err;
             $ec = -1 if !defined($ec);
         } elsif (!defined($ec)) {
             $ec = 0;
         }
         POSIX::_exit($ec);
     }
     while (waitpid($child, 0) != $child) {}
     my $exit_code = $?;
     if ($exit_code != 0 && !$noerr) {
         my $method = (caller(1))[3];
         die "error in setup task $method\n";
     }

     return $exit_code;
}


--

The diff for seeing what changed:


diff --git a/src/PVE/LXC/Setup.pm b/src/PVE/LXC/Setup.pm
index b9e112b..501d36d 100644
--- a/src/PVE/LXC/Setup.pm
+++ b/src/PVE/LXC/Setup.pm
@@ -93,8 +93,8 @@ sub new {
      return $self;
  }

-sub protected_call {
-    my ($self, $sub) = @_;
+sub forked_call {
+    my ($self, $sub, $noerr) = @_;

      die "internal error" if !$self->{plugin};

@@ -113,22 +113,28 @@ sub protected_call {
         # avoid recursive forks
         $self->{in_chroot} = 1;
         $self->{plugin}->{in_chroot} = 1;
+       my $ec;
         eval {
             chroot($rootdir) or die "failed to change root to: 
$rootdir: $!\n";
             chdir('/') or die "failed to change to root directory\n";
-           $sub->();
+           $ec = $sub->();
         };
         if (my $err = $@) {
             warn $err;
-           POSIX::_exit(1);
+           $ec = -1 if !defined($ec);
+       } elsif (!defined($ec)) {
+           $ec = 0;
         }
-       POSIX::_exit(0);
+       POSIX::_exit($ec);
      }
      while (waitpid($child, 0) != $child) {}
-    if ($? != 0) {
+    my $exit_code = $?;
+    if ($exit_code != 0 && !$noerr) {
         my $method = (caller(1))[3];
         die "error in setup task $method\n";
      }
+
+    return $exit_code;
  }

  sub template_fixup {






More information about the pve-devel mailing list