Centos7 builds KVM command line to install virtual machine

[toc]

Preparation before installation

Verify that the CPU supports KVM

If there is vmx (Intel) or svm(AMD) in the result, it indicates that the CPU supports.

egrep '(vmx|svm)' /proc/cpuinfo

Close SELinux

Change SELinux=enforcing in / etc/sysconfig/selinux to SELinux=disabled

Install some of the most basic services

Optional, because I have just installed CentOS, so for the convenience of the following, first install some necessary tools

yum install epel-release net-tools vim unzip zip wget ftp -y

Install KVM and its dependencies

yum install qemu-kvm libvirt virt-install bridge-utils -y

Verify installation results

The following figure shows that the has been successfully installed

lsmod | grep kvm

Turn on kvm service

And set it to start automatically

systemctl start libvirtd systemctl enable libvirtd

View status operation results

As shown in the figure below, it indicates that the operation is good

systemctl status libvirtd

systemctl is-enabled libvirtd

Configure bridge mode

First, back up a copy of the network card configuration file in the / etc / sysconfig / network scripts / directory

Create ifcfg-br0 file

The IP address of the created br0 file should be consistent with the IP address of the physical network card. If you command ipconfig to view the physical network card, the IP address will not be displayed

The contents are as follows:

[root@bogon ~]# vim /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
BOOTPROTO=none
DEFROUTE=yes
ONBOOT=yes
TYPE=Bridge
IPV4_FAILURE_FATAL=yes
IPADDR=192.168.1.130
NETMASK=255.255.255.0
GATEWAY=192.168.1.254
DNS1=221.6.4.66
DELAY=0
USERCE=no

Modify the original network card configuration

vim /etc/sysconfig/network-scripts/ifcfg-eno1s
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="YES"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="eno1"
UUID="bb40d726-8d67-4187-90c3-eb61e1b42d61"
DEVICE="eno1"
ONBOOT="yes"
IPADDR="192.168.1.130"
NETAMSK=255.255.255.0
GATEWAY="192.168.1.254"
DNS1="221.6.4.66"
IPV6_PRIVACY="no"
BRIDGE=br0

service network restart

systemctl restart network

Use ifconfig to verify the operation results. There is an additional network card br0. Now you can use 192.168.1.130 to access the host computer.

Installing virtual machines

Preparing the operating system installation image file

In this article, you will use CentOS 7.2, which is the same as the host environment, and put the file in the / home/iso directory

Mount U SB flash disk

[root@nkgtsv01 ~]# yum install fuse-ntfs-3g -y
[root@nkgtsv01 ~]# ls /mnt/
udisk  usb
[root@nkgtsv01 ~]# ls /mnt/udisk/
CentOS-7.2-x86_64-DVD-1611.iso  CentOS-7-x86_64-DVD-1708.iso  maven_storey2.zip
[root@bogon data]# mkdir -p /data/iso
[root@bogon data]# ls
iso  kvm-bak  network
[root@nkgtsv01 ~]# mount  -o loop /mnt/udisk/CentOS-7-x86_64-DVD-1708.iso /data/iso/
mount: /dev/loop0 Write protected, will mount as read-only

Create a directory where virtual machine files are stored

mkdir -p /data/kvm-images

Creating virtual machines using virt install

virt-install --virt-type=kvm --name=njkvm07 --vcpus=4 --memory=6000 --location=/data/iso/CentOS-7-x86-64-DVD-1708.iso --disk path=/data/kvm-images/njkvm07.qcow2,size=200,format=qcow2 --network bridge=br0 --graphics none --extra-args='console=ttyS0' --force

After executing this command

Exclamation marks are to be selected

c save q exit b start installation

Select region time

If selected, c save

Auto return to main page

Select hard disk

Select Save to hard disk c

New installation C save

If selected, add IP address and host name

Enter enter

Add IP address

Select ipv4

Add IP address enter

Add netmask gateway C save

Add password

B start installation

installation is complete

The host is directly connected to the virtual machine

Wait for the virtual machine to restart. After the virtual machine starts, it will be ok to start it remotely.

Virt clone clone virtual machine

Clone a new virtual machine using virt clone

(virtual machine needs to be shut down first)

virt-clone -o njvm02 -n njvm03 -f /data/kvm-img/njvm03.img

After cloning, view all virtual machines and their status

virsh list --all

Delete virtual machine njvm01

virsh undefine njvm01 
virsh destroy njvm01

{{< alert theme="warning" dir="ltr" >}}
⚠️ be careful

After you cancel the definition and delete, you need to find the virtual machine file path. The virtual machine files are also deleted
{{< /alert >}}

[root@nkgtsv01 data]# virsh shutdown njvm01
 field njvm01 Closed
[root@nkgtsv01 data]# virsh start njvm02
 field njvm02 Started
[root@nkgtsv01 data]# virsh list --all

{{< alert theme="warning" dir="ltr" >}}
⚠️ be careful

After cloning, because the IP address is still njvm01's IP address, we need to modify the IP address
{{< /alert >}}

Turn on the virtual machine we cloned

Remote login

[root@nkgtsv-vm01 ~]# cd /etc/sysconfig/network-scripts/
[root@nkgtsv-vm01 network-scripts]# ls
ifcfg-eth0   ifdown-ppp       ifup-eth     ifup-sit
ifcfg-lo     ifdown-routes    ifup-ippp    ifup-Team
ifdown       ifdown-sit       ifup-ipv6    ifup-TeamPort
ifdown-bnep  ifdown-Team      ifup-isdn    ifup-tunnel
ifdown-eth   ifdown-TeamPort  ifup-plip    ifup-wireless
ifdown-ippp  ifdown-tunnel    ifup-plusb   init.ipv6-global
ifdown-ipv6  ifup             ifup-post    network-functions
ifdown-isdn  ifup-aliases     ifup-ppp     network-functions-ipv6
ifdown-post  ifup-bnep        ifup-routes
[root@nkgtsv-vm01 network-scripts]# vim ifcfg-eth0

Change IPADDR=192.168.1.121 to the IP address we want

Save exit

Service network restart

Restart the network

re-link

Reference documents:
http://www.linuxidc.com/Linux...
http://blog.csdn.net/u0114142...
https://www.cnblogs.com/52013...
http://blog.51cto.com/7834466...
https://www.cnblogs.com/Yemil...

Tags: kvm

Posted on Fri, 05 Nov 2021 22:05:43 -0400 by tiki