Sometimes a single operating system just isn't enough. However impractical, many users keep multiple computers on their desk, sometimes dedicating each computer to a very specific, solitary task. But wasting hardware isn't always necessary. Computers are very flexible machines -- flexible enough that one computer can emulate another. Emulation allows a physical computer to pretend to be another one.
Sometimes a single operating system just isn’t enough. However impractical, many users keep multiple computers on their desk, sometimes dedicating each computer to a very specific, solitary task. But wasting hardware isn’t always necessary. Computers are very flexible machines — flexible enough that one computer can emulate another. Emulation allows a physical computer to pretend to be another one.
Several different types of emulators exist. Some of these, such as WINE, emulate only operating system calls. (Some people prefer not to call such packages emulators, hence the expansion of the WINE acronym to mean WINE Is Not an Emulator.) The most complete form of emulation is one in which the entire computing environment, including the CPU, hard disk, memory, and so on, is emulated. Such CPU emulators are very powerful, but they can be slow — in most cases, the emulator must perform multiple CPU operations to perform each operation of the emulated CPU. However, if your CPU is substantially faster than the target system, though, this problem is largely moot. CPU emulators typically run the target’s original operating system in the emulator.
Why run an emulator? For several reasons.
* TO RUN NON-NATIVE PROGRAMS. If no Linux program exists to do precisely what you want, running an emulator may be your only option. For example, you might need to run Microsoft Office. A vibrant community also exists around emulators for older computers (such as the Apple II and Amiga) and game systems (such as the Atari 2600), giving users access to nostalgic games and programs.
* TO TEST NEW OPERATING SYSTEMS OR OPERATING SYSTEMS INSTALLATIONS. Perhaps you need to test a new OS (say, the most recent version of your Linux distribution), but you lack the hardware to devote to a test machine. Using an emulator lets you to check out that new release without repartitioning your hard disk or wiping out your working installation.
* TO TEST NETWORK OPERATIONS. If you don’t have ready access to a large network, you can nonetheless experiment with networking features by using an emulator. Each emulated computer can function as a node on a virtual network that exists entirely on a single computer.
* TO SAVE SPACE AND MONEY. If you lack the funds or the real estate for a new computer, an emulator may be just the ticket. By emulating one computer on another, you can keep your costs and space demands down, while still benefitting from the flexibility of having multiple operating systems.
* TO DEVELOP CUTTING-EDGE SOFTWARE. Before releasing a new CPU, manufacturers often use emulators to learn about its features. Sometimes, these emulators are released publicly, or at least to a limited number of people, to enable outsiders to develop software for the new CPU. A great deal of development surrounding the new 64-bit CPUs from Intel and AMD has been done with the help of emulators running on older CPUs.
Of course, not every emulator is well-suited for each of these purposes. You wouldn’t get far using an Atari 2600 emulator for testing AMD Opteron software, for instance.
The rest of this column is devoted to Bochs, an emulator that’s capable of handling the first four tasks, although it’s not ideal for all purposes.
A common emulator for Linux is Bochs (available online at http://bochs.sourceforge.net). Bochs is a CPU emulator that implements x86 instructions — specifically, it emulates i386, i486, and Pentium CPUs. Once running, Bochs can boot and run many different x86 OSs, including Linux and Windows. The fact that it’s a CPU emulator, though, makes Bochs quite sluggish: programs running under Bochs may appear to run at perhaps a tenth of their normal, native speed.
On the other hand, the fact that it’s a CPU emulator makes Bochs ideal for Linux users who need to run x86 programs on a non-x86 CPU, such as those who run Linux on Alpha or PowerPC, but who need to run Word or other Windows applications on occasion.
Even using Bochs on an x86 system has its advantages. For instance, you can use Bochs to create screen shots of an OS installation process — something that’s usually difficult or impossible, short of using a camera to take a photograph of the computer’s screen.
Before investing considerable time and effort into installing Bochs and its guest operating system, you should be aware of some of the alternatives:
* Plex86, headquartered at http://plex86.sourceforge.net, has recently undergone a major transformation. Earlier versions were similar to Bochs, but didn’t emulate the CPU. This made those versions of Plex86 faster than Bochs, but similar in capabilities when run on x86 systems. However, those earlier Plex86 systems were unreliable. More recent versions of Plex86 are slimmer and more focused on running the Linux kernel in user mode, even though this makes Plex86 less useful for cross-OS emulation.
* Commercial application VMware (see http://www.vmware.com) is a machine emulator that omits the CPU emulation. Nonetheless, VMware can run many different x86 operating systems. It requires custom kernel patches, which can make synchronizing it with your kernel difficult if you regularly update your Linux kernel.
* Another commercial emulator is Win4Lin, distributed by Netraverse (http://www.netraverse.com). This emulator is conceptually similar to VMware, but is faster and less expensive. Like VMware, it requires custom kernel patches. On the downside, it only runs Windows 95, Windows 98, and Windows Me.
* The popular program WINE, available at http://www.winehq.com, is an effort to implement the Windows Application Program Interface (API) on Linux, effectively enabling Linux to run Windows programs. As such, WINE doesn’t require a copy of the OS you intend to run, unlike most other emulators. WINE can run Windows programs very efficiently and almost seamlessly with Linux programs, but its success rate is low — most Windows programs exhibit quirks or ultimately crash. Both CodeWeavers (http://www.codeweavers.com) and Transgaming (http://www.transgaming.com) distribute commercial derivatives of WINE aimed at running specific types of programs.
Although VMware, Win4Lin, and perhaps even WINE, may be more suitable than Bochs for many common purposes, Bochs has the advantage of running on more hardware and being less finicky than many of its alternatives.
For example, Bochs requires no kernel patches, and it runs most software for the host OS, assuming that software doesn’t need access to exotic hardware that Bochs doesn’t emulate. Nonetheless, Bochs can be a sensitive package, and getting it working may require avoiding certain pitfalls.
The Bochs web site includes copies of the emulator in precompiled RPM form for x86 platforms, source RPMs, and source tarballs. The binary RPM bochs-2.0.2.i386.rpm and a Red Hat 9.0 system was used to develop this article.
In addition to downloading and installing the Bochs emulator itself, you may want to obtain one or more disk images. Bochs uses ordinary files as stand-ins for hard disks. Therefore, you can obtain a file that contains a pre-installed OS for use with Bochs. Several such images are available from the Bochs web site, but of course the selection is limited to open source operating systems.
You’ll need to set aside space for your disk images. You can store these images in a convenient system directory, such as /usr/local/bochs, or you can use a directory in your home directory. For most purposes, you must have full read/write access to the disk file as the user who’ll be running Bochs, so be sure to set permissions accordingly.
If you don’t download a disk image, you’ll need to create one. You can do this with the bximage program, which ships with Bochs. This program asks you a few questions about the image you want to create — whether it’s a floppy or hard disk, its size, and its filename. The program concludes by displaying a line that you should include in the Bochs configuration file to enable the emulator to use the disk image. Be sure to copy this information to a safe place if you haven’t delved into the configuration file yet. Once you’re through configuring Bochs, it will see the file as a completely blank disk — it won’t be partitioned or contain filesystems, just like a new, store-bought hard disk.
Bochs relies on a configuration file, which is typically stored in your home directory as .bochsrc. A sample file should come with the Bochs package, perhaps located in /usr/share/doc/ bochs/bochsrc-sample.txt. Copy this file to ~/.bochsrc and edit it. Most of the items are well commented and show several sample options, so take some time reading through them. Some features that deserve some extra attention are:
* romimage. As a CPU emulator, Bochs emulates the entire computer, including its Basic Input/Output System (BIOS). Bochs therefore relies upon the presence of a BIOS in a file, which you point to with this option. The package ships with several BIOSes (stored in /usr/share/ bochs). Each BIOS is customized for emulating a system with a specific number of CPUs. You may need to adjust the path to the BIOS file, but the default BIOS file should work.
* vgaromimage. This option works much like romimage, but it sets the location of the ROM for the emulated video card. Bochs 2.02 ships with two different VGA ROMs. Chances are either will work, but you may need to adjust the path. (The images are usually stored in the same directory as the BIOS images.)
* floppya and floppyb. These options tell Bochs how to handle floppy disks. You can give the emulator direct access to your real floppy disk or feed it a disk image (say, created from a real floppy using dd or created using bximage). It’s best to provide the complete path to the floppy disk image file, if you use a disk image. The status option on this line tells Bochs whether to boot up with the virtual floppy disk inserted in its drive or removed. You can change this setting while running Bochs.
* DIRECT ATA ACCESS. Options headed ata0, ata1, and so on, identify ATA (hard disk) interfaces. Note that these options enable direct access to the hard disk controller, not to emulated hardware. The primary reason to enable such access is to give the emulator the ability to control a CD-ROM drive. The default options work well on most systems, but you may need to enable ata1 if your CD-ROM drive is on your second ATA chain.
* HARD DISK IMAGES. Options called ata0-master, ata0-slave, and so on identify hard disk images, such as the one you created with bximage or downloaded from the Bochs site. You may need to modify the path to the file from what bximage specified, particularly if you move the file after creating it. This same type of option can point to a CD-ROM device, such as /dev/cdrom, although the syntax is slightly different. (The default file provides examples of each type of syntax.) Earlier versions of Bochs used diskc, diskd, cdromd, and so on, but the syntax for these earlier options was different.
* boot. Specify the boot device (floppy, disk, or cdrom) with this option. Be sure it’s set appropriately for installing your OS. You may need to change this option after installing the OS.
* ips. Some computer operations are timing-sensitive, so Bochs must know how fast your CPU is. The sample configuration file provides several examples in millions of instructions per second (MIPS), so you should be able to locate an appropriate value. Note, though, that you specify this value in instructions per second, not in MIPS, so you must multiply the sample value by 1,000,000. Although the value isn’t extremely sensitive, setting it too far out can prevent some OSs from booting or cause other problems.
The .bochsrc file contains other options, but most of these are self-explanatory or relate to particularly exotic features. You might or might not find these features important, so you should peruse the default configuration file before proceeding further.
Once you’ve set up the configuration file to your satisfaction, prepared a hard disk image, and set all the files in the locations specified in .bochsrc, you can try running the bochs emulator.
|Figure One: Bochs enables you to run another operating system in a Linux window|
In an xterm or similar window, type bochs. The program displays a menu of options that enable you to load a new configuration file, alter configuration options, and so on. One of these options, #5, enables you to start the emulator. Type 5 and press the Enter key to start Bochs running. If all goes well, a window with Bochs will open. For instance, Figure One shows Bochs running FreeDOS, one of the prepared disk images from the Bochs site. (The web site also offers disk images for NetBSD, Debian, and OpenBSD.)
Once Bochs is running, the strip along the top of the window provides options and tools to modify the program’s emulated hardware. Some of these features may seem a bit tricky at first and deserve some elaboration.
* REMOVABLE DISKS. Inserting or removing removable disks can be frustrating if you don’t know how. The three leftmost icons on the icon bar enable you to activate or deactivate the floppy disks and CD-ROM. An icon with an “X” through it (as in the case of all the disk icons in Figure One) denotes a deactivated disk. To change disks, you may need to click the disk icon to deactivate a disk, remove the disk, try to access the disk in the emulator, insert a disk, click the icon to reactivate it, and then try accessing it again. Sometimes you can omit some of these steps, though.
* MOUSE. When you activate the mouse, Bochs takes over control of the mouse, which can make it hard to switch between other programs. Clicking the middle button toggles the mouse on and off, enabling you to work with other programs along with Bochs.
* SNAPSHOTS. Click the Snapshot icon to create a dump of the contents of a text display to a text file (snapshot.txt). If you want to create a screen shot of a graphics mode, you can use Linux graphics utilities to capture a screen shot of Bochs, including its icon bar and window widgets. If desired, you can then trim the image to the emulated OS alone. The GIMP is an excellent, if big, program that can handle this job.
* CONFIGURATION. Clicking the Config icon brings up a configuration tool (typically in the console in which Bochs is running). You can adjust various options normally set in the configuration file, tweaking them as necessary for the software you’re running.
Bochs isn’t without its limitations, of course. As already noted, it’s quite slow compared to running programs directly, although, of course, running x86 programs directly isn’t possible if you’re using a non-x86 host computer.
Bochs is also limited in its video mode: by default, it provides VGA (640×480) resolution at best, which can be quite limiting.
Nonetheless, for some purposes Bochs can be extremely useful. You can run multiple operating systems on one computer, test a new OS or OS version without disturbing your working configuration, run x86 operating systems on non-x86 hardware, emulate networking, and so on. The Bochs web site offers a great deal of documentation, including many tips and techniques for common tasks, like making disk images and running multiple operating systems. Need emulation? Think inside the Bochs.
Roderick W. Smith is the author or co-author of eleven books, including
Advanced Linux Networking and
Linux Power Tools. He can be reached at email@example.com.