cross-compiling - chroot 或虚拟 jail 环境——用于交叉编译?

标签 cross-compiling embedded-linux chroot

我希望在我的 Ubuntu 主机上为我的 ARM 目标进行编译。
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=31&t=8478

上面的链接指出使用 chroot 并直接将程序编译到主机上目标的根文件系统中。

有些人建议使用 jail 虚拟环境,例如scratchbox。
Setting up a cross-compilation environment for a specific target platform

https://en.wikipedia.org/wiki/Chroot

The chroot mechanism is not intended to defend against intentional tampering by privileged (root) users. On most systems, chroot contexts do not stack properly and chrooted programs with sufficient privileges may perform a second chroot to break out. To mitigate the risk of this security weakness, chrooted programs should relinquish root privileges as soon as practical after chrooting, or other mechanisms – such as FreeBSD Jails - should be used instead. Note that some systems, such as FreeBSD, take precautions to prevent the second chroot attack.[1]
So i am investigating on it for few days here i am not able to understand what above statement means. 

1> 虚拟 jail 环境相对于 chroot 的具体优势是什么?

2> chroot 是否会影响所有打开的终端或..运行命令的特定终端?

3> 我们到底应该使用什么来交叉编译 Jail,比如 scrap-box 或 chroot。

最佳答案

维基百科谈论 chroot 的安全性,因为 chroot 经常被与 sandbox 进行比较(不提供 chroot) 或其他旨在通过隔离提供安全性的目的。
chroot() (这是一个 UNIX 系统调用) 是将表观根目录 ( / ) 更改为系统调用指向的另一个目录的过程。 这意味着如果/dir/target 的 chroot 可执行文件想要访问加载 /lib/ld-linux.so.2 (可执行文件中的硬编码路径),真正的访问将发生在 /dir/target/lib/ld-linux.so.2
因此,这意味着程序需要访问的每个文件和库都需要将其通常的真实路径(前缀为 / ) 到 chrooted 路径(本例中为/dir/target) 。如果您在 chroot 中使用完整的系统,您最终将得到 man hier 中描述的目录结构。但以 chroot 路径为前缀(对于 chroot 程序)。这也意味着您可以使用不同架构的不同二进制文件(如果您的 CPU 支持多个架构;这样可以提供一种隔离)。

正如您在查看 chroot 时所看到的,主要目的并不是提供安全性,而是用于其他 purposes从 Linux 中的 initramfs 根目录切换到磁盘上挂载的根目录(除了进程 mount -o move)。
然而,正如Wikipedia article所述,某些实现(例如 FreeBSD)选择提供一定程度的安全性:例如禁用在 chroot 内创建第二个 chroot 的可能性。维基百科告诉 chroot 需要以 root 身份运行是错误的。如今大多数系统都有更多 fine-grained mechanism比用户/组权限。

Jails 旨在直接提供隔离,它允许限制进程可以使用的 RAM 和 CPU 数量;禁用共享内存;限制权限...
某些实现(例如沙箱命令)不提供 chroot。 jail 有申请Operating system–level virtualization ,或避免在 testing special code 时损坏系统的其余部分.
如果您以沙盒命令为例,您会发现它本身无法使用备用根目录结构。

http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=8478提到qemu-user这显然是为了测试程序,而不必将它们放在 Rasberry Pi 上。这与虚拟机不同,因为这里没有虚拟化硬件:程序的二进制指令被转换为 native 指令,这意味着系统调用的处理方式就像程序在 native 运行一样。
这就需要使用不同的根目录结构:一些共享对象文件路径名在所有发行版和体系结构中都是通用的(例如 /lib/ld-linux.so.2 )。您不能将多种架构混合到同一个二进制文件中。如果将共享库替换为其 ARM 等效库,则这些文件将无法用于 native 可执行文件。出于同样的原因,qemu-user 或整个目标系统需要静态编译。

我真的建议您安装并设置binfmt 。它将使您能够通过自动启动 qemu-arm-static 命令来运行程序,就好像它们具有与您的计算机相同的架构一样...
然后,您可能希望在不进行交叉编译的情况下编译软件,只需在 chroot 中安装 native ARM 编译器即可。

关于cross-compiling - chroot 或虚拟 jail 环境——用于交叉编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14981867/

相关文章:

linux - 在 Ubuntu 重启时删除挂载文件夹的内容

makefile - 如何为 C 源文件生成 makefile

linux - 在 RPI3/ModelB 上实现 CPU 热插拔的系统调用

video-streaming - 用于 WebRTC 的嵌入式 linux SDK

Qt 5.11 : Touch input inverted in my application

linux - 在 chroot 中执行服务(openoffice headless)很慢

c - Travis 上的 MIPS 交叉编译

c++ - 无法使用 Eigen 库编译 ChibiOS

c++ - VxWorks 的 GCC 交叉编译器无法编译 C++

android - 无法执行子项,getpt 失败 : no suck file or directory