CentOS 7 Installation of KVM Virtual Machine

Detailed process of installing KVM in command line mode based on entOS Linux release 7.2.1511 (Core).
https://github.com/jaywcjlove/handbook

Detecting whether KVM is supported

KVM is a virtual machine software based on x86 virtualization extension technology (Intel VT or AMD-V), so if you look at whether the CPU supports VT technology, you can judge whether it supports KVM. If the result has the words vmx (Intel) or svm(AMD), the CPU supports it.

cat /proc/cpuinfo | egrep 'vmx|svm'

flags   : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm arat epb pln pts dtherm tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm xsaveopt cqm_llc cqm_occup_llc

Turn off SELinux and change SELinux = en forcing in / etc/sysconfig/selinux to SELinux=disabled

vi /etc/sysconfig/selinux

Installing KVM environment

adopt yum Install kvm Foundation Package and Management Tools

# Installation of kvm Foundation Package
# ------------------------
yum -y install kvm

# Install kvm management tools
# ------------------------
yum -y install qemu-kvm python-virtinst libvirt libvirt-python virt-manager libguestfs-tools bridge-utils virt-install

# qemu-kvm: KVM module

# pyhon-virtinst: contains python modules and tools (virt-install, virt-clone and virt-image),
# libvirt is used to install and clone virtual machines. It fully supports paravirtualized guests and guest virtual guests. 
# Supported hypervisors are Xen, QEMU (QEMU) and kvm

# libvirt: Virtual Management Module
# virt-manager: Graphic Interface Management Virtual Machine
# Support package for commands such as libguestfs*: virt-cat


# Restart the host to load the kvm module
# ------------------------
reboot

# Check whether the KVM module is loaded correctly
# ------------------------
lsmod | grep kvm

kvm_intel             162153  0
kvm                   525259  1 kvm_intel

Open the kvm service and set it to boot automatically

systemctl start acpid.service
systemctl enable acpid.service
systemctl start libvirtd.service
systemctl enable libvirtd.service

Viewing the results of status operations, such as Active: active (running), shows that the operation is in good condition.

systemctl status libvirtd
systemctl is-enabled libvirtd

● libvirtd.service - Virtualization daemon
   Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
   Active: active (running) since Two 2001-01-02 11:29:53 CST; 1h 41min ago
     Docs: man:libvirtd(8)
           http://libvirt.org

Installation of Virtual Machines

Before installation, set the environment language to English LANG="en_US.UTF-8". If it is Chinese, some versions may report errors. CentOS 7 modifies / etc/locale.conf here.

kvm creates virtual machines, pay special attention to. iso image files must be placed in / home or the root directory to recreate the directory, otherwise the virtual machine can not be created because of permission error.

virt-install \
--virt-type=kvm \
--name=centos78 \
--vcpus=2 \
--memory=4096 \
--location=/tmp/CentOS-7-x86_64-Minimal-1511.iso \
--disk path=/home/vms/centos78.qcow2,size=40,format=qcow2 \
--network bridge=br0 \
--graphics none \
--extra-args='console=ttyS0' \
--force
# ------------------------
virt-install --virt-type=kvm --name=centos88 --vcpus=2 --memory=4096 --location=/tmp/CentOS-7-x86_64-Minimal-1511.iso --disk path=/home/vms/centos88.qcow2,size=40,format=qcow2 --network bridge=br0 --graphics none --extra-args='console=ttyS0' --force

Command Line Configuration System

The above commands for creating virtual machines ultimately require you to configure the basic system settings, with [!] basically to configure, in order to configure downwards, according to the number used to set it.

Installation

 1) [x] Language settings                 2) [!] Timezone settings
        (English (United States))                (Timezone is not set.)
 3) [!] Installation source               4) [!] Software selection
        (Processing...)                          (Processing...)
 5) [!] Installation Destination          6) [x] Kdump
        (No disks selected)                      (Kdump is enabled)
 7) [ ] Network configuration             8) [!] Root password
        (Not connected)                          (Password is not set.)
 9) [!] User creation
        (No user will be created)
  Please make your choice from above ['q' to quit | 'b' to begin installation |
  'r' to refresh]:

