Chapter 31. Linux emulation

Table of Contents

31.1. Emulation setup
31.1.1. Configuring the kernel
31.1.2. Installing the Linux libraries
31.1.3. Running Linux programs
31.2. Directory structure
31.3. Using Linux browser plugins
31.4. Further reading

The NetBSD port for amd64, i386, alpha, mac68k, macppc, and many others can execute a great number of native Linux programs, using the Linux emulation layer. Generally, when you think about emulation you imagine something slow and inefficient because, often, emulations must reproduce hardware instructions and even architectures (usually from old machines) in software. In the case of the Linux emulation this is radically different: it is only a thin software layer, mostly for system calls which are already very similar between the two systems. The application code itself is processed at the full speed of your CPU, so you don't get a degraded performance with the Linux emulation and the feeling is exactly the same as for native NetBSD applications.

31.1. Emulation setup

The installation of the Linux emulation is described in the compat_linux(8) man page; using the package system only two steps are needed.

31.1.1. Configuring the kernel

If you use a GENERIC kernel you don't need to do anything because Linux compatibility is already enabled.

If you use a customized kernel, check that the following options are enabled:

option COMPAT_LINUX
option EXEC_ELF32

or the following options if you are going to use 64-bit ELF binaries:

option COMPAT_LINUX
option EXEC_ELF64

when you have compiled a kernel with the previous options you can start installing the necessary software.

31.1.2. Installing the Linux libraries

Usually, applications are linked against shared libraries, and for Linux applications, Linux shared libraries are needed. You can get the shared libraries from any Linux distribution, provided it's not too old, but the suggested method is to use the package system to install the provided libraries from openSUSE.

All Linux binaries exist entirely within the separate root directories inside /emul/linux and /emul/linux32. The kernel will always search these paths first when looking for shared objects required by Linux programs.

A number of useful Linux shared object binaries is provided by pkgsrc, for running both 64-bit and 32-bit applications. The absolute minimum required to run dynamically linked Linux applications are are provided by the suse131_base and suse131_32_base packages (or, if using binary packages suse_base-13 and suse32_base-13). Many other libraries are also provided as separate packages.

Some packages in pkgsrc are provided as Linux binaries and will also install all the required SUSE dependencies when installed. However, this is uncommon. One such package is adoptopenjdk11-bin.

It is possible to examine which libraries are required by a Linux program with readelf(1):

$ readelf -d ./runner
Dynamic section at offset 0x3a2e94 contains 40 entries:
  Tag        Type	    Name/Value
 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 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.

31.1.3. Running Linux programs

Once the correct libraries are installed, no special steps are required to run a Linux program - simply type the command (if you acquired it as a non-pkgsrc download, include the full path on the filesystem). The kernel will detect it is a Linux executable and run it in the correct translation mode.

31.2. Directory structure

If we examine the outcome of the installation of the Linux libraries and programs we find that /emul/linux is a symbolic link pointing to /usr/pkg/emul/linux, where the following directories have been created:

bin/
dev/
etc/
lib/
lib64/
proc/
sbin/
usr/
var/

Note

Please always refer to /emul/linux and not to /usr/pkg/emul/linux. The latter is an implementation detail and may change in the future.

How much space is required for the Linux emulation software? On one system we got the following figure:

# cd /usr/pkg/emul
# du -k /emul/linux/
...
399658  /emul/linux/

31.3. Using Linux browser plugins

Linux plugins for Mozilla-based browsers can be used on native NetBSD Firefox builds through nspluginwrapper, a wrapper that translates between the native browser and a foreign plugin. At the moment, nspluginwrapper only works reliably on Mozilla-based browsers that link against GTK2+ (GTK1+ is not supported). nspluginwrapper can be installed through pkgsrc:

# cd /usr/pkgsrc/www/nspluginwrapper
# make install
    

Plugins can then be installed in two steps: first, the plugin has to be installed on the system (e.g. through pkgsrc). After that the plugin should be registered with the nspluginwrapper by the users who want to use that plugin.

In this short example we will have a look at installing the Macromedia Flash plugin. We can fullfill the first step by installing the Flash plugin through pkgsrc:

# cd /usr/pkgsrc/multimedia/ns-flash
# make install
    

After that an unprivileged user can register the Flash plugin:

$ nspluginwrapper -i /usr/pkg/lib/netscape/plugins/libflashplayer.so
    

The plugin should then be registered correctly. You can check this by using the -l option of nspluginwrapper (nspluginwrapper -l). If the plugin is listed, you can restart Firefox, and verify that the plugin was installed by entering about:plugins in the location bar.

31.4. Further reading

The following articles may be of interest for further understanding Linux (and other) emulation:

Bibliography

[chap-linux-further-implementing-linux-emul-on-netbsd] Implementing Linux emulation on NetBSD . Peter Seebach. May 2004.