Explain the method of QEMU network configuration

  • 2020-05-27 07:35:32
  • OfStack

Today, I'm going to introduce you to how to configure the QEMU network.

qemu two ways to surf the Internet:

user mode network :

This way of virtual machine Internet access is very simple, similar to nat in vmware, qemu startup when adding -user-net parameters, virtual machine using dhcp, you can communicate with the Internet, but this way of communication between the virtual machine and the host is not convenient.

tap/tun network :

This is a little more complicated than user mode, but with the virtual machine set up < -- > Internet virtual machine < -- > Host communication is easy

This setup is similar to vmware's host-only. qemu USES the tun/tap device to add a virtual network device (tun0) to the host and can then be configured like a real network card 1.

First, tap/tuns devices are supported in the kernel:

Device Drivers --- >
Networking support --- >
[M] Universal TUN/TAP device driver support

If the current kernel is not supported, you can recompile only the corresponding modules to join, the method is very simple:

Put the current kernel configuration file cp into the kernel source directory:


[root@LFS ~]#cp /boot/config-[kernel-version] /usr/src/linux 
[root@LFS ~]#cd /usr/src/linux

Configure the kernel and select the TUN/TAP module (M), as shown above:


[root@LFS ~]#make menuconfig 

Recompile only the module (M), not the core (*) support:


[root@LFS ~]#make modules

After the compilation good/usr/src/linux drivers/net can find tun. ko:


[root@LFS net]#ls -l /usr/src/linux/drivers/net/tun.ko
-rw-r--r-- 1 root root 11116 Mar 23 20:29 /usr/src/linux/drivers/net/tun.ko
[root@LFS net]#

Put it cp to the corresponding location in the module directory of the current kernel:


[root@LFS net]#cp /usr/src/linux/drivers/net/tun.ko /lib/modules/`uname -r`/kernel/drivers/net

Re-establish module dependencies:


[root@LFS net]#depmod

Now you can load it:


[root@LFS net]#modprobe tun

Check 1:


[root@LFS net]#lsmod |grep tun
tun           8704 0
[root@LFS net]#

OK. Succeeded in not recompiling the entire kernel to add special module support

If your xx card is not supported by the kernel and can be compiled into a module, you can use this method to compile only the module you need, then manually install it to the corresponding location and load it.

This also compiles faster than the entire kernel without any damage to the system, so you can use the XX card. ^_^

Three things to note:

1. The kernel source code must be identical to the current kernel version 1, otherwise the compiled module will not be usable.

2. Note that only make modules(compilation module), not make modules_install(auto-install module to /lib/modules)

3. depmod must be run before loading the newly compiled module, otherwise modprobe cannot find it

In fact, using the current kernel configuration file (/boot/config-[kernel-version]), adding only the modules you need without any other changes, make modules_install should have no problem.

However, the safest way is to install it manually, the control is more secure in their own hands :-)

OK, go back to qemu

If you use udev to manage the device (udev is usually used in distributions of 2.6.x kernel), modprobe tun will automatically create the /dev/net directory, create the tun device, and make the relevant links:


[root@LFS net]#ls -l /dev/net/tun
lrwxrwxrwx 1 root root 6 Mar 25 15:35 /dev/net/tun -> ../tun
[root@LFS net]#

If, unfortunately, you don't see it, you'll have to do the work yourself :(


[root@LFS ~]#mkdir /dev/net
[root@LFS ~]#mknod /dev/net/tun c 10 200

OK, the relevant equipment is ready, and one more initialization script of tun/tap is needed:


[root@LFS ~]#make menuconfig 
0

Then give qemu-ifup x execution permission under /etc.

This script can only be executed by root users. If you want normal users to use qemu, you need to change it to sudo /sbin/ifconfig... Set the sudo permissions.

After starting qemu, it will add a virtual network device (tun0) to the host:


[root@LFS ~]#make menuconfig 
1

Now you can start the qemu configuration virtual machine network parameters, like vmware host-only1:

ip is the same as tun0,gateway is tun0, ip is the same as tun0,dns is the same as the host:

tun0 : ip :172.20.0.1 broadcast:172.20.255.255 netmask :255.255.0.0

qemu : ip :172.20.0.100 broadcast:172.20.255.255 netmask :255.255.0.0 gateway:172.20.0.1

Like host-only1, this only implements the virtual machine < ---- > For communication between hosts, router needs to be set up, and nat can be connected to the Internet


[root@LFS ~]#make menuconfig 
2

OK, virtual machine < --- > Host virtual machine < ---- > Internet communication is complete


Related articles: