运行结果如下:
$ ./nova-ifinfo a7026868-407c-4c54-bb8f-a68623d7e13f
vm name is: instance-00000035 # 查找VM的名字
qemu pid is: 43146, iface host fd: 28, iface fd: 26, iface MAC: fa:16:3e:3f:ae:50
# 查找VM的tap设备的fd,通过fd找到对应的bridge。
************************************************
VM:instance-00000035 ---> tap: tap4ab104d3-5f ---> bridge: qbr4ab104d3-5f
************************************************
# 遍历VM的bridge。
************************************************
bridge: qbr4ab104d3-5f
|__ qvb4ab104d3-5f
|__ tap4ab104d3-5f
************************************************
# 找到VM的bridge的veth设备,通过veth设备,找到对端接在哪个bridge上。
************************************************
bridge: qbr4ab104d3-5f bridge: br-int
|__ veth: qvb4ab104d3-5f <----> veth: qvo4ab104d3-5f __|
************************************************
# 接续查找该bridge接到了哪里。
************************************************
bridge: br-tun bridge: br-int
|__ veth: patch-tun <----> veth: patch-int __|
************************************************
内容如下:
$ cat nova-ifinfo
1 #!/bin/bash 2 ST=`nova hypervisor-servers $HOSTNAME | grep $1` 3 if [[ $? != 0 ]] ; then 4 echo "not find $1 on $HOSTNAME" 5 nova hypervisor-servers $HOSTNAME 6 exit 1 7 fi 8 9 ST=`nova show $1` 10 if [[ $? != 0 ]] ; then 11 nova list 12 exit 1 13 fi 14 15 QEMU_NAME=`nova show $1 |grep "OS-EXT-SRV-ATTR:instance_name" | awk ‘{print $4}‘` 16 if [[ $? != 0 ]] ; then 17 exit 1 18 fi 19 20 ST=`sudo virsh domstate $1` 21 if [[ $? != 0 ]] ; then 22 exit 1 23 fi 24 if [[ "$ST" =~ "shut off" ]] ; then 25 sudo virsh list --all 26 echo "$QEMU_NAME is shut off" 27 exit 1 28 fi 29 echo "vm name is: $QEMU_NAME" 30 31 QEMU_CMD=`ps -ef |grep " $QEMU_NAME " |grep -v grep` 32 # echo $QEMU_CMD 33 QEMU_PID=`cut -d " " -f2 <<< $QEMU_CMD` 34 CMD_IFS="" 35 IF_FD="" 36 HOST_FD="" 37 [[ "$QEMU_CMD" =~ "-netdev" ]] && CMD_IFS=${QEMU_CMD#*-netdev} 38 [[ "$QEMU_CMD" =~ "tap,fd=" ]] && CMD_IFS=${CMD_IFS#*tap,fd=} && IF_FD=${CMD_IFS%%,*} 39 [[ "$QEMU_CMD" =~ "vhostfd=" ]] && CMD_IFS=${CMD_IFS#*vhostfd=} && HOST_FD=${CMD_IFS%% *} 40 [[ "$QEMU_CMD" =~ "mac=" ]] && CMD_MAC=${QEMU_CMD#*mac=} && CMD_MAC=${CMD_MAC%%,*} 41 42 echo "qemu pid is: $QEMU_PID, iface host fd: $HOST_FD, iface fd: $IF_FD, iface MAC: $CMD_MAC" 43 44 # egrep -l iff:.*tap* /proc/*/fdinfo/* 2>/dev/null|cut -d/ -f3 45 TAPNAME=`sudo cat /proc/$QEMU_PID/fdinfo/$IF_FD | grep "iff:.*tap*"` 46 TAPNAME=${TAPNAME#iff:[[:space:]]*} 47 # TAPNAME="${TAPNAME##[[:space:]]}" 48 # TAPNAME="${TAPNAME//[[:space:]]/}" 49 50 # kernel version less than 3.14 51 if [[ -z $TAPNAME ]]; then 52 TAPNAME=`ifconfig |grep ${CMD_MAC:2}` 53 TAPNAME="${TAPNAME%%[[:space:]]*}" 54 fi 55 56 BR=`ls -l /sys/class/net/$TAPNAME/brport/bridge` 57 BR=${BR##*/} 58 # BR="" 59 # for br in `ls /sys/class/net/`; do 60 # if [[ -d "/sys/class/net/$br/bridge" && -d "/sys/class/net/$br/lower_$TAPNAME" ]]; then 61 # BR=$br 62 # break 63 # fi 64 # done 65 66 echo "" 67 echo "************************************************" 68 echo "VM:$QEMU_NAME ---> tap: $TAPNAME ---> bridge: $BR" 69 echo "************************************************" 70 71 echo "" 72 echo "************************************************" 73 echo "bridge: $BR" 74 for port in `ls /sys/class/net/$BR/brif/`; do 75 echo " |__ $port" 76 done 77 echo "************************************************" 78 79 # BR_PORT="qvb${BR:3}" 80 # OVS_PORT="qvo${BR_PORT:3}" 81 OVS_BR_INT="" 82 echo "" 83 echo "************************************************" 84 for port in `ls /sys/class/net/$BR/brif/`; do 85 PORT_TY=`ethtool -i $port | grep "driver:" |awk ‘{print $2}‘` 86 if [[ $PORT_TY == "veth" ]] ; then 87 PEER_INDEX=`ethtool -S $port | grep "peer_ifindex:" |awk ‘{print $2}‘` 88 for peer in `ls /sys/class/net/`; do 89 INDEX=`cat /sys/class/net/$peer/ifindex` 90 if [[ $PEER_INDEX == $INDEX ]]; then 91 if [[ -d /sys/class/net/$OVS_PORT/brport/bridge ]]; then 92 OVS_BR_INT=`ls /sys/class/net/$OVS_PORT/brport/bridge -l` 93 OVS_BR_INT=${OVS_BR_INT##*/} 94 else 95 OVS_BR_INT=`sudo ovs-vsctl port-to-br $peer` 96 # OVS_BR_INT=`sudo ovs-vsctl iface-to-br $peer` 97 fi 98 echo "bridge: $BR bridge: $OVS_BR_INT" 99 echo " |__ veth: $port <----> veth: $peer __|" 100 fi 101 done 102 fi 103 done 104 echo "************************************************" 105 106 IS_OVS=`sudo ovs-vsctl list-br | grep $OVS_BR_INT` 107 OVS_INFOS=`sudo ovs-vsctl show |tr "\n" " "` 108 ALL="" 109 110 echo "" 111 echo "************************************************" 112 while [[ "$OVS_INFOS" =~ "{peer=" ]] 113 do 114 PORT=${OVS_INFOS%%\}*} 115 PORT=${PORT%[[:space:]]*type*} 116 PORT=${PORT##*Interface} 117 PORT=`echo $PORT` 118 PEER=${OVS_INFOS#*\{peer=} 119 PEER=${PEER%%\}*} 120 OVS_INFOS=${OVS_INFOS#*\{peer=*\}} 121 if [[ ! "$ALL" =~ "$PORT" ]] 122 then 123 ALL="$ALL $PORT $PEER" 124 PEER_BR=`sudo ovs-vsctl iface-to-br $PORT` 125 BR=`sudo ovs-vsctl iface-to-br $PEER` 126 echo "bridge: $BR bridge: $PEER_BR" 127 echo " |__ veth: $PORT <----> veth: $PEER __|" 128 echo "" 129 else 130 continue 131 fi 132 done 133 echo "************************************************" 134 135 # for port in `sudo ovs-vsctl show |grep "options: {peer=" |cut -d "=" -f2` 136 # do 137 # port=${port%%\}*} 138 # THIS_BR=`sudo ovs-vsctl iface-to-br $port` 139 # if [[ $THIS_BR == $OVS_BR_INT ]]; then 140 # 141 # echo "bridge: $THIS_BR bridge: $PEER_BR" 142 # echo " |__ veth: $port <----> veth: $peer __|" 143 # 144 # fi 145 # done 146 147 148 # echo "" 149 # echo "************************************************" 150 # if [[ $IS_OVS == $OVS_BR_INT ]]; then 151 # for port in `sudo ovs-vsctl list-ifaces br-int`; do 152 # PORT_TY=`ethtool -i $port | grep "driver:" |awk ‘{print $2}‘` 153 # if [[ $PORT_TY == "veth" ]] ; then 154 # PEER_INDEX=`ethtool -S $port | grep "peer_ifindex:" |awk ‘{print $2}‘` 155 # for peer in `ls /sys/class/net/`; do 156 # INDEX=`cat /sys/class/net/$peer/ifindex` 157 # if [[ $PEER_INDEX == $INDEX ]]; then 158 # PEER_INT="" 159 # if [[ -d /sys/class/net/$peer/brport/bridge ]]; then 160 # PEER_INT=`ls /sys/class/net/$peer/brport/bridge -l` 161 # PEER_INT=${PEER_INT##*/} 162 # else 163 # PEER_INT=`sudo ovs-vsctl port-to-br $peer` 164 # # PEER_INT=`sudo ovs-vsctl iface-to-br $peer` 165 # fi 166 # if [[ "x$PEER_INT" == "x" ]]; then 167 # continue 168 # fi 169 # echo "bridge: $BR bridge: $PEER_INT" 170 # echo " |__ veth: $port <----> veth: $peer __|" 171 # echo "" 172 # fi 173 # done 174 # fi 175 # done 176 # fi 177 # echo "************************************************"