Installing the Software
[email protected] ~]# Yum grouplist
Virtualization
Virtualization Client
Virtualization Platform
Virtualization Tools
Centos6.4-minimal-64bit default is 200 packages
[email protected] ~]# Yum install libvirt python-virtinst QEMU-KVM
Transaction Summary
=============================================================================================================== ======================================
Install Package (s)
Upgrade 4 Package (s)
Total Download size:36 M
Is this OK [y/n]: Y
[email protected] ~]# Yum install Tigervnc-server
Transaction Summary
=============================================================================================================== ======================================
Install Package (s)
Total Download size:24 M
Installed size:70 M
Is this OK [y/n]: Y
[email protected] kvm]# Yum install Virt-viewer
Transaction Summary
=============================================================================================================== ======================================
Install Package (s)
Upgrade 2 package (s)
Total Download size:8.2 M
Is this OK [y/n]:
[Email protected] ~]# lsmod |grep KVM
Kvm_intel 53484 0
KVM 316506 1 Kvm_intel
[Email protected] ~]# Vi/etc/selinux/config
Change to Selinux=disabled
[email protected] ~]# reboot
[Email protected] ~]# Getenforce
Disabled
[[Email protected] ~]# service iptables stop
[[Email protected] ~]# service LIBVIRTD start
Starting LIBVIRTD Daemon:
[Email protected] ~]# RPM-QL Libvirt
[Email protected] ~]# RPM-QL python-virtinst
Virt-install-provision (preparation, preparation) new virtual machines
[Email protected] networks]# Cat/etc/libvirt/qemu/networks/default.xml
[Email protected] ~]# vi/etc/libvirt/qemu.conf
the relationship between Qemu and KVM :
Virtualization, which is now described, is generally referred to as virtualization technology based on CPU hardware support. KVM relies on this technology as well as Hyper-V and Xen. Without the support of CPU hardware virtualization, KVM is not working.
To be exact, KVM is a module of Linux. You can use Modprobe to load the KVM module. After the module is loaded, you can further create the virtual machine from another tool. But only the KVM module is not enough, because the user cannot directly control the kernel module to do things:
You must also have a tool for user space. This user-space tool, the developer chooses the already formed open source virtualization software QEMU. It says QEMU is also a virtualization software. It is characterized by a virtual different CPU.
For example, a power CPU can be virtualized on a x86 CPU, and it can be used to compile programs that can run on power. KVM uses a subset of QEMU and is transformed into a user-space tool that can control KVM.
So you'll see that the official KVM download has two major three files, namely the KVM module, the Qemu tool, and the collection. That is, you can upgrade only the KVM module, or you can upgrade only the Qemu tool. This is the relationship between KVM and QEMU.
Nic Bridging
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, where you have to look at the software's own documentation, it will be a great help.
Note that the KVM kernel module is reloaded after the system restarts: Of course, you can also modify the system-related settings to automatically load the module at startup. Is KVM much more cool than VB and VMS?
KVM's default network mode is NAT, that is, with the use of host mode to surf the Internet, now we want to change to bridge mode, so that the outside world can directly communicate with the virtual machine in the host.
[[Email protected] ~] #vi/etc/sysconfig/network-scripts/ifcfg-eth0
Device=eth0
Onboot=yes
Type=ethernet
Bridge=br0
ipaddr=192.168.1.187
netmask=255.255.255.0
When our installation is complete, there is no configuration file about the Br0 NIC, copy the eth0 and rename it to Br0
Cp/etc/sysconfig/network-scripts/ifcfg-eth0/etc/sysconfig/network-scripts/ifcfg-br0 Br0 below four items
Device=br0
Bootproto=static
Onboot=yes
Type=bridge
Save NIC Configuration Restart NIC
[[Email protected] ~] #service Network restart
Check if BR0 is successful
[[Email protected] ~] #ifconfig
In fact, when we build the virtual machine, we finally care about the network card is: eth0, Br0, Vnet0,vnet1 ... Among them, our eth0 is the original physical network card, BR0 is our virtual bridge device, and Vnetxxx is the bridge map to the virtual machine inside the network
Card. When the configuration is finished, we use Ifconfig, see, Eth0 is no IP address, and br0 as if it is a virtual out of the original eth0 access interface, it has an IP address, can replace the original eth0 is accessed, and VNETXX address can be in
After the virtual machine is established, it is configured inside the virtual machine and can be accessed externally after bridging.
[Email protected] ~]# brctl?
[Email protected] ~]# Brctl showmacs br0
[Email protected] ~]# Brctl Show
Bridge name Bridge ID STP enabled interfaces
Br0 8000.4487fc5bf0b0 No eth0
Virbr0 8000.525400a64f66 Yes Virbr0-nic
The system default build interface is mainly used for NAT, if you want to delete can ifconfig virbr0 down interface First, then use Brctl delbr virbr0 to remove
Of course, sometimes we may not only use a network card in the virtual machine, but in the new network card when there will be unrecognized situation, how we respond, the following record a method, perhaps can help us solve the problem oh.
[[Email protected] ~] #vi/etc/udev/rules.d/70-persistent-net.rules
# PCI Device 0x10ec:0x8168 (r8169) This is a new network card
subsystem== "NET", action== "Add", drivers== "? *", attr{address}== "c8:61:00:4f:cd:76", attr{type}== "1", kernel== "eth*" , name= "Eth1"
# PCI Device 0x10ec:0x8169 (r8169) This is the old NIC.
subsystem== "NET", action== "Add", drivers== "? *", attr{address}== "00:23:56:9a:e8:5c", attr{type}== "1", kernel== "eth*" , name= "Eth0"
Delete the old eth0 configuration information, then change the new eth1 to eth0, then go to/etc/sysconfig/network-script/Modify the Ifcfg-eth0 configuration file, change the MAC address (changed to the new network card), Then restart the network or reboot the system configuration to take effect.
Virtual Machine connection and view do not know how to connect when the virtual machine is installed, Win7 install the VNC client to connect to the guest created by the host, but a flash, is not the host must install the desktop environment to connect to view
There are several ways to connect a KVM virtual machine, and the following is how the guest system is installed and then connected
I now know that there are 3 methods:
1,SSH: If the virtual machine network is not configured, only the following two methods can be used.
2,VNC: can use Virt-viewer, or Vncviewer command, but virt-viewer I encountered an error, eventually did not resolve.
Unable to connect to the graphic server localhost:5900
3, connect via serial console. Here's what you do in guest
Http://nixcraft.com/kernel-based-virtual-machine/15075-kvm-virsh-redirect-centos-redhat-console-serial-port.html
1) echo "Ttys0″>>/etc/securetty
2) Edit the/boot/grub/grub.conf file at the end of the kernel by adding the following:
Console=ttys0
3) Edit the/etc/inittab file at the end of the add:
S0:12345:respawn:/sbin/agetty ttyS0 115200
4) Restart the KVM virtual machine, then execute the following command sequentially on the host:
Desktop # Virsh--connect qemu:///system
Virsh # console CentOS
5) Enter the user name password to login.
By default, QEMU launches a VNC server port (as in the above:: 1:5,900), which can be connected to the QEMU VNC port by using the Vncviwer tool to view the client machine.
Here you can use the "vncviewer:5900" command to connect to the QEMU-initiated window
Steps
KVM supports a large number of images, often the original image (*.img), and the QOCW2 format (preferred) that supports dynamic size expansion.
The better choice is the system disk such as the C drive in the IMG format, the data disk in QCOW2 format to reduce the server disk idle space.
Qemu-img create-f Raw Test.img 10G
Create a qcow2 image, but the actual disk size is only about 193K, and the virtual machine internal display disk size is 10G, that is, disk space is allocated only when the use of the so-called dynamic expansion.
Qemu-img create-f qcow2 Ubuntu.qcow2 10G
To run a virtual machine on a host typically requires three steps:
First step: Create a virtual machine image
Qemu-img create-f Raw/images/vm1.raw 8G
The image created by qmeu-img is a sparse file, which means that the file you just created does not have 8G, and it increases slowly as the data grows, until 8G
Step Two: Create the guest system (three ways)
Step Three: Manage guest system (startup, shutdown, etc.)
Virtual Machine creation
The more detailed http://www.open-open.com/lib/view/open1346511443506.html speaking
For users or administrators, the creation of virtual machines has many methods, such as: KVM comes with command-line tools, uses the Virsh command to create, uses Virt-manager with graphical interfaces, and so on.
But the underlying implementations are the same, and they are basically developed through the Open Source virtualization library Libvirt. Let's talk about three ways users can create virtual machines.
Host called host, virtual machine called guest
1.1 Creating with the KVM command-line tool
DD If=/dev/zero of=rhel6u3.img bs=1m\ count=8192 You can also create a hard disk file with this command the same as the qemu-img create command effect
[Email protected] ~]#/USR/LIBEXEC/QEMU-KVM--help
[Email protected] ~]# qemu-img--help
[Email protected] ~]# qemu-img create-f Raw test.img 10G
[Email protected] ~]# qemu-img create-f qcow2 centos65-64.qcow2 10G
[Email protected] ~]#/usr/libexec/qemu-kvm-boot Order=dc-hda centos65-64.qcow2-cdrom centos-6.5-x86_64-minimal.iso -M 512-SMP 2-vnc:0 Installation system
[[email protected] ~]#/usr/libexec/qemu-kvm-boot C-HDA test.img-m 512 boot system
1.2 creating with the Virsh command-line tool
Virt-install is a command-line tool for building a new virtual machine using the "Libvirt" hypervisor Management Library, which uses a serial console, an SDL (Simple DirectMedia Layer) graphic, or a VNC client/server.
Support for command-line and graphical installations.
Write an XML configuration file, as described below
Virsh Create Test.xml
Creating a virtual machine with the Virsh tool you must write an XML configuration file that specifies the parameters of the virtual machine, such as the name of the virtual machine, the location of the disk image, the size of the memory, the display configuration, and so on. An example of a simple configuration file is given below.
#test. xml
<domain type= ' Qemu ' >
<name>windowsXP</name>
<uuid></uuid>
<memory>500000</memory>
<currentMemory>500000</currentMemory>
<vcpu>1</vcpu>
<os>
<type arch= ' i686 ' machine= ' pc ' >hvm</type>
<boot dev= ' HD '/>
<boot dev= ' cdrom '/>
</os>
<devices>
<emulator>/usr/bin/qemu-system-x86_64</emulator>
<disk type= ' file ' device= ' cdrom ' >
<sourcefile= '/home/turnupthesun/kvm/windowsxp.iso '/>
<target dev= ' hdc '/>
<readonly/>
</disk>
<disk type= ' file ' device= ' disk ' >
<sourcefile= '/home/turnupthesun/kvm/windowsxp.img '/>
<target dev= ' Hda '/>
</disk>
<graphicstype= ' vnc ' port= ' listen= ' 127.0.0.1 '/>
</devices>
</domain>
Here are a few of the more important elements and attributes.
(1) The type attribute of the <domain> element specifies the virtual Machine Manager running the domain, and "qemu" should be selected for KVM.
(2) The contents of the <name> element specify the name of the field.
(3) The contents of the <memory> elements and <currentMemory> elements do not specify the maximum memory allocated for the domain at startup and the actual allocated memory.
(4) The content between the <os></os> elements is used to specify some information that the operating system initiates. The duplicate <boot> elements form a boot order table, such as starting from disk in the example, the disk does not start and then starts from the CD.
(5) The device property of the <disk> element indicates the location of these devices by the file property of the,<source> tag of the different devices.
[Email protected] ~]# Virsh
Welcome to Virsh, the virtualization interactive terminal.
Type: ' Help ' for help with commands
' Quit ' to quit
Virsh #
[Email protected] ~]# virsh-c qemu:///system list
Virt-install-n CentOS .... Installing virtual machines
Virt-viewer-c qemu:///system CENTOSVNC Remote to Virtual machine installation system
If the error does not connect to the virtual machine, install Vncviewer
Vncviewer 192.168.2.8
Yum-y Install Dacpid Installing the virtual machine system installs the ACPID, otherwise cannot shut down the virtual machine through the Virsh installation
Virsh-c Qemu:///system
Virsh # list--all
Virsh # Start CentOS
Virsh # shutdown CentOS gracefully shuts down the virtual machine command (cannot shut down the virtual machine with shutdown if the virtual machine does not have the DACPI command installed)
Virsh # Destroy CentOS forced shutdown
# virt-clone--connect=qemu:///system-o centos-n kvm1-f centos1.qcow2virt-clone command clone virtual machine
1, the KVM virtual machine cannot be shutdown shut down through Virsh interaction because the KVM virtual machine does not have Acpid installed.
2,vnc problem, virt-viewer error, can only use Vncviewer 127.0.0.1来 connect the virtual machine.
Virsh List//view virtual machine information
Virsh destroy Kvmtest1//virsh destroy Id/name deleting a virtual machine
Virsh start kvmtest1//virsh start name launch virtual machine
Virsh console kvmtest1//virsh console id/name console connection virtual machine
Using CTRL +] to switch to host hosts, host hosts use Virsh console id/name to switch to the guest system.
The virtual machine configuration file is saved in the/etc/libvert/qemu directory, the extension is XML, this file can only be edited with Virsh edit with VI invalid, modify the corresponding file can reconfigure the virtual machine.
[[email protected] kvm]# virt-install-n centos65-r--disk path=/root/kvm/centos65-64.img-c CentOS-6.5-x86 _64-minimal.iso--network bridge=br0--graphics vnc,password=123456,port=5910
virt-install-v-N kvmtest1-r 512– Vcpus=1-l http://mirrors.163.com/centos/6.3/os/x86_64/-nographics-virt-type=kvm-f/home/vmimages/kvmtest1.img-s 10-w bridge:br0-w bridge:br1-extra-args= ' console=tty0 console=ttys0,115200,n,8 ks=http://192.168.xx.xx/test1.cfg '- Connect Qemu:///system
-W Networking mode (Birdge bridge:br0/nat bridge:virbr0)
-os-type= ' windows '-os-variant=win2k3 Install Windows It is best to add this otherwise will error, we notice a problem, if you are virtual WIN2003, then do not use model=e1000 in the-net parameter, otherwise the host and guest cannot ping through
After the Virt-install tool installs the virtual machine, the XML configuration file is generated under directory/etc/libvirt/qemu/
1.3 How to create a virtual machine from a graphical interface Virt-manager
Virt-manager is an open source virtualization management software shared by Redhat, which is a GUI program written in Python, and the underlying uses Libvirt to manage various hypervisor.
Virt-manger is both a virtual Machine Manager and a graphical tool for creating and managing virtual clients. The specific steps are:
A. Start the tool from the Console window, enter the Virt-manager command from root, and click on the "new" option in the File menu.
B.virt-manager shows two ways to virtualize: QEMU/KVM or Xen, where QEMU/KVM is chosen as hypervisor.
C. Select the virtual machine name and specify an installation method, either through the network installation server or local CD/DVD drive including the local ISO file, where I use the local ISO installation method.
D. Enter the local ISO file path and file name (assuming that the local ISO path is in the root directory, the name is Mini-bt3.6.1.iso)
E. Set the memory capacity and number of processors used by the virtual machine.
F. Configure the storage method for the virtual machine. There are two options for the storage backend: a physical storage device or a previously created disk file. If you are in a simple test, create the file as the storage backend. When you create a virtual disk, the default is 10GB.
G. Network configuration, select the NAT mode here.
Such a virtual machine will start to start up, the start interface appears, and finally the interface of the operating system in the virtual machine.
Virtual machine Backup and recovery
Backup: Copy the XML configuration file under the/etc/libvirt/qemu directory, as well as the IMG file set in the XML.
Recovery: Enter Virsh, execute define