diff --git a/mmdebstrap b/mmdebstrap index 4577056..d8ea6a5 100755 --- a/mmdebstrap +++ b/mmdebstrap @@ -954,37 +954,55 @@ sub main() { $options->{nativearch} = $nativearch; $options->{foreignarchs} = \@foreignarchs; - my $deb2qemu = { - alpha => 'alpha', - amd64 => 'x86_64', - arm => 'arm', - arm64 => 'aarch64', - armel => 'arm', - armhf => 'arm', - hppa => 'hppa', - i386 => 'i386', - m68k => 'm68k', - mips => 'mips', - mips64 => 'mips64', - mips64el => 'mips64el', - mipsel => 'mipsel', - powerpc => 'ppc', - ppc64 => 'ppc64', - ppc64el => 'ppc64le', - riscv64 => 'riscv64', - s390x => 's390x', - sh4 => 'sh4', - sparc => 'sparc', - sparc64 => 'sparc64', - }; - open my $fh, '-|', 'arch-test', '-n', $nativearch // die "failed to fork(): $!"; - chomp (my $content = do { local $/; <$fh> }); - close $fh; - if ($? != 0 or $content ne "$nativearch: ok") { - if (!exists $deb2qemu->{$nativearch}) { - die "no mapping from $nativearch to qemu-user binary"; + { + my $deb2qemu = { + alpha => 'alpha', + amd64 => 'x86_64', + arm => 'arm', + arm64 => 'aarch64', + armel => 'arm', + armhf => 'arm', + hppa => 'hppa', + i386 => 'i386', + m68k => 'm68k', + mips => 'mips', + mips64 => 'mips64', + mips64el => 'mips64el', + mipsel => 'mipsel', + powerpc => 'ppc', + ppc64 => 'ppc64', + ppc64el => 'ppc64le', + riscv64 => 'riscv64', + s390x => 's390x', + sh4 => 'sh4', + sparc => 'sparc', + sparc64 => 'sparc64', + }; + chomp (my $hostarch = `dpkg --print-architecture`); + if ($hostarch ne $nativearch) { + my $pid = open my $fh, '-|' // die "failed to fork(): $!"; + if ($pid == 0) { + { exec 'arch-test', '-n', $nativearch; } + # if exec didn't work (for example because the arch-test program is + # missing) prepare for the worst and assume that the architecture + # cannot be executed + print "$nativearch: not supported on this machine/kernel\n"; + exit 1; + } + chomp (my $content = do { local $/; <$fh> }); + close $fh; + if ($? != 0 or $content ne "$nativearch: ok") { + print STDERR "I: $nativearch cannot be executed, falling back to qemu-user\n"; + if (!exists $deb2qemu->{$nativearch}) { + die "no mapping from $nativearch to qemu-user binary"; + } + $options->{qemu} = $deb2qemu->{$nativearch}; + } else { + print STDERR "I: $nativearch can be executed on this $hostarch machine\n"; + } + } else { + print STDERR "I: chroot architecture $nativearch is equal to the host's architecture\n"; } - $options->{qemu} = $deb2qemu->{$nativearch}; } if (scalar @ARGV > 0) {