在 GCP VM 中创建 TAP 网络设备

在 GCP VM 中创建 TAP 网络设备

我正在尝试在 GCP VM 中创建 QEMU VM。我希望 QEMU VM 使用 TAP 网络设备与主机和外部世界进行通信。

我的期望设置如下所示。

    +-------------------+        +-----------------+
    | Physical Ethernet |        | Virtual Machine |
    | Interface (ens4)  |        |     (qemu)      |
    +---------+---------+        +--------+--------+
              |                           |
              |                           |
              |                           |
+-------------v------------+              |
|        Bridge (br0)      |              |
+-------------+------------+              |
              |                           |
              |                           |
              |                           |
    +---------v---------+        +--------v--------+
    | TAP Device (tap0) |        | Virtual NIC in  |
    |                   |<------>|   VM (e.g.      |
    |                   |        |  virtio-net)    |
    +-------------------+        +-----------------+

我使用了以下步骤:

# Step 1: Create a TAP device:
sudo ip tuntap add dev tap0 mode tap user $(whoami)
sudo ip link set tap0 up

# Step 2: Create a network bridge
sudo ip link add name br0 type bridge
sudo ip link set dev br0 up

# Step 3: Add the host's physical ethernet device ens4 to the bridge
sudo ip link set dev ens4 master br0

# Step 4: Add the TAP device to the bridge
sudo ip link set dev tap0 master br0

我注意到我的 GCP VM 在步骤 3 之后丢失了互联网连接。此时,我的网络如下所示:

$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: ens4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1460 qdisc mq master br0 state UP group default qlen 1000
    link/ether 42:01:0a:80:0f:cb brd ff:ff:ff:ff:ff:ff
    altname enp0s4
    inet 10.128.15.203/32 metric 100 scope global dynamic ens4
       valid_lft 730sec preferred_lft 730sec
    inet6 fe80::4001:aff:fe80:fcb/64 scope link 
       valid_lft forever preferred_lft forever
3: tap0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop master br0 state DOWN group default qlen 1000
    link/ether 12:ec:b9:f2:0a:70 brd ff:ff:ff:ff:ff:ff
4: br0: <BROADCAST,MULTICAST> mtu 1460 qdisc noop state DOWN group default qlen 1000
    link/ether 7a:fc:a9:79:fb:4f brd ff:ff:ff:ff:ff:ff

10.128.15.203 是分配给 GCP VM 的 IP 地址。在将 ens4 添加到 br0 之前,互联网运行正常。

任何帮助都将受到赞赏。

相关内容