1.\" $NetBSD: compat_linux.8,v 1.37 2011/11/21 14:27:41 njoly Exp $ 2.\" 3.\" Copyright (c) 1995 Frank van der Linden 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. All advertising materials mentioning features or use of this software 15.\" must display the following acknowledgement: 16.\" This product includes software developed for the NetBSD Project 17.\" by Frank van der Linden 18.\" 4. The name of the author may not be used to endorse or promote products 19.\" derived from this software without specific prior written permission 20.\" 21.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31.\" 32.Dd April 30, 2007 33.Dt COMPAT_LINUX 8 34.Os 35.Sh NAME 36.Nm compat_linux 37.Nd setup procedure for running Linux binaries 38.Sh DESCRIPTION 39.Nx 40supports running Linux binaries. 41This applies to amd64, arm, alpha, i386, m68k, and powerpc systems for now. 42Both the a.out and ELF binary formats are supported. 43Most programs should work, including the ones that use the Linux SVGAlib (only 44on i386). 45.Nx 46amd64 can execute both 32bit and 64bit linux programs. 47Programs that will not work include some that use 48i386-specific calls, such as enabling virtual 8086 mode. 49Currently, sound is only partially supported for Linux binaries (they will 50probably run, depending on what Linux sound support features are used). 51.Pp 52The Linux compatibility feature is active 53for kernels compiled with the 54.Dv COMPAT_LINUX 55option enabled. 56If support for Linux a.out executables is desired, the 57.Dv EXEC_AOUT 58option should be enabled in addition to option 59.Dv COMPAT_LINUX . 60Similarly, if support for Linux 32-bit and/or 64-bit ELF executables 61is desired, the 62.Dv EXEC_ELF32 63and/or 64.Dv EXEC_ELF64 65options (respectively) should be enabled in addition to 66.Dv COMPAT_LINUX . 67.Pp 68A lot of programs are dynamically linked. 69This means that you will also need the Linux shared libraries that the 70program depends on, and the runtime linker. 71Also, you will need to create a 72.Dq shadow root 73directory for Linux binaries on your 74.Nx 75system. 76This directory is named 77.Pa /emul/linux 78or 79.Pa /emul/linux32 80for 32bit emulation on 64bit systems. 81Any file operations done by Linux programs run under 82.Nx 83will look in this directory first. 84So, if a Linux program opens, for example, 85.Pa /etc/passwd , 86.Nx 87will 88first try to open 89.Pa /emul/linux/etc/passwd , 90and if that does not exist open the 91.Sq real 92.Pa /etc/passwd 93file. 94It is recommended that you install 95Linux packages that include configuration files, etc under 96.Pa /emul/linux , 97to avoid naming conflicts with possible 98.Nx 99counterparts. 100Shared libraries should also be installed in the shadow tree. 101Filenames that start "/../" are only looked up in the real root. 102.Pp 103Generally, you will need to look for the shared libraries that Linux 104binaries depend on only the first few times that you install a Linux 105program on your 106.Nx 107system. 108After a while, you will have a sufficient 109set of Linux shared libraries on your system to be able to run newly 110imported Linux binaries without any extra work. 111.Ss Setting up shared libraries 112How to get to know which shared libraries Linux binaries need, and where 113to get them? Basically, there are 2 possibilities (when following 114these instructions: you will need to be root on your 115.Nx 116system to 117do the necessary installation steps). 118.Bl -enum 119.It 120For i386, you can simply install the SuSE shared libs using the 121.Pa pkgsrc/emulators/suse100_linux 122package(s). 123On PowerPC ports, the 124.Pa pkgsrc/emulators/linuxppc_lib 125will install the needed libraries. 126If you are on other platforms, or this doesn't supply you with all 127the needed libraries, read on. 128.It 129You have access to a Linux system. 130In this case you can temporarily install the binary there, see what 131shared libraries it needs, and copy them to your 132.Nx 133system. 134Example: you have just ftp-ed the Linux binary of Doom. 135Put it on the Linux system you have access to, and check which shared libraries it 136needs by running 137.Sq ldd linuxxdoom : 138.Bd -literal -offset indent 139(me@linux) ldd linuxxdoom 140 libXt.so.3 (DLL Jump 3.1) =\*[Gt] /usr/X11/lib/libXt.so.3.1.0 141 libX11.so.3 (DLL Jump 3.1) =\*[Gt] /usr/X11/lib/libX11.so.3.1.0 142 libc.so.4 (DLL Jump 4.5pl26) =\*[Gt] /lib/libc.so.4.6.29 143.Ed 144.Pp 145You would need go get all the files from the last column, and 146put them under 147.Pa /emul/linux , 148with the names in the first column 149as symbolic links pointing to them. 150This means you eventually have these files on your 151.Nx 152system: 153.Bl -item -compact 154.It 155.Pa /emul/linux/usr/X11/lib/libXt.so.3.1.0 156.It 157.Pa /emul/linux/usr/X11/lib/libXt.so.3 158(symbolic link to the above) 159.It 160.Pa /emul/linux/usr/X11/lib/libX11.so.3.1.0 161.It 162.Pa /emul/linux/usr/X11/lib/libX11.so.3 163(symbolic link to the above) 164.It 165.Pa /emul/linux/lib/libc.so.4.6.29 166.It 167.Pa /emul/linux/lib/libc.so.4 168(symbolic link to the above) 169.El 170.Pp 171Note that if you already have a Linux shared library with a 172matching major revision number to the first column of the 173.Xr ldd 1 174output, you won't need to copy the file named in the last column 175to your system, the one you already have should work. 176It is advisable to copy the shared library anyway if it is a newer version, 177though. 178You can remove the old one, as long as you make the symbolic 179link point to the new one. 180So, if you have these libraries on your system: 181.Pp 182.Bl -item -compact 183.It 184.Pa /emul/linux/lib/libc.so.4.6.27 185.It 186.Pa /emul/linux/lib/libc.so.4 187-\*[Gt] 188.Pa /emul/linux/lib/libc.so.4.6.27 189.El 190.Pp 191and you find that the 192.Ic ldd 193output for a new binary you want to install is: 194.Bd -literal 195libc.so.4 (DLL Jump 4.5pl26) =\*[Gt] /lib/libc.so.4.6.29 196.Ed 197.Pp 198you won't need to worry about copying 199.Pa /lib/libc.so.4.6.29 200too, because the program should work fine with the slightly older version. 201You can decide to replace the libc.so anyway, and that should leave 202you with: 203.Bl -item -compact 204.It 205.Pa /emul/linux/lib/libc.so.4.6.29 206.It 207.Pa /emul/linux/lib/libc.so.4 208-\*[Gt] 209.Pa /emul/linux/lib/libc.so.4.6.29 210.El 211.Pp 212Please note that the symbolic link mechanism is 213.Em only 214needed for Linux binaries, the 215.Nx 216runtime linker takes care of 217looking for matching major revision numbers itself, you 218don't need to worry about that. 219.Pp 220Finally, you must make sure that you have the Linux runtime linker 221and its config files on your system. 222You should copy these 223files from the Linux system to their appropriate place on your 224.Nx 225system (in the 226.Pa /emul/linux 227tree): 228.Bl -item -compact 229.It 230.Pa /lib/ld.so 231.It 232.Pa /etc/ld.so.cache 233.It 234.Pa /etc/ld.so.config 235.El 236.It 237You don't have access to a Linux system. 238In that case, you should get the extra files you need from various ftp sites. 239Information on where to look for the various files is appended below. 240For now, let's assume you know where to get the files. 241.Pp 242Retrieve the following files (from _one_ ftp site to avoid 243any version mismatches), and install them under 244.Pa /emul/linux 245(i.e. 246.Pa /foo/bar 247is installed as 248.Pa /emul/linux/foo/bar ) : 249.Pp 250.Bl -item -compact 251.It 252.Pa /sbin/ldconfig 253.It 254.Pa /usr/bin/ldd 255.It 256.Pa /lib/libc.so.x.y.z 257.It 258.Pa /lib/ld.so 259.El 260.Pp 261.Ic ldconfig 262and 263.Ic ldd 264don't necessarily need to be under 265.Pa /emul/linux , 266you can install them elsewhere in the system too. 267Just make sure they don't conflict with their 268.Nx 269counterparts. 270A good idea would be to install them in 271.Pa /usr/local/bin 272as 273.Ic ldconfig-linux 274and 275.Ic ldd-linux . 276.Pp 277Create the file 278.Pa /emul/linux/etc/ld.so.conf , 279containing the directories in which the Linux runtime linker should look 280for shared libs. 281It is a plain text file, containing a directory name on each line. 282.Pa /lib 283and 284.Pa /usr/lib 285are standard, you could add the following: 286.Bl -item -compact 287.It 288.Pa /usr/X11/lib 289.It 290.Pa /usr/local/lib 291.El 292.Pp 293Note that these are mapped to 294.Pa /emul/linux/XXXX 295by 296.Nx Ap s 297compat 298code, and should exist as such on your system. 299.Pp 300Run the Linux 301.Ic ldconfig 302program. 303It should be statically 304linked, so it doesn't need any shared libraries by itself. 305It will create the file 306.Pa /emul/linux/etc/ld.so.cache 307You should rerun the Linux version of 308.Ic ldconfig 309each time you add a new shared library. 310.Pp 311You should now be set up for Linux binaries which only need 312a shared libc. 313You can test this by running the Linux 314.Ic ldd 315on itself. 316Suppose that you have it installed as 317.Ic ldd-linux , 318it should produce something like: 319.Pp 320.Bl -tag -width 123 -compact -offset indent 321.It (me@netbsd) ldd-linux `which ldd-linux` 322libc.so.4 (DLL Jump 4.5pl26) =\*[Gt] /lib/libc.so.4.6.29 323.El 324.Pp 325This being done, you are ready to install new Linux binaries. 326Whenever you install a new Linux program, you should check 327if it needs shared libraries, and if so, whether you have 328them installed in the 329.Pa /emul/linux 330tree. 331To do this, you run the Linux 332.Ic ldd 333on the new program, and watch its output. 334.Ic ldd 335(see also the manual page for 336.Xr ldd 1 ) 337will print a list 338of shared libraries that the program depends on, in the 339form 340.Aq majorname 341.Pq Aq jumpversion 342=\*[Gt] 343.Aq fullname . 344.Pp 345If it prints 346.Dq not found 347instead of 348.Aq fullname 349it means that you need an extra library. 350Which library this is, is shown in 351.Aq majorname , 352which will be of the form libXXXX.so.\*[Lt]N\*[Gt] 353You will need to find a libXXXX.so.\*[Lt]N\*[Gt].\*[Lt]mm\*[Gt] on a 354Linux ftp site, and install it on your system. 355The XXXX (name) and 356.Aq N 357(major revision number) should match; the minor number(s) 358.Aq mm 359are less important, though it is advised to take the most recent version. 360.It 361Set up linux specific devices: 362.Pp 363.Bl -tag -width 123 -compact -offset indent 364.It (me@netbsd) cd /usr/share/examples/emul/linux/etc 365.br 366.It (me@netbsd) cp LINUX_MAKEDEV /emul/linux/dev 367.br 368.It (me@netbsd) cd /emul/linux/dev \*[Am]\*[Am] sh LINUX_MAKEDEV all 369.El 370.El 371.Ss Setting up procfs 372Some Linux binaries expect procfs to be mounted and that it would 373contain some Linux specific stuff. 374If it's not the case, they behave unexpectedly or even crash. 375.Pp 376Mount procfs on 377.Nx 378using following command: 379.Bl -tag -width 123 -offset indent 380.It (me@netbsd) mount_procfs -o linux procfs /emul/linux/proc 381.El 382.Pp 383You can also set up your system so that procfs is mounted automatically 384on system boot, by putting an entry like the one below to 385.Pa /etc/fstab . 386.Bl -tag -width 123 -offset indent 387.It procfs /emul/linux/proc procfs ro,linux 388.El 389.Pp 390See 391.Xr mount_procfs 8 392for further information. 393.Ss Setting up other files 394Newer version of Linux use 395.Pa /etc/nsswitch.conf 396for network information, such as 397.Tn NIS 398and DNS. 399You must create or get a valid copy of this file and put it in 400.Pa /emul/linux/etc . 401.Ss Finding the necessary files 402.Em Note : 403the information below is valid as of the time this 404document was first written (March, 1995), but certain details 405such as names of ftp sites, directories and distribution names 406may have changed by the time you read this. 407.Pp 408Linux is distributed by several groups that make their own set 409of binaries that they distribute. 410Each distribution has its own name, like 411.Dq Slackware 412or 413.Dq Yggdrasil . 414The distributions are 415available on a lot of ftp sites. 416Sometimes the files are unpacked, 417and you can get the individual files you need, but mostly they 418are stored in distribution sets, usually consisting of subdirectories 419with gzipped tar files in them. 420The primary ftp sites for the distributions are: 421.Bl -item -compact -offset indent 422.It 423.Pa sunsite.unc.edu:/pub/Linux/distributions 424.It 425.Pa tsx-11.mit.edu:/pub/linux/distributions 426.El 427.Pp 428Some European mirrors: 429.Bl -item -compact -offset indent 430.It 431.Pa ftp.luth.se:/pub/linux/distributions 432.It 433.Pa ftp.demon.co.uk:/pub/linux/distributions 434.It 435.Pa src.doc.ic.ac.uk:/packages/linux/distributions 436.El 437.Pp 438For simplicity, let's concentrate on Slackware here. 439This distribution 440consists of a number of subdirectories, containing separate packages. 441Normally, they're controlled by an install program, but you can 442retrieve files 443.Dq by hand 444too. 445First of all, you will need to look in the 446.Pa contents 447subdir of the distribution. 448You will find a lot of small textfiles here describing the contents of 449the separate packages. 450The fastest way to look something up is to retrieve all the files in the 451contents subdirectory, and grep through them for the file you need. 452Here is an example of a list of files that you might need, and 453in which contents-file you will find it by grepping through them: 454.Bd -literal -offset indent 455Needed Package 456 457ld.so ldso 458ldconfig ldso 459ldd ldso 460libc.so.4 shlibs 461libX11.so.6.0 xf_lib 462libXt.so.6.0 xf_lib 463libX11.so.3 oldlibs 464libXt.so.3 oldlibs 465.Ed 466.Pp 467So, in this case, you will need the packages ldso, shlibs, xf_lib and oldlibs. 468In each of the contents-files for these packages, look for a line saying 469.Dq PACKAGE LOCATION , 470it will tell you on which 471.Sq disk 472the package is, 473in our case it will tell us in which subdirectory we need to look. 474For our example, we would find the following locations: 475.Bd -literal -offset indent 476Package Location 477 478ldso diska2 479shlibs diska2 480oldlibs diskx6 481xf_lib diskx9 482.Ed 483.Pp 484The locations called 485.Pa diskXX 486refer to the 487.Pa slakware/XX 488subdirectories 489of the distribution, others may be found in the 490.Pa contrib 491subdirectory. 492In this case, we could now retrieve the packages we need by retrieving 493the following files (relative to the root of the Slackware distribution 494tree): 495.Bl -item -compact 496.It 497.Pa slakware/a2/ldso.tgz 498.It 499.Pa slakware/a2/shlibs.tgz 500.It 501.Pa slakware/x6/oldlibs/tgz 502.It 503.Pa slakware/x9/xf_lib.tgz 504.El 505.Pp 506Extract the files from these gzipped tarfiles in your /emul/linux directory 507(possibly omitting or afterwards removing files you don't need), and you 508are done. 509.Ss Programs using SVGAlib 510SVGAlib binaries require some extra care. 511You need to have 512.Cd options WSDISPLAY_COMPAT_USL 513in your kernel (see 514.Xr wscons 4 ) , 515and you will also have to create 516some symbolic links in the 517.Pa /emul/linux/dev 518directory, namely: 519.Bl -item -compact 520.It 521.Pa /emul/linux/dev/console 522-\*[Gt] 523.Pa /dev/tty 524.It 525.Pa /emul/linux/dev/mouse 526-\*[Gt] whatever device your mouse is connected to 527.It 528.Pa /emul/linux/dev/ttyS0 529-\*[Gt] 530.Pa /dev/tty00 531.It 532.Pa /emul/linux/dev/ttyS1 533-\*[Gt] 534.Pa /dev/tty01 535.El 536.Pp 537Be warned: the first link mentioned here makes SVGAlib binaries 538work, but may confuse others, so you may have to remove it again at 539some point. 540.Sh BUGS 541The information about Linux distributions may become outdated. 542.Pp 543Absolute pathnames pointed to by symbolic links are only looked up in the 544shadow root when the symbolic link itself was found by an absolute 545pathname inside the shadow root. 546This is not consistent. 547.Pp 548Linux executables cannot handle directory offset cookies \*[Gt] 32 bits. 549Should such an offset occur, you will see the message 550.Dq linux_getdents: dir offset too large for emulated program . 551Currently, this can only 552happen on NFS mounted file systems, mounted from servers that return 553offsets with information in the upper 32 bits. 554These errors should rarely happen, but can be avoided by mounting this 555file system with offset translation enabled. 556See the 557.Fl X 558option to 559.Xr mount_nfs 8 . 560The 561.Fl 2 562option to 563.Xr mount_nfs 8 564will also have the desired effect, but is less preferable. 565