KVM Virtualization (i) Introduction and simple use of--KVM virtual machines _KVM

Source: Internet
Author: User

I. Structure and INTRODUCTION

KVM (kernel-based Virtual Machine) It was developed by Quramnet, the company was acquired by Red Hat in 2008;

Integrated into the kernel from the Linux 2.6.20, the kernel module makes Linux become a hypervisor layer;

It relies on the CPU virtualization instruction set, performance, security, compatibility, stability performance is good, each virtualization operating system performance as a single system process, and Linux Security Module SELinux Security module is a good combination;

Official website for Http://www.linux-kvm.org/page/Main_Page

Second, the installation of KVM

(1) Before installing to see if the CPU supports virtualization, the following commands have output on behalf of the support virtualization; If you determine that the CPU can support virtualization, but the above command has no output, go to bash to load virtualization

Intel CPUs:

[root@localhost ~] #cat/proc/cpuinfo | grep VMX

AMD CPU:

[root@localhost ~] #cat/proc/cpuinfo | grep SMV
 [root@localhost ~]# modprobe KVM  #加载kvm模块

(2) Installation method

A: When installing the system, select Desktop installation, and then select the Virtualization option

Method II: On the basis of existing systems, the installation of KVM required software.

Must safety package:

qemu-kvm.x86_64//KVM Modules
qemu-img.x86_64//QEMU components, creating disks, starting virtual machines, etc.

Optional Kit:

Yum-y groupinstall "Desktop"//Install GNOME desktop Environment
Virt-manager//graphical interface management virtual machine tools
libvirt//graphical virtual machine management tools
Python-virtinst.noarch//python component, record the XML file
bridge-util.x86.64//Network support tool when creating VM, Bridge function module
libguestfs-tools  File management tools
acpid    //virsh command dependent services that need to boot up, he is a service, not a toolkit

(3) Verify the installation result

[root@localhost ~]# lsmod | grep KVM
kvm_intel 54285 0
KVM 333172 1 Kvm_intel

Third, KVM Network

1. Introduction of KVM Virtual Machine network

There are two ways to run a KVM network in Libvirt: Nat and Bridge, and the default is NAT

(1) NAT mode: User mode, which can access the extranet, but cannot access the virtual machine network from outside.

(2) Bridge mode: Bridging mode, this mode allows the virtual machine to be like a stand-alone host network, external machines can directly access to the internal virtual machine, but the need for network card support, general wired network card support. You also need to edit the network card parameter file to support bridging.
As shown in the figure, the two virtual machine network card corresponding to the host's two virtual network card, they will be called Br0 Network Bridge Connection, virtual machine network card and Network Bridge connection, you can get set and host a network segment of the IP address permissions, so you can access the network through the host to the extranet.

2. Set up KVM Bridge network

[Root@localhost network-scripts]# cat Ifcfg-eth0
device=eth0
hwaddr=00:0c:29:a6:73:18
TYPE=Ethernet
uuid=ea80305d-532a-4c31-883f-d9797e279e29
Onboot=yes
nm_controlled=no #关闭network Manager to control the network card
bootproto=none  #不需要给物理机设置ip地址
bridge= "Br0"    #设置eth0网卡支持网桥
[Root@localhost network-scripts]# VI ifcfg-br0
add:
device=br0
type=bridge
onboot=yes
Controlled=no
bootproto=none
ipaddr=192.168.1.10
netmask=255.255.255.0
[root@localhost ~]#/ Etc/init.d/networkmanager Stop #关掉网卡守护进程
 

Iv. managing virtual Machines

There are two ways to create a virtual machine, one based on the command line and one based on a graphical interface.

1. Graphical Interface Management

Virt-manager is based on libvirt virtual machine management software, different versions of the Virt-manager interface and operating methods may be different, he is the simplest way to create a KVM virtual machine. This method must be based on the packages we have previously installed Virt-manager and libvirt. There is not much elaboration here.

The steps for creating a virtual machine are as follows:

(1) Create a storage pool and create a storage volume, the so-called storage pool is actually a folder, the storage volume is a KVM virtual machine entity mirroring

(2) Create a mirrored storage pool for operating system mirroring, mirrored storage pool is the folder where the operating system mirrors

(3) Create the virtual machine and set the corresponding parameters

(4) The clock offset can be changed to "LocalTime"

(5) can set host boot time virtual machine

(6) can be set up through VNC remote management, KEYMAP option is set to copy local Keymap

2. Create virtual machines in command-line style

To create a virtual machine by using a command

--NAME Specifies the name of the KVM virtual machine
--ram Memory Size
--file the path to the disk file
--vcpus Specify the number of CPUs for a virtual machine
--FILE-SIZE=30 (the default unit is G) Set the hard disk size
--cdrom Optical Drive provides boot.iso mirroring
--location provides boot.iso mirroring locally
--network Network:default Set network card (use default)
--vnc--vncport=5911 VNC Port connected to desktop environment

