一、安装kvm
查看CPU是否支持虚拟化
grep -E 'svm|vmx' /proc/cpuinfo
- vmx is for Intel processors
- svm is for AMD processors
安装kvm相关软件并启动服务
yum install qemu-kvm libvirt libvirt-python libguestfs-tools virt-install
systemctl enable libvirtd && systemctl start libvirtd
查看kvm,kvm_intel模块是否加载
lsmod |grep kvm
配置网络
/etc/sysconfig/network-scripts/ifcfg-xxx加上
BRIDGE=br0
vim /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE="br0"
# BOOTPROTO is up to you. If you prefer “static”, you will need to
# specify the IP address, netmask, gateway and DNS information. BOOTPROTO="dhcp"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
ONBOOT="yes"
TYPE="Bridge"
DELAY=""
启用网络转发
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
sysctl -p /etc/sysctl.conf
重启网络
systemctl restart NetworkManager
systemctl restart network
关闭防火墙和SElinux
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
vim /etc/selinux/config
SELINUX=disabled
二、创建centos7虚拟机
将系统镜像文件传入主机/opt/centos7_iso目录下
创建虚拟磁盘文件存放目录
mkdir /kvm-img
virt-install --virt-type=kvm --name=centos7_original --vcpus= --memory= --location=/opt/centos7_iso/CentOS--x86_64-Minimal-.iso --disk path=/kvm-img/centos7_original.qcow2,size=,format=qcow2 --network bridge=br0 --graphics none --extra-args='console=ttyS0'
按系统安装提示安装完centos7系统
三、管理虚拟机
克隆虚拟机
virsh suspend vm1
virt-clone --connect=qemu:///system -o centos7_original -n centos7_clone -f /kvm-img/centos7_clone.qcow2
virsh resume vm1
virsh start centos7_clone
操作虚拟机
virsh list --all #列出所有虚拟机,包括running和disable
virsh dominfo vm1 #查看虚拟机信息
virt-top #查看所有虚拟机VCPU/memory使用情况
virt-df vm1 #查看虚拟机磁盘信息
virsh shutdown vm1 #关闭虚拟机
virsh start vm1 #开启虚拟机
virsh autostart vm1 #开机自启
virsh autostart -disable vm1 #关闭开机自启
virsh console vm1 #通过虚拟控制台进行系统进行管理
virsh edit vm1 #编辑虚拟机配置文件
virsh create /etc/libvirt/qemu/vm1.xml
virsh destroy vm1-clone #强制关闭虚拟机
virsh undefine vm1-clone #取消虚拟机
rm /kvm-img/xxx.qcow2 #删除磁盘文件