准备:
Centos7的虚拟机,虚拟机镜像文件


1.KVM的安装

[root@localhost ~]# yum -y install qemu-kvm libvirt libguestfs-tools virt-manager libvirt-python
qemu-kvm   #kvm的主程序,KVM虚拟化模块
virt-manager    #KVM 图形化管理工具
libvirt   #虚拟化服务
libguestfs-tools    #虚拟机的系统管理工具
libvirt-python     #python调用libvirt虚拟化服务的api接口库文件

[root@localhost ~]# systemctl start libvirtd    #开启虚拟化服务
[root@localhost ~]# systemctl enable libvirtd   #添加到开机自启动

确定正确加载kvm

[root@localhost ~]# lsmod | grep kvm
    kvm_intel             174841  3 
    kvm                   578518  1 kvm_intel
    irqbypass              13503  3 kvm

配置KVM的网桥
作用:用于连接用以网段内的所有机器,也就是让客户机和宿主机共享一个物理网络设备。


1. 编辑配置文件:

    [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=80960b0d-54cd-41db-bb42-28d30da2afa4
DEVICE=ens33
ONBOOT=yes
BRIDGE="br0" //添加这一行,如果配置过静态ip,需要把ipaddr,gateway,netmask,dns配置删除

`


2.添加文件:

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-br0 
DEVICE="br0"
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Bridge"
BOOTPROTO=dhcp   #这里也可以设置成静态IP

[root@localhost ~]# systemctl restart network

查看当前ip

[root@localhost ~]# ifconfig

br0: flags=4163  mtu 1500
    inet 192.168.10.13  netmask 255.255.255.0  broadcast 192.168.10.255
    inet6 fe80::20c:29ff:febe:8be8  prefixlen 64  scopeid 0x20
    ether 00:0c:29:be:8b:e8  txqueuelen 1000  (Ethernet)
    RX packets 1109336  bytes 4514990433 (4.2 GiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 984697  bytes 53197602 (50.7 MiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33: flags=4163 mtu 1500
ether 00:0c:29:be:8b:e8 txqueuelen 1000 (Ethernet)
RX packets 3280291 bytes 4647765745 (4.3 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 984898 bytes 59123703 (56.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73  mtu 65536
    inet 127.0.0.1  netmask 255.0.0.0
    inet6 ::1  prefixlen 128  scopeid 0x10
    loop  txqueuelen 1000  (Local Loopback)
    RX packets 0  bytes 0 (0.0 B)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 0  bytes 0 (0.0 B)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099  mtu 1500
    inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
    ether 52:54:00:67:94:c0  txqueuelen 1000  (Ethernet)
    RX packets 0  bytes 0 (0.0 B)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 0  bytes 0 (0.0 B)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vnet0: flags=4163  mtu 1500
    inet6 fe80::fc54:ff:fed1:8167  prefixlen 64  scopeid 0x20
    ether fe:54:00:d1:81:67  txqueuelen 1000  (Ethernet)
    RX packets 85  bytes 7298 (7.1 KiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 731  bytes 62680 (61.2 KiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

查看桥接信息

[root@localhost ~]# brctl show
bridge name bridge id       STP enabled interfaces
    br0     8000.000c29be8be8       no      ens33 
                                                                        vnet0
virbr0      8000.5254006794c0   yes     virbr0-nic

创建一台KVM的虚拟机:

如果硬盘容量足够大,跳过这一步:
1. 首先,添加一块一盘,
2. 刷新
        for((i=0;i<=2;i++)); do echo "- - -" > /sys/class/scsi_host/host$i/scan done
3. 刷新后lsblk看看硬盘个数是否添加成功;
4. 分区: fdisk   添加了一个sdb1的分区
5. 格式化:mkfs.xfs /dev/sdb1  
6. 挂载分区:  mount /dev/sdb1 /var/lib/libvrit/image
这里就结束了

上传iso镜像到/var/lib/libvirt/image下面,随便放到哪儿,但是不要放到含有特殊权限的目录下。

运行virt-manager启动虚拟服务
[root@localhost ~]# virt-manager
![](https://s1.51cto.com/images/blog/201806/04/28b171eaefe1442d7d23f9bf62af13e0.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

文件-----添加新的虚拟机---------本地安装介质---------使用镜像(刚才上传镜像的位置)------一直默认下去--------开机直接安装即可(安装时候和vmware一样)
注意:在选择网络的时候选择刚才创建好的br0桥接网络即可

安装KVM虚拟机完成

KVM常用命令:

[root@localhost ~]# virsh list   列出正在运行的虚拟机
[root@localhost ~]# virsh start centos7     #开启物理机 
[root@localhost ~]# virsh shoutdown  centos7    #关闭物理机
[root@localhost ~]# virsh autostart centos7  #设置为物理开机后自动启动