How to get started with kvm and how to get started with kvm
Kvm is a typical Implementation of virtualization technology. It is very powerful and easy to use. Kvm is mainly used to virtualize the CPU. The memory and IO virtualization uses the open-source software qemu, which is a pure software-level virtualization and is actually a simulator. Kvm requires that the cpu must support hardware virtualization, which can be viewed using Linux commands.
Speaking of kvm, libvirt assembly must be mentioned. It is used to manage kvm virtual machines. Of course, it can also manage other virtual machines such as xen. Libvirt includes three parts: (1 ). libmongod is a background service program; (2 ). libvirt is an API interface for managing virtual machines. You can use python c java and other languages to write programs to manage virtual machines. A typical virt-manager is a visual tool written in python. (3 ). virsh and other command line management tools.
I. Preparations
1. experiment environment
Operating System: CentOS 6.4 x86_64 mini
HOST: vmware workstation Virtual Machine
2. Check whether the host machine processor supports virtualization.
[root@kvm ~]# egrep -o 'vmx | svm' /proc/cpuinfo | wc -l
If the value is 0, the CPU does not support virtualization.
3. Configure or set the host
CPU: 2-4 core enables cpu Virtualization (bios setting or vmware setting)
Memory: 4-8 GB
Hard Disk: 100 GB
4. Disable iptables and selinux.
Disable the iptables service:
[root@kvm ~]# service iptables stop[root@kvm ~]# chkconfig iptables off
Disable selinux:
[root@kvm ~]# setenforce 0[root@kvm ~]# vi /etc/selinux/configSELINUX=disabled
Ii. install and configure the kvm Environment
1. Install kvm virtualization software packages
[root@kvm ~]# yum install -y kvm virt-* libvirt bridge-utils qemu-img
2. Check whether the kvm module is loaded to the kernel.
[root@kvm ~]# lsmod | grep kvm_intelkvm_intel 53484 0kvm 316506 1 kvm_intel
If not, run modprobe kvm_intel. If not, restart the host machine.
3. Set related networks
Method 1: Bridge Mode.
[Root @ kvm ~] # Cd/etc/sysconfig/network-scripts/[root @ kvm ~] # Cp ifcfg-eth0 ifcfg-br0 [root @ kvm ~] # Vi ifcfg-eth0DEVICE = eth0TYPE = EthernetONBOOT = yesBOOTPROTO = noneBRIDGE = br0 [root @ kvm ~] # Vi ifcfg-br0DEVICE = br0TYPE = BridgeONBOOT = yesBOOTPROTO = staticIPADDR = 172.16.1.8PREFIX = 24 GATEWAY = 172.16.1.1DNS1 = 114.114.114.114NAME = br0 # restart the network [root @ kvm ~] # Service network restart
Method 2: NAT mode.
Temporarily omitted.
4. Start libmongod-related services
[root@kvm ~]# /etc/init.d/libvirtd start[root@kvm ~]# /etc/init.d/messagebus restart
Encountered error:
[Root @ vip ~] #/Etc/init. d/libmongod startlibmongod: relocation error: libmongod:... version... libdevmapper. so.1.02 [failure]
Solution:
[root@kvm ~]# yum upgrade device-mapper-libs
Result:
[root@kvm ~]# brctl showbridge name bridge id STP enabled interfacesbr0 8000.000c29181c75 no eth0virbr0 8000.525400c207c7 yes virbr0-nic
3. Install virtual machines
1. Create a virtual machine image
There are many types of VM images: raw, qcow2, and vmdk. We recommend that you use qcow2 images because qcow2 images support snapshots and are widely used. Before creating a virtual machine, you must manually create an image disk file in qcow2 format for use when installing the virtual machine. Run the following command to create an instance:
qemu-img create -f qcow2 -o preallocation=metadata /data/kvm/liwei01.qcow2 50G
2. Execute Virtual Machine Installation
Installation Method 1: Install using a network image, text console, without vnc support.
virt-install --name liwei01 --ram 1024 --vcpus 1 \ -f /data/kvm/liwei01.qcow2 --os-type linux \ --os-variant rhel6 --network bridge=br0 \ --graphics none --console pty,target_type=serial \ --location 'http://mirrors.163.com/centos/6.8/os/i386/' \ --extra-args 'console=ttyS0,115200n8 serial'
Installation Method 2: vnc is supported through network images. By default, no text console is available.
virt-install --name liwei01 --ram 1024 --vcpus 1 \ -f /data/kvm/liwei01.qcow2 --os-type linux \ --os-variant rhel6 --network bridge=br0 \ --graphics vnc,listen=0.0.0.0,port=5920 \ --location 'http://mirrors.163.com/centos/6.8/os/i386/'
Installation Method 3: Local installation using an iso image, vnc supported, and no text console.
virt-install --name liwei01 --ram 1024 --vcpus 1 \ -f /data/kvm/liwei01.qcow2 --os-type linux \ --os-variant rhel6 --network bridge=br0 \ --cdrom CentOS-6.8-i386-minimal.iso \ --graphics vnc,listen=0.0.0.0,port=5920
Installation Method 4: quick installation (copy) using the basic image Template)
Create an image file:
[Root @ kvm ~] # Qemu-img create-f qcow2/data/kvm/liwei. qcow2 50G
# Use liwei. qcow2 to install the VM... installation is complete.
[Root @ kvm ~] # Cp/data/kvm/liwei. qcow2/data/kvm/liwei01.qcow2
Installation command:
# Use the copied liwei01.qcow2 template for installation. The installation method is to start from the liwei01.qcow2 image.
[Root @ kvm ~] # Virt-install -- name liwei01 -- ram 1024 -- vcpus = 1 \ -- disk/data/kvm/liwei01.qcow2, format = qcow2, bus = virtio \ -- network bridge = br0 -- graphics vnc, listen = 0.0.0.0, port = 5904 \ -- boot hd
Note:
This method is not specified when creating an img ImagePreallocation = metadataIn this way, the storage file space is relatively small for convenient copying. If this option is not added, you need-- DiskAddBus = virtioIf it seems that the disk space cannot be identified when the operating system is not installed, the system will prompt that the disk space is insufficient. The installation speed is very fast. In fact, starting a virtual machine from an existing operating system image and deconfiguring a new Virtual Machine liwei01, you can use scripts to quickly create multiple virtual machines with the same configuration. Of course, you can install the public software package in the basic image and set the same configuration, so that the subsequent virtual machines installed based on this img have similar configurations, saving the trouble of repeatedly installing the software package.
Installation Method 5: quick installation (Sharing) using basic image templates)
Create an image:
[Root @ kvm ~] # Qemu-img create-f qcow2-o preallocation = metadata/data/kvm/liwei. qcow2 50G
# Use liwei. qcow2 to install the VM... installation is complete.
# Create a liwei01.qcow2 image using the liwei. qcow2 image Template
[Root @ kvm ~] # Qemu-img create-f qcow2-o backing_file = liwei. qcow2 liwei01.qcow2 10G
Installation command:
[root@kvm ~]# virt-install --name liwei01 --ram 1024 --vcpus=1 \ --disk /data/kvm/liwei01.qcow2,format=qcow2,bus=virtio \ --network bridge=br0 --graphics vnc,listen=0.0.0.0,port=5904 \ --boot hd
Note:
Backing_file = liwei. qcow2 indicates liwei. qcow2 is a back-end image. In the future, all write operations on the Virtual Machine liwei01 will be recorded in the liwei01 image. The actual operating system is in liwei. in the qcow2 image, liwei. qcow2 images are read-only. That is to say, all the backend VMS using the liwei. qcow2 image will share this image, and the write operation content of a specific VM must be recorded in the corresponding image file. Note the difference with method 4.
3. system installation through vnc or text Console
Method 1: You can use the text console to manage and install virsh console liwei01 to log on to and manage virtual machines later.
Method 2: connect through the vnc client. The virsh vncdisplay liwei01: 20 client connects through url: 172.16.1.8: 20.
Method 3: similar to method 2, the installation process is the same as that of a common operating system.
Iv. Common basic operation commands for kvm virtual machines
Boot: virsh start vm
Shutdown: If the virsh shutdown vm does not take effect, run: yum install-y acpid In the vm.
Strong: virsh destroy vm
Delete: virsh undefine vm
Definition: virsh define vm
Pending: virsh suspend vm
Recovery: virsh resume vm
Virtual Machine list: virsh list
Contains the shut-down VM: virsh list -- all
Set automatic start: virsh autostart vm
Disable Automatic Start: virsh autostart -- disable vm
Log on to the vm console: virsh console vm # it only works for virtual machines with the specified console. method 1
Log out of the VM Console: ctrl +]
V. Cloning of virtual machines
Clone Virtual Machine liwei01 to virtual machine liwei02
[root@kvm ~]# virt-clone --original liwei01 --name liwei02 --file /data/kvm/liwei02.qcow2
Note: Shut down the virtual machine before cloning. After cloning, you need to set the network of the virtual machine.
6. Create a VM Snapshot
1. Conditions for creating a snapshot
- The Virtual Machine is shut down.
- The Virtual Machine image format is qcow2.
2. Create a snapshot
[root@kvm ~]# virsh snapshot-create liwei
3. view the snapshot list
[Root @ kvm ~] # Virsh snapshot-list liwei # You can use qemu-img to view the snapshot information of the image [root @ kvm ~] # Qemu-img info/data/kvm/liwei. img
4. Switch snapshots
[root@kvm ~]# virsh snapshot-revert liwei 1477285698
5. view the current Snapshot
[root@kvm ~]# virsh snapshot-current liwei
6. delete a snapshot
[root@kvm ~]# virsh snapshot-delete liwei 1477285698
7. snapshot file storage location
/var/lib/libvirt/qemu/snapshot
VII. Resize and add a virtual machine Disk
1. Expand the disk capacity of the VM to increase the capacity of the existing disk.
[Root @ kvm ~] # Qemu-img resize/data/kvm/liwei. qcow2 + 5G # restarting the VM reboot VM does not take effect [root @ kvm ~] # Virsh destroy liwei [root @ kvm ~] # Virsh start liwei
Use fdisk-l to view the block id in the virtual machine. You can see that there is more storage space by observing the block id. You must also use extra partitions and formatting. lvm is used by default.
2. Add disks to virtual machines
Follow these steps:
- Disable Virtual Machine
- Use qemu-img to create a disk image
- Use virsh edit liwei to edit the VM configuration file, add a disk record, and modify the information as appropriate.
- VM boot-> fdisk-> Format-> OK.
Note: You can try to format it without partitioning, or try to use lvm.
8. Use a virtual disk to restore a virtual machine
Ideas: First, you must have an image file (existing) + xml configuration file.
[Root @ kvm ~] # Virsh dumpxml liwei>/etc/libvirt/qemu/liwei01.xml # edit the configuration file, modify it to an appropriate value # Add the definition virsh define/etc/libvirt/qemu/liwei01.xmlvirsh list -- all # To check the Virtual Machine
9. Adjust the CPU and memory specifications
If the number of cpu cores and memory to be adjusted exceed the maximum value specified when the VM is installed, You need to disable the VM to modify the maximum value. The value of dynamic adjustment cannot exceed the maximum value, generally, the use value and the maximum value are consistent and modified together. So OnLine dynamic modification is meaningless. It is recommended that you modify the configuration file directly.
[root@kvm ~]# virsh edit liwei01<memory unit='KiB'>1048576</memory><currentMemory unit='KiB'>1048576</currentMemory><vcpu placement='static'>1</vcpu>
Restart the VM liwei01.
10. Adjust the VM Nic
1. Add a VM Nic
# The temporary command takes effect [root @ kvm ~] # Virsh attach-interface liwei -- type bridge -- source br0 # modify the VM configuration file [root @ kvm ~] # Virsh dumpxml liwei>/etc/libvirt/qemu/liwei. xml [root @ kvm ~] # Virsh define/etc/libvirt/qemu/liwei. xml
2. Delete the VM Nic
[root@kvm ~]# virsh detach-interface liwei --type bridge --mac 52:54:00:14:86:cf
3. Specify the NIC type
Temporarily omitted.
11. Migration of virtual machines
Steps:
1. Disable the Virtual Machine 2. Copy the image file 3. Copy the configuration file 4. virsh define vm
To be updated ......