2) Timezone settings Time Zone Settings Selection 5) Asia Asia, then 62) Shanghai Shanghai

Available regions
 1)  Africa                 6)  Atlantic              10)  Pacific
 2)  America                7)  Australia             11)  US
 3)  Antarctica             8)  Europe                12)  Etc
 4)  Arctic                 9)  Indian
 5)  Asia
Please select the timezone.
Use numbers or type names directly [b to region list, q to quit]: 5
--------------------

 8)  Baghdad               35)  Kathmandu             61)  Seoul
 9)  Bahrain               36)  Khandyga              62)  Shanghai
10)  Baku                  37)  Kolkata               63)  Singapore
26)  Hong_Kong             53)  Pontianak
27)  Hovd
Please select the timezone.
Use numbers or type names directly [b to region list, q to quit]: 62

3) Installation source installation source input number 2

Choose an installation source type.
 1)  CD/DVD
 2)  local ISO file
 3)  Network
  Please make your choice from above ['q' to quit | 'c' to continue |
  'r' to refresh]: 2

4) Software selection software selection

Base environment
Software selection

Base environment

 1)  [x] Minimal Install
  Please make your choice from above ['q' to quit | 'c' to continue |
  'r' to refresh]:

5) Installation Destination installation destination

Installation Destination

[x] 1) : 40 GiB (vda)

1 disk selected; 40 GiB capacity; 40 GiB free ...

  Please make your choice from above ['q' to quit | 'c' to continue |
  'r' to refresh]: c


Autopartitioning Options Auto partition option

[ ] 1) Replace Existing Linux system(s) Replace existing Linux system

[x] 2) Use All Space Use all spaces

[ ] 3) Use Free Space Use available space

================================================================================
Partition Scheme Options Partition scheme options

[ ] 1) Standard Partition Standard partition

[ ] 2) Btrfs Btrfs

[x] 3) LVM LVM(Logical Volume Management)

[ ] 4) LVM Thin Provisioning Thin Provisioning

Select a partition scheme configuration.

  Please make your choice from above ['q' to quit | 'c' to continue |
  'r' to refresh]: c

Here you can also set only the Root password and Installation Destination installation destination. Other entry settings, such as time zone settings, are as follows:

echo "TZ='Asia/Shanghai'; export TZ" >> /etc/profile

Connect virtual machines

Connect virtual machines through virsh console < virtual machine name > command

# View Virtual Machines
virsh list              # View the running virtual machine
virsh list –all         # View all virtual machines

 Id    Name                           State
----------------------------------------------------
 7     centos72                       running

Connect virtual machines

virsh console centos72

Configure the virtual machine network and edit vi/etc/sysconfig/network-scripts/ifcfg-eth0

TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.120.200
PREFIX=24
GATEWAY=192.168.120.1
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eth0
UUID=adfa3b7d-bf60-47e6-8482-871dee686fb5
DEVICE=eth0
ONBOOT=yes

Adding DNS configuration can also be placed in ifcfg-eth0. DNS is not set casually, you can use it. host,dig,nslookup Command to query DNS, if these tools do not exist, you can install them through Yum install bind-utils-y.

# If DNS is not added to the network configuration, DNS can be added in this way.
echo "nameserver 192.168.188.1" > /etc/resolv.conf

Activate Network Card

ifup eth0 # Activate Network Card

Configuring Physical Machine Network

At present, I have only one fixed IP. By configuring eno2, the bridge acts as a router, and the virtual machine shares physical machines in and out of the network. Physical Machine Network Configuration, Network in and out eno2 Editor vi/etc/sysconfig/network-scripts/ifcfg-eno2

TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eno2
UUID=f66c303e-994a-43cf-bd91-bb897dc2088d
DEVICE=eno2
ONBOOT=yes

IPADDR=<Fixed here IP Places to configure>  # Setting IP Address
PREFIX=24                   # Setting Subnet Mask
GATEWAY=<Set up gateway here>        # Setting up Gateway
DNS1=<Here settings DNS>           # DNS

ifcfg-br0 bridge network card is configured in the same directory.

TYPE=Bridge
BOOTPROTO=static
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.120.1
PREFIX=24

ifcfg-eno1 Physical Network Card Specifies Bridge Network Card BRIDGE="br0"

TYPE=Ethernet
BOOTPROTO=none
NAME=eno1
DEVICE=eno1
ONBOOT=yes
BRIDGE="br0"

Configure routing forwarding vi/etc/sysctl.conf

# Controls IP packet forwarding
net.ipv4.ip_forward = 0
 Amend to read
# Controls IP packet forwarding
 net.ipv4.ip_forward = 1 allows built-in routing

Then execute sysctl-p to make it effective. Edit vi/etc/rc.d/rc.local to add the following commands to achieve the boot-up reboot configuration network forwarding rules.

# Start Network Forwarding Rules
iptables -t nat -A POSTROUTING -s 192.168.188.0/24 -j SNAT --to-source 210.14.67.7

adopt iptables Command to set forwarding rules, source SNAT rules, source network address translation, SNAT is the source IP address of the rewrite package.

# The source NAT(SNAT) is used for data packets, and the system routes first - > then filters (FORWARD) - > and finally translates POSTROUTING SNAT address.
# - T < Table >: Specify the table to be manipulated;
# - A: Add entries to the rule chain;
# - s: specify the ip address of the packet source to match;
# - J < Goal >: Specify the target to jump;
# - j SNAT: Source Network Address Translation, SNAT is the source IP address of the rewritten package
# --to-source ipaddr[-ipaddr][:port-port] 
#   It can specify a single new source IP address, the range of IP addresses included, and the optional port range (valid only if the rule also specifies - p tcp or - p udp). 
#   If no port range is specified, the source port below 512 will map to other ports below 512: the port between 512 and 1023 will map to ports below 1024.
#   Other ports will be mapped to 1024 or higher. Where possible, no port changes will occur.
#   Up to 2.6.10 in the kernel, you can add several - source options. 
#   For these kernels, if multiple source addresses are specified through address ranges or multiple source options, simple loops (loops in loops) occur between these addresses. 
#   Later kernels (> = 2.6.11-rc1) no longer had the ability to NAT to multiple ranges.
iptables -t nat -A POSTROUTING -s 192.168.120.0/24 -j SNAT --to-source <fixed IP>
# cat /etc/sysconfig/iptables

Configuring Host Network

  1. KVM virtual machine is a network configuration based on NAT.
  2. Only virtual keys of the same host can be accessed to each other, and cross-host can not be accessed.
  3. The virtual machine needs to be configured in bridge mode with the host so that the virtual machine can be visible in the LAN.

Bridge mode configuration

Bridge mode is the network connection mode of virtual bridge, which means that the client and the machine in the subnet can communicate with each other. Virtual machines can be made hosts with independent IP in the network. Bridged networks (also known as physical device sharing) are used to copy a physical device to a virtual machine. Bridges are mostly used as advanced settings, especially when the host has multiple network interfaces.

┌─────────────────────────┐      ┌─────────────────┐
│          HOST           │      │Virtual Machine 1│
│ ┌──────┐      ┌───────┐ │      │    ┌──────┐     │
│ │ br0  │──┬───│ vnet0 │─│─ ─ ─ │    │ br0  │     │
│ └──────┘  │   └───────┘ │      │    └──────┘     │
│     │     │             │      └─────────────────┘
│     │     │   ┌───────┐ │      ┌─────────────────┐
│ ┌──────┐  └───│ vnet1 │─│─     │Virtual Machine 2│
│ │ eno0 │      └───────┘ │ │    │    ┌──────┐     │
│ └──────┘                │  ─ ─ │    │ br0  │     │
│ ┌──────┐                │      │    └──────┘     │
│ │ eno1 │                │      └─────────────────┘
│ └──────┘                │
└─────────────────────────┘

