KVM命令行安装
环境:centos 7 2核4G
1、关闭防火墙:
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# systemctl disable firewalld
[[email protected] ~]# setenforce 0
setenforce: SELinux is disabled
[[email protected] ~]# sed -i '/^SELINUX/s#enforcing#disabled#g' /etc/selinux/config
查看系统设置:
[[email protected] ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
[[email protected] ~]# uname -r
3.10.0-1062.el7.x86_64
[[email protected] ~]# getenforce
Disabled
查看cpu是否支持虚拟化:
[[email protected] ~]# egrep -o '(vmx|svm)' /proc/cpuinfo
vmx
vmx
检查cpu是否开启虚拟化:
[[email protected] ~]# dmesg | grep kvm
安装KVM的用户模块:
[[email protected] ~]# yum -y install qemu-kvm qemu-kvm-tools libvirt
启动libvirt:
[[email protected] ~]# systemctl start libvirtd
[[email protected] ~]# systemctl enable libvirtd
[[email protected] ~]# systemctl status libvirtd
创建虚拟机
1.准备虚拟机硬盘 2.需要一个系统iso镜像 3.需要安装一个vnc的客户端来连接
查看磁盘空间:
上传镜像
这里我们使用dd命令,复制系统的镜像,只需要挂载上光盘就可以了
[[email protected] ~]# cd /opt/
[[email protected] opt]# ls
CentOS-7-x86_64-Minimal-1810.iso
[[email protected] opt]# dd if=/dev/cdrom of=/opt/Cent0S-7-x86_64-Minima1-1810.iso
创建磁盘
qemu-lmg软件包是我们安装qemu-kvm-tools依赖给安装上的
[[email protected] opt]# qemu-img create -f qcow2 /opt/c73.qcow2 6G
安装虚拟机:
[[email protected] opt]# yum -y install virt-install
[[email protected] opt]# virt-install --virt-type=kvm --name=c73 --vcpus=1 -r 1024 --cdrom=/opt/CentOS-7-x86_64-Minimal-1810.iso --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole --os-type=linux --os-variant=rhel7 --disk path=/opt/c73.qcow2,size=6,format=qcow2
[[email protected] opt]# virsh list --all
Id Name State
----------------------------------------------------
1 c73 running
[[email protected] opt]# netstat -ntlp|grep 5900
tcp 0 0 0.0.0.0:5900 0.0.0.0:* LISTEN 3947/qemu-kvm
VNC连接虚拟机
注意: 如果查看5900端口开启,但是vnc无法连接虚拟机时,看下防火墙是否开启,创建虚机用vnc连接是从默认端口5900开始
安装完之后vnc会断开在虚拟机执行
[[email protected] opt]# virsh start c73
Domain c73 started
[[email protected] opt]# virsh list --all
Id Name State
----------------------------------------------------
2 c73 running
[[email protected] opt]# virsh start c73
error: Domain is already active
[[email protected] opt]#
重连一下vnc
KVM桥接配置
[[email protected] opt]# brctl show
bridge name bridge id STP enabled interfaces
virbr0 8000.5254000622b3 yes virbr0-nic
vnet0
查看物理机网卡设备
[[email protected] opt]# ifconfig virbr0
配置桥接设备br0
[[email protected] opt]# yum -y install bridge-utils
手动添加临时生效
[[email protected] c7-41 ~]# brctl addbr br0
[[email protected] c7-41 ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000000000000 no
virbr0 8000.5254009a0ee3 yes virbr0-nic
[[email protected] c7-41 ~]# brctl addif br0 ens33
通过配置文件配置桥接设备永久生效
为kvm宿主机创建虚拟网卡,并将物理网卡作为桥设备
[[email protected] ~]# cp /etc/sysconfig/network-scripts/ifcfg-ens33 .
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
DEVICE=ens33 TYPE=Ethernet ONBOOT=yes BRIDGE=br0 NM_CONTROLLED=no
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
TYPE=Bridge
ONBOOT=yes
BOOTPROTO=static
IPADDR=10.0.0.41
NETMASK=255.255.255.0
GATEWAY=10.0.0.254
NM_CONTROLLED=no
[[email protected] ~]# systemctl restart network.service
通过vnc连接kvm虚机修改网卡配置