为KVM虚拟机增加网卡

Posted happyorange

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为KVM虚拟机增加网卡相关的知识,希望对你有一定的参考价值。

用virt install 完成虚拟机的安装后,发现里面只有一块网卡,有些时候我们需要两块或多块网卡怎么办? 可以通过virsh attach-iterface来实现

例如原来的ubuntu虚拟机是这样的, 只有一块网卡:

[email protected]:~$ virsh domiflist ubuntu1604-test-vm1
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      bridge     virbr0     virtio      52:54:00:34:06:26

[email protected]:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:34:06:26 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.2/24 brd 192.168.122.255 scope global ens3
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe34:626/64 scope link
       valid_lft forever preferred_lft forever

添加网卡有两种途径:

1 动态的增加网卡,当时生效,但是虚拟机重启以后就没有了

[email protected]:~$ virsh attach-interface ubuntu1604-test-vm1 --type bridge --source virbr0
Interface attached successfully

[email protected]:~$ virsh domiflist
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      bridge     virbr0     virtio      52:54:00:34:06:26
vnet1      bridge     virbr0     rtl8139     52:54:00:9c:c4:dd

[email protected]:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:34:06:26 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.2/24 brd 192.168.122.255 scope global ens3
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe34:626/64 scope link
       valid_lft forever preferred_lft forever
3: ens7: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 52:54:00:9c:c4:dd brd ff:ff:ff:ff:ff:ff

 

2 方式二,通过修改配置文件的方式永久的增加网卡

[email protected]:~$ virsh attach-interface ubuntu1604-test-vm1 --type bridge --source virbr0 --config
Interface attached successfully

[email protected]:~$ virsh domiflist ubuntu1604-test-vm1
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      bridge     virbr0     virtio      52:54:00:34:06:26

 

虽然这时没有virsh domiflist没有列出来新增加的interface,登陆进虚拟机也没看到ip, 但是 virsh edit ubuntu1604-test-vm1 可以看到多了一个网卡

<interface type=‘bridge‘>
      <mac address=‘52:54:00:34:06:26‘/>
      <source bridge=‘virbr0‘/>
      <model type=‘virtio‘/>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x03‘ function=‘0x0‘/>
    </interface>
    <interface type=‘bridge‘>
      <mac address=‘52:54:00:ab:2d:39‘/>
      <source bridge=‘virbr0‘/>
      <model type=‘rtl8139‘/>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x07‘ function=‘0x0‘/>
    </interface>

经过重启后,可以看到网卡已被永久加上:

 

virsh shutdown ubuntu1604-test-vm1

virsh start domiflist ubuntu1604-test-vm1

[email protected]:~$ virsh domiflist ubuntu1604-test-vm1
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      bridge     virbr0     virtio      52:54:00:34:06:26
vnet1      bridge     virbr0     rtl8139     52:54:00:ab:2d:39

[email protected]:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:34:06:26 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.2/24 brd 192.168.122.255 scope global ens3
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe34:626/64 scope link
       valid_lft forever preferred_lft forever
3: ens7: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 52:54:00:ab:2d:39 brd ff:ff:ff:ff:ff:ff
 

3 如果网卡不用了想删掉,就用下面命令, 重启后就可生效

virsh detach-interface ubuntu1604-test-vm1 --type bridge --mac 52:54:00:ab:2d:39 --config
Interface detached successfully

 

以上是关于为KVM虚拟机增加网卡的主要内容,如果未能解决你的问题,请参考以下文章

KVM虚拟机桥接网卡变更导致网络不通解决方案

查看kvm虚拟机是否桥接方式

KVM:虚拟机网卡模式改为bridge

kvm无线网卡设置桥接网络

初始化Windows虚拟机(KVM)的网卡IP

KVM虚拟化网络优化技术总结