COMPAT_LINUX(8) | System Manager's Manual | COMPAT_LINUX(8)

# NAME

`compat_linux` —
setup procedure for running Linux binaries

# DESCRIPTION

NetBSD supports running Linux binaries. This applies to aarch64, alpha, amd64, arm, i386, m68k, and powerpc systems for now. Both the a.out and ELF binary formats are supported. Most programs should work. NetBSD aarch64 and amd64 can execute both 32-bit and 64-bit Linux programs. Programs that will not work include some that use i386-specific calls, such as enabling virtual 8086 mode. Currently, sound is supported through OSSv3 compat.

The Linux compatibility feature is active for kernels compiled with the `COMPAT_LINUX` option enabled. If support for Linux a.out executables is desired, the `EXEC_AOUT` option should be enabled in addition to option `COMPAT_LINUX`. Similarly, if support for Linux 32-bit and/or 64-bit ELF executables is desired, the `EXEC_ELF32` and/or `EXEC_ELF64` options (respectively) should be enabled in addition to `COMPAT_LINUX`. If sound support is desired, `COMPAT_OSSAUDIO` should be enabled.

A lot of programs are dynamically linked. This means that you will also need the Linux shared libraries that the program depends on, and the runtime linker. Also, you will need to create a “shadow root” directory for Linux binaries on your NetBSD system. This directory is named /emul/linux or /emul/linux32 for 32-bit emulation on 64-bit systems. Any file operations done by Linux programs run under NetBSD will look in this directory first. So, if a Linux program opens, for example, /etc/passwd, NetBSD will first try to open /emul/linux/etc/passwd, and if that does not exist open the ‘real’ /etc/passwd file. It is recommended that you install Linux packages that include configuration files, etc under /emul/linux, to avoid naming conflicts with possible NetBSD counterparts. Shared libraries should also be installed in the shadow tree. Filenames that start "/../" are only looked up in the real root.

Generally, you will need to look for the shared libraries that Linux binaries depend on only the first few times that you install a Linux program on your NetBSD system. After a while, you will have a sufficient set of Linux shared libraries on your system to be able to run newly imported Linux binaries without any extra work.

## Setting up shared libraries

Find the dependencies of a Linux binary using readelf(1):

    $ readelf -d ./runner | grep Shared
     0x00000001 (NEEDED)                     Shared library: [libstdc++.so.6]
     0x00000001 (NEEDED)                     Shared library: [libz.so.1]
     0x00000001 (NEEDED)                     Shared library: [libXxf86vm.so.1]
     0x00000001 (NEEDED)                     Shared library: [libGL.so.1]
     0x00000001 (NEEDED)                     Shared library: [libopenal.so.1]
     0x00000001 (NEEDED)                     Shared library: [libm.so.6]
     0x00000001 (NEEDED)                     Shared library: [librt.so.1]
     0x00000001 (NEEDED)                     Shared library: [libpthread.so.0]
     0x00000001 (NEEDED)                     Shared library: [libdl.so.2]
     0x00000001 (NEEDED)                     Shared library: [libcrypto.so.1.0.0]
     0x00000001 (NEEDED)                     Shared library: [libXext.so.6]
     0x00000001 (NEEDED)                     Shared library: [libX11.so.6]
     0x00000001 (NEEDED)                     Shared library: [libXrandr.so.2]
     0x00000001 (NEEDED)                     Shared library: [libGLU.so.1]
     0x00000001 (NEEDED)                     Shared library: [libssl.so.1.0.0]
     0x00000001 (NEEDED)                     Shared library: [libgcc_s.so.1]
     0x00000001 (NEEDED)                     Shared library: [libc.so.6]

For x86, you can simply install the openSUSE shared libraries using the pkgsrc/emulators/suse131_* or pkgsrc/emulators/suse131_32_* packages.

For example, an application which requires libcrypto.so.1.0.0, libXext.so.6, and libGL.so.1 will require `openssl`, `x11`, and `glx`, in addition to the `base` SUSE package.

Otherwise, you may have to obtain shared libraries from another Linux system, and copy them to e.g. /emul/linux/lib64.

## Setting up procfs

Some Linux binaries expect procfs to be mounted and that it contains some Linux-specific extensions. If it's not the case, they behave unexpectedly or even crash.

Mount procfs on NetBSD using following command:

$ mount_procfs procfs /emul/linux/proc

You can also set up your system so that procfs is mounted automatically on system boot, by putting an entry like the one below to /etc/fstab.

procfs /emul/linux/proc procfs ro

Note: mount_procfs(8) defaults to Linux flavored procfs since NetBSD 5.0. Ensure you do not mount procfs with nolinux.

See mount_procfs(8) for further information.

## Setting up other files

Newer version of Linux use /etc/nsswitch.conf for network information, such as NIS and DNS. You must create or get a valid copy of this file and put it in /emul/linux/etc.

# CAVEATS

`compat_linux` is generally not enabled in `GENERIC` kernels for security reasons, but is available as a module. It must be added to modules.conf(5) to be used. It will not be loaded automatically.

# BUGS

The information about Linux distributions will become outdated.

Absolute pathnames pointed to by symbolic links are only looked up in the shadow root when the symbolic link itself was found by an absolute pathname inside the shadow root. This is not consistent.

Linux executables cannot handle directory offset cookies > 32 bits. Should such an offset occur, you will see the message “linux_getdents: dir offset too large for emulated program”. Currently, this can only happen on NFS mounted file systems, mounted from servers that return offsets with information in the upper 32 bits. These errors should rarely happen, but can be avoided by mounting this file system with offset translation enabled. See the `-X` option to mount_nfs(8). The `-2` option to mount_nfs(8) will also have the desired effect, but is less preferable.

September 26, 2021 | NetBSD 10.1