Use qemu for System Simulation

Source: Internet
Author: User
Http://www.cnblogs.com/WuCountry/admin/EditPosts.aspx? Opt = 1

Use qemu for System Simulation

Machines in the Machine

Document options

Print this page

Level: elementary

M. Tim Jones (mtj@mtjones.com), consulting engineer, emulex

October 22, 2007

Qemu is an open source Simulator for a complete PC system. In addition to the simulation processor, qemu allows simulation of all necessary subsystems, such as network hardware and Video hardware. It also allows advanced conceptual simulation (such as Symmetric Multi-processing systems (up to 255 CPUs) and other processor architectures (such as arm and PowerPC ). This article will study qemu and its architecture, and show how to simulate the guest operating system on a Linux host.

What is qemu?

At present, simply calling virtualization a hot technology seems somewhat conservative. Search by GoogleVirtualizationAbout 22 million records can be obtained for one word. For example, in just one month, EMC announced its initial public offering for VMware, Citrix Systems announced its plan to buy xensource, and new virtualization startups emerged everywhere. In the huge market of virtualization products, various emerging small markets are emerging. However, when talking about initial public offerings and acquisitions of virtualization, it is easy to ignore some other existing Virtualization Technologies.

This article describes an interesting virtualization application.ProgramQemu is not currently a hot technology. The qemu application applies to various settings. It can be used to virtualize guest operating systems or run operating systems that use the host CPU or other CPU architecture as a complete machine simulator.



Back to Top

Introduction to Virtualization

First, we will briefly introduce virtualization and describe the background of qemu.

The virtualization mentioned in this article actually refersPlatform Virtualization. On physical hardware, the control program may be the host operating system or Management Program (see figure 1 ). In some cases, the host operating system is a management program. The guest operating system is located in the hypervisor. In some cases, the guest operating system and the control program use the same CPU, while in other cases, it may be different (for example, the PowerPC guest operating system runs on x86 hardware ).

Figure 1. Basic architecture of platform Virtualization

You can achieve virtualization in multiple ways, but there are three most common methods. The first type is calledLocal Virtualization (or full virtualization). In this virtualization, hypervisor achieves basic Isolation Elements, separating physical hardware from the guest operating system. This technology was first introduced in 1966 in IBM CP-40 virtual machines/virtual memory operating systems, and is also used by VMware ESX Server.

Another popular virtualization technology is calledSemi-Virtualization. In semi-virtualization, the control program implements the application interface (API) of the Management Program, which will be used by the guest operating system. Xen and Linux kernel-based Virtual Machine (KVM) both use semi-virtualization technology.

The third useful technology is calledSimulation. Simulation, as its name implies, virtualizes the guest platform by simulating a complete hardware environment. Simulation can be implemented in multiple ways, even in the same solution. The Virtualization Technologies through simulation include qemu and bochs.



Back to Top

Qemu Architecture

First, let's take a look at how qemu implements simulation. This section describes two qemu operation modes and some interesting features of the qemu dynamic translation program.

Basic qemu operations

Qemu supports two operation modes: User Mode Simulation and system mode simulation.User Mode SimulationAllows a process built by one CPU to be executed on another CPU (execute dynamic translation of host CPU commands and convert them to Linux calls accordingly ).System Mode SimulationAllows simulation of the entire system, including processors and peripheral devices.

Simulate x86 on an x86 host systemCodeUseQemu acceleratorSimilar local performance can be achieved. This allows us to directly execute the simulation code on the host CPU (run through the kernel module on Linux ).

However, from a technical perspective, qemu is interesting in its fast and portable dynamic translation program.Dynamic Translation ProgramAllows you to convert the commands used for the target (guest) CPU to the host CPU at runtime for simulation. This can be achieved through a forced method (ing commands from one CPU to another), but this is not always so simple. In some cases, according to the translated architecture, you may need to use multiple commands or behavior changes.

Qemu implements dynamic translation by converting the target commandMicro operation. These micro operations are some C code compiled into objects. Then build the core translation program. It maps target commands to micro operations for dynamic translation. This not only produces high efficiency, but also can be transplanted.

Qemu's dynamic translation program also caches translated code blocks to minimize the memory overhead of the translation program. When you use the target code block for the first time, translate the block and store itTranslated code block. Qemu caches recently used translated code blocks in a 16 MB block. Qemu can support self-Modification of code by changing translated code blocks into invalid ones in the cache.

For more internal details about qemu and its dynamic translation program, refer to the interesting article by Fabrice bellard (author of qemu) in reference material.Article.

Supported peripheral devices

Using qemu as a PC system simulator provides various peripheral devices. The required standard peripherals include hardware video graphics array (VGA) simulators, PS/2 mouse and keyboard, integrated development environment (IDE) hard drives and CD-ROM interfaces, and floppy disk simulation. In addition, qemu includes the ne2000 Peripheral Controller Interconnect (PCI) network adapter, serial port, a large number of sound cards and PCI universal Host Controller Interface (uhci) Universal Serial Bus (USB) simulation of the Controller (with virtual USB hub. Processor shortric multiprocessing (SMP) supports 255 CPUs.

In addition to simulating standard PC or isa pc (without PCI Bus), qemu can also simulate other non-PC hardware, such as arm versatile baseline Board (using 926e) and Malta million instructions per second (MIPs) board. Works properly for a variety of other platforms, including Power Macintosh G3 (blue & white) and Sun-4u platforms.



Back to Top

Build and install qemu

Building and installing qemu is as easy as using standard GNU tools. Download and open the qemu release,Configure,Make, And thenMake installThe task is completed (see Listing 1 ).

List 1. Build a qemu Simulator

$ Wget http://fabrice.bellard.free.fr/qemu/qemu-0.9.0.tar.gz$ tar xfvz qemu-0.9.0.tar.gz $ D qemu-0.9.0 $./configure $ make install $

This process not only creates executableQemuImages, And you can create a group of images for other architectures (including arm, MIPS, PowerPC, 68 K, and iSCSI. In this way, you can boot the Linux kernel built for different target architectures.

If the host operating system and guest operating system run on the same processor architecture, you can use the qemu accelerator (kqemu) to achieve similar local performance. Kqemu is a driver (Linux kernel module) that allows user-mode code and kernel code to be executed directly on the host CPU. The build qemu accelerator is the same as the build qemu itself (see Listing 2 ).

Listing 2. Building a qemu accelerator

$ Http://fabrice.bellard.free.fr/qemu/kqemu-1.3.0pre11.tar.gz$ tar xvfz kqemu-1.3.0pre11.tar.gz $ CD kqemu-1.3.0pre11 $./configure $ make install

You can compile and install kqemu on many operating systems, including Microsoft Windows, FreeBSD, and Linux. After creating a qemu accelerator, run the following command to install the accelerator in Linux:

$ Insmod kqemu. Ko $


Back to Top

Use qemu

Now let's take a look at how to use qemu to virtualize another machine with a typical desktop GNU/Linux environment. Simulating another machine is similar to processing a new computer. The first step is to install the operating system. The new computer must have space to install the operating system, so a hard disk is required.

Qemu provides a special command to create a hard disk. This command is calledQemu-img. This tool can create images in various formats, but the best format (Qemu) Is calledQcow(OrQemuCopy when writing ). The advantage of this format is that the size of the disk image is different from the size of the physical file that represents the image. In other words, this format allows for more compact disk images. For example, an empty 4 GB disk image only requires 16 KB space.

ForQemu-img, You need to provide the operation type (CreateCreate a new disk image), format (QcowUsedQemuImage Format), size, and disk image name. In this example, a real-name computer is used for a micro-Linux Release Used in flash. Therefore, the 128 MB disk image is created as follows:

$ Qemu-IMG create-F qcow disk. IMG 128 mformating 'disk. IMG ', FMT = qcow, size = 131072 kb $

Note: If you plan to install a common operating system, such as Windows, Linux, or FreeBSD, a larger disk space is required. The result of this operation is that a disk. IMG file is displayed during simulation. The format is 128 MB.

Now you have created a hard disk. you can install a new operating system on it. For demonstration purposes, I will use a small Linux release cflinux. Cflinux is used as a small embedded system based on Linux. The system should be applicable to gateways, wireless entry points, firewalls or routers. You can useWgetDownload the ISO release:

Wget ftp://ftp.cflinux.fu/pub/cflinux/iso/cflinux-1.0.iso

An ISO image is a common CD-ROM format (known elsewhere as an ISO 9660 File System ).

Now that you have simulated the hard disk (disk. IMG) and CD-ROM, you can install the operating system on it. The next step is to install the operating system on the hard disk. Easy to useQemuTo complete this task:

$ Qemu-hda disk. IMG-CDROM/root/cflinux-1.0.iso-boot d $

UseQemuWhen you useHdaSpecifies the hard disk image.CDROMSpecifies the CDROM (file where the ISO image is located ).BootOption specifies the boot from CD-ROM. ParametersDSpecifies to boot from the CD-ROM, whereASpecify boot from a floppy disk,CSpecify boot from the hard disk (default), whileNSpecify to boot from the network. After this command is issued, a new qemu window indicating the simulated Machine Appears (see figure 2 ).

Figure 2. Install cflinux on a simulation disk using qemu

Follow the installation instructions and follow the CD-ROM installation to complete the ISO installation on the simulation hard disk. The installer requires you to restart. In this case, you can terminate the simulation (QemuPress ctrl-C in the window ). You can use the following command to guide the latest installed operating system:

$ Qemu-hda disk. IMG $

This command only indicates that the hard disk simulation standard PC is represented by the disk. IMG image file (default option ). Linux images boot from the simulated hard disk, resulting in a qemu window, as shown in 3.

Figure 3. boot the latest cflinux installation from the simulated Hard Disk

This is simple. In fact, you can install and boot any type of Operating System (Linux product release, windows, or other) in the same order ).



Back to Top

Other Simulators

Although qemu is an excellent simulation environment, it is worth studying in other environments. Wine is an open-source implementation of Windows APIs. It allows you to run Windows programs without a Windows operating system. However, as the wine acronyms indicate,Wine is not a simulator. On the contrary, wine implements a set of APIs that allow applications that run the X86 architecture. Therefore, applications running on wine can be executed well.

The simulator similar to qemu is bochs. Bochs is a machine simulator that can simulate not only intel's i386, i386, Pentium, Pentium Pro, and Advanced Micro Devices amd64 CPUs, but also common PC peripherals, such as disks, memory, monitors, and network devices. Bochs has been used to simulate Linux, DOS, and Windows 95/98/XP/2000/NT operating systems.



Back to Top

Conclusion

Using qemu as a machine simulator allows you to experiment with various operating systems, because you may not have additional machines to perform the test directly. Reactos is an example of an open-source Windows XP compatible operating system (as shown in Figure 4 ). Reactos is designed to be binary compatible with Windows XP, so you can directly run applications built for Windows XP on reactos. For more information about application compatibility, see references.

Figure 4. reactos simulation standard PC

You can find reactos and qemu images for many other operating systems at free operating systems zoo (see references for more details ). These images include live CD images, floppy disk images, or hard disk images (QcowFormat ). Qemu is a good way to try a new operating system, and it does not take time to install it.

References

Learning

    • For more information, see the original article on the developerworks global site.

    • View "qemu, a fast and portable dynamic translator" (PDF) of Fabrice bellard to learn the internal details of qemu dynamic translation.
    • Learn about other Linux virtualization options in "virtual Linux" (developerworks, December 2006.
    • "Explore Linux kernel virtual machines" (developerworks, April 2007) introduced the Linux KVM architecture and why its close integration with the kernel can change the way Linux is used.
    • In the developerworks Linux area, find more resources for Linux developers and view the most popular articles and tutorials.
    • View All Linux tips and Linux tutorials on developerworks.
    • Stay tuned to developerworks technical events and network broadcasts.

Obtain products and technologies

    • Download the qemu open source processor simulator and qemu accelerator from the Fabrice bellard web site. On this site, you can also find the current status of documents, APIs, and qemu.

    • For more information about cflinux, the author uses this small release to demonstrate how to install the operating system on qemu machines.
    • Get pre-packaged images from free operating systems zoo to save time. On this site, you will find many operating system images, from the standard Linux release to other rare operating systems (Plan 9, opensolaris, minux, reactos, Darwin, menuetos, etc) yes. After downloading a disk image, you will have a disk image that can start to boot.
    • Reactos is an open-source windows Binary-compatible operating system that allows you to run multiple Windows applications.
    • Wine allows Windows applications to be executed on non-native Windows operating systems (such as Linux.
    • Bochs is similar to qemu because it provides complete system simulation.
    • Order SEK for Linux. These two dvds contain the latest IBM trial software on the Linux platform, including DB2, Lotus, rational, Tivoli, and websphere.
    • Use the IBM trial software that can be downloaded directly from developerworks to build your next Linux development project.

Discussion

    • Join developerworksCommunityTo participate in blogs, forums, podcast, and community topics in the new developerworks space.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.