aboutsummaryrefslogtreecommitdiff
path: root/documentation/content/en/books/handbook/linuxemu/_index.adoc
blob: e9d720ef51edbbeb7641ff2348009ded611a8a9a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
---
title: Chapter 11. Linux Binary Compatibility
part: Part II. Common Tasks
prev: books/handbook/printing
next: books/handbook/wine
description: FreeBSD provides binary compatibility with Linux, allowing users to install and run most Linux binaries on a FreeBSD system without having to first modify the binary
tags: ["linux", "linuxulator", "emulation", "binary", "compatibility"]
showBookMenu: true
weight: 14
path: "/books/handbook/"
---

[[linuxemu]]
= Linux Binary Compatibility
:doctype: book
:toc: macro
:toclevels: 1
:icons: font
:sectnums:
:sectnumlevels: 6
:sectnumoffset: 11
:partnums:
:source-highlighter: rouge
:experimental:
:images-path: books/handbook/linuxemu/

ifdef::env-beastie[]
ifdef::backend-html5[]
:imagesdir: ../../../../images/{images-path}
endif::[]
ifndef::book[]
include::shared/authors.adoc[]
include::shared/mirrors.adoc[]
include::shared/releases.adoc[]
include::shared/attributes/attributes-{{% lang %}}.adoc[]
include::shared/{{% lang %}}/teams.adoc[]
include::shared/{{% lang %}}/mailing-lists.adoc[]
include::shared/{{% lang %}}/urls.adoc[]
toc::[]
endif::[]
ifdef::backend-pdf,backend-epub3[]
include::../../../../../shared/asciidoctor.adoc[]
endif::[]
endif::[]

ifndef::env-beastie[]
toc::[]
include::../../../../../shared/asciidoctor.adoc[]
endif::[]

[[linuxemu-synopsis]]
== Synopsis

FreeBSD provides optional binary compatibility with Linux(R), allowing users to install and run unmodified Linux binaries.
It is available for the i386, amd64, and arm64 architectures.

Some Linux-specific operating system features are not yet supported; this mostly happens with functionality specific to hardware or related to system management, such as cgroups or namespaces.

After reading this chapter, you will know:

* How to enable Linux binary compatibility on a FreeBSD system.
* How to install additional Linux shared libraries.
* How to install Linux applications on a FreeBSD system.
* The implementation details of Linux compatibility in FreeBSD.

Before reading this chapter, you should:

* Know how to install crossref:ports[ports,additional third-party software].

[[linuxemu-lbc-install]]
== Configuring Linux Binary Compatibility

By default, Linux binary compatibility is not enabled.
To enable it at boot time, add this line to [.filename]#/etc/rc.conf#:

[.programlisting]
....
linux_enable="YES"
....

Once enabled, it can be started without rebooting by running:
[source,shell]
....
# service linux start
....

The [.filename]#/etc/rc.d/linux# script will load necessary kernel modules and mount filesystems expected by Linux applications under [.filename]#/compat/linux#.
This is enough for statically linked Linux binaries to work.
They can be started in the same way native FreeBSD binaries can; they behave almost exactly like native processes and can be traced and debugged the usual way.

Linux binaries linked dynamically (which is the vast majority) also require Linux shared libraries to be installed - they can run on top of the FreeBSD kernel, but they cannot use FreeBSD libraries; this is similar to how 32-bit binaries cannot use native 64-bit libraries.
There are several ways of providing those libraries: one can copy them over from an existing Linux installation using the same architecture, install them from FreeBSD packages, or install using man:debootstrap[8] (from package:sysutils/debootstrap[]), and others.

[[linuxemu-packages]]
== CentOS Base System from FreeBSD Packages

[NOTE]
====
This method is not yet available for arm64.
====

The easiest way to install Linux libraries is to install package:emulators/linux_base-c7[] package or port, which places the CentOS 7-derived base system into [.filename]#/compat/linux#:

[source,shell]
....
# pkg install linux_base-c7
....

FreeBSD provides packages for some Linux binary applications.
For example, to install Sublime Text 4, along with all the Linux libraries it depends on, run this command:
[source,shell]
....
# pkg install linux-sublime-text4
....

[[linuxemu-debootstrap]]
== Debian / Ubuntu Base System with man:debootstrap[8]

An alternative way of providing Linux shared libraries is by using package:sysutils/debootstrap[].
This has the advantage of providing a full Debian or Ubuntu distribution.
To use it, follow the instructions at FreeBSD Wiki: https://wiki.freebsd.org/LinuxJails[FreeBSD Wiki - Linux Jails].

After debootstrapping, man:chroot[8] into the newly created directory and install software in a way typical for the Linux distribution inside, for example:

[source,shell]
....
# chroot /compat/ubuntu /bin/bash
root@hostname:/# apt update
....

