macos 在 Mac OS X 下模拟 Linux 二进制文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1954223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Emulating Linux binaries under Mac OS X
提问by flouxgoux
How do I run Linux binaries under Mac OS X?
如何在 Mac OS X 下运行 Linux 二进制文件?
Googling around I found a couple of emulators but none for running Linux binaries on a Mac. There are quite a few posts about running Mac OS X on Linux and that kind of stuff - but that's the opposite of what I want to do.
谷歌搜索我找到了几个模拟器,但没有一个可以在 Mac 上运行 Linux 二进制文件。有很多关于在 Linux 上运行 Mac OS X 之类的帖子 - 但这与我想要做的相反。
Update:
更新:
Thanks for all the answers! I am fully aware of MacPorts and Fink or any of the other things; and no, I do not want any of these utilities, and I do not want any of the package managers, I prefer to compile things myself. I also have Parallels and could set up virtual machines and all that jazz...
感谢所有的答案!我完全了解 MacPorts 和 Fink 或任何其他东西;不,我不想要任何这些实用程序,我也不想要任何包管理器,我更喜欢自己编译。我也有 Parallels,可以设置虚拟机和所有爵士乐......
The only thing I want to do is to find a way to run a binary that I do not have the source code for and has been compiled for Linux, but I do not want to run it under Linux but under Mac OS X. Therefore my question about emulators.
我唯一想做的就是找到一种方法来运行我没有源代码并已为 Linux 编译的二进制文件,但我不想在 Linux 下运行它,而是在 Mac OS X 下运行它。因此我的关于模拟器的问题。
回答by Maxthon Chan
Well there is a project introducing something like Linux's binfmt_misc to OS X so now what you need is an ELF loader, a dynamic linker that can load both Mach-O and ELF, and some mechanism to translate Linux calls to OS X ones.
嗯,有一个项目将 Linux 的 binfmt_misc 之类的东西引入 OS X,所以现在你需要的是一个 ELF 加载器,一个可以加载 Mach-O 和 ELF 的动态链接器,以及一些将 Linux 调用转换为 OS X 调用的机制。
Just for inspiration, you can implement the dynamic linker in the fashion that it ignores filename extension - both libfoo.so.1 (as an Linux ELF) and libfoo.1.dylib (as an Mach-O) can be loaded so that OS X versions of system libraries can be reused so that you do not need to write a "hosted on OS X" libc.so and syscalls can be handled by an kext that translates Linux calls to OS X ones in kernel.
只是为了获得灵感,您可以以忽略文件扩展名的方式实现动态链接器 - 可以加载 libfoo.so.1(作为 Linux ELF)和 libfoo.1.dylib(作为 Mach-O),以便操作系统可以重用 X 版本的系统库,这样您就不需要编写“托管在 OS X 上”的 libc.so,系统调用可以由 kext 处理,将 Linux 调用转换为内核中的 OS X 调用。
Or, in an more elegant way, implement a stripped down Linux kernel as a kext that makes the OS X kernel a dual-purpose. However that will require you to use two sets of libraries. (Binaries do not clash so it is largely okay)
或者,以更优雅的方式,将精简的 Linux 内核实现为使 OS X 内核具有双重用途的 kext。但是,这将需要您使用两组库。(二进制文件不冲突,所以基本上没问题)
回答by mipadi
Set up a virtual machine (I personally use VMWare Fusion) and then install whatever distro of Linux you desire on the virtual machine.
设置一个虚拟机(我个人使用 VMWare Fusion),然后在虚拟机上安装您想要的任何 Linux 发行版。
Or, if you have the source to the Linux program, chances are you can recompile it on a Mac and run it natively. If you install Finkor MacPorts, you can install a lot of open source programs without much trouble.
或者,如果您拥有 Linux 程序的源代码,您很有可能可以在 Mac 上重新编译它并在本机运行它。如果你安装Fink或MacPorts,你可以毫不费力地安装很多开源程序。
回答by snd
I recently found Noah, which you can use to run Linux binaries on macOS. You can install Noah via homebrew (brew install linux-noah/noah/noah
). Then you should be able to do this:
我最近发现了Noah,您可以使用它在 macOS 上运行 Linux 二进制文件。您可以通过自制软件 ( brew install linux-noah/noah/noah
)安装 Noah 。那么你应该能够做到这一点:
noah linux_binary
In my experience the behavior of the binary matches what I see on my Ubuntu machine.
根据我的经验,二进制文件的行为与我在 Ubuntu 机器上看到的相匹配。
回答by sigjuice
You might have some luck with running Linux executables under Mac OS X using Qemu's User Space Emulator
使用 Qemu 的用户空间模拟器在 Mac OS X 下运行 Linux 可执行文件可能会有一些运气
回答by Gregory Pakosz
If you decide to go the virtualization route, consider also VirtualBox.
如果您决定走虚拟化路线,还可以考虑VirtualBox。
Also, if you only need UNIX like command line tools, there is the MacPortsproject. This is basically how I set up git
on my mac: after having installed MacPorts you just have to run the sudo port install git
command to install git
on your system.
此外,如果您只需要类似 UNIX 的命令行工具,还有MacPorts项目。这基本上是我git
在 Mac 上的设置方式:安装 MacPorts 后,您只需运行sudo port install git
命令即可git
在系统上安装。
回答by jimh
noah
does not allow the binaries to execute properly for me. Use Docker Desktop for Mac.
noah
不允许二进制文件为我正确执行。使用Mac 版 Docker 桌面。
Just do:
docker pull centos:latest # 73MB CentOS docker image
做就是了:
docker pull centos:latest # 73MB CentOS docker image
Make a folder for what is needed to run your binary, and in your Dockerfile:
为运行二进制文件所需的内容创建一个文件夹,并在您的 Dockerfile 中:
FROM centos
COPY your_binary /bin/
ENTRYPOINT ["your_binary"]
and you can build it with
你可以用
docker build -t image_name
docker build -t image_name
then execute with
然后执行
docker run image_name
as if it were the binary itself. Worked for me. Hope it helps someone else. And if you need specific outputs or to store files somewhere you can mount volumes onto the docker with -v
, for example:
docker run image_name
就好像它是二进制文件本身一样。对我来说有效。希望它可以帮助别人。如果您需要特定的输出或将文件存储在某个地方,您可以使用 将卷挂载到 docker 上-v
,例如:
docker run -v path_to_my_stuff:/docker_stuff image_name
,
docker run -v path_to_my_stuff:/docker_stuff image_name
,
though adding a WORKDIR /docker_stuff
line to the Dockerfile
before ENTRYPOINT
is probably best.
尽管WORKDIR /docker_stuff
在Dockerfile
前面添加一行ENTRYPOINT
可能是最好的。
If you change ENTRYPOINT
to
如果你ENTRYPOINT
改为
ENTRYPOINT ["bash", "-c"]
and add
ENTRYPOINT ["bash", "-c"]
并添加
CMD ["your_binary"]
CMD ["your_binary"]
underneath it, you can actually pass the command into the image like
在它下面,您实际上可以将命令传递到图像中,例如
docker run -v path_on_local:/in_container_path image_name "your_binary some_parameters -optionrequiringzerowhitespacebeforeinputvalue"