如何在docker容器中伪造cpu架构?

时间:2014-10-21 15:52:19

标签: centos docker centos5

当我创建32位CentOS 5 docker镜像时,我希望将cpu架构报告为i386 / i686(在此容器中测试的安装程序检查架构并安装64位二进制文​​件而不是32位)。 我设置了yum变量并创建了uname包装器,因此yum和bash脚本中的检查工作正常:

bash-3.2# uname -a
Linux c538cf9bf508 3.13.0-24-generic #47-Ubuntu SMP Fri May 2 23:30:00 UTC 2014 i686 i686 i386 GNU/Linux
bash-3.2# uname -p -m -i
i686 i686 i386
bash-3.2# cat /etc/yum/vars/arch && cat /etc/yum/vars/basearch
i686
i386

但是python仍然报告64位

bash-3.2# python
Python 2.4.3 (#1, Jan  9 2013, 06:49:54) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, platform
>>> platform.machine()
'x86_64'
>>> os.uname()
('Linux', 'c538cf9bf508', '3.13.0-24-generic', '#47-Ubuntu SMP Fri May 2 23:30:00 UTC 2014', 'x86_64')

有没有办法在任何地方伪造cpu架构?

3 个答案:

答案 0 :(得分:5)

我希望有一种更优雅的方式来做到这一点,但这就是我所做的:前言任何你想用linux32运行的命令。例如:

$ docker run -t -i toopher/centos-i386:centos6 /bin/bash
[root@b027ad7830ac /]# uname -a
Linux b027ad7830ac 3.16.4-tinycore64 #1 SMP Thu Oct 23 16:14:24 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@b027ad7830ac /]# linux32 uname -a
Linux b027ad7830ac 3.16.4-tinycore64 #1 SMP Thu Oct 23 16:14:24 UTC 2014 i686 i686 i386 GNU/Linux
[root@b027ad7830ac /]# linux32 python
Python 2.6.6 (r266:84292, Jan 22 2014, 09:37:14) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, platform
>>> platform.machine()
'i686'
>>> os.uname()
('Linux', 'b027ad7830ac', '3.16.4-tinycore64', '#1 SMP Thu Oct 23 16:14:24 UTC 2014', 'i686')

或者,您可以在调用linux32

时使用docker run
$ docker run -t -i toopher/centos-i386:centos6 /usr/bin/linux32 /bin/bash
[root@0f289d955fe1 /]# uname -a
Linux 0f289d955fe1 3.16.4-tinycore64 #1 SMP Thu Oct 23 16:14:24 UTC 2014 i686 i686 i386 GNU/Linux
[root@0f289d955fe1 /]# python
Python 2.6.6 (r266:84292, Jan 22 2014, 09:37:14) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, platform
>>> platform.machine()
'i686'
>>> os.uname()
('Linux', '0f289d955fe1', '3.16.4-tinycore64', '#1 SMP Thu Oct 23 16:14:24 UTC 2014', 'i686')

或者甚至更好,使用docker图像(或构建自己的图像)将linux32配置为ENTRYPOINT,例如:

FROM toopher/centos-i386:centos6
ENTRYPOINT ["linux32"]

答案 1 :(得分:3)

Evan的回答确实有效,但您仍然需要为RUNDockerfile的几乎所有linux32行添加前缀Dockerfile。为避免这种情况,我在RUN rm /bin/sh && \ echo -e '#!/bin/bash\n/usr/bin/linux32 -- /bin/bash "$@"' > /bin/sh && \ chmod +x /bin/sh 顶部附近添加了以下内容:

/bin/sh

/bin/bash通常是#!/bin/bash /usr/bin/linux32 -- /bin/bash "$@" 的符号链接。这将使用以下脚本替换符号链接:

RUN

这使得Dockerfilelinux32的每次调用都在RUN ["command"...]下运行。除非您使用绕过调用/bin/sh的{​​{1}}格式。在这种情况下,您必须手动添加linux32前缀。

答案 2 :(得分:0)

对于CentOS,您只需修改以下文件:

{% block js %}
<script type="text/javascript"src="//s7.addthis.com/js/300/addthis_widget.js#pubid=fdfs" async="async"></script>
<script src="{% static 'js/blog/blog_list.js' %}"></script>

<script>
$(function () {
    addthis.sharecounters.getShareCounts('facebook', function(obj) {
        console.log(obj);
    });
})
</script>
{% endblock %}

(就像我在the pull-request to toopher/centos-i386:centos6中所做的那样)

+RUN echo "i686" > /etc/yum/vars/arch && \
+    echo "i386" > /etc/yum/vars/basearch