It is possible to debootstrap into [.filename]#/compat/linux#, but it is discouraged to avoid collisions with files installed from FreeBSD ports and packages.
Instead, derive the directory name from the distribution or version name, e.g., [.filename]#/compat/ubuntu#.
If the bootstrapped instance is intended to provide Linux shared libraries without having to explicitly use chroot or jails, one can point the kernel at it by updating the `compat.linux.emul_path` sysctl and adding a line like this to [.filename]#/etc/sysctl.conf#:

[.programlisting]
....
compat.linux.emul_path="/compat/ubuntu"
....

This sysctl controls the kernel's path translation mechanism; see man:linux[4] for details.
Please note that changing it might cause trouble for Linux applications installed from FreeBSD packages; one reason is that many of those applications are still 32-bit, while Ubuntu seems to be deprecating 32-bit library support.

[[linuxemu-advanced]]
== Advanced Topics

The Linux compatibility layer is a work in progress.
Consult https://wiki.freebsd.org/Linuxulator[FreeBSD Wiki - Linuxulator] for more information.

A list of all Linux-related man:sysctl[8] knobs can be found in man:linux[4].

Some applications require specific filesystems to be mounted.
This is normally handled by the [.filename]#/etc/rc.d/linux# script, but can be disabled by adding this line to [.filename]#/etc/rc.conf#:

[.programlisting]
....
linux_mounts_enable="NO"
....

Filesystems mounted by the rc script will not work for Linux processes inside chroots or jails; if needed, configure them in [.filename]#/etc/fstab#:
....
devfs      /compat/linux/dev      devfs      rw,late                    0  0
tmpfs      /compat/linux/dev/shm  tmpfs      rw,late,size=1g,mode=1777  0  0
fdescfs    /compat/linux/dev/fd   fdescfs    rw,late,linrdlnk           0  0
linprocfs  /compat/linux/proc     linprocfs  rw,late                    0  0
linsysfs   /compat/linux/sys      linsysfs   rw,late                    0  0
....

Since the Linux binary compatibility layer has gained support for running both 32- and 64-bit Linux binaries (on 64-bit x86 hosts), it is no longer possible to link the emulation functionality statically into a custom kernel.

[[linuxemu-libs-manually]]
=== Installing Additional Libraries Manually

[NOTE]
====
For base system subdirectories created with man:debootstrap[8], use the instructions above instead.
====

If a Linux application complains about missing shared libraries after configuring Linux binary compatibility, determine which shared libraries the Linux binary needs and install them manually.

From a Linux system using the same CPU architecture, `ldd` can be used to determine which shared libraries the application needs.
For example, to check which shared libraries `linuxdoom` needs, run this command from a Linux system that has Doom installed:

[source,shell]
....
% ldd linuxdoom
libXt.so.3 (DLL Jump 3.1) => /usr/X11/lib/libXt.so.3.1.0
libX11.so.3 (DLL Jump 3.1) => /usr/X11/lib/libX11.so.3.1.0
libc.so.4 (DLL Jump 4.5pl26) => /lib/libc.so.4.6.29
....

Then, copy all the files in the last column of the output from the Linux system into [.filename]#/compat/linux# on the FreeBSD system.
Once copied, create symbolic links to the names in the first column.
This example will result in the following files on the FreeBSD system:

[source,shell]
....
/compat/linux/usr/X11/lib/libXt.so.3.1.0
/compat/linux/usr/X11/lib/libXt.so.3 -> libXt.so.3.1.0
/compat/linux/usr/X11/lib/libX11.so.3.1.0
/compat/linux/usr/X11/lib/libX11.so.3 -> libX11.so.3.1.0
/compat/linux/lib/libc.so.4.6.29
/compat/linux/lib/libc.so.4 -> libc.so.4.6.29
....

If a Linux shared library already exists with a matching major revision number to the first column of the `ldd` output, it does not need to be copied to the file named in the last column, as the existing library should work.
It is advisable to copy the shared library if it is a newer version, though.
The old one can be removed, as long as the symbolic link points to the new one.

For example, these libraries already exist on the FreeBSD system:

[source,shell]
....
/compat/linux/lib/libc.so.4.6.27
/compat/linux/lib/libc.so.4 -> libc.so.4.6.27
....

and `ldd` indicates that a binary requires a later version:

[source,shell]
....
libc.so.4 (DLL Jump 4.5pl26) -> libc.so.4.6.29
....

Since the existing library is only one or two versions out of date in the last digit, the program should still work with the slightly older version.
However, it is safe to replace the existing [.filename]#libc.so# with the newer version:

[source,shell]
....
/compat/linux/lib/libc.so.4.6.29
/compat/linux/lib/libc.so.4 -> libc.so.4.6.29
....

Generally, one will need to look for the shared libraries that Linux binaries depend on only the first few times that a Linux program is installed on FreeBSD.
After a while, there will be a sufficient set of Linux shared libraries on the system to be able to run newly installed Linux binaries without any extra work.

=== Branding Linux ELF Binaries

