码迷,mamicode.com
首页 > 其他好文 > 详细

为KVM虚拟机增加网卡

时间:2018-04-29 15:26:24      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:res   mod   fas   ns3   ash   oba   vm虚拟机   fifo   queue   

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

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

test@test-Lenovo-Product:~$ virsh domiflist ubuntu1604-test-vm1
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      bridge     virbr0     virtio      52:54:00:34:06:26

test@ubuntu1604-test-vm1:~$ 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 动态的增加网卡,当时生效,但是虚拟机重启以后就没有了

test@test-Lenovo-Product:~$ virsh attach-interface ubuntu1604-test-vm1 --type bridge --source virbr0
Interface attached successfully

test@test-Lenovo-Product:~$ 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

test@ubuntu1604-test-vm1:~$ 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 方式二,通过修改配置文件的方式永久的增加网卡

test@test-Lenovo-Product:~$ virsh attach-interface ubuntu1604-test-vm1 --type bridge --source virbr0 --config
Interface attached successfully

test@test-Lenovo-Product:~$ 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

test@test-Lenovo-Product:~$ 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

test@ubuntu1604-test-vm1:~$ 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虚拟机增加网卡

标签:res   mod   fas   ns3   ash   oba   vm虚拟机   fifo   queue   

原文地址:https://www.cnblogs.com/happyorange/p/8970970.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!