[pve-devel] [PATCH] Fixed hardcoded LXC container architecture detection

Thomas Lamprecht t.lamprecht at proxmox.com
Tue Jul 21 10:06:53 CEST 2015


Execute '/usr/bin/file -b -L /usr/bin/file' in the container to extract
architecture information. Falls back to amd64 on failure and notifies
the user. Assumes that the file command is available and we're on a x86
(32 or 64bit) linux system.

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 src/PVE/LXCCreate.pm | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/src/PVE/LXCCreate.pm b/src/PVE/LXCCreate.pm
index fdcc6d6..91851ee 100644
--- a/src/PVE/LXCCreate.pm
+++ b/src/PVE/LXCCreate.pm
@@ -57,8 +57,21 @@ sub restore_archive {
     #$cmd = [@$userns_cmd, 'mkdir', '-p', "$rootdir/dev/pts"];
     #PVE::Tools::run_command($cmd);
 
-    # template/OS specific configuration
-    $conf->{'lxc.arch'} = 'i386'; #fixme: || x86_64
+    #determine file type of /usr/bin/file itself to get guests' architecture
+    $cmd = [@$userns_cmd, '/usr/bin/file', '-b', '-L', "$rootdir/usr/bin/file"];
+    PVE::Tools::run_command($cmd, outfunc => sub {
+	shift =~ /^ELF (\d{2}-bit)/; # safely assumes x86 linux
+	my $arch_str = $1;
+	$conf->{'lxc.arch'} = 'amd64'; # defaults to 64bit
+	if(defined($arch_str)) {
+	    $conf->{'lxc.arch'} = 'i386' if $arch_str =~ /32/;
+	    print "Detected container architecture: $conf->{'lxc.arch'}\n";
+	} else {
+	    print "CT architecture detection failed, falling back to amd64.\n".
+	          "Edit the config in /etc/pve/nodes/{node}/lxc/{vmid}/config".
+	          " to set another arch.\n";
+	}
+    });
 }
 
 sub restore_and_configure {
-- 
2.1.4




More information about the pve-devel mailing list