安装

sudo apt-get install -y openvswitch-switch

建立网桥

sudo ovs-vsctl add-br ovs-br0
sudo ovs-vsctl add-port ovs-br0 enp0s31f6

# sudo ovs-vsctl show
475d0100-2d9e-4b97-8351-3c6c9d7256ac
    Bridge ovs-br0
        Port enp0s31f6
            Interface enp0s31f6
        Port ovs-br0
            Interface ovs-br0
                type: internal

配置ip

sudo ip addr flush enp0s31f6
sudo ip addr add 192.168.8.52/24 dev ovs-br0
sudo ip link set dev ovs-br0 up
sudo ip route add default via 192.168.8.1

验证网络连通

# ip addr
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: enp0s31f6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel master ovs-system state UP group default qlen 1000
    link/ether f4:8e:38:de:1d:17 brd ff:ff:ff:ff:ff:ff
3: wlp3s0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether b8:8a:60:d9:0b:43 brd ff:ff:ff:ff:ff:ff
14: ovs-system: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 9e:25:a7:d3:d9:aa brd ff:ff:ff:ff:ff:ff
15: ovsbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 1000
    link/ether f4:8e:38:de:1d:17 brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.52/24 scope global ovsbr0
       valid_lft forever preferred_lft forever
    inet6 fe80::f68e:38ff:fede:1d17/64 scope link 
       valid_lft forever preferred_lft forever

ping 114.114.114.114

建立虚拟机vm

virt-install \
  --os-type=linux --os-variant=rhel8.4 \
  --name=vm --vcpus=1 --ram=1024 \
  --location=/tmp/Rocky-8.4-x86_64-minimal.iso \
  --disk path=/var/lib/libvirt/images/vm01.img,bus=virtio,size=10,cache=writeback,format=qcow2 \
  --network bridge=ovs-br0,virtualport.type=openvswitch,model=virtio \
  --extra-args "console=ttyS0,115200" \
  --nographics

virsh edit vm

    <interface type='bridge'>
      <mac address='52:54:00:5d:55:33'/>
      <source bridge='ovs-br0'/>
      <virtualport type='openvswitch'>
        <parameters interfaceid='6ede0801-111a-4d7e-9879-2e1e1880091c'/>
      </virtualport>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x01' slot='0x00' function='0x0'/>
    </interface>

连接

# ip addr
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: enp0s31f6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel master ovs-system state UP group default qlen 1000
    link/ether f4:8e:38:de:1d:17 brd ff:ff:ff:ff:ff:ff
3: wlp3s0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether b8:8a:60:d9:0b:43 brd ff:ff:ff:ff:ff:ff
5: ovs-system: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 9e:25:a7:d3:d9:aa brd ff:ff:ff:ff:ff:ff
6: ovs-br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 1000
    link/ether f4:8e:38:de:1d:17 brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.52/24 scope global ovs-br0
       valid_lft forever preferred_lft forever
    inet6 fe80::f68e:38ff:fede:1d17/64 scope link 
       valid_lft forever preferred_lft forever
8: vnet1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master ovs-system state UNKNOWN group default qlen 1000
    link/ether fe:54:00:5d:55:33 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::fc54:ff:fe5d:5533/64 scope link 
       valid_lft forever preferred_lft forever

# sudo ovs-vsctl show
475d0100-2d9e-4b97-8351-3c6c9d7256ac
    Bridge ovs-br0
        Port enp0s31f6
            Interface enp0s31f6
        Port ovs-br0
            Interface ovs-br0
                type: internal
        Port vnet1
            Interface vnet1