adopt ip Command to view the name of the host configuration file

ip addr

6: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 38:63:bb:44:cf:6c brd ff:ff:ff:ff:ff:ff
    inet 192.168.188.132/24 brd 192.168.188.255 scope global dynamic eno1
       valid_lft 2822sec preferred_lft 2822sec
    inet6 fe80::3a63:bbff:fe44:cf6c/64 scope link
       valid_lft forever preferred_lft forever

You can see that eno1 above has an ip address, and the corresponding file is in the / etc/sysconfig/network-scripts / directory, the physical network card configuration file of ifcfg-eno1 host.

# cat ifcfg-eno1
TYPE=Ethernet
BOOTPROTO=static
NAME=eno1
DEVICE=eno1
UUID=242b3d4d-37a5-4f46-b072-55554c185ecf
ONBOOT=yes

BRIDGE="br0" # Specify the name of the bridge network card

ifcfg-br0 bridge network card is configured in the same directory.

# cat ifcfg-br0
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=br0
UUID=242b3d4d-37a5-4f46-b072-55554c185ecf
DEVICE=br0
ONBOOT=yes
TYPE=bridge  # It will be formulated as a bridge type
IPADDR=192.168.188.133  # Setting IP Address
PREFIX=24               # Setting Subnet Mask
GATEWAY=192.168.188.1   # Setting up Gateway

After configuring, pass systemctl Command to restart the network card.

ifup eno1 # Activate Network Card
ifup br0 # Activate Bridge Network Card
# Two ways to restart the network
systemctl restart network.service
service network restart

# Check bridge interface
brctl show

bridge name bridge id   STP enabled interfaces
br0   8000.3863bb44cf6c no    eno1
              vnet0
virbr0    8000.525400193f0f yes   virbr0-nic

NAT Model

NAT(Network Address Translation) is the default mode after kvm installation. It supports the mutual access between host and virtual machine, and also supports virtual machine access to the Internet, but does not support external access to virtual machine.

virsh net-list --all

 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     no            no

default is automatically installed when the host installs the virtual machine support module.

ip a l
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens1f0: <BROADCAST,MULTICAST> mtu 1500 qdisc mq state DOWN qlen 1000
    link/ether 2c:44:fd:8c:43:44 brd ff:ff:ff:ff:ff:ff
3: ens1f1: <BROADCAST,MULTICAST> mtu 1500 qdisc mq state DOWN qlen 1000
    link/ether 2c:44:fd:8c:43:45 brd ff:ff:ff:ff:ff:ff
4: ens1f2: <BROADCAST,MULTICAST> mtu 1500 qdisc mq state DOWN qlen 1000
    link/ether 2c:44:fd:8c:43:46 brd ff:ff:ff:ff:ff:ff
5: ens1f3: <BROADCAST,MULTICAST> mtu 1500 qdisc mq state DOWN qlen 1000
    link/ether 2c:44:fd:8c:43:47 brd ff:ff:ff:ff:ff:ff
6: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq master br0 state UP qlen 1000
    link/ether 38:63:bb:44:cf:6c brd ff:ff:ff:ff:ff:ff
7: eno2: <BROADCAST,MULTICAST> mtu 1500 qdisc mq state DOWN qlen 1000
    link/ether 38:63:bb:44:cf:6d brd ff:ff:ff:ff:ff:ff
8: eno3: <BROADCAST,MULTICAST> mtu 1500 qdisc mq state DOWN qlen 1000
    link/ether 38:63:bb:44:cf:6e brd ff:ff:ff:ff:ff:ff
