Steps for configuring and installing the KVM virtual machine under linux

  • 2020-06-07 05:34:15
  • OfStack

Recently to do a set of monitoring system in the external network, just have a server configuration is relatively high, also did not run what important business, to delimit 1 KVM virtual machine, may be usually a variety of virtual machines used more, configuration up feel more simple than imagined, simple record the process

The preparatory work

Host centos 6.864 bit, check whether host CPU supports virtualization:

cat /proc/cpuinfo | egrep '(vmx|svm)' | wc -l;

A result greater than 0 indicates support

Install kvm

KVM Core Software Package:

yum install kvm libvirt python-virtinst qemu-kvm virt-viewer bridge-utils

If you have a desktop environment on your server and want to use the GRAPHICAL Interface Manager ES23en-ES24en, you can install the full KVM environment:

yum groupinstall Virtualization 'Virtualization Client' 'Virtualization Platform' 'Virtualization Tools'

Verify whether the kernel module is loaded:

lsmod | grep kvm

Start virtual Machine Management Interface Service:

/etc/init.d/libvirtd start

Set startup:

chkconfig libvirtd on

When libvirtd is started, a network card is automatically created and the dnsmasq service is started to assign IP addresses to the virtual machine

Creating a virtual machine

After downloading the ISO system image file to be installed on the virtual machine, you need to create a storage pool, specify the storage location on the virtual machine disk on the host, and create a storage directory:

mkdir -p /opt/kvm

Define a storage pool and binding directory:

virsh pool-define-as vmspool --type dir --target /opt/kvm

Create and activate a storage pool:

virsh pool-build vmspool
virsh pool-start vmspool

Create the virtual machine using the storage pool and connect via vnc:

virt-install \
--hvm \ # Full virtualization 
--name=zabbix \# Virtual machine name 
--ram=4096 \ # Allocate memory 
--vcpus=4 \ # distribution CPU The number 
--cdrom=/opt/kvm/iso/CentOS-7-x86_64-DVD-1511.iso \ # The use of ISO
--virt-type=kvm \ # Virtual machine type 
--disk path=/opt/kvm/zabbix.qcow2,device=disk,format=qcow2,bus=virtio,cache=writeback,size=100 \ # Disk size, format 
--network netwrok=default \ # Network Settings, defalut for NAT model 
--accelerate \ #KVM The kernel to accelerate 
--graphics vnc,listen=,port=5922,password=123123\ #vnc configuration 
--force \

Then use the vnc client to connect to the host IP:5922 to install the system using graphics. You can also choose nographics mode, which does not require vnc to be installed at the command line, and vnc is recommended

After installation, the following will be generated:

Virtual machine configuration file: / etc/libvirt/qemu/zabbix xml Virtual hard disk file: / / opt kvm/zabbix qcow2 NAT network configuration file: / / etc libvirt qemu/networks/default xml

Configure the network

KVM can be configured with two types:

NAT networking: Virtual machines use the host's network to access the public network. The host and the virtual machine can access each other, but external access to the virtual machine is not supported Bridging network: The virtual machine multiplexes the host physical network card. The virtual machine plays the same role as the host in the network and supports external access

Configure the NAT network

By default, there will be an NAT virtual network called default.

virsh net-list --all

To create or modify NAT networks, first edit ES106en.ES107en:

virsh net-edit default

Reload and activate the configuration:

lsmod | grep kvm

Start NAT network:

virsh net-start default
virsh net-autostart default

After starting NAT, a virtual bridging device virbr0 will be generated automatically, and the address of IP will be assigned to check the status:

brctl show

Under normal circumstances, after starting libirtd, virbr0 will be started and IPtables rule will be automatically added to realize NAT. Make sure to open ip_forward in /etc/ sysctl.conf:

lsmod | grep kvm

Just start the virtual machine and set up IP automatically. If you want to specify IP manually, please note that the configured IP should be in the NAT segment

Configure the bridge network

If a desktop environment is installed in the system, the network will be managed by NetworkManager. NetworkManager does not support bridging, so NetworkManger needs to be closed:

lsmod | grep kvm

If you do not want to close NetworkManager, you can manually add the parameter "NM_CONTROLLED=no" in ifcfg-br0

Create a bridge:

lsmod | grep kvm

After creation, ifconfig will see the br0 bridge. If there are multiple IP on eth0, change the corresponding file name, such as es166EN-eth0:1 to ES168en-br0:1

Edit the configuration file for the virtual machine using the new bridge:

lsmod | grep kvm

Find the network card configuration and change to:

<interface type='bridge'>
 <mac address='52:54:00:7a:f4:9b'/>
 <source bridge='br0'/>
 <model type='virtio'/>
 <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>

I use br0. To add multiple network CARDS to the virtual machine, I only need to copy multiple interface and make sure that the addresses of mac address and PCI are different

Reload configuration:

virsh define /etc/libvirt/qemu/zabbix.xml

Restart the virtual machine:

lsmod | grep kvm

Then use VNC to connect to the virtual machine and set up the network

Common operations

KVM related operations through the vish command to complete, although many parameters, but the function 1 eye clear, very intuitive

Create a virtual machine snapshot:

virsh snapshot-create-as --domain zabbix --name init_snap_1

It can also be shortened to:

virsh snapshot-create-as zabbix init_snap_1

Snapshot created after the configuration file in/var/lib/libvirt/qemu/snapshot/zabbix/init_snap_1 xml

View snapshots:

snapshot-list zabbix

Delete snapshot:

 snapshot-delete zabbix init_snap_1


1, ERROR Format be for unmanaged storage.

virt-manager did not find a storage pool. Just create a storage pool

2. KVM VNC Client connection flash back

Use real vnc or another vnc client to connect kvm to flash back and set the ColourLevel value in the client Settings to rgb222 or full

3. virsh shutdown cannot close the virtual machine

When using this command to shut down the virtual machine, KVM is to send 1 ACPI instruction to the virtual machine, which requires the virtual machine to install acpid service:

yum -y install acpid && /etc/init.d/acpid start

Otherwise you can only use virsh destroy to force the virtual machine to shut down

Related articles: