QEMU NETWORK --- Usage
When running a virtual machine with QEMU,the network is very useful.
You can access the internet in guest and communicate with host and other guests.
1.Using NAT(Network Address Translation),that is virbr0
libvirt daemon will create a virbr0 bridge,you can see that by ifconfig and brctl show command.
The libvirt daemon also provide DHCP and DNS service for NAT by using dnsmasq tool.
DO NOT attempt to attach a physical device to virbr0. The virbr0 bridge is only for Network Address Translation (NAT) connectivity.
You can use the command below to check whether the libvirt daemon is running and the details of the dnsmasq.
ps -ef | grep libvirt
#:tunctl -p -t tap0
#Then start up the tap device
#:ip link set tap0 up
#Add the interface to the virtual bridge
#:sudo brctl addif vritbr0 tap0
Now,you can start the virtual machine with QEMU:
#:qemu -drive file=vm.img,if=virtio,format=qcow2,cache=none,aio=threads -smp 2 -net tap,ifname=tap0,script=no,downscript=no -net nic,model=virtio,macaddr=52:54:00:d6:be:ee -m 1024 --enable-kvm
ATTENTION:
a).MUST add script sub option for the -net option.
b).If you create more than one guest,you should create relevant TAP device/interface with different name
c).Specified macaddr is recommended,otherwise the other guests may be assigned with same IP address through DHCP because of the same MAC address.
Before virtual machine can get its own IP address,the driver of Ethernet interface must be installed correctly beforehand.
libvirt adds iptables rules which allow traffic to and from guests attached to the virbr0 device
in the INPUT, FORWARD,OUTPUT and POSTROUTING chains. libvirt then attempts to enable the
ip_forward parameter. Some other applications may disable ip_forward(in default disabled), so the best option is to
2. Bridged network
Bridged network configure is a little more complex than NAT.
a).
Because NetworkManager does not support bridging.So NetworkManager must be disabled to use networking with network scripts that are located
in /etc/sysconfig/network-scripts/ directory.
chkconfig NetworkManager off
chkconfig network on
service NetworkManager stop
service network start
You can also add "NM_CONTROLLED=no" in the ifcfg-eth0 script(will be mentioned later) instead of using chkconfig and service.
Remove the original script and create 2 new script.
The example here is remove ifcfg-Auto_eth0_-_Wire and add ifcfg-eth0 and ifcfg-br0.
Add lines below to add eth0(interface) and br0(bridge) in ifcfg-eth0.
DEVICE=eth0
HWADDR=xx:xx:xx:xx:xx:xx
ONBOOT=yes
BRIDGE=br0
MTU=9000
Add
DEVICE=br0
TYPE=Bridge
BOOTPROTO=dhcp
ONBOOT=yes
DELAY=0
to ifcfg-br0.
NEXT,
Configure iptables to allow all traffic to be forwarded across the bridge.
#: iptables -I FORWARD -m physdev --physdev-is-bridged -j ACCEPT
#: service iptables save
#: service iptables restart
Another method:
In /etc/sysctl.conf append the following lines:
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0
Reload the kernel parameters configured with sysctl.
# sysctl -p /etc/sysctl.conf
Finally,verify the bridge
Verify the new bridge is available with the bridge control command (brctl).
# brctl show
NOTE:The bridge is completely independent of the virbr0 bridge.
Then add the tap0 to the br0,surely,you can detach the tap0 from virbr0.
#:sudo brctl addif br0 tap0
You can also start up the guest using the command and the guest will acquire IP address by DHCP which behave like the HOST.
#:sudo qemu -drive file=vm.img,if=virtio,format=qcow2,cache=none,aio=threads -smp 2 -net tap,ifname=tap0,script=no,downscript=no -net nic,model=virtio,macaddr=52:54:00:d6:be:de -m 1024 --enable-kvm
Problems:
1).When using bridged networking,the speed of network is far more slower than no bridge.
2).CAN not access the teamroom in notes when using bridged networking.
3).How to access the guest from the external network when using NAT?
I have no idea what is the matter.
Anyway,You can enjoy the network in guest by doing configuration.
When running a virtual machine with QEMU,the network is very useful.
You can access the internet in guest and communicate with host and other guests.
1.Using NAT(Network Address Translation),that is virbr0
libvirt daemon will create a virbr0 bridge,you can see that by ifconfig and brctl show command.
The libvirt daemon also provide DHCP and DNS service for NAT by using dnsmasq tool.
DO NOT attempt to attach a physical device to virbr0. The virbr0 bridge is only for Network Address Translation (NAT) connectivity.
You can use the command below to check whether the libvirt daemon is running and the details of the dnsmasq.
ps -ef | grep libvirt
Next, we need add a tun Ethernet device for QEMU.
#:tunctl -p -t tap0
#Then start up the tap device
#:ip link set tap0 up
#Add the interface to the virtual bridge
#:sudo brctl addif vritbr0 tap0
Now,you can start the virtual machine with QEMU:
#:qemu -drive file=vm.img,if=virtio,format=qcow2,cache=none,aio=threads -smp 2 -net tap,ifname=tap0,script=no,downscript=no -net nic,model=virtio,macaddr=52:54:00:d6:be:ee -m 1024 --enable-kvm
ATTENTION:
a).MUST add script sub option for the -net option.
b).If you create more than one guest,you should create relevant TAP device/interface with different name
c).Specified macaddr is recommended,otherwise the other guests may be assigned with same IP address through DHCP because of the same MAC address.
Before virtual machine can get its own IP address,the driver of Ethernet interface must be installed correctly beforehand.
libvirt adds iptables rules which allow traffic to and from guests attached to the virbr0 device
in the INPUT, FORWARD,OUTPUT and POSTROUTING chains. libvirt then attempts to enable the
ip_forward parameter. Some other applications may disable ip_forward(in default disabled), so the best option is to
add the following to sysctl.conf (/etc/sysctl.conf):
net.ipv4.ip_forward = 12. Bridged network
Bridged network configure is a little more complex than NAT.
a).
Because NetworkManager does not support bridging.So NetworkManager must be disabled to use networking with network scripts that are located
in /etc/sysconfig/network-scripts/ directory.
chkconfig NetworkManager off
chkconfig network on
service NetworkManager stop
service network start
You can also add "NM_CONTROLLED=no" in the ifcfg-eth0 script(will be mentioned later) instead of using chkconfig and service.
Remove the original script and create 2 new script.
The example here is remove ifcfg-Auto_eth0_-_Wire and add ifcfg-eth0 and ifcfg-br0.
Add lines below to add eth0(interface) and br0(bridge) in ifcfg-eth0.
DEVICE=eth0
HWADDR=xx:xx:xx:xx:xx:xx
ONBOOT=yes
BRIDGE=br0
MTU=9000
Add
DEVICE=br0
TYPE=Bridge
BOOTPROTO=dhcp
ONBOOT=yes
DELAY=0
to ifcfg-br0.
restart the network
NEXT,
Configure iptables to allow all traffic to be forwarded across the bridge.
#: iptables -I FORWARD -m physdev --physdev-is-bridged -j ACCEPT
#: service iptables save
#: service iptables restart
Another method:
In /etc/sysctl.conf append the following lines:
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0
Reload the kernel parameters configured with sysctl.
# sysctl -p /etc/sysctl.conf
Finally,verify the bridge
Verify the new bridge is available with the bridge control command (brctl).
# brctl show
NOTE:The bridge is completely independent of the virbr0 bridge.
Then add the tap0 to the br0,surely,you can detach the tap0 from virbr0.
#:sudo brctl addif br0 tap0
You can also start up the guest using the command and the guest will acquire IP address by DHCP which behave like the HOST.
#:sudo qemu -drive file=vm.img,if=virtio,format=qcow2,cache=none,aio=threads -smp 2 -net tap,ifname=tap0,script=no,downscript=no -net nic,model=virtio,macaddr=52:54:00:d6:be:de -m 1024 --enable-kvm
Problems:
1).When using bridged networking,the speed of network is far more slower than no bridge.
2).CAN not access the teamroom in notes when using bridged networking.
3).How to access the guest from the external network when using NAT?
I have no idea what is the matter.
Anyway,You can enjoy the network in guest by doing configuration.