KVM虚拟化技术的-虚拟网卡

Virtio 是一种 I/O 半虚拟化解决方案,是一套通用 I/O 设备虚拟化的程序,是对半虚拟化 Hypervisor 中的一组通用 I/O 设备的抽象。提供了一套上层应用与各 Hypervisor 虚拟化设备(KVM,Xen,VMware等)之间的通信框架和编程接口,减少跨平台所带来的兼容性问题,大大提高驱动程序开发效率。
网卡:
全虚拟化网卡: 虚拟化层完全模拟出来的网卡;
半虚拟化网卡: 通过驱动对操作系统进行了修改,即Virtio技术。
KVM虚拟机操作:

[root@localhost ~]# 
[root@localhost ~]# virt-what 
kvm
[root@localhost ~]# ethtool -i eth0 #显示网卡驱动的信息,如驱动的名称、版本等
driver: virtio_net
version: 1.0.0
firmware-version: 
bus-info: 0000:00:03.0
supports-statistics: no
supports-test: no
supports-eeprom-access: no
supports-register-dump: no
supports-priv-flags: no

#查看配置文件信息
[root@node3 ~]# virsh dumpxml centos7.2 |grep interface -C2 
      
'pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/> 'network'> '52:54:00:5f:d4:8c'/> 'default' bridge='virbr0'/> -- 'net0'/>
'pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> 'pty'> '/dev/pts/0'/> [root@node3 ~]# virsh domblklist centos7.2 #查看存储硬盘 Target Source ------------------------------------------------ vda /var/lib/libvirt/images/centos7.2.qcow2 hda -
[root@node3 ~]# virsh net-list  #查看网络列表
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           ye

[root@node3 ~]# virsh net-info default 
Name:           default
UUID:           8e7a52c9-2051-4cad-bc50-93c13008ec0e
Active:         yes
Persistent:     yes
Autostart:      yes
Bridge:         virbr0

[root@node3 ~]# virsh  net-edit default   #编辑default网卡
[root@node3 ~]# sysctl   -a | grep ip_for    #查看配置路由转发
net.ipv4.ip_forward = 1
net.ipv4.ip_forward_use_pmtu = 0

获取虚拟机网卡信息:virsh domiflist 虚拟机名

[root@node3 ~]# brctl show  
bridge name bridge id       STP enabled interfaces
virbr0      8000.fe54005fd48c   yes     vnet0
                                        vnet3
virbr1      8000.fe54008b1fcc   yes     vnet1
                                        vnet2

[root@node3 ~]# virsh domiflist centos7.2
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      network    default    virtio      52:54:00:5f:d4:8c
vnet1      network    allen      virtio      52:54:00:8b:1f:cc
vnet2      network    allen      virtio      52:54:00:96:7b:91
vnet3      network    default    virtio      52:54:00:f2:2f:c7


#永久添加网卡命令
[root@node3 ~]# virsh attach-interface centos7.2 --type network --source allen --model virtio --config --live 
Interface attached successfully

#永久删除网卡命令
[root@node3 ~]# virsh detach-interface centos7.2 --type network --mac 52:54:00:96:7b:91 
Interface detached successfully

[root@node3 ~]# virsh domiflist centos7.2  #查看虚拟机网络
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      network    default    virtio      52:54:00:5f:d4:8c
vnet1      network    allen      virtio      52:54:00:8b:1f:cc
vnet3      network    default    virtio      52:54:00:f2:2f:c7

[root@node3 ~]# virsh domif-getlink centos7.2   vnet3     #获取网口
vnet3 up
[root@node3 ~]# virsh domif-setlink centos7.2 vnet3 down  #关闭或打开某个网口
Device updated successfully
[root@node3 ~]# virsh domif-getlink centos7.2   vnet3
vnet3 down
[root@node3 ~]# virsh domif-getlink centos7.2   vnet3
vnet3 down
[root@node3 ~]# virsh domif-setlink centos7.2 vnet3  up 
Device updated successfully
[root@node3 ~]# virsh domif-getlink centos7.2   vnet3
vnet3 up



