qemu virtual machine configuration network

1. Implement qemu virtual machine & host network interoperability

For the network introduction and principles of qemu virtual machine, please refer to previous articles: Linux kernel debugging seven: qemu network configuration_lqonlylove's blog-CSDN blog

Here we only briefly outline the operation process to quickly achieve network interoperability.

1. The host creates tap0

[root@localhost ~]# ifconfig
[root@localhost ~]# ip tuntap add dev tap0 mode tap
[root@localhost ~]# ip link set dev tap0 up
[root@localhost ~]# ip address add dev tap0 192.168.2.128/24
[root@localhost ~]# ifconfig

The difference between the two ifconfigs can be seen in the newly added tap0 device on the host, and the IP of tap0 has been set by us.

2. Start the qemu virtual machine in another window, be sure to bring the -net command

qemu-system-ppc64 -m 8G -smp 4 -name pseries --enable-kvm -boot cd -hda linux.img -serial tcp::4444,server=on,wait=off\
-net nic -net tap,ifname=tap0,script=no,downscript=no

 This time we manually created the tap0 device on the host and bound it to a fixed IP, so the startup script and shutdown script of the network card are not used when qemu starts.

3. Configure the network in the virtual machine

 # ip addr
 # ip addr add 192.168.2.129/24 dev exxx
 # ip addr
 # ip link set env2 up
 # ping 192.168.2.128 -c 4

 

 The dev device needs to be replaced with the device of your own virtual machine. In my case, it is env2. At this point, you can see that we use the env2 network card to ping tap0 of the host machine, and the ping can be successful.

If the ping error message Destination Host Unreachable is reported at this time, you can check whether the tap module is loaded on the physical machine. After loading the mod module, deleting the tap0 device and repeating the above steps, you can find that the ping can succeed.

 

4. Copy the host files to the qemu virtual machine

At this time, you can scp [email protected]:/... to copy files according to personal needs.

 5. The host pings the virtual machine

After the above method realizes the network interoperability of qemu virtual machine & host machine, the corresponding env2 configuration information will be lost every time the guest restarts, so it must be reconfigured after restarting. If it is troublesome, you can directly write it into rc.local. Of course, you can also Specify through the net startup script script.

2. Implement the qemu virtual machine to access the external network

Following the above steps, if you want the virtual machine to continue to access the external network, you need to further configure route and iptables.

Here is a reference to the configuration method of Qemu connecting to the external network_qemu uses user networking_Mculover666's blog-CSDN blog

1. The host needs to enable IP forwarding

echo 1 > /proc/sys/net/ipv4/ip_forward

This option is usually turned on by default.

2. Add static routes to the host

This route usually exists. If not, you need to add it manually:

route add -net 192.168.2.0 netmask 255.255.255.0 dev tap0

3. Set iptables rules on the host

Turn on the NAT function of iptables:

iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -o enP1p22s0 -j MASQUERADE

The meaning of this rule is that the packets coming from 192.168.2.0/24 and going out from enP1p22s0 need to be NATed, and the returned packets will also be NATed. If there is only one subnet, -s 192.168.2.0/24 can be omitted.

 4.qemu adds default gateway

Add default gw to the virtual machine, which means that all network data packets of the virtual machine will be handed over to the physical machine tap0 for processing.

route add default gw <宿主机tap0的ip地址> dev exxx

route add default gw 192.168.2.128 dev env2

At this moment, pinging external networks such as centos has been successful! You can now yum install freely. ^_^

3. Appendix

1. For the repo source I use, domain name resolution needs to be added to /etc/hosts first.

        18.225.36.18  mirrorlist.centos.org

        150.139.241.192 mirrors.aliyun.com

# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the 
# remarked out baseurl= line instead.
#
#
 
[base]
name=CentOS-8.5.2111 - Base - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos-vault/8.5.2111/BaseOS/$basearch/os/
        http://mirrors.aliyuncs.com/centos-vault/8.5.2111/BaseOS/$basearch/os/
        http://mirrors.cloud.aliyuncs.com/centos-vault/8.5.2111/BaseOS/$basearch/os/
gpgcheck=0
enabled=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
 
#additional packages that may be useful
[extras]
name=CentOS-8.5.2111 - Extras - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos-vault/8.5.2111/extras/$basearch/os/
        http://mirrors.aliyuncs.com/centos-vault/8.5.2111/extras/$basearch/os/
        http://mirrors.cloud.aliyuncs.com/centos-vault/8.5.2111/extras/$basearch/os/
gpgcheck=0
enabled=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
 
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-8.5.2111 - Plus - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos-vault/8.5.2111/centosplus/$basearch/os/
        http://mirrors.aliyuncs.com/centos-vault/8.5.2111/centosplus/$basearch/os/
        http://mirrors.cloud.aliyuncs.com/centos-vault/8.5.2111/centosplus/$basearch/os/
gpgcheck=0
enabled=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
 
[PowerTools]
name=CentOS-8.5.2111 - PowerTools - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos-vault/8.5.2111/PowerTools/$basearch/os/
        http://mirrors.aliyuncs.com/centos-vault/8.5.2111/PowerTools/$basearch/os/
        http://mirrors.cloud.aliyuncs.com/centos-vault/8.5.2111/PowerTools/$basearch/os/
gpgcheck=0
enabled=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official


[AppStream]
name=CentOS-8.5.2111 - AppStream - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos-vault/8.5.2111/AppStream/$basearch/os/
        http://mirrors.aliyuncs.com/centos-vault/8.5.2111/AppStream/$basearch/os/
        http://mirrors.cloud.aliyuncs.com/centos-vault/8.5.2111/AppStream/$basearch/os/
gpgcheck=0
enabled=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official

2. Automatically set up when the virtual machine is started

Add the following command to /etc/rc.d/rc.local and chmod +x the file

ip addr add 192.168.2.129/24 dev env2
ip link set env2 up
route add default gw 192.168.2.128 dev env2

In addition, if you want to modify the system kernel parameters, you can add the kernel parameter configuration under /proc/sys/ to /etc/sysctl.conf, and then execute #sysctl -p to make the configuration take effect immediately.

3. The network card types supported by qemu can be queried with qemu_system_xxx -net nic,model=help. -net nic is created in vlan mode by default. If you do not want to use the default network card, for example, you want to create a virtio-net-pci type. network card, you can replace the network card part in the qemu command line with

-net nic,model=virtio -net  tap,ifname=tap0,script=no,downscript=no

The rest of the steps are exactly the same, so you can use info qtree to see that there is a virtual virtio network card architecture. 

If you don’t want to use the hub creation type of -net nic, you can also use the traditional device/xxxdev method to create it. You can replace the network card part in the qemu command line with

-device e1000,netdev=foo     -netdev tap,ifname=tap0,script=no,downscript=no,id=foo

Guess you like

Origin blog.csdn.net/jcf147/article/details/131290211