[Root@localhost ~]# virt-install--name=benet--ram=2048--vcpus=1-- File-size=30--location=/tmp/rhel6.5.iso--force &

3. KVM Command Set

To use the Virsh command, you must open the Acpid service

Yum-y Install acpid
/etc/init.d/acpid start
chkconfig on

Virtual machine important Two files directory

/etc/libvirt/qemu/   //virtual Machine profile directory
/var/lib/libvirt/images///virtual machine disk File directory

Modify the configuration of a virtual machine

Vim/etc/libvirt/qemu/benet.xml//Modify virtual machine configuration information (to modify system memory size, disk file, etc.)
Virsh edit Benet      //Modify virtual machine configuration information via Virsh command

Basic commands

Virsh-h     //view commands help
Virsh list     //view the running virtual machine
Virsh list--all   //View all virtual machines
Virsh create/etc/ Libvirt/qemu/benet.xml//Boot the virtual machine system instance through
the profile Virsh start Benet   //Start, need to confirm Acpid service install and run
Virsh shutdown benet //  shutdown, you need to confirm that the Acpid service is installed and running
Virsh destroy Benet  //forced shutdown
virsh autostart benet//  with host hosts automatically started, this command will create /etc/libvirt/qemu/autostart/directory, the content of the contents of the system to boot automatically.
Virsh suspend Benet  //Hang
Virsh resume Benet   //restore
Virsh dumpxml > Benet Benet2.xml//Export virtual machine configuration
virsh undefine benet  //delete virtual machine, will find that the profile is purged, but the disk files will not be deleted
mv/etc/libvirt/qemu/ benet2.xml//etc/libvirt/qemu/benet.xml//change back up the configuration file to redefine the virtual machine
Virsh define Benet.xml  // Redefine the virtual machine (because the disk file has not been deleted)

4. KVM File Management

File management allows you to directly view, modify, and replicate the internal files of a virtual machine, for example, when a system cannot be started because of a configuration problem, you can directly modify the virtual machine's files. The virtual machine disk files have raw and qcow2 formats, the QCOW2 format is the standard format for KVM support, and the raw format is a common format for virtual disk files. KVM Virtual machine defaults to the RAW format, raw format performance, the fastest, the disadvantage is not to support some new features, such as mirroring, zlib disk compression, AES encryption, and other raw format files than the Qcow2 format file is much larger, nearly 15 times times it.

File management requires specialized tools to complete, and different format disks have different file management tools. Local Yum the command line tool that is generated after the Libguestfs-tools is installed (this tool can directly read the disk files in qcow2 format, so you need to convert the raw format disk files to the QCOW2 format)

To convert a disk format method:

Qemu-img info/var/lib/libvirt/images/benet.img # See Current Disk format
Virsh shutdown benet qemu-img convert-f raw-o qcow2
/ Var/lib/libvirt/images/benet.img/var/lib/libvirt/images/benet.qcow2 #转换磁盘文件格式, transformed after a new Qcow2 file, attention is added
Virsh Edit Benet #修改benet的xml配置文件 ...
//omitted portions
<disk type= ' file ' device= ' disk ' >
<driver ' qemu ' name= E= ' Qcow2 ' chche= ' None '/>
<source file= '/var/lib/libvirt/images/benet.qcow2 '/>

Other uses are as follows:

~]# virt-cat-a/var/lib/libvirt/images/benet.qcow2/etc/sysconfig/network #查看命令, similar to the Cat command, in the format: Virt-cat disk mirroring path File absolute path (the last thing to see is this network file)
networking=yes
hostname=benet
~]# virt-edit-a/var/lib/libvirt/images/              benet.qcow2/etc/resolv.conf #与vim用法基本一致
nameserver 8.8.8.8
~]# virt-df-h benet #用于查看虚拟机磁盘信息
filesystem Size Used Available use%
benet:/dev/sda1 484M 32M 427M    7%
Benet:/dev/volgroup/lv_root  7.4G    1.6G    5.4G    5.4G    22%

5, cloning and snapshots

[Root@localhost ~]# virt-clone-o benet-n benet2-f/var/lib/libvirt/images/benet2.img  //virtual machine cloning
[ Root@localhost ~]# virsh snapshot-create benet  //Create a new snapshot
Domain snapshot 1382572463-created
[root@localhost ~]# Virsh snapshot-delete benet 1382572463//delete snapshot
Domain snapshot 1382572463
[deleted root@localhost ~]# SN Apshot-revert benet 1382572463//restore virtual machine status
[root@localhost ~]# virsh snapshot-current benet   //View snapshot version number
[ Root@localhost ~]# virsh snapshot-list benet    //view snapshot information

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.