服务器里Centos 7安装KVM,并通过KVM安装Centos 7
一、安装KVM
1、进入系统后,检查cpu参数是否支持虚拟化:
[root@localhost ~]# grep -Ei 'vmx|svm' /proc/cpuinfo
如果有出现vmx或者svm关键字就代表支持虚拟化,vmx代表Intel的CPU,svm代表AMD的CPU。
2、进入需要安装的目录(找到磁盘空间尽量大的目录),创建data目录,在data目录下创建两个文件夹iso和img,分别存放以后需要安装的操作系统镜像和之后创建的虚拟机镜像
[root@localhost ~]# cd /home [root@localhost home]# mkdir data [root@localhost home]#cd data [root@localhost data]# mkdir iso [root@localhost data]# mkdir img
3、安装KVM
[root@localhost ~]# yum install -y virt-* libvirt bridge-utils qemu-img
4、查看kvm模块支持确认载入kvm模块验证方法
[root@localhost ~]# lsmod | grep kvm
5、启动libvirtd服务
[root@localhost ~]# systemctl start libvirtd
二、配置网卡
安装完KVM之后,需要配置一下网卡,增加一个桥接网卡:
[root@localhost ~]# cd /etc/sysconfig/network-scripts/ [root@localhost /etc/sysconfig/network-scripts]# cp ifcfg-eth0 ifcfg-br0 # 拷贝当前的网卡文件,并修改名字 [root@localhost /etc/sysconfig/network-scripts]# vim ifcfg-eth0 # 修改文件内容如下 TYPE=Ethernet BOOTPROTO=dhcp 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 DEVICE=eth0 ONBOOT=yes BRIDGE=br0 [root@localhost /etc/sysconfig/network-scripts]# vim ifcfg-br0 # 修改文件内容如下 TYPE=Bridge BOOTPROTO=dhcp 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 DEVICE=br0 ONBOOT=yes [root@localhost /etc/sysconfig/network-scripts]# systemctl restart network # 重启服务(如果是服务器有多个网口使用ifdown命令关闭网络,ifup重启网络即可)
重启网络服务后,执行ifconfig命令可以看出,eth0网卡的IP到br0上了,这样网卡就配置完成了:
三、使用KVM安装centos7
[root@localhost ~]# virt-install --name=test01 --memory=512,maxmemory=1024 --vcpus=1,maxvcpus=2 --os-type=linux --os-variant=rhel7 --location=/kvm_data/iso/CentOS-7-x86_64-DVD-1810.iso --disk path=/kvm_data/img/test01.img,size=10 --bridge=br0 --graphics=none --console=pty,target_type=serial --extra-args="console=tty0 console=ttyS0"
(Centsos6把memory改成ram,只有一个ram)
管理虚拟机的一些命令
[root@localhost ~]# virsh console xxx # 进入指定的虚拟机,进入的时候还需要按一下回车 [root@localhost ~]# virsh start xxx # 启动虚拟机 [root@localhost ~]# virsh shutdown xxx # 关闭虚拟机 [root@localhost ~]# virsh destroy xxx # 强制停止虚拟机 [root@localhost ~]# virsh undefine xxx # 彻底销毁虚拟机,会删除虚拟机配置文件,但不会删除虚拟磁盘 [root@localhost ~]# virsh autostart xxx # 设置宿主机开机时该虚拟机也开机 [root@localhost ~]# virsh autostart --disable xxx # 解除开机启动 [root@localhost ~]# virsh suspend xxx # 挂起虚拟机 [root@localhost ~]# virsh resume xxx # 恢复挂起的虚拟机
[root@localhost ~]# virt-clone --connect qemu:///system --original test01 --name test02 --file /kvm_data/img/test02.img
(test01是模板机名称 后面name和file是克隆出新机器的名称和image位置)
四、进去虚拟机后的配置
[root@localhost ~]# dhclient #获取动态ip地址
[root@localhost ~]# cd /etc/sysconfig/network-scripts #修改Onboot=yes
五、错误处理
如果出现网桥链接异常的情况,这个一般不会出现,如果乱改过网卡名称,比如把ens19f0改成了eth0,可以按照以下办法修改:
[root@localhost ~]# brctl show
如果不是显示以下连接情况

那么首先删除之前的网桥连接
ip link set eth0 down brctl delbr eth0
手动连接一下br0和真实的网卡名字
ip link set ens19f0 up ip link set br0 up brctl addif br0 ens19f0 brctl show
这样就显示正常了。
智慧往往在已无用武之地时才来到我们身边。

浙公网安备 33010602011771号