LIBVIRT: HowTo add and remove Network Device from KVM Guest
Sometime we need to add Network device in Virtualized Guest of libvirt API. Addition of network device in guest machine after installation is basic requirement which we usually do with various client or GUI interface in various virtualization scenarios like Vmware, Citrix Xen Server etc. But when we work in Linux terminal, we should at least know how to add network device in Guest machines of libvirt API. In this post we would see HowTo add and remove Network Device from KVM Guest
In our setup, we have
Host machine srv3 192.168.1.11 Guest machine Guest_image_1 192.168.1.110
Addition of Network device
So now let’s see through which command, we can add network interface.
virsh attach-interface 9 bridge xenbr0 --target vif9.1 --mac 00:16:3e:1c:db:22
Explanation for this command
virsh
|
This is main command
|
attach-interface
|
This is just an options of virsh that carried all options in this command
|
9 or Guest_image_1
|
This is machine number displayed in “virsh list” command, you can use machine name as well here
|
bridge
|
Network type
|
Xenbr0
|
This is source network address of Host machine to which new address will bind
|
–target vif9.1
|
This is target address that get assigned to Guest machine and also reflect on Host machine in ifconfig output
|
–mac 00:16:3e:1c:db:22
|
This is simply a mac address that need to assigned on Guest new network device
|
So now we can see this in a screenshot, please have a look for reference.
Update : 25 Sep 2017
SetUP
KVM Host root@jarvis:~# lsb_release -d Description: Ubuntu 16.04.3 LTS root@jarvis:~# kvm --version QEMU emulator version 2.5.0 (Debian 1:2.5+dfsg-5ubuntu10.16), Copyright (c) 2003-2008 Fabrice Bellard
Commands used to add Network interface on KVM virtual machine
In Below commands, we can see MySQL_drbd_1 virtual machine has one ethernet card and we are going to add another ethernet card on it.
root@jarvis:~# virsh console MySQL_drbd_1 Connected to domain MySQL_drbd_1 Escape character is ^] CentOS Linux 7 (Core) Kernel 3.10.0-693.2.2.el7.x86_64 on an x86_64 mysql-pri login: root Password: Last login: Mon Sep 25 22:51:07 on ttyS0 COLUMNS=189; LINES=55; export COLUMNS LINES; [root@mysql-pri ~]# netstat -i Kernel Interface table Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg eth0 1500 1662 0 5 0 221 0 0 0 BMRU lo 65536 0 0 0 0 0 0 0 0 LRU
Let’s see how to add ethernet card on virtual machine.
root@jarvis:~# virsh attach-interface MySQL_drbd_1 bridge privatebr0 --target vnet2 --mac 52:54:00:5f:d0:15
Interface attached successfully
Let’s connect virtual machine’s serial console once again and see ethernet cards.
root@jarvis:~# virsh console MySQL_drbd_1
Connected to domain MySQL_drbd_1
Escape character is ^]
[root@mysql-pri ~]# netstat -i
Kernel Interface table
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
ens7 1500 0 0 0 0 0 0 0 0 BMRU
eth0 1500 1730 0 5 0 221 0 0 0 BMRU
lo 65536 0 0 0 0 0 0 0 0 LRU
We can see above, through this way we can attach another Ethernet card on running virtual machine.
In my case below logs comes on serial console opened from Hosts machine.
[root@mysql-pri ~]# [ 916.731087] pci 0000:00:07.0: BAR 6: assigned [mem 0x40000000-0x4003ffff pref]
[ 916.731997] pci 0000:00:07.0: BAR 0: assigned [io 0x1000-0x10ff]
[ 916.732571] pci 0000:00:07.0: BAR 1: assigned [mem 0x40040000-0x400400ff]
[ 916.750461] 8139cp: 8139cp: 10/100 PCI Ethernet driver v1.3 (Mar 22, 2004)
[ 916.751034] 8139cp 0000:00:07.0: enabling device (0000 -> 0003)
[ 916.771614] 8139cp 0000:00:07.0 eth1: RTL-8139C+ at 0xffffc90000202000, 52:54:00:5f:d0:15, IRQ 11
[ 916.781722] 8139too: 8139too Fast Ethernet driver 0.9.28
[ 916.795585] IPv6: ADDRCONF(NETDEV_UP): ens7: link is not ready
[ 916.796354] 8139cp 0000:00:07.0 ens7: link up, 100Mbps, full-duplex, lpa 0x05E1
Detach of KVM Guest Interface
For detach interface from KVM Guest machine, we should have complete information about Interface attached on KVM Guest machine. For same we need to find interface details like below
Syntax
virsh domiflist <Domain name/Dmain-id>
We can use Domain name or Domain ID display in virsh list command
Need to run below commands from Host Machine.
root@jarvis:~# virsh domiflist Cluster_host1 Interface Type Source Model MAC ------------------------------------------------------- vnet0 network default virtio 52:54:00:4f:a2:58 vnet1 bridge virbr0 rtl8139 52:54:00:5f:d0:26 vnet2 bridge virbr0 rtl8139 52:54:00:5f:d1:20
Once we decide which interface need to detach from KVM Guest machine.
root@jarvis:~# virsh detach-interface Cluster_host1 --type network --mac 52:54:00:4f:a2:58 Interface detached successfully
I believe detaching the interface this way does not make the change permanent and you should make this explicit. IF you want to make the change permanent the –config flag should be added at the end
Yes , Agree and also like to add
With –config —-> Will only change it in config, no immediate effect on Running Virtual machine.
Without –config —-> Will immediate effect on Running virtual machine , but not persistent like run with –config again
Let’s try to explain in below scenario.Addition of Interface.
▶ virsh attach-interface CentOS6_1 bridge virbr0 –target vnet3 –mac 52:54:00:3E:BD:03
Interface attached successfully
(Adding of interface without –config), Added interface immediately
▶ grep mac.*: /etc/libvirt/qemu/CentOS6_1.xml| awk -F= ‘{print substr($2,0,18)}’
’52:54:00:3e:bd:01
’52:54:00:3e:bd:02
(See virtual xml file not containing mac that added above)
▶ virsh attach-interface CentOS6_1 bridge virbr0 –target vnet3 –mac 52:54:00:3E:BD:04 –config
Interface attached successfully
(Once added above with –config, it didn’t effect on running virtual machine)
▶ grep mac.*: /etc/libvirt/qemu/CentOS6_1.xml| awk -F= ‘{print substr($2,0,18)}’
’52:54:00:3e:bd:01
’52:54:00:3e:bd:02
’52:54:00:3e:bd:04
(But added on virtual machine XML file)
Would update it on post ASAP. Thanks for notice this point and make comment on same.