其他分享
首页 > 其他分享> > 重新加载网卡默认驱动

重新加载网卡默认驱动

作者:互联网

shell

#!/bin/bash

N3=enp134s0f0
N3=0000:86:00.0
N6=enp134s0f3
N6=86:00.3

function get_nic_bdf(){
  IFC=$1
  # echo "Load driver for interface: $IFC"
  ls /sys/class/net/$IFC/device > /dev/null
  if [ $? -ne 0 ]; then
    echo "No interface: $IFC. Need to provide the PCI address for $IFC.";
    return 1
  fi
  bdf=`ls /sys/class/net/$IFC/device -l | awk -F'/' '{print $9}'`
  echo $bdf
}



function get_nic_bdf(){
  IFC=$1
  # echo "Load driver for interface: $IFC"
  ls /sys/class/net/$IFC/device > /dev/null
  if [ $? -ne 0 ]; then
    echo "No interface: $IFC. Need to provide the PCI address for $IFC.";
    return 1
  fi
  bdf=`ls /sys/class/net/$IFC/device -l | awk -F'/' '{print $9}'`
  echo $bdf
}


function load_driver(){
  addr=$1
  bdf=""
  if [[ $addr =~ ^0{4}:[0-9a-f]{2}:[0-9a-f]{2}\.[0-9]$ ]]; then
    bdf=$1
    echo "load driver for PCI device: $addr"
  elif [[ $addr =~ ^[0-9a-f]{2}:[0-9a-f]{2}\.[0-9]$ ]]; then
    bdf=0000:$1
    echo "load driver for PCI device: $addr"
  fi
  if [ -z "$bdf" ]; then
    IFC=$1
    echo "Load driver for interface: $IFC"
    ls /sys/class/net/$IFC/device > /dev/null
    if [ $? -ne 0 ]; then
      echo "No BDF found for interface: $IFC, let's guess from the name."
      if [[ $IFC =~ ^.*p[0-9]{1,3}s[0-9]{1,3}f[0-9]$ ]]; then
        bdf=${IFC#*p}
        b=${bdf%%s*}
        b=`printf '%0.2x\n' $b`
        s=${bdf%%f*}
        s=${s##*s}
        s=`printf '%0.2x\n' $s`
        f=${bdf##*f}
        bdf="0000:$b:$s.$f"
        echo "Guess interface: $IFC PCI address is $bdf"
      else
        echo "No interface: $IFC. Need to provide the PCI address for $IFC.";
        return 1
      fi
    else
      bdf=`ls /sys/class/net/$IFC/device -l | awk -F'/' '{print $9}'`
    fi
  fi
  path=/sys/bus/pci/devices/$bdf
  VDID=`lspci -nnv |grep ^${bdf#*:} | grep -E -o '[0-9a-f]{4}:[0-9a-f]{4}'`
  DID=${VDID#*:}
  VID=${VDID%:*}
  driver=$(grep "v0000${VID}d0000${DID}" /lib/modules/$(uname -r)/modules.alias |awk '{print $3}')
  cdriver=`ls /sys/bus/pci/devices/0000:88:00.0/driver -l`
  if [ $? -eq 0 ]; then
    cdriver=${cdriver##*/}
    # if [ "$driver" == "$cdriver" ]; then
    #   echo "Driver is $driver, No need to reload, exit."
    #   return 0
    # fi
    if [ -n "$cdriver" ]; then
      echo "Unload $bdf driver: $cdriver"
      echo "echo $bdf | sudo tee /sys/bus/pci/drivers/$cdriver/unbind"
      echo "$bdf" | sudo tee /sys/bus/pci/drivers/$cdriver/unbind
      sleep 1
    fi
  fi
  echo "echo \"$VID $DID\" | sudo tee /sys/bus/pci/drivers/$driver/new_id"
  echo "$VID $DID" | sudo tee /sys/bus/pci/drivers/$driver/new_id
  echo "echo \"${bdf}\" > /sys/bus/pci/drivers/$driver/bind"
  echo ${bdf} > /sys/bus/pci/drivers/$driver/bind
  ls -d /sys/bus/pci/drivers/$driver/$bdf
  if [ -f "${path}/driver_override" ]; then
    echo "echo $driver >  ${path}/driver_override"
    echo "$driver" > ${path}/driver_override
  fi

  ls -d /sys/bus/pci/drivers/$driver/$bdf
  if [ $? -ne 0 ]; then echo "Bind $driver for $bdf failed"; fi
  cdriver=`ls /sys/bus/pci/devices/0000:88:00.0/driver -l`
  if [ $? -eq 0 ]; then
    cdriver=${cdriver##*/}
    if [ "$driver" == "$cdriver" ]; then
      echo "Load driver: $driver for $bdf successfully."
    fi
  fi
  echo "$bdf, $driver, $VDID"
  echo ""
}


load_driver $N3

load_driver $N6

 

标签:sys,bdf,driver,默认,echo,网卡,IFC,fi,加载
来源: https://www.cnblogs.com/shaohef/p/15130951.html