9: eno4: <BROADCAST,MULTICAST> mtu 1500 qdisc mq state DOWN qlen 1000
    link/ether 38:63:bb:44:cf:6f brd ff:ff:ff:ff:ff:ff
10: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
    link/ether 52:54:00:19:3f:0f brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
11: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 500
    link/ether 52:54:00:19:3f:0f brd ff:ff:ff:ff:ff:ff
12: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
    link/ether 38:63:bb:44:cf:6c brd ff:ff:ff:ff:ff:ff
    inet 192.168.188.132/24 brd 192.168.188.255 scope global dynamic br0
       valid_lft 3397sec preferred_lft 3397sec
    inet 192.168.188.133/24 brd 192.168.188.255 scope global secondary br0
       valid_lft forever preferred_lft forever
    inet6 fe80::3a63:bbff:fe44:cf6c/64 scope link
       valid_lft forever preferred_lft forever
19: vnet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UNKNOWN qlen 500
    link/ether fe:54:00:72:12:a8 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::fc54:ff:fe72:12a8/64 scope link
       valid_lft forever preferred_lft forever

virbr0 is a virtual network interface generated when the host virtual machine support module is installed. It is also a switch and bridge, responsible for distributing content to the virtual machines. The interface relationships generated by several virtual machine management modules are as follows:

┌───────────────────────┐                      
│         HOST          │                      
│ ┌──────┐              │   ┌─────────────────┐
│ │ br0  │─┬──────┐     │   │Virtual Machine 1│
│ └──────┘ │      │     │   │   ┌──────┐      │
│     │    │  ┌───────┐ │ ─ │   │ br0  │      │
│     │    │  │ vnet0 │─│┘  │   └──────┘      │
│ ┌──────┐ │  └───────┘ │   └─────────────────┘
│ │virbr0│ │  ┌───────┐ │   ┌─────────────────┐
│ │ -nic │ └──│ vnet1 │─│┐  │Virtual Machine 2│
│ └──────┘    └───────┘ │   │                 │
│ ┌──────┐              │└ ─│   ┌──────┐      │
│ │ eno0 │              │   │   │ br0  │      │
│ └──────┘              │   │   └──────┘      │
│ ┌──────┐              │   └─────────────────┘
│ │ eno1 │              │
│ └──────┘              │
└───────────────────────┘

As can be seen from the graph, there is no connection between virtual interface and physical interface, so the virtual machine can only access the outside world through virtual network, and can not locate and access the virtual host from the network.

virbr0 is a bridge that receives all content to network 192.168.122 *. Verify from the following command:

brctl show
# Output results
# ---------------------
# bridge name bridge id   STP enabled interfaces
# br0   8000.3863bb44cf6c no    eno1
#               vnet0
# virbr0    8000.525400193f0f yes   virbr0-nic

ip route
# default via 192.168.188.1 dev br0
# 169.254.0.0/16 dev br0  scope link  metric 1012
# 192.168.122.0/24 dev virbr0  proto kernel  scope link  src 192.168.122.1
# 192.168.188.0/24 dev br0  proto kernel  scope link  src 192.168.188.132

At the same time, the virtual machine support module will modify the iptables rules, which can be viewed by command:

iptables -t nat -L -nv
iptables -t filter -L -nv

If there is no default, or you need to expand your virtual network, you can use commands to reinstall NAT.

virsh net-define /usr/share/libvirt/networks/default.xml

This command defines a virtual network, default.xml:

<network>
  <name>default</name>
  <bridge name="virbr0" />
  <forward/>
  <ip address="192.168.122.1" netmask="255.255.255.0">
    <dhcp>
      <range start="192.168.122.2" end="192.168.122.254" />
    </dhcp>
  </ip>
</network>

You can also modify xml to create your own virtual network.

Marked as auto-start:

virsh net-autostart default
# Network default marked as autostarted

Start the network:

