Create a virtual machine with QEMU

Source: Internet
Author: User
Tags bz2 using git git clone
QEMU is an efficient and practical simulator and virtual machine monitor, and this series of blogs is an attempt to introduce qemu in depth, starting with the most basic installation use. This article starts with QEMU's most basic knowledge, describes how QEMU is installed, the fundamentals of system emulation, and how to use QEMU to create and manage virtual machines. QEMU Virtualization First, Qemu introduction

QEMU is an open-source emulator and hypervisor (Virtual Machine Monitor, VMM). Qemu mainly provides two of features for users to use. First, as a user-state simulator, the dynamic code translation mechanism is used to execute code different from the host architecture. Second, as a virtual machine monitor, simulate the whole system, using other VMM (Xen, KVM, etc) to use the virtualization support provided by hardware, to create a virtual machine near the host performance.

Users can install QEMU through the Package Manager that is available from different Linux distributions. You can use the following command to install on the release version of the Debian series:

sudo apt-get install Qemu

or use the following command to install on the release of the Red Hat series:

sudo yum install qemu-y

In addition, you can also choose to install from the source code. get QEMU Source

You can download the QEMU source tar package from the QEMU Web site and download the 2.0 version of QEMU from the command line for example:

$wget http://wiki.qemu-project.org/download/qemu-2.0.0.tar.bz2
$tar xjvf qemu-2.0.0.tar.bz2

If you need to participate in QEMU development, it's best to use Git to get the source code:

$git Clone git://git.qemu-project.org/qemu.git compilation and Installation

After obtaining the source code, you can configure and compile qemu on demand.

$CD qemu-2.0.0//If you are using git to download the source code, perform CD qemu
$./configure--ENABLE-KVM--enable-debug--enable-vnc--enable-werror  --target-list= "X86_64-softmmu"
$make-j8
$sudo make install

The Configure script is used to generate makefile, and its options can be viewed with the./configure--help. The options used here have the following meanings:

--ENABLE-KVM: The KVM module is compiled so that QEMU can use KVM to access the virtualization services provided by the hardware.
--enable-vnc: VNC enabled.
--enalbe-werror: When compiling, all warnings are treated as errors.
--target-list: Select the schema of the target machine. By default, all schemas are compiled, but for faster compilation, the required schema is specified.
second, the Basic principles

When QEMU is a system emulator, it simulates a virtual machine that can run the operating system independently. As shown in the following illustration, each virtual machine corresponds to one QEMU process in the host (host), and the vcpu of the virtual machine corresponds to a thread of the QEMU process.

System virtualization is mostly virtual CPU, memory and I/O devices. Virtual out of the CPU called VCPU,QEMU in order to enhance efficiency, borrow KVM, Xen and other virtualization technology, direct use of hardware to virtualization support, on the Host Security Run Virtual machine code (requires hardware support). Virtual machine VCPU The process of calling a KVM interface to perform a task (the code originates from the technical blog of the QEMU developer Stefan):

Open ("/DEV/KVM")
ioctl (KVM_CREATE_VM)
IOCTL (KVM_CREATE_VCPU) for
(;;) {
     ioctl (kvm_run)
     switch (exit_reason) {case
     kvm_exit_io:/* ... */case
     kvm_exit_hlt:/* ... *
     }
}

QEMU initiates Ioctrl to invoke the KVM interface, while the KVM uses hardware extensions to run the virtual machine code directly above the host, and once the VCPU needs to operate the device register, VCPU will stop and return to Qemu,qemu to simulate the results of the operation.

The virtual machine memory is mapped to QEMU's process address space and is allocated at startup. In the virtual machine's view, the virtual address space on the host that QEMU allocates is the virtual machine's physical address space.

QEMU simulates the hardware device of the virtual machine in the host user state, the results of the VCPU to the hardware are simulated in the user state, such as the virtual machine needs to write data to the hard disk, the actual result is to write the data to a mirrored file in the host. Iii. Creating and using virtual machines command line to create and start virtual machines

After you successfully install QEMU, you can create your own virtual machines. The specific steps are as follows:

1, use QEMU-IMG to create virtual machine mirroring. Virtual machine mirroring is used to simulate the hard disk of a virtual machine, and you need to create a mirrored file before starting the virtual machine.

[Kelvin@kelvin tmp]$ qemu-img create-f qcow2 fedora.img 10G formatting
' fedora.img ', Fmt=qcow2 size=10737418240 enc Ryption=off cluster_size=65536 lazy_refcounts=off 
[kelvin@kelvin tmp]$ ls
fedora.img

The-F option specifies the format of the mirror, which is the most commonly used mirror format for QEMU and uses the write-time replication technology to optimize performance. Qcow2 The fedora.img is the name of the mirrored file, and the 10G is the mirrored file size. After the mirror file is created, you can use Qemu-system-x86 to start the virtual machine for the x86 schema:

Qemu-system-x86_64 fedora.img

A window pops up to act as a display for the virtual machine, which reads as follows:

Because the fedora.img does not have an operating system installed on the virtual machine, it prompts "no bootable device" and no bootable device.

2, prepare the operating system mirroring.

An installation image can be obtained from the official website of different Linux distributions, taking FEDORA20 as an example:

[Kelvin@kelvin tmp]$ wget Http://ftp6.sjtu.edu.cn/fedora/linux/releases/20/Live/x86_64/Fedora-Live-Desktop-x86_64-20-1.iso

3, check to see if the KVM is available.

QEMU uses KVM to increase virtual machine performance, which can cause performance loss if KVM is not enabled. To use KVM, first check to see if the hardware has virtualization support:

[Kelvin@kelvin ~]$ grep-e ' VMX|SVM '/proc/cpuinfo

If there is output, the hardware has virtualization support. Second, check to see if the KVM module has been loaded:

[Kelvin@kelvin ~]$ Lsmod | grep KVM
kvm_intel             142999  0 
KVM                   444314  1 Kvm_intel

If the KVM_INTEL/KVM_AMD, KVM module is displayed, the KVM module is already loaded. Finally make sure QEMU makes the KVM available at compile time, that is, the –ENABLE-KVM option is added when executing the Configure script.

4, start the virtual machine to install the operating system.

Execute the following command to start the virtual machine with CDROM:

[Kelvin@kelvin tmp]$ qemu-system-x86_64-m 2048-enable-kvm fedora.img-cdrom./fedora-live-desktop-x86_64-20-1.iso

-m specifies the virtual machine memory size, the default unit is MB,-ENABLE-KVM uses KVM for acceleration,-cdrom add Fedora installation image. You can operate the virtual machine in a pop-up window, install the operating system, and restart the virtual machine after the installation is completed from the hard drive (fedora.img). Before you start the virtual machine, you only need to perform:

[Kelvin@kelvin tmp]$ qemu-system-x86_64-m 2048-ENABLE-KVM fedora.img

Can. graphical interface to create and start virtual machines

Command line startup virtual machine is more cumbersome, suitable for developers, but for ordinary users, the use of graphical interface to manage virtual machines is more convenient. Using a graphical interface to manage QEMU virtual machines requires installation of Virt-manager, and the Red Hat series release is only required to execute commands:

$sudo Yum Install virt-manager-y

Start Virt-manager with Root when Setup is complete:

$su-
#virt-manager

The starting interface is shown in the following illustration:

Click on the computer icon in the upper left corner to create a virtual machine. Follow the steps to complete the creation of the virtual machine.

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.