1.\" $NetBSD: compat_linux.8,v 1.33 2006/09/22 15:37:20 christos 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 September 22, 2006 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 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). 45Programs that will not work include some that use 46i386-specific calls, such as enabling virtual 8086 mode. 47Currently, sound is only partially supported for Linux binaries (they will 48probably run, depending on what Linux sound support features are used). 49.Pp 50The Linux compatibility feature is active 51for kernels compiled with the 52.Dv COMPAT_LINUX 53option enabled. 54If support for Linux a.out executables is desired, the 55.Dv EXEC_AOUT 56option should be enabled in addition to option 57.Dv COMPAT_LINUX . 58Similarly, if support for Linux 32-bit and/or 64-bit ELF executables 59is desired, the 60.Dv EXEC_ELF32 61and/or 62.Dv EXEC_ELF64 63options (respectively) should be enabled in addition to 64.Dv COMPAT_LINUX . 65.Pp 66A lot of programs are dynamically linked. 67This means that you will also need the Linux shared libraries that the 68program depends on, and the runtime linker. 69Also, you will need to create a 70.Dq shadow root 71directory for Linux binaries on your 72.Nx 73system. 74This directory is named 75.Pa /emul/linux . 76Any file operations done by Linux programs run under 77.Nx 78will look in this directory first. 79So, if a Linux program opens, for example, 80.Pa /etc/passwd , 81.Nx 82will 83first try to open 84.Pa /emul/linux/etc/passwd , 85and if that does not exist open the 86.Sq real 87.Pa /etc/passwd 88file. 89It is recommended that you install 90Linux packages that include configuration files, etc under 91.Pa /emul/linux , 92to avoid naming conflicts with possible 93.Nx 94counterparts. 95Shared libraries should also be installed in the shadow tree. 96.Pp 97Generally, you will need to look for the shared libraries that Linux 98binaries depend on only the first few times that you install a Linux 99program on your 100.Nx 101system. 102After a while, you will have a sufficient 103set of Linux shared libraries on your system to be able to run newly 104imported Linux binaries without any extra work. 105.Ss Setting up shared libraries 106How to get to know which shared libraries Linux binaries need, and where 107to get them? Basically, there are 2 possibilities (when following 108these instructions: you will need to be root on your 109.Nx 110system to 111do the necessary installation steps). 112.Bl -enum 113.It 114For i386, you can simply install the SuSE shared libs using the 115.Pa pkgsrc/emulators/suse_linux 116package(s). 117On PowerPC ports, the 118.Pa pkgsrc/emulators/linuxppc_lib 119will install the needed libraries. 120If you are on other platforms, or this doesn't supply you with all 121the needed libraries, read on. 122.It 123You have access to a Linux system. 124In this case you can temporarily install the binary there, see what 125shared libraries it needs, and copy them to your 126.Nx 127system. 128Example: you have just ftp-ed the Linux binary of Doom. 129Put it on the Linux system you have access to, and check which shared libraries it 130needs by running 131.Sq ldd linuxxdoom : 132.Pp 133.Bl -tag -width 123 -compact -offset indent 134.It (me@linux) ldd linuxxdoom 135.nf 136libXt.so.3 (DLL Jump 3.1) =\*[Gt] /usr/X11/lib/libXt.so.3.1.0 137libX11.so.3 (DLL Jump 3.1) =\*[Gt] /usr/X11/lib/libX11.so.3.1.0 138libc.so.4 (DLL Jump 4.5pl26) =\*[Gt] /lib/libc.so.4.6.29 139.fi 140.El 141.Pp 142You would need go get all the files from the last column, and 143put them under 144.Pa /emul/linux , 145with the names in the first column 146as symbolic links pointing to them. 147This means you eventually have these files on your 148.Nx 149system: 150.Bl -item -compact 151.It 152.Pa /emul/linux/usr/X11/lib/libXt.so.3.1.0 153.It 154.Pa /emul/linux/usr/X11/lib/libXt.so.3 155(symbolic link to the above) 156.It 157.Pa /emul/linux/usr/X11/lib/libX11.so.3.1.0 158.It 159.Pa /emul/linux/usr/X11/lib/libX11.so.3 160(symbolic link to the above) 161.It 162.Pa /emul/linux/lib/libc.so.4.6.29 163.It 164.Pa /emul/linux/lib/libc.so.4 165(symbolic link to the above) 166.El 167.Pp 168Note that if you already have a Linux shared library with a 169matching major revision number to the first column of the 170.Xr ldd 1 171output, you won't need to copy the file named in the last column 172to your system, the one you already have should work. 173It is advisable to copy the shared library anyway if it is a newer version, 174though. 175You can remove the old one, as long as you make the symbolic 176link point to the new one. 177So, if you have these libraries on your system: 178.Pp 179.Bl -item -compact 180.It 181.Pa /emul/linux/lib/libc.so.4.6.27 182.It 183.Pa /emul/linux/lib/libc.so.4 184-\*[Gt] 185.Pa /emul/linux/lib/libc.so.4.6.27 186.El 187.Pp 188and you find that the 189.Ic ldd 190output for a new binary you want to install is: 191.nf 192.Pp 193libc.so.4 (DLL Jump 4.5pl26) =\*[Gt] /lib/libc.so.4.6.29 194.fi 195.Pp 196you won't need to worry about copying 197.Pa /lib/libc.so.4.6.29 198too, because the program should work fine with the slightly older version. 199You can decide to replace the libc.so anyway, and that should leave 200you with: 201.Bl -item -compact 202.It 203.Pa /emul/linux/lib/libc.so.4.6.29 204.It 205.Pa /emul/linux/lib/libc.so.4 206-\*[Gt] 207.Pa /emul/linux/lib/libc.so.4.6.29 208.El 209.Pp 210Please note that the symbolic link mechanism is 211.Em only 212needed for Linux binaries, the 213.Nx 214runtime linker takes care of 215looking for matching major revision numbers itself, you 216don't need to worry about that. 217.Pp 218Finally, you must make sure that you have the Linux runtime linker 219and its config files on your system. 220You should copy these 221files from the Linux system to their appropriate place on your 222.Nx 223system (in the 224.Pa /emul/linux 225tree): 226.Bl -item -compact 227.It 228.Pa /lib/ld.so 229.It 230.Pa /etc/ld.so.cache 231.It 232.Pa /etc/ld.so.config 233.El 234.It 235You don't have access to a Linux system. 236In that case, you should get the extra files you need from various ftp sites. 237Information on where to look for the various files is appended below. 238For now, let's assume you know where to get the files. 239.Pp 240Retrieve the following files (from _one_ ftp site to avoid 241any version mismatches), and install them under 242.Pa /emul/linux 243(i.e. 244.Pa /foo/bar 245is installed as 246.Pa /emul/linux/foo/bar ) : 247.Pp 248.Bl -item -compact 249.It 250.Pa /sbin/ldconfig 251.It 252.Pa /usr/bin/ldd 253.It 254.Pa /lib/libc.so.x.y.z 255.It 256.Pa /lib/ld.so 257.El 258.Pp 259.Ic ldconfig 260and 261.Ic ldd 262don't necessarily need to be under 263.Pa /emul/linux , 264you can install them elsewhere in the system too. 265Just make sure they don't conflict with their 266.Nx 267counterparts. 268A good idea would be to install them in 269.Pa /usr/local/bin 270as 271.Ic ldconfig-linux 272and 273.Ic ldd-linux . 274.Pp 275Create the file 276.Pa /emul/linux/etc/ld.so.conf , 277containing the directories in which the Linux runtime linker should look 278for shared libs. 279It is a plain text file, containing a directory name on each line. 280.Pa /lib 281and 282.Pa /usr/lib 283are standard, you could add the following: 284.Bl -item -compact 285.It 286.Pa /usr/X11/lib 287.It 288.Pa /usr/local/lib 289.El 290.Pp 291Note that these are mapped to 292.Pa /emul/linux/XXXX 293by 294.Nx Ns 's 295compat 296code, and should exist as such on your system. 297.Pp 298Run the Linux 299.Ic ldconfig 300program. 301It should be statically 302linked, so it doesn't need any shared libraries by itself. 303It will create the file 304.Pa /emul/linux/etc/ld.so.cache 305You should rerun the Linux version of 306.Ic ldconfig 307each time you add a new shared library. 308.Pp 309You should now be set up for Linux binaries which only need 310a shared libc. 311You can test this by running the Linux 312.Ic ldd 313on itself. 314Suppose that you have it installed as 315.Ic ldd-linux , 316it should produce something like: 317.Pp 318.Bl -tag -width 123 -compact -offset indent 319.It (me@netbsd) ldd-linux `which ldd-linux` 320libc.so.4 (DLL Jump 4.5pl26) =\*[Gt] /lib/libc.so.4.6.29 321.El 322.Pp 323This being done, you are ready to install new Linux binaries. 324Whenever you install a new Linux program, you should check 325if it needs shared libraries, and if so, whether you have 326them installed in the 327.Pa /emul/linux 328tree. 329To do this, you run the Linux 330.Ic ldd 331on the new program, and watch its output. 332.Ic ldd 333(see also the manual page for 334.Xr ldd 1 ) 335will print a list 336of shared libraries that the program depends on, in the 337form 338.Aq majorname 339.Pq Aq jumpversion 340=\*[Gt] 341.Aq fullname . 342.Pp 343If it prints 344.Dq not found 345instead of 346.Aq fullname 347it means that you need an extra library. 348Which library this is, is shown in 349.Aq majorname , 350which will be of the form libXXXX.so.\*[Lt]N\*[Gt] 351You will need to find a libXXXX.so.\*[Lt]N\*[Gt].\*[Lt]mm\*[Gt] on a 352Linux ftp site, and install it on your system. 353The XXXX (name) and 354.Aq N 355(major revision number) should match; the minor number(s) 356.Aq mm 357are less important, though it is advised to take the most recent version. 358.It 359Set up linux specific devices: 360.Pp 361.Bl -tag -width 123 -compact -offset indent 362.It (me@netbsd) cd /usr/share/examples/emul/linux/etc 363.br 364.It (me@netbsd) cp LINUX_MAKEDEV /emul/linux/dev 365.br 366.It (me@netbsd) cd /emul/linux/dev \*[Am]\*[Am] sh LINUX_MAKEDEV all 367.El 368.El 369.Ss Setting up procfs 370Some Linux binaries expect procfs to be mounted and that it would 371contain some Linux specific stuff. 372If it's not the case, they behave unexpectedly or even crash. 373.Pp 374Mount procfs on 375.Nx 376using following command: 377.Bl -tag -width 123 -offset indent 378.It (me@netbsd) mount_procfs -o linux procfs /emul/linux/proc 379.El 380.Pp 381You can also set up your system so that procfs is mounted automatically 382on system boot, by putting an entry like the one below to 383.Pa /etc/fstab . 384.Bl -tag -width 123 -offset indent 385.It procfs /emul/linux/proc procfs ro,linux 386.El 387.Pp 388See 389.Xr mount_procfs 8 390for further information. 391.Ss Setting up other files 392Newer version of Linux use 393.Pa /etc/nsswitch.conf 394for network information, such as 395.Tn NIS 396and DNS. 397You must create or get a valid copy of this file and put it in 398.Pa /emul/linux/etc . 399.Ss Finding the necessary files 400.Em Note : 401the information below is valid as of the time this 402document was first written (March, 1995), but certain details 403such as names of ftp sites, directories and distribution names 404may have changed by the time you read this. 405.Pp 406Linux is distributed by several groups that make their own set 407of binaries that they distribute. 408Each distribution has its own name, like 409.Dq Slackware 410or 411.Dq Yggdrasil . 412The distributions are 413available on a lot of ftp sites. 414Sometimes the files are unpacked, 415and you can get the individual files you need, but mostly they 416are stored in distribution sets, usually consisting of subdirectories 417with gzipped tar files in them. 418The primary ftp sites for the distributions are: 419.Bl -item -compact -offset indent 420.It 421.Pa sunsite.unc.edu:/pub/Linux/distributions 422.It 423.Pa tsx-11.mit.edu:/pub/linux/distributions 424.El 425.Pp 426Some European mirrors: 427.Bl -item -compact -offset indent 428.It 429.Pa ftp.luth.se:/pub/linux/distributions 430.It 431.Pa ftp.demon.co.uk:/pub/linux/distributions 432.It 433.Pa src.doc.ic.ac.uk:/packages/linux/distributions 434.El 435.Pp 436For simplicity, let's concentrate on Slackware here. 437This distribution 438consists of a number of subdirectories, containing separate packages. 439Normally, they're controlled by an install program, but you can 440retrieve files 441.Dq by hand 442too. 443First of all, you will need to look in the 444.Pa contents 445subdir of the distribution. 446You will find a lot of small textfiles here describing the contents of 447the separate packages. 448The fastest way to look something up is to retrieve all the files in the 449contents subdirectory, and grep through them for the file you need. 450Here is an example of a list of files that you might need, and 451in which contents-file you will find it by grepping through them: 452.Pp 453.Bd -literal -offset indent 454Needed Package 455 456ld.so ldso 457ldconfig ldso 458ldd ldso 459libc.so.4 shlibs 460libX11.so.6.0 xf_lib 461libXt.so.6.0 xf_lib 462libX11.so.3 oldlibs 463libXt.so.3 oldlibs 464.Ed 465.Pp 466So, in this case, you will need the packages ldso, shlibs, xf_lib and oldlibs. 467In each of the contents-files for these packages, look for a line saying 468.Dq PACKAGE LOCATION , 469it will tell you on which 470.Sq disk 471the package is, 472in our case it will tell us in which subdirectory we need to look. 473For our example, we would find the following locations: 474.Pp 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 543Pathnames pointed to by symbolic links are not looked up in the 544shadow root when running a Linux executable. 545This is not consistent. 546.Pp 547Linux executables cannot handle directory offset cookies \*[Gt] 32 bits. 548Should such an offset occur, you will see the message 549.Dq linux_getdents: dir offset too large for emulated program . 550Currently, this can only 551happen on NFS mounted filesystems, mounted from servers that return 552offsets with information in the upper 32 bits. 553These errors should rarely happen, but can be avoided by mounting this 554filesystem with offset translation enabled. 555See the 556.Fl X 557option to 558.Xr mount_nfs 8 . 559The 560.Fl 2 561option to 562.Xr mount_nfs 8 563will also have the desired effect, but is less preferable. 564