One, what is KVM
KVM includes many parts: first, it is a Linux kernel module (now included in the mainline) used to convert the processor into a new user (Guset) mode. User mode has its own set of ring states, but the command of privileged RING0 is caught in the code of the manager (hypervisor). Since this is a new processor execution model, the code does not require any changes. In addition to processor state transitions, this kernel module also handles a small subset of low-level simulations, such as the MMU registration (for managing VMS) and part of the PCI simulation hardware. For the foreseeable future, the QEMU team focuses on hardware emulation and portability, while the KVM team focuses on the kernel modules (KVM will move a small portion of the analog code in if certain parts do have a performance boost) and interact with the rest of the user space code. The Kvm-qemu executable is like normal qemu: allocates RAM, loads code, differs from recompiling or calls calling Kqemu, it creates a thread (which is important), the thread calls the KVM kernel module to switch to user mode, and executes the VM code. When a privileged command is encountered, it switches from the KVM kernel module, which, when needed, handles most of the hardware emulation like the QEMU line Cheng signal. One of the more ingenious parts of this architecture is that the client code is modeled on a POSIX thread, which allows you to use the usual Linux tools to manage. If you need a virtual machine with 2 or 4 cores, Kvm-qemu creates 2 or 4 threads, and each thread calls the KVM kernel module and starts executing. Concurrency (if you have enough real cores) or scheduling (if you don't care) is a generic Linux scheduler, this makes KVM code very small when working together, KVM manages CPU and mem access, QEMU emulation hardware resources (HDD, sound card, USB, And so on) when QEMU is running alone, QEMU simulates both the CPU and the hardware.
Second, the KVM architecture
The KVM basic structure consists of 2 parts:
The KVM driver is now a module of the Linux kernel. It is responsible for the creation of virtual machines, the allocation of virtual memory, the reading and writing of Vcpus registers, and the operation of Vcpus.
Another component is QEMU, which simulates the user-space components of a virtual machine, provides an I/O device model, and accesses peripherals.
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7A/51/wKiom1anbZXQ-MYYAADARdvbDoU668.png "title=" KVM Basic structure Flowchart "alt=" Wkiom1anbzxq-myyaadardvbdou668.png "/>
Third, how KVM works
Overview of how KVM works:
User-mode QEMU uses LIBKVM to enter kernel mode through the IOCTL, and the KVM module does not create virtual memory for virtual machines, and the virtual CPU executes the Vmlauch instruction into customer mode. Load the guest OS and execute. If the guest OS has an external interrupt or a shadow page table that is missing, the guest OS will be paused, the customer mode travel exception handled, and then re-entered into customer mode to execute the customer code. If an I/O event occurs or a signal arrives in the signal queue, it enters user mode processing. such as
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7A/50/wKioL1anbITBOK8RAAFSyoiGZ9k918.png "title=" KVM working principle "alt=" wkiol1anbitbok8raafsyoigz9k918.png "/>
Iv. manual installation of the KVM configuration
1. Pre-installation     1.CPU support Virtualization (INTER-VT, amd-v) [[email protected] ~]# grep --color ' SVM|VMX|LM ' /proc/cpuinfo Related CPU function flags include: svm= Security virtual Machine (AMD-V) vmx= Virtual machine x86 (INTER-VT) lm= long mode (64-bit support) 2. BIOS turn on CPU virtualization support 1) restart 2) press DELETE key 3) Enter BIOS on 2. Install virtualization (Yum configuration complete) # yum -y install kvm python-virtinst libvirt bridge-utils virt-manager qemu-kvm-tools virt-viewer virt-v2v Qemu-kvm tunctl[[email protected] ~] #vim /etc/sysconfig/selinux //off selinuxselinux= Disabled3. Start libvirtd[[email protected] ~]# /etc/init.d/libvirtd start //Boot [[ Email protected] ~]# ps -e|grep libvirtd //see if [[email protected] ~] #chkconfig libvirtd on comments: Open forever! Otherwise, the system will not start automatically after the ~# virsh iface-bridge eth0 br0 Note: Create a network bridge! Verify: ifconfig br0 Note: see if the IP address has run to the BR0 NIC! 4. Configure the network Bridge [email protected] network-script]# vi ifcfg-eth0 device=eth0type= ethernetonboot=yesbootproto=nonebridge=br0[[email protected] network-scripts]# vi ifcfg-br0device=br0type=bridgeonboot=yesbootproto=staticipaddr=192.168.0.100gateway=192.168.0.1netmask= 255.255.255.0dns1=192.168.0.1delay=05. Restarting the network [[Email protected]]# service networkmanager stop //this off. Stop NetworkManager Daemon: [OK] Note: Why turn it off ~ because it conflicts with the KVM Bridge adapter! #chkconfig NetworkManager off Comments: Permanently turn off the service!!! Otherwise, the system will be restarted after the service opened the Yo ~[[email protected] rules.d]# service network Restart6. Using Virt-install to generate an. img file (parameter meaning has been commented on in the previous KVM first experience) # virt-install --name=ubuntu1 --ram 1024 --vcpus=1 --disk path=/root/ubuntu1.img,size=10 --accelerate --cdrom / Root/redhat6.4.iso --graphics vnc,port=5920 --network bridge=br0[[email protected] ~]# vi /etc/libvirt/qemu.conf vnc_listen = "0.0.0.0" user = "root" //Remove Annotations group = "root" //remove annotations Dynamic_ ownership = 0 //Remove the comment and change the 1 to 07. To start the virtual machine virtual machine using method:virsh destroy Virtual machine name # Turn off virtual machine virsh undefine virtual machine name &NBsp; #删除虚拟机virsh start Virtual machine name #开启虚拟机virsh list --all Show all virtual machine virsh console virtual machine name #连接虚拟机
The manual installation is now ready to be operated on the command line.
V. Automated scripting installation of KVM
#!/bin/bashecho "[1] Configure Yum" echo "[2] install KVM Tool" echo "[3] set Bridge" echo "[4] Manually install virtual machine "echo " [5] View virtual machine "echo " [6] turn on Virtual machine "echo " [7] shut down virtual machine "echo " [8] Connect virtual machine "echo " [9] automatically install virtual machine "echo " [0] exit "read -p " type: " numif [ $NUM = 0 ];thenexit;elif [ $NUM = 1 ];then# configuration yum rm -rf /etc/yum.repos.d/*;cat > /etc/yum.repos.d/yum.repo << EOF[yum]name=yumenabled=1gpgcheck=0baseurl=ftp://192.168.0.200/rhel6.4EOFelif [ $NUM = 2 ];then# Installing KVM Tools LANG=en yum groupinstall " virtualization* " -y;elif [ $NUM = 3 ];then# set Bridge chkconfig NetworkManager off; chkconfig network on; Service networkmanager stop; service network start; yum install " Bridge-utils " -y; service libvirtd restart; chkconfig libvirtd on; virsh iface-bridge eth0 br0;elif [ $NUM = 4 ];then# Install the virtual machine read -p Enter the name of the virtual machine: name read -p "input virtual and hard disk size (G): " SIZE read -p "Input virtual machine memory size (M): " MEM ping -c 1 192.168.0.200 > /dev/null if [ $? -ne 0 ];then echo "Unable to connect 192.168.0.200, please check the network!" "; exit; fi virt-install -- nographics -n $NAME --os-type=linux --os-variant=rhel6 -r $MEM &nbsP;--arch=x86_64 --vcpus=1 --disk path=/var/lib/libvirt/images/$NAME, size= $SIZE, Format=qcow2 -w bridge=br0 -l ftp://172.16.8.100/rhel6.4 -x "&NBSP;CONSOLE=TTYS0";elif [ $NUM = 5 ];then# View virtual machine virsh list --allelif [ $NUM = 6 ];then# boot read -p "virtual machine name: " XNAMEvirsh start $XNAME;elif [ $NUM = 7 ];then# Close read -p "virtual machine name: " xname &> /dev/nullvirsh destroy $XNAME;elif [ $NUM = 8 ];then# connect virtual machine read -p "Virtual machine name: XNAMEvirsh console $XNAME;elif [ $NUM = 9 ];then# automatically install the virtual machine read -p "Enter the name of the virtual machine:" NAME read -p "enter the virtual and hard disk size ( G): " SIZE read -p " Enter the virtual machine memory size (M): " mem&Nbsp; ping -c 1 192.168.0.200 > /dev/null if [ $? -ne 0 ];then echo "Unable to connect 192.168.0.200, please check the network!" "; exit; fi virt-install --nographics -n $NAME -- os-type=linux --os-variant=rhel6 -r $MEM --arch=x86_64 --vcpus=1 --disk Path=/var/lib/libvirt/images/$NAME, size= $SIZE, format=qcow2 -w bridge=br0 -l ftp:// 192.168.0.200/rhel6.4 -x "KS=FTP://192.168.0.200/RHEL6.4.KS&NBSP;CONSOLE=TTYS0";elseecho "Please enter: 0 ~ Number! "; Fi
Note: This script has basically implemented the basic functions of the KVM Automation installation.
Summarize:
to praise, to seek diffusion ...
In fact, the installation and use of KVM is very convenient and simple, we have to understand the meaning of the various parameters of KVM. The key is the KVM Network Bridge settings, but now KVM in some aspects still have a certain flaw (such as the creation of optical drive to shut down, etc.), I hope that in the subsequent version of the improvements, here you have to look at the official software itself, the document will be a great help.
This article is from the "sub-non-fish" blog, please be sure to keep this source http://5323197.blog.51cto.com/5313197/1738805
KVM Experience-Manual and automated installation of KVM scripts