{"id":179,"date":"2001-01-23T18:33:15","date_gmt":"2001-01-24T02:33:15","guid":{"rendered":"http:\/\/www.jeffcarl.com\/?p=179"},"modified":"2020-07-08T19:04:32","modified_gmt":"2020-07-09T02:04:32","slug":"running-linux-programs-on-freebsd","status":"publish","type":"post","link":"https:\/\/www.jeffcarl.com\/index.php\/2001\/01\/23\/running-linux-programs-on-freebsd\/","title":{"rendered":"Running Linux Programs on FreeBSD"},"content":{"rendered":"\n<p class=\"has-medium-font-size\"><strong>By Jeffrey Carl<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignright\"><img loading=\"lazy\" decoding=\"async\" width=\"350\" height=\"109\" src=\"http:\/\/www.jeffcarl.com\/wp-content\/uploads\/2020\/04\/bwatch.gif\" alt=\"Boardwatch Magazine\" class=\"wp-image-22\"\/><figcaption>Boardwatch Magazine, January 2001<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"has-background has-light-gray-background-color\"><em>Boardwatch Magazine was the place to go for Internet Service Provider industry news, opinions and gossip for much of the 1990s. It was founded by the iconoclastic and opinionated Jack Rickard in the commercial Internet&#8217;s early days, and by the time I joined it had a niche following but an influential among ISPs, particularly for its annual ranking of Tier 1 ISPs and through the ISPcon tradeshow. Writing and speaking for Boardwatch was one of my fondest memories of the first dot-com age.<\/em><\/p>\n\n\n\n<p>Even though many server admins prefer BSD Unix, there\u2019s no denying that Linux is \u201cwhere it\u2019s at\u201d for third-party software development. So, what\u2019s a BSD admin to do?<\/p>\n\n\n\n<p>In the bygone misty past of Unix (in the era historians call \u201cthe early \u201890s\u201d), acceptance and market share were hampered by the diverging standards that forced application developers to write for only one version of Unix. This (as the youngsters today say) \u201creally sucked,\u201d and resulted in the Unix wars that left many syadmins hospitalized with &#8220;flame&#8221; wounds from heated Usenet exchanges. However, the tide has fortunately turned in favor of compatibility among the many *nixes.<\/p>\n\n\n\n<p>To commercial software vendors, market share is&nbsp;<em>everything<\/em>. As such, if *BSD forced Linux vendors to develop for their OSes, it would be like your average Unix sysadmin saying, \u201cIf Cindy Crawford wants to go out,&nbsp;<em>she<\/em>&nbsp;can call&nbsp;<em>me<\/em>.\u201d Fortunately, Jordan Hubbard of FreeBSD has indicated a prudent recognition that Linux\u2019s API is becoming the default for third party developers (Indpendent Software Vendors, or ISVs) to develop for free *nixes. Therefore, it&#8217;s better for BSD users to accept the Linux ABI (Application Binary Interface) rather than to force ISVs to choose between Linux and BSD. This, in my opinion, is a very pragmatic and wise attitude.<\/p>\n\n\n\n<p>FreeBSD, NetBSD and OpenBSD&nbsp;<em>all<\/em>&nbsp;include options to allow Linux binaries to run on their systems (at least for the Intel x86 architecture; on other architectures, your mileage may vary). For OpenBSD and NetBSD, you can check your system documentation or see their respective websites (<em>www.openbsd.org<\/em>&nbsp;and&nbsp;<em>www.netbsd.org<\/em>); for the purpose of this article, we\u2019ll look at Linux binary compatibility on FreeBSD for x86.&nbsp;<em>NOTE<\/em>: Much of the information on the inner workings of Linux compatibility came from information graciously provided by Terry Lambert (<em>terry@lambert.org<\/em>).<\/p>\n\n\n\n<p>Under FreeBSD, Linux binary compatibility is managed by creating a \u201cshadow\u201d Linux file system with the requisite programs and libraries, and re-routing the Linux program\u2019s system calls into this file system. These libraries then deal with a Linux kernel embedded into the FreeBSD kernel.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Setting Up Linux Compatibility<\/h3>\n\n\n\n<p>The development of an &#8220;executable class&#8221; loader for FreeBSD has been under development since 1994 (although when the project was started, Linux wasn&#8217;t one of the primary target OSes). There are three essential items that enable this functionality.<\/p>\n\n\n\n<p>The first is a KLD (\u201cKernel LoaDable\u201d) object called&nbsp;linux.ko, which is essentially a Linux kernel that can be loaded dynamically into the FreeBSD kernel. As a KLD, it can be loaded or unloaded without rebooting, using the&nbsp;kldload&nbsp;and&nbsp;kldunload&nbsp;commands. To check and see whether&nbsp;linux.ko&nbsp;is loaded properly on your system, use the&nbsp;kldstatcommand:<\/p>\n\n\n\n<p><strong>schnell#<\/strong>&nbsp;kldstat<\/p>\n\n\n\n<p>Id Refs Address&nbsp;&nbsp;&nbsp;&nbsp;Size&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name<\/p>\n\n\n\n<p>&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;4 0xc0100000 1d9a60&nbsp;&nbsp;&nbsp;kernel<\/p>\n\n\n\n<p>&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;1 0xc1038000 3000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;daemon_saver.ko<\/p>\n\n\n\n<p>&nbsp;3&nbsp;&nbsp;&nbsp;&nbsp;1 0xc1822000 4d000&nbsp;&nbsp;&nbsp;&nbsp;nfs.ko<\/p>\n\n\n\n<p>&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;1 0xc1020000 10000&nbsp;&nbsp;&nbsp;&nbsp;linux.ko<\/p>\n\n\n\n<p>The second necessary item is the set of basic Linux libraries (pre-existing code that developers can link their programs to, so they don&#8217;t have to write every function from scratch) and binaries (like&nbsp;bash,&nbsp;cp,&nbsp;rm, etc.). These can be installed via the ports collection from&nbsp;\/usr\/ports\/emulators\/linux_base, which is based on a minimal Red Hat installation. As of this writing in late September, for the i386 architecture, the installed libraries matched a Red Hat 6.1-1 release including&nbsp;glibc 2.1.2-11,&nbsp;libc 5.3.12-31,&nbsp;glib 1.2.5-1,&nbsp;ld.so 1.9.5-11,&nbsp;libstdc++-2.9.0-24,&nbsp;gdbm-1.8.0-2&nbsp;and&nbsp;XFree86 libs 3.3.5-3.&nbsp;<\/p>\n\n\n\n<p>To be able to use a wider range of Linux apps, you may also wish to install the Linux development tools, found in the ports collection at&nbsp;\/usr\/ports\/devel\/linux_devtools. Note that you&#8217;ll want to do this&nbsp;<em>after<\/em>&nbsp;installing&nbsp;linux_base, since&nbsp;linux_devtools&nbsp;requires&nbsp;rpm&nbsp;(the Red Hat Package Manager application, also available as a FreeBSD app through the ports collection) and&nbsp;\/compat\/linux\/etc\/redhat-release&nbsp;to be installed. This currently includes (also for i386)&nbsp;kernel-headers-2.2.12-20,&nbsp;glibc-devel-2.1.2-11,&nbsp;make-3.77-6,&nbsp;cpp-1.1.2-24,&nbsp;egcs&nbsp;(plus&nbsp;egcs-c++&nbsp;and&nbsp;egcs-g77)-1.1.2-24,&nbsp;gdb-4.18-4, and&nbsp;XFree86-devel-3.3.5-3.<\/p>\n\n\n\n<p>Also, any necessary binaries\/libraries can be set up manually by installing them into their original paths under the FreeBSD directory&nbsp;\/compat\/linux&nbsp;(e.g., install the Linux&nbsp;\/usr\/X11R6\/lib\/libX11.so.6.1&nbsp;library onto FreeBSD as&nbsp;\/compat\/linux\/usr\/X11R6\/lib\/ libX11.so.6.1). This process is also necessary for installing libraries that aren\u2019t part of the default ports collection sets.<\/p>\n\n\n\n<p>The third item is \u201cbranding\u201d Linux ELF (\u201cExecutable and Linking Format\u201d) binaries. Although the GNU toolchain now brands Linux ELF binaries with the OS name (so newer Linux binaries should work without branding), some older binaries may not include this information (although ELF has replaced&nbsp;a.out&nbsp;as the primary binary format since Linux kernel 2.x and FreeBSD 3.0). To do this, use the&nbsp;brandelf&nbsp;command (which sounds like a move from \u201cDungeons and Dragons\u201d to poke elves with hot sticks) with the \u201c-t\u201d (type) flag like:<\/p>\n\n\n\n<p><strong>schnell#<\/strong>&nbsp;brandelf -t Linux \/usr\/local\/games\/doom<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How Linux Compatibility Works<\/h3>\n\n\n\n<p>Originally, Unix supported only one \u201cbinary loader.\u201d When you ran a program, it examined the first few bytes of the file to see if it was a recognized binary type. If the file type that was attempted to being loaded and run wasn\u2019t the singular binary type that it recognized, it would pass the file in question to the basic shell interpreter (\/bin\/sh) and try to run the program with that. Failing that, it would spit a nasty message back to you about how you couldn&#8217;t run whatever it was that you were trying to run.<\/p>\n\n\n\n<p>Nowadays, FreeBSD supports a number of loaders, which include an ELF loader (by the way, Linux binaries can be stored on either a FreeBSD&nbsp;UFS&nbsp;or Linux&nbsp;ext2&nbsp;file system, assuming you have mounted the ext2 file system the app is located on using the&nbsp;mount -t ext2fs&nbsp;command). If the \u201cbrand\u201d is seen in the ELF file as \u201cLinux,\u201d it replaces a pointer in the \u201cproc\u201d structure of the binary to point automatically to&nbsp;\/compat\/linux\/, and the binary is executed using the Linux libraries and binaries, instead of the FreeBSD ones. If it isn\u2019t a FreeBSD or Linux binary, and the list of loaders (including those examining the first line for \u201c#!\u201d lines like Perl, Python or shell scripts) is exhausted without recognizing a \u201ctype,\u201d then it is attempted to be executed as a&nbsp;\/bin\/sh&nbsp;script. If this attempt to run is unsuccessful, it is spit back out as an \u201cunrecognized\u201d program.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Linux and BSD Binaries: Both \u201cNative?\u201d<\/h3>\n\n\n\n<p>If a binary or library is required by a Linux program that is not included under the Linux compatibility \u201carea,\u201d the FreeBSD tree is searched. Thus, a Linux application that is expected to run with the Linux version of&nbsp;\/compat\/linux\/usr\/lib\/libmenu.so&nbsp;\u2013 if it doesn\u2019t find it \u2013 is checked to see whether it can run under the FreeBSD&nbsp;\/usr\/lib\/libmenu.so. Only if a compatible library is not found in either the Linux-compatible \u201cre-rooted (\/compat\/linux\/whatever)\u201d filesystem or the regular FreeBSD libraries is an executable rejected.&nbsp;<\/p>\n\n\n\n<p>Because of this construction, it isn&#8217;t really accurate to say that Linux is \u201cemulated\u201d under FreeBSD, especially in the sense that, say, platforms are emulated under the MAME (<em>www.mame.net<\/em>) emulator, since it doesn\u2019t involve a program which impersonates another platform on a binary level and translates all of its native system calls into local system calls. Linux binaries are simply re-routed to a different set of libraries, which interface natively with a (Linux kernel) module plugged into the FreeBSD kernel. Rather than \u201cemulation,\u201d this is largely just the implementation of a new ABI into FreeBSD.<\/p>\n\n\n\n<p>Under the current implementation of binary compatibility, programs calling FreeBSD\u2019s&nbsp;glue()&nbsp;functions are statically linked to FreeBSD\u2019s libraries, and programs calling the Linux&nbsp;glue()&nbsp;function calls are statically linked to its own libraries, so their executions are dependent on completely different code libraries; however, the importance of&nbsp;glue()is on the wane. In the future, this might change, which would seriously open the question as to whether Linux apps under FreeBSD are any more \u201cnative\u201d than apps written specifically for FreeBSD are.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Running Linux Binaries on BSD<\/h3>\n\n\n\n<p>Currently, the major problem with running Linux binaries on FreeBSD is similar to the major problem with using new apps on Linux: discovering which libraries they require and downloading and installing them. Unfortunately for BSD users, the only certain way to do this right now is to have access to a Linux system which already has the app in question installed, and run the&nbsp;ldd&nbsp;command on the application to list the libraries needed. If you don&#8217;t have access to a Linux system that already has the program installed, your next best bet is to check info on the program&#8217;s website, or search for it at Freshmeat (<em>www.freshmeat.net<\/em>).<\/p>\n\n\n\n<p>Under current FreeBSD Linux binary emulation, most Linux binaries should work without problem. However, due to operating system differences, it&nbsp;<em>won\u2019t<\/em>&nbsp;support those that heavily use the Linux&nbsp;\/proc&nbsp;filesystem (which operates differently from the FreeBSD&nbsp;\/proc), ones that make i386-specific calls like enabling virtual 8086 mode, or ones that use a significant amount of Linux-specific assembly code. While some people have reported that some Linux binaries run faster in this BSD environment than they do natively in Linux, this is a highly subjective claim, and one that should be taken with a grain of salt (at least until some real standardized testing is done with it). While Linux binary emulation on FreeBSD shouldn\u2019t be considered as a major security risk, it is worth noting that a buffer overflow bug was discovered in August 2000 (<em><a href=\"http:\/\/www.securityfocus.com\/frames\/?content=\/vdb\/bottom.html%3Fvid%3D1628\">www.securityfocus.com\/frames\/?content=\/vdb\/bottom.html%3Fvid%3D1628<\/a><\/em>).<\/p>\n\n\n\n<p><strong>Popular Uses for Linux Compatibility<\/strong><\/p>\n\n\n\n<p>The most popular binaries for using Linux compatibility are the ISV applications that have been developed for Linux from popular commercial\/semi-commercial vendors. These include programs like Sun&#8217;s StarOffice, Wolfram&#8217;s Mathematica, Oracle 8, and games like Bungie&#8217;s Myth II and id Software&#8217;s Quake III (there are already tutorials on installing Oracle 8.0.5 and Mathematica 4.x included in the&nbsp;<em>FreeBSD Handbook<\/em>; see below for more).&nbsp;<\/p>\n\n\n\n<p>Other popular installations include the Netscape Navigator 4.6-7.x browser. Particular advantages to installing the Linux version (even though there is a FreeBSD version already) are that the Linux version under FreeBSD compatibility is reportedly more stable than the &#8220;native&#8221; FreeBSD version. Also, binaries for popular plug-ins (Flash 4, Real Player 7, etc.) are available, since &#8220;native&#8221; FreeBSD versions aren&#8217;t. Info on installing VMware for Linux on FreeBSD can be found at&nbsp;<em><a href=\"http:\/\/www.mindspring.com\/~vsilyaev\/vmware\/\">www.mindspring.com\/~vsilyaev\/vmware\/<\/a><\/em>.<\/p>\n\n\n\n<p><strong>Getting Help with Linux Compatibility<\/strong><\/p>\n\n\n\n<p>The primary reference on Linux binary emulation on FreeBSD is the \u201chandbook\u201d page at&nbsp;<em>www.freebsd.org\/handbook\/linuxemu.html<\/em>, including specific pages on Oracle (<em>www.freebsd.org\/handbook\/linuxemu-oracle.html<\/em>) and Mathematica (<em><a href=\"http:\/\/www.freebsd.org\/handbook\/linuxemu-mathematica.html\">www.freebsd.org\/handbook\/linuxemu-mathematica.html<\/a>)<\/em>. To keep up-to-date on Linux emulation under FreeBSD, subscribe to the mailing list&nbsp;freebsd-emulation (send e-mail to&nbsp;<em><a href=\"mailto:majordomo@freebsd.org\">majordomo@freebsd.org<\/a>&nbsp;<\/em>with the text [in the body]&nbsp;subscribe freebsd-emulation).<\/p>\n\n\n\n<p>Additional information on Linux compatibility (including instructions for older versions of FreeBSD), see<em>&nbsp;<a href=\"http:\/\/www.defcon1.org\/html\/Linux_mode\/linux_mode.html\">www.defcon1.org\/html\/Linux_mode\/linux_mode.html<\/a><\/em>. For a list of Linux applications that are known to run on FreeBSD under Linux binary compatibility, see&nbsp;<em>www.freebsd.org\/ports\/linux.html<\/em>. For an example of setting up Word Perfect 8 in Linux compatibility \u2013&nbsp;as well as an account of running Windows 2000 on top of VMware on top of Linux on top of FreeBSD, read the excellent article at BSD Today (<em><a href=\"http:\/\/www.bsdtoday.com\/2000\/August\/Features252.html\">www.bsdtoday.com\/2000\/August\/Features252.html<\/a><\/em>). You can find some (mildly outdated but still useful) instructions on installing StarOffice on FreeBSD at&nbsp;<em><a href=\"http:\/\/www.stat.duke.edu\/~sto\/StarOffice51a\/install.html\">www.stat.duke.edu\/~sto\/StarOffice51a\/install.html<\/a><\/em>. And, for fun, there\u2019s info on setting up an Unreal Tournament server on FreeBSD via Linux compatibility at&nbsp;<em><a href=\"http:\/\/www.doctorschwa.com\/ut\/freebsd_server.html\">www.doctorschwa.com\/ut\/freebsd_server.html<\/a><\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>By Jeffrey Carl Boardwatch Magazine was the place to go for Internet Service Provider industry news, opinions and gossip for much of the 1990s. It was founded by the iconoclastic and opinionated Jack Rickard in the commercial Internet&#8217;s early days, and by the time I joined it had a niche following but an influential among &hellip; <a href=\"https:\/\/www.jeffcarl.com\/index.php\/2001\/01\/23\/running-linux-programs-on-freebsd\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Running Linux Programs on FreeBSD<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":22,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,66],"tags":[29,30,27],"class_list":["post-179","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-boardwatch-writing","category-tech","tag-bsd-unix","tag-freebsd","tag-linux"],"jetpack_featured_media_url":"https:\/\/www.jeffcarl.com\/wp-content\/uploads\/2020\/04\/bwatch.gif","_links":{"self":[{"href":"https:\/\/www.jeffcarl.com\/index.php\/wp-json\/wp\/v2\/posts\/179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jeffcarl.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jeffcarl.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jeffcarl.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jeffcarl.com\/index.php\/wp-json\/wp\/v2\/comments?post=179"}],"version-history":[{"count":1,"href":"https:\/\/www.jeffcarl.com\/index.php\/wp-json\/wp\/v2\/posts\/179\/revisions"}],"predecessor-version":[{"id":180,"href":"https:\/\/www.jeffcarl.com\/index.php\/wp-json\/wp\/v2\/posts\/179\/revisions\/180"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jeffcarl.com\/index.php\/wp-json\/wp\/v2\/media\/22"}],"wp:attachment":[{"href":"https:\/\/www.jeffcarl.com\/index.php\/wp-json\/wp\/v2\/media?parent=179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jeffcarl.com\/index.php\/wp-json\/wp\/v2\/categories?post=179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jeffcarl.com\/index.php\/wp-json\/wp\/v2\/tags?post=179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}