Linux under FreeBSD
Michael Lucas
FreeBSD has several options for using software from other platforms, such as Wine for Win32 and dosemu for DOS. BSDI, NetBSD, and OpenBSD binaries will run unmodified, and source code from many UNIX or Linux programs can be compiled without modification on FreeBSD. Additionally, FreeBSD includes kernel modules for Linux, SCO, and SVR4. These modules allow you to run unmodified binaries for these platforms on your FreeBSD machine. FreeBSD's Linux module works well enough that several commercial software packages for Linux have been included in the FreeBSD ports system. Similarly, this article was written on Sun StarOffice 5.1 for Linux, on my FreeBSD-current box. Because the last version of RealVideo for FreeBSD is version 3.0, I run RealVideo 5.0 for Linux.
In FreeBSD 3.3-stable or higher, Linux mode has two components: the kernel system call module, and the runtime environment.
The Linux Kernel Module
The kernel-loadable module, or KLM, supports Linux-specific system calls. You can determine whether the Linux KLM is loaded on your system by using:
#kldstat -v | grep linux
2 1 0xc08dc000 e000 linux.ko
15 linuxelf
16 linuxaout
#
(kldstat -v provides other useful information about your kernel, and is generally informative on any FreeBSD system.) If the module is not loaded, you can load it by becoming root and typing:
# linux
You can make this automatic at boot by setting linux_enable="YES" in /etc/rc.conf. Strictly speaking, you don't have to compile a custom kernel to support the Linux module. You'll be more satisfied with your results, however, if your kernel includes the following options:
options "P1003_1B" #POSIX infrastructure
options "_KPOSIX_PRIORITY_SCHEDULING" #Built-in POSIX priority scheduling
options "_KPOSIX_VERSION=199309L" #POSIX version kernel is built for
options SYSVSHM #System V shared memory
options SYSVMSG #System V semaphores
options SYSVSEM #System V messaging
options USER_LDT #allow user-level control of i386 ldt
For full instructions on building a custom kernel, check:
http://www.FreeBSD.org/handbook/handbook50.html
If you have the FreeBSD docs installed on your local system, see:
/usr/share/doc/handbook/handbook50.html
The kernel emulator does have a small overhead, but it is negligible. If you're interested in the details and don't feel like reading the source code, search for overhead in the freebsd-emulation mailing list archives (http://www.FreeBSD.org/search/).
Runtime Environment
Dynamically linked Linux binaries expect to have certain libraries available. Linux has a variety of library sets, such as libc4, libc5, and glibc2. Additionally, some Linux programs expect to have particular Linux userland programs available (sh, test, etc.) Because FreeBSD uses the same codebase as original UNIX, whereas Linux was written from scratch, these may not be 100% compatible. A selection of Linux userland utilities can also be helpful, especially when dealing with closed-source commercial software products.
The dynamic libraries and Linux userland utilities are available as a port. Look under /usr/ports/emulators/linux_base for the latest stable version. Install these as you would any other port:
#cd /usr/ports/emulators/linux_base
#make install
The port installs a subset of Red Hat Linux's userland under /compat/linux.
The original source files (or distfiles, in FreeBSD parlance) are quite large. You will probably want to load these from a FreeBSD CD, a Red Hat CD, or from behind a large Internet circuit. If you're downloading over a 56 Kb modem, I'd recommend doing this when you don't plan to use your 'Net connection for several hours. The linux_base port installs everything under /compat/linux. If you're curious, do an ls:
#ls
bin boot etc lib mnt proc sbin usr var
#
This looks suspiciously like a subset of the root directory. /compat/linux contains resources for Linux binaries that expect to find different resources than those available on a FreeBSD machine. While it's not chrooted, it's built in a similar manner.
For example, on a Linux machine the file /etc/nsswitch.conf contains vital configuration information. FreeBSD keeps that information elsewhere. Any program run under Linux mode will check /compat/linux/etc for a system config file, and then fall back to /etc.
Linux Distributions and Emulation
At the moment, FreeBSD's Linux mode is based on Red Hat 5.2. (RedHat 6.0 emulation is in -current, and should be backported to -stable by the time you read this.) This doesn't mean that you can't use software designed for other distributions, however.
Using a particular Red Hat distribution simply indicates the level of Linux kernel and userland library support available in your FreeBSD system. Red Hat 5.2 uses libc5, glibc2, and Linux kernel 2.0.36. RedHat 6.0 uses glibc2.1 and kernel 2.2.5. Linux software that will run under these libraries, and this kernel will work in FreeBSD's Linux mode.
Running Linux Apps
When you have the linux_base port installed, and kldstat -v shows the Linux module is running, just run your Linux program from a command prompt.
Adding Linux Libraries
FreeBSD's install routines and ports system take care of almost all the ugly details of configuring shared libraries, insulating the administrator from the majority of these issues. This system does not cover software from other operating systems running under kernel modules. Linux systems tend to accumulate shared libraries like pocket lint. Eventually, you'll need to add additional Linux shared libraries to your system.
The Linux emulator has its own ldconfig, /compat/linux/sbin/ldconfig, and its own ld.so.conf file, /compat/linux/etc/ld.so.conf. If you look at this file, you'll see:
#more ld.so.conf
/usr/X11R6/lib
/usr/i486-linux-libc5/lib
#
These are the directories where the module will look for Linux libraries, in addition to /lib and /usr/lib. Despite the initial slash, these directory names are all under /compat/linux. I recommend installing additional libraries under /compat/linux/usr/local/lib. Edit /compat/linux/etc/ldconfig.conf to add this path. When you have large programs that include many libraries of their own, you might wish to create new directories for those shared libraries. My /compat/linux/etc/ld.so.conf looks like:
#more ld.so.conf
/usr/local/lib
/usr/local/lib/soffice
/usr/X11R6/lib
/usr/i486-linux-libc5/lib
/usr/openwin/lib
#
Whenever you add new libraries, rerun /compat/linux/sbin/ldconfig. This rereads the directories in the configuration file and updates the list of available libraries. Be sure you run /compat/linux/sbin/ldconfig, and not FreeBSD's /sbin/ldconfig. Not only will the FreeBSD ldconfig not accomplish what you want, you'll find yourself either digging through /etc/rc.* scripts to learn how to properly reconfigure your shared library cache or simply rebooting in annoyance.
When a Linux program fails because it cannot find a library, check to be sure you have the library. If you don't have it, call up your favorite Internet search engine and fetch a copy. Install it in /compat/linux/usr/local/lib, run Linux ldconfig, and you'll be up and running.
Alternately, you can use the environment variable LD_LIBRARY_PATH to tell a shell to check a particular directory for libraries (see man 8 ldconfig). Many problems can be caused by the indiscriminate use of LD_LIBRARY_PATH, however, so you're better off editing /usr/compat/linux/etc/ld.so.conf.
Installing Linux Software as FreeBSD Ports
Some Linux software packages are available via FreeBSD's ports system. One well-known example is WordPerfect 8. If you become root, cd /usr/ports/editors/wordperfect, and type make, you will get:
#make
===> wordperfect-8.0 'The source to this port may not be automatically fetched
due to licensing restrictions. You MUST fetch the source manually after reading
and agreeing to the license at: http://linux.corel.com/linux8/download.htm
Once GUILG00.GZ has been downloaded, move it to
/usr/ports/distfiles and then restart this build.'.
#
In this case, FreeBSD's automatic fetch mechanism won't work -- WordPerfect's distribution terms require the user to agree to a license before downloading. Go to the Web page, accept their terms, and download WordPerfect. Install the file in /usr/ports/distfiles, return to /usr/ports/editors/wordperfect, and type make. The port will automatically install WordPerfect for you.
Linux Packages
One of the tools linux_base installs is Red Hat's RPM. You can use this to install RPMs on your FreeBSD machine. When using RPMs, you will want to be certain to install the software under /compat/linux. Because of FreeBSD's monolithic upgrade procedure (see my article Maintaining Patch Levels in Open-Source BSDs, Sys Admin, September 1999), any RPM components that overwrite system components will be blown away the next time you upgrade. If you overwrite the wrong files, your system might even become unbootable. To install an RPM, do:
rpm -i --ignoreos --dbpath /var/lib/rpm --root
/compat/linux [package]
Add other RPM flags as desired. Of course, RPM packages are completely separate from FreeBSD's usual package system. Do not expect to be able to pkg_delete these; you will need to use rpm to handle them.
Be very careful in your use of RPM; if you accidentally overwrite part of your FreeBSD installation with Linux binaries, you can expect to find yourself booting off CD-ROM to restore your system. (The Linux module is good, but not that good.) If you're using a Linux program built into a FreeBSD package, such as linux_glide, you can use the standard FreeBSD pkg_add pkgname.
Manual Installations
Even with FreeBSD's excellent package management, some software has to be installed the hard way, by hand. I'll use Sun's StarOffice 5.1 as an example. Rather than provide a specific set of instructions on how to install this particular application, we'll troubleshoot a typical closed-source, Linux-binary software installation.
You can download StarOffice at www.stardivision.com. A full download runs about 70 megs, so you might wish to spend the $10 for a CD-ROM. Copy the file so51a_lnx_01.tar to your home directory and untar it.
After decompressing, cd so51inst/bin and run ./setup. At this point, you might get a warning about /tmp being full. StarOffice is a huge office suite. If you run out of space, you will need to repoint your tmp directory. The easiest way to do this is with the environment variable $TMPDIR. Point TMPDIR at a directory with a lot of space, and try again.
You'll see:
#./setup
./setup: Window manager didn't set icon sizes - using default.
/tmp/sv001.tmp/setup.bin: error in loading shared libraries
libvos517li.so: cannot open shared object file: No such file or directory
#
This is a shared library problem, as described above. You can simply do:
#LD_LIBRARY_PATH=/tmp/sv001.tmp ; export LD_LIBRARY_PATH
Rerun the setup routine. This time, it will open a nice GUI window, ask you some questions, and install itself. After the install, look in Office51/bin under your home directory. Typing ./soffice& will bring up StarOffice. I use a simple shell script:
#!/bin/sh
/home/mwlucas/Office51/bin/soffice&
If you are interested in server applications, you should take a look at How to run Oracle for Linux on FreeBSD, available at:
http://www.scc.nl/~marcel/howto-oracle.html
Problems
Any number of things can go wrong with a software install, let alone one running on a platform it wasn't built for. Some programs will require branding before you can run them. Branding a binary leaves an ELF comment that the kernel uses to choose an ABI. brandelf programname will tell you which ABI a program thinks it needs. You can use brandelf -t linux programname to manually point a binary at the Linux module. (SysV binaries do not use branding.)
If the program complains about unsupported functions, check your kernel config. Do you have the POSIX and SysV options configured? Check kldstat -v to confirm the Linux module is loaded. If the above fails, your first resource should be the FreeBSD mailing list archives. Look at:
http://www.FreeBSD.org/search.html
Unless you're the first person in the world to have a problem with this piece of software, you'll find an archived question about it. The questions and emulation archives are excellent places to search.
If the mailing lists archives don't help, send a message to freebsd-questions@FreeBSD.org. Be sure to give the version of FreeBSD you have, the version of linux_base you're using, and any and all details about the program you're trying to run, including any error messages when you try to run it. If it's an obvious problem, someone will probably answer you quickly. For more difficult problems, you might be directed to the freebsd-emulation mailing list.
If you find yourself using Linux emulation more and more frequently, you would find it worthwhile to subscribe to the freebsd-emulation mailing list. Email majordomo@FreeBSD.org with a body of subscribe freebsd-emulation.
Conclusion
FreeBSD's Linux mode gives FreeBSD users access to a wide variety of commercial software packages. I encourage you to contact any software vendor that does not offer a FreeBSD version of a desired program and let them know you would purchase a native version. Vendors don't realize that demand exists unless someone tells them. Although good for your karma, these requests won't help you listen to espn.com's daily broadcasts on your computer. Combined with the SVR4 and SCO kernel modules, the Linux module gives you access to almost the entire range of UNIX software on your FreeBSD box.
About the Author
Michael Lucas is a networking and FreeBSD consultant working for the Great Lakes Technologies Group. He lives in Detroit, Michigan with his wife Liz, four gerbils, and assorted fish. He can be reached at: mwlucas@exceptionet.com.
The author wishes to thank Marcel Moolenar and Steven Arlow for their assistance with this article.
|