(OK) Add a second NIC for a libvirt guest - virsh


https://kashyapc.fedorapeople.org/virt/add-network-card-in-guest.txt


Add a second NIC for a libvirt guest
====================================

Purpose: attach a new libvirt network to a live guest -- this takes
effect immediately, and the NIC will be persistent on further reboots.

(0) Before adding a NIC, enumerate the network interfaces for vm1:

    $ virsh domiflist vm1
    Interface  Type       Source     Model       MAC
    -------------------------------------------------------
    vnet0      network    default    virtio      52:54:00:cd:58:29


(1) Let's add a new NIC for the guest (the MAC address was chosen
    randomly):

    $ virsh attach-interface --domain vm1 --type network \
        --source openstackvms --model virtio \
        --mac 52:54:00:4b:73:5f --config --live
    Interface attached successfully


(2) Now again, enumerate the network interfaces for vm1, invoke the
    below command on the host:

    $ virsh domiflist vm1
    Interface  Type       Source     Model       MAC
    -------------------------------------------------------
    vnet0      network    default    virtio      52:54:00:cd:58:29
    vnet1      network    openstackvms virtio      52:54:00:4b:73:5f
  

(3) Inside the guest, you can see the second interface is now available:

    $ ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
        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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether 52:54:00:cd:58:29 brd ff:ff:ff:ff:ff:ff
        inet 192.168.122.50/24 brd 192.168.122.255 scope global dynamic eth0
           valid_lft 2397sec preferred_lft 2397sec
        inet6 fe80::5054:ff:fecd:5829/64 scope link 
           valid_lft forever preferred_lft forever
    3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether 52:54:00:4b:73:5f brd ff:ff:ff:ff:ff:ff
        inet 192.169.142.233/24 brd 192.169.142.255 scope global dynamic eth1
           valid_lft 3226sec preferred_lft 3226sec
        inet6 fe80::5054:ff:fe4b:735f/64 scope link 
           valid_lft forever preferred_lft forever


To detach the NIC
-----------------

(1) If you want to detach the network interface, this will take effect on
    next boot of the guest:

    $ virsh detach-interface --domain vm1 --type network \
        --mac 52:53:00:4b:75:6f --config
    Interface detached successfully


(2) Reboot the guest, and enumerate the network interfaces for vm1:

    $ virsh domiflist vm1
    Interface  Type       Source     Model       MAC
    -------------------------------------------------------
    vnet0      network    default    virtio      52:54:00:cd:58:29


(3) And, inside vm1, you can now see only see the interface for
    associated with the default network:

    $ ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
        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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether 52:54:00:cd:58:29 brd ff:ff:ff:ff:ff:ff
        inet 192.168.122.50/24 brd 192.168.122.255 scope global dynamic eth0
           valid_lft 3600sec preferred_lft 3600sec
        inet6 fe80::5054:ff:fecd:5829/64 scope link 
           valid_lft forever preferred_lft forever


NOTES:

  - While detaching, using the "--live" parameter is not recommended as
    it might affect any existing network activities on the NIC to be
    removed.
  - If detaching the NIC doesn't go as expected or it still shows up, to
    get the guest back to its previous networking state, edit the guest
    XML:

        $ virsh edit vm1
        [. . .]
        
    and remove the network interface attribute:

       <interface type='network'>
          <mac address='52:54:00:4b:74:6f'/>
          <source network='testnw'/>
          <model type='virtio'/>
          <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
        </interface>

posted @ 2016-07-07 15:36  张同光  阅读(115)  评论(0编辑  收藏  举报