virsh net-start default
# Network default started

The command brctl show can be used to view and verify the network after it is started.

Modify the parameters in vi/etc/sysctl.conf to allow ip forwarding. CentOS7 is modified in vi/usr/lib/sysctl.d/00-system.conf

net.ipv4.ip_forward=1

View the modification results through sysctl-p

Custom NAT Network

Create a NAT network called management, vi/usr/share/libvirt/networks/management.xml

<network>
  <name>management</name>
  <bridge name="virbr1"/>
  <forward/>
  <ip address="192.168.123.1" netmask="255.255.255.0">
    <dhcp>
      <range start="192.168.123.2" end="192.168.123.254"/>
    </dhcp>
  </ip>
</network>

Enabling a new NAT network

virsh net-define /usr/share/libvirt/networks/management.xml
virsh net-start management
virsh net-autostart management

Verification

brctl show
# bridge name bridge id   STP enabled interfaces
# br0   8000.3863bb44cf6c no    eno1
#               vnet0
# virbr0    8000.525400193f0f yes   virbr0-nic
# virbr1    8000.52540027f0ba yes   virbr1-nic

virsh net-list --all
#  Name                 State      Autostart     Persistent
# ----------------------------------------------------------
#  default              active     no            no
#  management           active     yes           yes

Exit Virtual Machine

exit # Exit system to login interface

Ctrl+5 # From the virtual machine login page, exit to the host command line page
Ctrl+] # Or

Modifying Virtual Machine Configuration Information

Modify directly through vim command

vim  /etc/libvirt/qemu/centos72.xml

Modify through the virsh command

virsh edit centos72

Cloning Virtual Machine

# Pause the original virtual machine
virsh shutdown centos72
virt-clone -o centos72 -n centos.112 -f /home/vms/centos.112.qcow2 -m 00:00:00:00:00:01
virt-clone -o centos88 -n centos.112 --file /home/vms/centos.112.qcow2 --nonsparse

Introduction of virt-clone parameters

  • View the version.
  • - h,--help to view help information.
  • -- connect=URI to the URI of the virtual machine manager libvirt.
  • - o The original virtual machine name, the original virtual machine name, must be closed or paused.
  • - n New Virtual Machine name - name New Virtual Machine name.
  • --auto-clone automatically generates clone names and storage paths from the original virtual machine configuration.
  • - u NEW_UUID, --uuid=NEW_UUID clones the new UUID of the virtual machine. The default value is a randomly generated UUID.
  • - M NEW_MAC,--mac=NEW_MAC sets a new Mac address, which is generated randomly by default.
  • - f NEW_DISKFILE, --file=NEW_DISKFILE uses the new disk mirror file address for the new client.
  • force-copy=TARGET Forced Replication Device.
  • nonsparse does not use sparse files to copy disk images.

Creating virtual machines by mirroring

Creating Virtual Machine Mirror Files

# Copy the first installed clean system image as the basic image file.
# Later, create a virtual machine to use this basic image
cp /home/vms/centos.88.qcow2 /home/vms/centos7.base.qcow2

# Create a new virtual machine image using the underlying image file
cp /home/vms/centos7.base.qcow2 /home/vms/centos7.113.qcow2

Create Virtual Machine Profile

# Copy the first installed clean system image as the basic configuration file.
virsh dumpxml centos.88 > /home/vms/centos7.base.xml

# Create a new virtual machine profile using the underlying virtual machine image profile
cp /home/vms/centos7.base.xml /home/vms/centos7.113.xml

# Edit the new virtual machine configuration file
vi /home/vms/centos7.113.xml

It is mainly to modify the virtual machine file name, UUID, mirror address and network card address, where UUID can be generated under Linux using the uuidgen command.