#生产虚拟机配置文件
[root@node3 ~]#virsh dumpxml centos7.2 > /etc/libvirt/qemu/centos7.3.xml  
#通过配置文件定义一个虚拟机 
[root@node3 ~]# virsh  define   /etc/libvirt/qemu/centos7.3.xml 

为接口绑定地址

临时
[root@node3 ~]# ip addr add dev eth0 172.16.30.240/24
[root@node3 ~]# ip addr add dev eth0 172.16.30.241/24
[root@node3 ~]# ip a s eth0

[root@node3 ~]# vim /etc/sysconfig/network-scripts/ifcfg-enp3s0 
IPADDR=172.16.30.30
PREFIX=24
IPADDR1=172.16.30.240
PREFIX1=24
IPADDR2=172.16.30.241  
PREFIX2=24
GATEWAY=172.16.30.254
[root@node3 ~]# nmcli connection show        #查看当前连接状态
[root@node3 ~]# nmcli connection reload      #重启服务
[root@node3 ~]# nmcli connection up enp3s0   #启用enp3s0 连接配置




[root@node1 ~]# virsh nodedev-list  --tree  
computer
  |
  +- net_lo_00_00_00_00_00_00
  +- net_virbr0_nic_52_54_00_35_6a_2b
  +- pci_0000_00_00_0
  +- pci_0000_00_01_0
  +- pci_0000_00_07_0
  +- pci_0000_00_07_1
  |   |
  |   +- scsi_host1
  |   +- scsi_host2
  |       |
  |       +- scsi_target2_0_0
  |           |
  |           +- scsi_2_0_0_0
  |               |
  |               +- block_sr0_VMware_Virtual_IDE_CDROM_Drive_10000000000000000001
  |               +- scsi_generic_sg1
  |                 
  +- pci_0000_00_07_3
  +- pci_0000_00_07_7
  +- pci_0000_00_0f_0
  |   |
  |   +- drm_card0
  |   +- drm_renderD128
  |     
  +- pci_0000_00_10_0
  |   |
  |   +- scsi_host0
  |       |
  |       +- scsi_target0_0_0
  |           |
  |           +- scsi_0_0_0_0
  |               |
  |               +- block_sda
  |               +- scsi_generic_sg0
  |                 
  +- pci_0000_00_11_0
  |   |
  |   +- pci_0000_02_00_0
  |   |   |
  |   |   +- usb_usb2
  |   |       |
  |   |       +- usb_2_0_1_0
  |   |       +- usb_2_1
  |   |       |   |
  |   |       |   +- usb_2_1_1_0
  |   |       |     
  |   |       +- usb_2_2
  |   |           |
  |   |           +- usb_2_2_1_0
  |   |             
  |   +- pci_0000_02_01_0
  |   |   |
  |   |   +- net_eno16777736_00_0c_29_7c_7a_33
  |   |     
  |   +- pci_0000_02_02_0
  |   +- pci_0000_02_03_0
  |   |   |
  |   |   +- usb_usb1
  |   |       |
  |   |       +- usb_1_0_1_0
  |   |         
  |   +- pci_0000_02_05_0
  |       |
  |       +- net_eno33554984_00_0c_29_7c_7a_3d
  |         
  +- pci_0000_00_15_0
  +- pci_0000_00_15_1


[root@node1 ~]# virsh nodedev-dumxml net_lo_00_00_00_00_00_00
error: unknown command: 'nodedev-dumxml'
[root@node1 ~]# virsh  nodedev-dumpxml net_lo_00_00_00_00_00_00

  net_lo_00_00_00_00_00_00
  /sys/devices/virtual/net/lo
  computer
  
    lo
    
00:00:00:00:00:00

补充命令:

[root@node1 ~]# openssl rand -hex 6 |sed -r 's/(..)/\1:/g; s/.$//'
0a:d8:af:09:13:1d
[root@node1 ~]# uuidgen 
86562401-120a-4fa4-ae5e-e31c2b78b31e

你可能感兴趣的:(虚拟化)