OpenStack: Recreate neutron port

Get broken / INACTIVE ports

DB_NAME=neutron
DB_USER=neutron
DB_PASS=$(grep neutron_database_password /etc/kolla/passwords.yml | cut -d " " -f2)
mysql --host=${DB_HOST} --password=${DB_PASS} --port=6033 --user=${DB_USER} --database=${DB_NAME}
 
select port_id, host from ml2_port_bindings where status = 'INACTIVE';
select port_id, host from ml2_port_bindings where status = 'INACTIVE' and host = 'com3-dev';
select * from ml2_port_bindings where port_id = "65db0c73-1533-4c36-9fd4-79b5e4e9704c";
#select * from ml2_port_binding_levels where port_id = "65db0c73-1533-4c36-9fd4-79b5e4e9704c" \G
 
# debug
PORT_IDS=$(mysql --host=${DB_HOST} --password=${DB_PASS} --port=6033 --user=${DB_USER} --database=${DB_NAME} \
  -se "select port_id from ml2_port_bindings where status = 'INACTIVE'")
echo ${PORT_IDS}
 
# get VMs
for PORT_ID in ${PORT_IDS}; do
    echo ${PORT_ID}
    openstack server show -c name -c "OS-EXT-SRV-ATTR:host" $(openstack port show -c device_id -f value ${PORT_ID})
done

Recreate port

SERVER_ID=f4b8828d-a717-49f6-8ad3-7b0bb1921123
 
nova interface-list ${SERVER_ID}
 
PORT_ID=57944199-014c-4907-9d6e-1fb233151234
 
# debug
openstack port show ${PORT_ID} -c name -c network_id -c mac_address -c fixed_ips -c security_group_ids -c project_id -c device_id

get port data

PORT_NAME=$(openstack port show ${PORT_ID} -c name -f value)
PORT_NETWORK=$(openstack port show ${PORT_ID} -c network_id -f value)
PORT_MAC=$(openstack port show ${PORT_ID} -c mac_address -f value)
PORT_IP=$(openstack port show ${PORT_ID} -c fixed_ips -f json | jq -r .fixed_ips[].ip_address)
PORT_SECURITY_GROUP=$(openstack port show ${PORT_ID} -c security_group_ids -f json | jq -r .security_group_ids[])
PORT_PROJECT=$(openstack port show ${PORT_ID} -c project_id -f value)
PORT_DEVICE=$(openstack port show ${PORT_ID} -c device_id -f value)
 
echo "
PORT_ID: ${PORT_ID}
PORT_NAME: ${PORT_NAME}
PORT_NETWORK: ${PORT_NETWORK}
PORT_MAC: ${PORT_MAC}
PORT_IP: ${PORT_IP}
PORT_SECURITY_GROUP: ${PORT_SECURITY_GROUP}
PORT_PROJECT: ${PORT_PROJECT}
PORT_DEVICE: ${PORT_DEVICE}
"

Re-Create port

# backup port data
openstack port show ${PORT_NAME} > /tmp/${PORT_NAME}_pre.out
 
# delete port
openstack port delete ${PORT_ID}
 
# create port
openstack port create ${PORT_NAME} \
    --network ${PORT_NETWORK} \
    --mac-address ${PORT_MAC} \
    --fixed-ip ip-address=${PORT_IP} \
    --security-group ${PORT_SECURITY_GROUP} \
    --project ${PORT_PROJECT}
 
# assign port
openstack server add port ${PORT_DEVICE} ${PORT_NAME}
 
# compare port data
openstack port show ${PORT_NAME} > /tmp/${PORT_NAME}_post.out
diff /tmp/${PORT_NAME}_pre.out /tmp/${PORT_NAME}_post.out

restart port

openstack port set --disable ${PORT_ID}
openstack port set --enable ${PORT_ID}