The FreeBSD kernel uses several methods to determine if the binary to be executed is a Linux one: it checks the brand in the ELF file header, looks for known ELF interpreter paths and checks ELF notes; finally, by default, unbranded ELF executables are assumed to be Linux anyway.
Should all those methods fail, an attempt to execute the binary might result in error message:

[source,shell]
....
% ./my-linux-elf-binary
ELF binary type not known
Abort
....

To help the FreeBSD kernel distinguish between a FreeBSD ELF binary and a Linux binary, use man:brandelf[1]:

[source,shell]
....
% brandelf -t Linux my-linux-elf-binary
....

=== Installing a Linux RPM Based Application

To install a Linux RPM-based application, first install the package:archivers/rpm4[] package or port.
Once installed, `root` can use this command to install a [.filename]#.rpm#:

[source,shell]
....
# cd /compat/linux
# rpm2cpio < /path/to/linux.archive.rpm | cpio -id
....

If necessary, `brandelf` the installed ELF binaries.
Note that this will prevent a clean uninstall.

=== Configuring the Hostname Resolver

If DNS does not work or this error appears:

[source,shell]
....
resolv+: "bind" is an invalid keyword resolv+:
"hosts" is an invalid keyword
....

configure [.filename]#/compat/linux/etc/host.conf# as follows:

[.programlisting]
....
order hosts, bind
multi on
....

This specifies that [.filename]#/etc/hosts# is searched first and DNS is searched second.
When [.filename]#/compat/linux/etc/host.conf# does not exist, Linux applications use [.filename]#/etc/host.conf# and complain about the incompatible FreeBSD syntax.
Remove `bind` if a name server is not configured using [.filename]#/etc/resolv.conf#.

[[linuxemu-misc]]
=== Miscellaneous

This section describes how Linux binary compatibility works and is based on an email written to {freebsd-chat} by Terry Lambert mailto:tlambert@primenet.com[tlambert@primenet.com] (Message ID: `<199906020108.SAA07001@usr09.primenet.com>`).

FreeBSD has an abstraction called an "execution class loader".
This is a wedge into the man:execve[2] system call.

Historically, the UNIX(R) loader examined the magic number (generally the first 4 or 8 bytes of the file) to see if it was a binary known to the system, and if so, invoked the binary loader.

If it was not the binary type for the system, the man:execve[2] call returned a failure, and the shell attempted to start executing it as shell commands.
The assumption was a default of "whatever the current shell is".

Later, a hack was made for man:sh[1] to examine the first two characters, and if they were `:\n`, it invoked the man:csh[1] shell instead.

FreeBSD has a list of loaders, instead of a single loader, with a fallback to the `#!` loader for running shell interpreters or shell scripts.

For the Linux ABI support, FreeBSD sees the magic number as an ELF binary.
The ELF loader looks for a specialized _brand_, which is a comment section in the ELF image, and which is not present on SVR4/Solaris(TM) ELF binaries.

For Linux binaries to function, they must be _branded_ as type `Linux` using man:brandelf[1]:

[source,shell]
....
# brandelf -t Linux file
....

When the ELF loader sees the `Linux` brand, the loader replaces a pointer in the `proc` structure.
All system calls are indexed through this pointer.
In addition, the process is flagged for special handling of the trap vector for the signal trampoline code, and several other (minor) fix-ups that are handled by the Linux kernel module.

The Linux system call vector contains, among other things, a list of `sysent[]` entries whose addresses reside in the kernel module.

When a system call is called by the Linux binary, the trap code dereferences the system call function pointer off the `proc` structure, and gets the Linux, not the FreeBSD, system call entry points.

Linux mode dynamically _reroots_ lookups.
This is, in effect, equivalent to `union` file system mounts.
First, an attempt is made to look up the file in [.filename]#/compat/linux/original-path#.
If that fails, the lookup is done in [.filename]#/original-path#.
This makes sure that binaries that require other binaries can run.
For example, the Linux toolchain can all run under Linux ABI support.
It also means that the Linux binaries can load and execute FreeBSD binaries, if there are no corresponding Linux binaries present, and that a man:uname[1] command can be placed in the [.filename]#/compat/linux# directory tree to ensure that the Linux binaries cannot tell they are not running on Linux.

In effect, there is a Linux kernel in the FreeBSD kernel.
The various underlying functions that implement all of the services provided by the kernel are identical to both the FreeBSD system call table entries, and the Linux system call table entries: file system operations, virtual memory operations, signal delivery, and System V IPC.
The only difference is that FreeBSD binaries get the FreeBSD _glue_ functions, and Linux binaries get the Linux _glue_ functions.
The FreeBSD _glue_ functions are statically linked into the kernel, and the Linux _glue_ functions can be statically linked, or they can be accessed via a kernel module.

Technically, this is not really emulation, it is an ABI implementation.
It is sometimes called "Linux emulation" because the implementation was done at a time when there was no other word to describe what was going on.
Saying that FreeBSD ran Linux binaries was not true, since the code was not compiled in.