<domain type='kvm'>
  <name>centos7.113</name>
  <uuid>1e86167a-33a9-4ce8-929e-58013fbf9122</uuid>
  <devices>
    <disk type='file' device='disk'>
      <source file='/home/vms/centos7.113.img'/>
    </disk>
    <interface type='bridge'>
      <mac address='00:00:00:00:00:04'/>
    </interface>    
    </devices>
</domain>
virsh define /home/vms/centos7.113.xml
# Domain centos.113 defined from /home/vms/centos7.113.xml

Mount disk

create disk

mkdir /home/vms

Create a mirror file

sudo qemu-img create /home/vms/centos78.img

Create a common qcow2 format image file

# Disks needed to create guest s
# create denotes creation, and - f qcow2 denotes creation of a disk in qcow2 format. 
# / home/vms/centos78.qcow2 denotes the name of the disk created and the disk file, and 40G denotes the available size of the disk.
qemu-img create -f qcow2 -o preallocation=metadata /home/vms/centos78.qcow2 40G

Common Command Description

virt-install

Description of Common Parameters

- name specifies the virtual machine name
 Memory allocates memory size.
- vcpus allocates CPU cores, the maximum of which is the same as the number of physical CPUs
 disk specifies the virtual machine image, and size specifies the allocation size in G.
Network network type, which is used by default, should be bridge d in general.
accelerate
 - cdrom specifies installation of mirror iso
 Vnc enables remote management of VNC, which is commonly used in installation systems.
vncport specifies VNC monitoring port, default port is 5900, port can not be repeated.
vnclisten specifies VNC binding IP, default binding 127.0.0.1, here changed to 0.0.0.0.
–os-type=linux,windows
–os-variant=rhel6

- name specifies the virtual machine name
 - ram virtual machine memory size in MB
 - vcpus allocates the CPU core number, the maximum being the same as the physical CPU core number
 VNC remote management is enabled in - vnc. Generally, installation systems are enabled.
- vncport specifies VNC monitoring port, default port is 5900, port can not be repeated.
- vnclisten specifies VNC binding IP, default binding 127.0.0.1, here changed to 0.0.0.0.
network Configuration of network Virtual Machine
  # Suboption, bridge=br0, specifies the name of the bridge network card.

–os-type=linux,windows
–os-variant=rhel7.2

--disk specifies the disk storage location of the virtual machine
  # Size, the initial disk size, in GB.

location specifies the installation media path, such as the file path of the CD-ROM image.
graphics graphical display configuration
  # There may be many interactions in the process of installing a new virtual machine, such as setting up the language, initializing the root password, and so on.
  # The graphics option configures graphical interaction, using vnc (a remote desktop software) for links.
  # Our column is installed on the command line, so we want to set it to none, but we need to specify terminal information through the -- extra-args option.
  # Only in this way can the interactive information during installation be output to the current console.
-- extra-args sets different additional options according to different installation methods

virsh

Basic commands

virsh list                 # View the running virtual machine
virsh dumpxml vm-name      # View the kvm virtual machine configuration file
virsh start vm-name        # Start the kvm virtual machine
virsh shutdown vm-name     # Normal shutdown

virsh destroy vm-name      # Abnormal shutdown, forced shutdown of virtual machine (equivalent to physical machine directly unplug power)
virsh undefine vm-name     # Delete the vm configuration file

ls  /etc/libvirt/qemu
# Viewing the deletion result, the configuration file of Centos-6.6 was deleted, but the disk file was not deleted.

virsh define file-name.xml # Define virtual machines according to configuration files
virsh suspend vm-name      # Suspend, terminate
virsh resumed vm-name      # Recovery of suspended virtual machines
virsh autostart vm-name    # Boot-up self-starting vm
virsh console <Virtual Machine Name>   # Connect virtual machines

Error Resolution

console test
Connected to domain test
Escape character is ^]

If the above string appears, use CTRL+Shift+5 CTRL+Shift+]

Reference Articles

Keywords: network xml CentOS vnc

Added by day_tripperz on Fri, 07 Jun 2019 01:47:09 +0300