# Make sure that Virtualization Technology (VT) is enabled in your server’s BIOS
$ lscpu | grep Virtualization
Virtualization: VT-x
# Install kvm
yum install qemu-kvm libvirt libvirt-python libguestfs-tools virt-install
# Disable and stop NetworkManager
# NetworkManager is known to cause problems when working with Linux Bridge, so for us it’s better to disable it:
systemctl stop NetworkManager
systemctl disable NetworkManager
# Start the libvirtd service:
systemctl enable libvirtd
systemctl start libvirtd
# Verify kvm installation
lsmod | grep -i kvm
# If you want your VMs avilable to other servers on your LAN,
# setup a a network bridge on the server that connected to the your LAN.
# Update your nic config file such as ifcfg-enp4s0 or em1:
cp ifcfg-enp4s0 ifcfg-br0
vi /etc/sysconfig/network-scripts/ifcfg-enp4s0
# 以下是修改后的全部文件内容(极简):
TYPE=Ethernet
BOOTPROTO=static
DEVICE=eno49
ONBOOT=yes
BRIDGE=br0
# Edit /etc/sysconfig/network-scripts/ifcfg-br0:
vi /etc/sysconfig/network-scripts/ifcfg-br0
# 以下是修改后的全部文件内容(极简),局域网地址192.168.8.0/24:
TYPE=Bridge
BOOTPROTO=static
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.8.250
NETMASK=255.255.255.0
GATEWAY=192.168.8.1
DNS1=202.96.134.133
# Restart the networking service (warning ssh command will disconnect, it is better to reboot the box):
systemctl restart network
# Download virtual machine image
cd /var/lib/libvirt/boot/
wget https://mirrors.edge.kernel.org/centos/7.9.2009/isos/x86_64/CentOS-7-x86_64-Minimal-2009.iso
# Verify ISO images:
wget https://mirrors.edge.kernel.org/centos/7.9.2009/isos/x86_64/sha256sum.txt
sha256sum -c sha256sum.txt
# Create CentOS 7.x VM
# 过程中一定要配置网络,否则虚拟机无法访问外部或被外部访问:
# IP设置为192.168.8.201,NETMASK设置为255.255.255.0,GATEWAY设置为192.168.8.1,DNS设置为202.96.134.133
# 两个可选项(Connect automatically after reboot 和 Apply configuration in installer)勾选上
virt-install \
--virt-type=kvm \
--name=vm1 \
--vcpus=1 \
--memory=2048 \
--network bridge=br0 \
--location=/var/lib/libvirt/boot/CentOS-7-x86_64-Minimal-2009.iso \
--disk path=/var/lib/libvirt/images/vm1.qcow2,size=20,bus=virtio,format=qcow2 \
--graphics none \
--extra-args='console=ttyS0' \
--force
# 克隆VM
virsh shutdown vm1
virt-clone --original vm1 --name vm2 --file /var/lib/libvirt/images/vm2.qcow2
# 登录vm2,修改ip地址和主机名:
vi /etc/sysconfig/network-scripts/ifcfg-ens3
vi /etc/hostname
virsh restart vm2
virsh start vm1
## 如果磁盘空间不足,为虚拟机准备磁盘空间(从/home目录分割出一部分空间)
tar -czvf /root/home.tgz -C /home .
umount /dev/mapper/centos-home
lvremove /dev/mapper/centos-home
lvcreate -L 200GB -n home centos
mkfs.xfs /dev/centos/home
mount /dev/mapper/centos-home
tar -xzvf /root/home.tgz -C /home