dcuopn_check.sh 2.45 KB
Newer Older
liumg's avatar
liumg committed
1
2
#!/usr/bin/bash

liumg's avatar
liumg committed
3
# 原始设备映射表
liumg's avatar
liumg committed
4
5
6
7
8
9
declare -A devices_id=(
    ["Z100"]="54b7"
    ["Z100L"]="55b7"
    ["K100"]="62b7"
    ["K100-AI"]="6210"
    ["K100-AI-ECO"]="6211"
liumg's avatar
liumg committed
10
11
    ["BW"]="6320"
    ["BW1100"]="6430"
liumg's avatar
liumg committed
12
13
14
15
16
17
)

# 构建反向映射表(设备ID → 设备名称)
declare -A devices
for name in "${!devices_id[@]}"; do
    id="${devices_id[$name]}"
liumg's avatar
liumg committed
18
    devices["$id"]="$name" 
liumg's avatar
liumg committed
19
20
21
done

get_dcu() {
liumg's avatar
liumg committed
22
23
24
25
26
27
    # 检查lspci命令是否存在
    if ! command -v lspci &> /dev/null; then
        echo "错误: lspci 命令未找到,请先安装 pciutils 包" >&2
        return 1
    fi

liumg's avatar
liumg committed
28
29
    # 获取设备ID列表
    mapfile -t dcu_list < <(lspci -nn | grep -i -E "display|co-processor" | awk -F'[][]' '{print $4}' | awk -F ":" '{print $2}')
liumg's avatar
liumg committed
30
    
liumg's avatar
liumg committed
31
32
33
    local index=0
    local dcu_num=0
    local total=${#dcu_list[@]}
liumg's avatar
liumg committed
34
35
36
37
38
39
40
41
42
    local detected_devices=()
    
    # 处理没有设备的情况
    if [ $total -eq 0 ]; then
        echo "未检测到任何DCU设备"
        DEVICE_NAME=""
        DEVICE_ID=""
        return 0
    fi
liumg's avatar
liumg committed
43

liumg's avatar
liumg committed
44
    echo "=== 检测到的DCU设备 ==="
liumg's avatar
liumg committed
45
46
47
48
    while [ $index -lt $total ]; do
        current_id="${dcu_list[$index]}"

        if [ -n "${devices[$current_id]}" ]; then
liumg's avatar
liumg committed
49
50
            echo "DCU #$dcu_num: ID ${current_id}${devices[$current_id]}"
            detected_devices+=("${devices[$current_id]}")
liumg's avatar
liumg committed
51
52
            ((dcu_num++))
        else
liumg's avatar
liumg committed
53
54
            echo "DCU #$dcu_num: 未知设备ID: $current_id" >&2
            detected_devices+=("unknown")
liumg's avatar
liumg committed
55
56
57
58
        fi

        ((index++))
    done
liumg's avatar
liumg committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
    
    # 统计信息
    echo "=========================="
    if [ $dcu_num -eq 0 ]; then
        echo "总计: 0张DCU设备"
    else
        # 使用关联数组统计每种设备的数量
        local -A device_count
        for dev in "${detected_devices[@]}"; do
            ((device_count[$dev]++))
        done
        
        # 输出统计信息
        echo "总计: $dcu_num张DCU设备"
        for dev_name in "${!device_count[@]}"; do
            if [ "$dev_name" != "unknown" ]; then
                echo "  - ${dev_name}: ${device_count[$dev_name]}张"
            fi
        done
        if [ ${device_count["unknown"]:-0} -gt 0 ]; then
            echo "  - 未知设备: ${device_count["unknown"]}张"
        fi
    fi
    
    # 设置全局变量(取第一个有效设备)
    if [ $dcu_num -gt 0 ]; then
        DEVICE_NAME="${detected_devices[0]}"
        DEVICE_ID="${dcu_list[0]}"
    else
        DEVICE_NAME=""
        DEVICE_ID=""
    fi
liumg's avatar
liumg committed
91
}
liumg's avatar
liumg committed
92
93

get_dcu